diff --git a/.gitignore b/.gitignore index 02c577c76..4527ab45f 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,6 @@ result-* # Mac OS .DS_Store + +# ClangBuildAnalyzer output, see maintainers/buildtime_report.sh +buildtime.bin diff --git a/doc/manual/rl-next-dev/build-timing-analysis.md b/doc/manual/rl-next-dev/build-timing-analysis.md new file mode 100644 index 000000000..e59f3ca02 --- /dev/null +++ b/doc/manual/rl-next-dev/build-timing-analysis.md @@ -0,0 +1,15 @@ +--- +synopsis: Clang build timing analysis +cls: 587 +--- + +We now have Clang build profiling available, which generates Chrome +tracing files for each compilation unit. To enable it, run `meson configure +build -Dprofile-build=enabled` then rerun the compilation. + +If you want to make the build go faster, do a clang build with meson, then run +`maintainers/buildtime_report.sh build`, then contemplate how to improve the +build time. + +You can also look at individual object files' traces in +. diff --git a/flake.nix b/flake.nix index 253e16116..8c967aaa2 100644 --- a/flake.nix +++ b/flake.nix @@ -152,6 +152,7 @@ build-release-notes = final.buildPackages.callPackage ./maintainers/build-release-notes.nix { }; + clangbuildanalyzer = final.buildPackages.callPackage ./misc/clangbuildanalyzer.nix { }; boehmgc-nix = (final.boehmgc.override { enableLargeConfig = true; }).overrideAttrs (o: { @@ -360,6 +361,8 @@ # FIXME(Qyriad): remove once the migration to Meson is complete. pkgs.buildPackages.meson pkgs.buildPackages.ninja + + pkgs.buildPackages.clangbuildanalyzer ]; src = null; diff --git a/maintainers/buildtime_report.sh b/maintainers/buildtime_report.sh new file mode 100755 index 000000000..54ce75eb5 --- /dev/null +++ b/maintainers/buildtime_report.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +# Generates a report of build time based on a meson build using -ftime-trace in +# Clang. +if [ $# -lt 1 ]; then + echo "usage: $0 BUILD-DIR [filename]" >&2 + exit 1 +fi + +scriptdir=$(cd "$(dirname -- "$0")" || exit ; pwd -P) +filename=${2:-$scriptdir/../buildtime.bin} + +if [ "$(meson introspect "$1" --buildoptions | jq -r '.[] | select(.name == "profile-build") | .value')" != enabled ]; then + echo 'This build was not done with profile-build enabled, so cannot generate a report' >&2 + # shellcheck disable=SC2016 + echo 'Run `meson configure build -Dprofile-build=enabled` then rebuild, first' >&2 + exit 1 +fi + +ClangBuildAnalyzer --all "$1" "$filename" && ClangBuildAnalyzer --analyze "$filename" diff --git a/meson.build b/meson.build index 66a30f716..0a6a6b4b1 100644 --- a/meson.build +++ b/meson.build @@ -307,6 +307,14 @@ if cxx.get_linker_id() in ['ld.bfd', 'ld.gold'] add_project_link_arguments('-Wl,--no-copy-dt-needed-entries', language : 'cpp') endif +# Generate Chromium tracing files for each compiled file, which enables +# maintainers/buildtime_report.sh BUILD-DIR to simply work in clang builds. +# +# They can also be manually viewed at https://ui.perfetto.dev +if get_option('profile-build').require(meson.get_compiler('cpp').get_id() == 'clang').enabled() + add_project_arguments('-ftime-trace', language: 'cpp') +endif + subdir('src') if enable_tests subdir('tests/unit') diff --git a/meson.options b/meson.options index 4e8323689..ef5a669dd 100644 --- a/meson.options +++ b/meson.options @@ -31,6 +31,10 @@ option('tests-brief', type : 'boolean', value : false, description : 'set to true for shorter tests output', ) +option('profile-build', type : 'feature', value: 'disabled', + description : 'whether to enable -ftime-trace in clang builds, allowing for speeding up the build.' +) + option('store-dir', type : 'string', value : '/nix/store', description : 'path of the Nix store', ) diff --git a/misc/clangbuildanalyzer.nix b/misc/clangbuildanalyzer.nix new file mode 100644 index 000000000..a317e5144 --- /dev/null +++ b/misc/clangbuildanalyzer.nix @@ -0,0 +1,27 @@ +# Upstreaming here, can be deleted once it's upstreamed: +# https://github.com/NixOS/nixpkgs/pull/297102 +{ stdenv, lib, cmake, fetchFromGitHub }: +stdenv.mkDerivation (finalAttrs: { + pname = "clangbuildanalyzer"; + version = "1.5.0"; + + src = fetchFromGitHub { + owner = "aras-p"; + repo = "ClangBuildAnalyzer"; + rev = "v${finalAttrs.version}"; + sha256 = "sha256-kmgdk634zM0W0OoRoP/RzepArSipa5bNqdVgdZO9gxo="; + }; + + nativeBuildInputs = [ + cmake + ]; + + meta = { + description = "Tool for analyzing Clang's -ftrace-time files"; + homepage = "https://github.com/aras-p/ClangBuildAnalyzer"; + maintainers = with lib.maintainers; [ lf- ]; + license = lib.licenses.unlicense; + platforms = lib.platforms.unix; + mainProgram = "ClangBuildAnalyzer"; + }; +})