The Chromium Chronicle #7: Preprocessing Source

Episode 7: by Bruce Dawson in Seattle, WA (October, 2019)
Previous episodes

Sometimes it is helpful to compile a single Chromium source file by hand, perhaps to experiment with compiler optimization options, to preprocess it to a single file to understand some subtle macro details, or to minimize a compiler bug.

A few tricks will let a Chromium developer find and execute the command that compiles a particular source file, with modifications as needed.

Start by going to your output directory and using autoninja (or ninja) to compile the file of interest (and any dependencies) using the ^ suffix. This suffix tells ninja to build the output of the specified file—version.o in this case. Then, touch the file, and compile it (and only it) again with the -v (verbose) flag to ninja:

On Linux or OSX:

autoninja ../../base/^
touch ../../base/
autoninja -v ../../base/^

In the Windows cmd shell ^ is a special character and must be escaped:

C:\> autoninja ../../base/^^
C:\> touch ../../base/
C:\> autoninja -v ../../base/^^

Typical output of the autoninja -v command looks like this (significantly trimmed):

..\..\third_party\llvm-build\Release+Asserts\bin\clang-cl.exe /nologo /showIncludes -imsvc ...

This command allows you to compile the file of interest. To get the preprocessed output, use the following steps:

On Linux or OSX, remove the -o obj/base/base/version.o block from the end, and add -E. This tells the compiler to print the preprocessed file to stdout.

Redirect the output to a file, like this:

../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD ... -E >version.i

On Windows, remove the /showIncludes option from the beginning (it prints a line of output for each #include) and then add /P in order to preprocess the file instead of compiling it. The results will be saved in the current directory in version.i:

..\..\third_party\llvm-build\Release+Asserts\bin\clang-cl.exe /nologo -imsvc ... /P

Now you can examine the preprocessed file to see what the macros are actually doing, or make experimental compiler-switch changes and recompile to see what happens.

Additional Resources

  • Fast Chrome Builds: For more build-optimization tips (focused on Windows).
  • ETW: Find out how to find Windows performance problems—in Chrome or in the build—by reading the ETW (also known as Xperf) docs.