Clang build analysis tool using -ftime-trace
Clang build analysis tool using -ftime-trace
Clang C/C++ build analysis tool when using Clang 9+ -ftime-trace. The -ftime-trace compiler flag
(see blog post or
Clang 9 release notes) can be useful
to figure out what takes time during compilation of one source file. This tool helps to aggregate time trace
reports from multiple compilations, and output "what took the most time" summary:
Start the build capture: ClangBuildAnalyzer --start
This will write current timestamp in a ClangBuildAnalyzerSession.txt file under the given artifacts_folder. The artifacts
folder is where the compiled object files (and time trace report files) are expected to be produced by your build.
Do your build. Does not matter how; an IDE build, a makefile, a shell script, whatever. As long as it invokes
Clang and passes -ftime-trace flag to the compiler (Clang 9.0 or later is required for this).
Stop the build capture: ClangBuildAnalyzer --stop
This will load all Clang time trace compatible *.json files under the given artifacts_folder that were modified after
--start step was done (Clang -ftime-trace produces one JSON file next to each object file), process them and store data file into
a binary capture_file.
Run the build analysis: ClangBuildAnalyzer --analyze
This will read the capture_file produced by --stop step, calculate the slowest things and print them. If a
ClangBuildAnalyzer.ini file exists in the current folder, it will be read to control how many of various things to print.
Aternatively, instead of doing --start and --stop steps, you can do ClangBuildAnalyzer --all after your build; that will
include all the compatible *.json files for analysis, no matter when they were produced.
The analysis output will look something like this:
…Granularity and amount of most expensive things (files, functions, templates, includes) that are reported can be controlled by having an
ClangBuildAnalyzer.ini file in the working directory. Take a look at ClangBuildAnalyzer.ini for an example.
projects/vs2019/ClangBuildAnalyzer.sln.projects/xcode/ClangBuildAnalyzer.xcodeproj.make -f projects/make/Makefile.CMakeLists.txt, if you want to build using CMake.License for the Clang Build Analyzer itself is Unlicense, i.e. public domain. However, the source code
includes several external library source files (all under src/external), each with their own license:
cute_files.h from RandyGaul/cute_headers: zlib or public domain,cwalk from likle/cwalk: MIT,enkiTS, from dougbinks/enkiTS: zlib, version 058150d2 (2024 Apr),flat_hash_map, from skarupke/flat_hash_map: Boost 1.0,inih, from benhoyt/inih: BSD 3 clause,llvm-Demangle, part of LLVM: Apache-2.0 with LLVM-exception,simdjson from lemire/simdjson: Apache-2.0, version 3.10.1 (2024 Aug),sokol_time.h from floooh/sokol: zlib/libpng,xxHash from Cyan4973/xxHash: BSD 2 clause, version 0.8.2 (2023 Jul).No open issues yet, or sync has not completed.