1*9880d681SAndroid Build Coastguard Worker============================= 2*9880d681SAndroid Build Coastguard WorkerAdvanced Build Configurations 3*9880d681SAndroid Build Coastguard Worker============================= 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Worker.. contents:: 6*9880d681SAndroid Build Coastguard Worker :local: 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard WorkerIntroduction 9*9880d681SAndroid Build Coastguard Worker============ 10*9880d681SAndroid Build Coastguard Worker 11*9880d681SAndroid Build Coastguard Worker`CMake <http://www.cmake.org/>`_ is a cross-platform build-generator tool. CMake 12*9880d681SAndroid Build Coastguard Workerdoes not build the project, it generates the files needed by your build tool 13*9880d681SAndroid Build Coastguard Worker(GNU make, Visual Studio, etc.) for building LLVM. 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard WorkerIf **you are a new contributor**, please start with the :doc:`GettingStarted` or 16*9880d681SAndroid Build Coastguard Worker:doc:`CMake` pages. This page is intended for users doing more complex builds. 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard WorkerMany of the examples below are written assuming specific CMake Generators. 19*9880d681SAndroid Build Coastguard WorkerUnless otherwise explicitly called out these commands should work with any CMake 20*9880d681SAndroid Build Coastguard Workergenerator. 21*9880d681SAndroid Build Coastguard Worker 22*9880d681SAndroid Build Coastguard WorkerBootstrap Builds 23*9880d681SAndroid Build Coastguard Worker================ 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard WorkerThe Clang CMake build system supports bootstrap (aka multi-stage) builds. At a 26*9880d681SAndroid Build Coastguard Workerhigh level a multi-stage build is a chain of builds that pass data from one 27*9880d681SAndroid Build Coastguard Workerstage into the next. The most common and simple version of this is a traditional 28*9880d681SAndroid Build Coastguard Workerbootstrap build. 29*9880d681SAndroid Build Coastguard Worker 30*9880d681SAndroid Build Coastguard WorkerIn a simple two-stage bootstrap build, we build clang using the system compiler, 31*9880d681SAndroid Build Coastguard Workerthen use that just-built clang to build clang again. In CMake this simplest form 32*9880d681SAndroid Build Coastguard Workerof a bootstrap build can be configured with a single option, 33*9880d681SAndroid Build Coastguard WorkerCLANG_ENABLE_BOOTSTRAP. 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard Worker.. code-block:: console 36*9880d681SAndroid Build Coastguard Worker 37*9880d681SAndroid Build Coastguard Worker $ cmake -G Ninja -DCLANG_ENABLE_BOOTSTRAP=On <path to source> 38*9880d681SAndroid Build Coastguard Worker $ ninja stage2 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard WorkerThis command itself isn't terribly useful because it assumes default 41*9880d681SAndroid Build Coastguard Workerconfigurations for each stage. The next series of examples utilize CMake cache 42*9880d681SAndroid Build Coastguard Workerscripts to provide more complex options. 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard WorkerThe clang build system refers to builds as stages. A stage1 build is a standard 45*9880d681SAndroid Build Coastguard Workerbuild using the compiler installed on the host, and a stage2 build is built 46*9880d681SAndroid Build Coastguard Workerusing the stage1 compiler. This nomenclature holds up to more stages too. In 47*9880d681SAndroid Build Coastguard Workergeneral a stage*n* build is built using the output from stage*n-1*. 48*9880d681SAndroid Build Coastguard Worker 49*9880d681SAndroid Build Coastguard WorkerApple Clang Builds (A More Complex Bootstrap) 50*9880d681SAndroid Build Coastguard Worker============================================= 51*9880d681SAndroid Build Coastguard Worker 52*9880d681SAndroid Build Coastguard WorkerApple's Clang builds are a slightly more complicated example of the simple 53*9880d681SAndroid Build Coastguard Workerbootstrapping scenario. Apple Clang is built using a 2-stage build. 54*9880d681SAndroid Build Coastguard Worker 55*9880d681SAndroid Build Coastguard WorkerThe stage1 compiler is a host-only compiler with some options set. The stage1 56*9880d681SAndroid Build Coastguard Workercompiler is a balance of optimization vs build time because it is a throwaway. 57*9880d681SAndroid Build Coastguard WorkerThe stage2 compiler is the fully optimized compiler intended to ship to users. 58*9880d681SAndroid Build Coastguard Worker 59*9880d681SAndroid Build Coastguard WorkerSetting up these compilers requires a lot of options. To simplify the 60*9880d681SAndroid Build Coastguard Workerconfiguration the Apple Clang build settings are contained in CMake Cache files. 61*9880d681SAndroid Build Coastguard WorkerYou can build an Apple Clang compiler using the following commands: 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard Worker.. code-block:: console 64*9880d681SAndroid Build Coastguard Worker 65*9880d681SAndroid Build Coastguard Worker $ cmake -G Ninja -C <path to clang>/cmake/caches/Apple-stage1.cmake <path to source> 66*9880d681SAndroid Build Coastguard Worker $ ninja stage2-distribution 67*9880d681SAndroid Build Coastguard Worker 68*9880d681SAndroid Build Coastguard WorkerThis CMake invocation configures the stage1 host compiler, and sets 69*9880d681SAndroid Build Coastguard WorkerCLANG_BOOTSTRAP_CMAKE_ARGS to pass the Apple-stage2.cmake cache script to the 70*9880d681SAndroid Build Coastguard Workerstage2 configuration step. 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard WorkerWhen you build the stage2-distribution target it builds the minimal stage1 73*9880d681SAndroid Build Coastguard Workercompiler and required tools, then configures and builds the stage2 compiler 74*9880d681SAndroid Build Coastguard Workerbased on the settings in Apple-stage2.cmake. 75*9880d681SAndroid Build Coastguard Worker 76*9880d681SAndroid Build Coastguard WorkerThis pattern of using cache scripts to set complex settings, and specifically to 77*9880d681SAndroid Build Coastguard Workermake later stage builds include cache scripts is common in our more advanced 78*9880d681SAndroid Build Coastguard Workerbuild configurations. 79*9880d681SAndroid Build Coastguard Worker 80*9880d681SAndroid Build Coastguard WorkerMulti-stage PGO 81*9880d681SAndroid Build Coastguard Worker=============== 82*9880d681SAndroid Build Coastguard Worker 83*9880d681SAndroid Build Coastguard WorkerProfile-Guided Optimizations (PGO) is a really great way to optimize the code 84*9880d681SAndroid Build Coastguard Workerclang generates. Our multi-stage PGO builds are a workflow for generating PGO 85*9880d681SAndroid Build Coastguard Workerprofiles that can be used to optimize clang. 86*9880d681SAndroid Build Coastguard Worker 87*9880d681SAndroid Build Coastguard WorkerAt a high level, the way PGO works is that you build an instrumented compiler, 88*9880d681SAndroid Build Coastguard Workerthen you run the instrumented compiler against sample source files. While the 89*9880d681SAndroid Build Coastguard Workerinstrumented compiler runs it will output a bunch of files containing 90*9880d681SAndroid Build Coastguard Workerperformance counters (.profraw files). After generating all the profraw files 91*9880d681SAndroid Build Coastguard Workeryou use llvm-profdata to merge the files into a single profdata file that you 92*9880d681SAndroid Build Coastguard Workercan feed into the LLVM_PROFDATA_FILE option. 93*9880d681SAndroid Build Coastguard Worker 94*9880d681SAndroid Build Coastguard WorkerOur PGO.cmake cache script automates that whole process. You can use it by 95*9880d681SAndroid Build Coastguard Workerrunning: 96*9880d681SAndroid Build Coastguard Worker 97*9880d681SAndroid Build Coastguard Worker.. code-block:: console 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker $ cmake -G Ninja -C <path_to_clang>/cmake/caches/PGO.cmake <source dir> 100*9880d681SAndroid Build Coastguard Worker $ ninja stage2-instrumented-generate-profdata 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard WorkerIf you let that run for a few hours or so, it will place a profdata file in your 103*9880d681SAndroid Build Coastguard Workerbuild directory. This takes a really long time because it builds clang twice, 104*9880d681SAndroid Build Coastguard Workerand you *must* have compiler-rt in your build tree. 105*9880d681SAndroid Build Coastguard Worker 106*9880d681SAndroid Build Coastguard WorkerThis process uses any source files under the perf-training directory as training 107*9880d681SAndroid Build Coastguard Workerdata as long as the source files are marked up with LIT-style RUN lines. 108*9880d681SAndroid Build Coastguard Worker 109*9880d681SAndroid Build Coastguard WorkerAfter it finishes you can use “find . -name clang.profdata” to find it, but it 110*9880d681SAndroid Build Coastguard Workershould be at a path something like: 111*9880d681SAndroid Build Coastguard Worker 112*9880d681SAndroid Build Coastguard Worker.. code-block:: console 113*9880d681SAndroid Build Coastguard Worker 114*9880d681SAndroid Build Coastguard Worker <build dir>/tools/clang/stage2-instrumented-bins/utils/perf-training/clang.profdata 115*9880d681SAndroid Build Coastguard Worker 116*9880d681SAndroid Build Coastguard WorkerYou can feed that file into the LLVM_PROFDATA_FILE option when you build your 117*9880d681SAndroid Build Coastguard Workeroptimized compiler. 118*9880d681SAndroid Build Coastguard Worker 119*9880d681SAndroid Build Coastguard WorkerThe PGO came cache has a slightly different stage naming scheme than other 120*9880d681SAndroid Build Coastguard Workermulti-stage builds. It generates three stages; stage1, stage2-instrumented, and 121*9880d681SAndroid Build Coastguard Workerstage2. Both of the stage2 builds are built using the stage1 compiler. 122*9880d681SAndroid Build Coastguard Worker 123*9880d681SAndroid Build Coastguard WorkerThe PGO came cache generates the following additional targets: 124*9880d681SAndroid Build Coastguard Worker 125*9880d681SAndroid Build Coastguard Worker**stage2-instrumented** 126*9880d681SAndroid Build Coastguard Worker Builds a stage1 x86 compiler, runtime, and required tools (llvm-config, 127*9880d681SAndroid Build Coastguard Worker llvm-profdata) then uses that compiler to build an instrumented stage2 compiler. 128*9880d681SAndroid Build Coastguard Worker 129*9880d681SAndroid Build Coastguard Worker**stage2-instrumented-generate-profdata** 130*9880d681SAndroid Build Coastguard Worker Depends on "stage2-instrumented" and will use the instrumented compiler to 131*9880d681SAndroid Build Coastguard Worker generate profdata based on the training files in <clang>/utils/perf-training 132*9880d681SAndroid Build Coastguard Worker 133*9880d681SAndroid Build Coastguard Worker**stage2** 134*9880d681SAndroid Build Coastguard Worker Depends of "stage2-instrumented-generate-profdata" and will use the stage1 135*9880d681SAndroid Build Coastguard Worker compiler with the stage2 profdata to build a PGO-optimized compiler. 136*9880d681SAndroid Build Coastguard Worker 137*9880d681SAndroid Build Coastguard Worker**stage2-check-llvm** 138*9880d681SAndroid Build Coastguard Worker Depends on stage2 and runs check-llvm using the stage2 compiler. 139*9880d681SAndroid Build Coastguard Worker 140*9880d681SAndroid Build Coastguard Worker**stage2-check-clang** 141*9880d681SAndroid Build Coastguard Worker Depends on stage2 and runs check-clang using the stage2 compiler. 142*9880d681SAndroid Build Coastguard Worker 143*9880d681SAndroid Build Coastguard Worker**stage2-check-all** 144*9880d681SAndroid Build Coastguard Worker Depends on stage2 and runs check-all using the stage2 compiler. 145*9880d681SAndroid Build Coastguard Worker 146*9880d681SAndroid Build Coastguard Worker**stage2-test-suite** 147*9880d681SAndroid Build Coastguard Worker Depends on stage2 and runs the test-suite using the stage3 compiler (requires 148*9880d681SAndroid Build Coastguard Worker in-tree test-suite). 149*9880d681SAndroid Build Coastguard Worker 150*9880d681SAndroid Build Coastguard Worker3-Stage Non-Determinism 151*9880d681SAndroid Build Coastguard Worker======================= 152*9880d681SAndroid Build Coastguard Worker 153*9880d681SAndroid Build Coastguard WorkerIn the ancient lore of compilers non-determinism is like the multi-headed hydra. 154*9880d681SAndroid Build Coastguard WorkerWhenever it's head pops up, terror and chaos ensue. 155*9880d681SAndroid Build Coastguard Worker 156*9880d681SAndroid Build Coastguard WorkerHistorically one of the tests to verify that a compiler was deterministic would 157*9880d681SAndroid Build Coastguard Workerbe a three stage build. The idea of a three stage build is you take your sources 158*9880d681SAndroid Build Coastguard Workerand build a compiler (stage1), then use that compiler to rebuild the sources 159*9880d681SAndroid Build Coastguard Worker(stage2), then you use that compiler to rebuild the sources a third time 160*9880d681SAndroid Build Coastguard Worker(stage3) with an identical configuration to the stage2 build. At the end of 161*9880d681SAndroid Build Coastguard Workerthis, you have a stage2 and stage3 compiler that should be bit-for-bit 162*9880d681SAndroid Build Coastguard Workeridentical. 163*9880d681SAndroid Build Coastguard Worker 164*9880d681SAndroid Build Coastguard WorkerYou can perform one of these 3-stage builds with LLVM & clang using the 165*9880d681SAndroid Build Coastguard Workerfollowing commands: 166*9880d681SAndroid Build Coastguard Worker 167*9880d681SAndroid Build Coastguard Worker.. code-block:: console 168*9880d681SAndroid Build Coastguard Worker 169*9880d681SAndroid Build Coastguard Worker $ cmake -G Ninja -C <path_to_clang>/cmake/caches/3-stage.cmake <source dir> 170*9880d681SAndroid Build Coastguard Worker $ ninja stage3 171*9880d681SAndroid Build Coastguard Worker 172*9880d681SAndroid Build Coastguard WorkerAfter the build you can compare the stage2 & stage3 compilers. We have a bot 173*9880d681SAndroid Build Coastguard Workersetup `here <http://lab.llvm.org:8011/builders/clang-3stage-ubuntu>`_ that runs 174*9880d681SAndroid Build Coastguard Workerthis build and compare configuration. 175