1*f4a2713aSLionel Sambuc========================= 2*f4a2713aSLionel SambucDriver Design & Internals 3*f4a2713aSLionel Sambuc========================= 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc.. contents:: 6*f4a2713aSLionel Sambuc :local: 7*f4a2713aSLionel Sambuc 8*f4a2713aSLionel SambucIntroduction 9*f4a2713aSLionel Sambuc============ 10*f4a2713aSLionel Sambuc 11*f4a2713aSLionel SambucThis document describes the Clang driver. The purpose of this document 12*f4a2713aSLionel Sambucis to describe both the motivation and design goals for the driver, as 13*f4a2713aSLionel Sambucwell as details of the internal implementation. 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel SambucFeatures and Goals 16*f4a2713aSLionel Sambuc================== 17*f4a2713aSLionel Sambuc 18*f4a2713aSLionel SambucThe Clang driver is intended to be a production quality compiler driver 19*f4a2713aSLionel Sambucproviding access to the Clang compiler and tools, with a command line 20*f4a2713aSLionel Sambucinterface which is compatible with the gcc driver. 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel SambucAlthough the driver is part of and driven by the Clang project, it is 23*f4a2713aSLionel Sambuclogically a separate tool which shares many of the same goals as Clang: 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel Sambuc.. contents:: Features 26*f4a2713aSLionel Sambuc :local: 27*f4a2713aSLionel Sambuc 28*f4a2713aSLionel SambucGCC Compatibility 29*f4a2713aSLionel Sambuc----------------- 30*f4a2713aSLionel Sambuc 31*f4a2713aSLionel SambucThe number one goal of the driver is to ease the adoption of Clang by 32*f4a2713aSLionel Sambucallowing users to drop Clang into a build system which was designed to 33*f4a2713aSLionel Sambuccall GCC. Although this makes the driver much more complicated than 34*f4a2713aSLionel Sambucmight otherwise be necessary, we decided that being very compatible with 35*f4a2713aSLionel Sambucthe gcc command line interface was worth it in order to allow users to 36*f4a2713aSLionel Sambucquickly test clang on their projects. 37*f4a2713aSLionel Sambuc 38*f4a2713aSLionel SambucFlexible 39*f4a2713aSLionel Sambuc-------- 40*f4a2713aSLionel Sambuc 41*f4a2713aSLionel SambucThe driver was designed to be flexible and easily accommodate new uses 42*f4a2713aSLionel Sambucas we grow the clang and LLVM infrastructure. As one example, the driver 43*f4a2713aSLionel Sambuccan easily support the introduction of tools which have an integrated 44*f4a2713aSLionel Sambucassembler; something we hope to add to LLVM in the future. 45*f4a2713aSLionel Sambuc 46*f4a2713aSLionel SambucSimilarly, most of the driver functionality is kept in a library which 47*f4a2713aSLionel Sambuccan be used to build other tools which want to implement or accept a gcc 48*f4a2713aSLionel Sambuclike interface. 49*f4a2713aSLionel Sambuc 50*f4a2713aSLionel SambucLow Overhead 51*f4a2713aSLionel Sambuc------------ 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel SambucThe driver should have as little overhead as possible. In practice, we 54*f4a2713aSLionel Sambucfound that the gcc driver by itself incurred a small but meaningful 55*f4a2713aSLionel Sambucoverhead when compiling many small files. The driver doesn't do much 56*f4a2713aSLionel Sambucwork compared to a compilation, but we have tried to keep it as 57*f4a2713aSLionel Sambucefficient as possible by following a few simple principles: 58*f4a2713aSLionel Sambuc 59*f4a2713aSLionel Sambuc- Avoid memory allocation and string copying when possible. 60*f4a2713aSLionel Sambuc- Don't parse arguments more than once. 61*f4a2713aSLionel Sambuc- Provide a few simple interfaces for efficiently searching arguments. 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel SambucSimple 64*f4a2713aSLionel Sambuc------ 65*f4a2713aSLionel Sambuc 66*f4a2713aSLionel SambucFinally, the driver was designed to be "as simple as possible", given 67*f4a2713aSLionel Sambucthe other goals. Notably, trying to be completely compatible with the 68*f4a2713aSLionel Sambucgcc driver adds a significant amount of complexity. However, the design 69*f4a2713aSLionel Sambucof the driver attempts to mitigate this complexity by dividing the 70*f4a2713aSLionel Sambucprocess into a number of independent stages instead of a single 71*f4a2713aSLionel Sambucmonolithic task. 72*f4a2713aSLionel Sambuc 73*f4a2713aSLionel SambucInternal Design and Implementation 74*f4a2713aSLionel Sambuc================================== 75*f4a2713aSLionel Sambuc 76*f4a2713aSLionel Sambuc.. contents:: 77*f4a2713aSLionel Sambuc :local: 78*f4a2713aSLionel Sambuc :depth: 1 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel SambucInternals Introduction 81*f4a2713aSLionel Sambuc---------------------- 82*f4a2713aSLionel Sambuc 83*f4a2713aSLionel SambucIn order to satisfy the stated goals, the driver was designed to 84*f4a2713aSLionel Sambuccompletely subsume the functionality of the gcc executable; that is, the 85*f4a2713aSLionel Sambucdriver should not need to delegate to gcc to perform subtasks. On 86*f4a2713aSLionel SambucDarwin, this implies that the Clang driver also subsumes the gcc 87*f4a2713aSLionel Sambucdriver-driver, which is used to implement support for building universal 88*f4a2713aSLionel Sambucimages (binaries and object files). This also implies that the driver 89*f4a2713aSLionel Sambucshould be able to call the language specific compilers (e.g. cc1) 90*f4a2713aSLionel Sambucdirectly, which means that it must have enough information to forward 91*f4a2713aSLionel Sambuccommand line arguments to child processes correctly. 92*f4a2713aSLionel Sambuc 93*f4a2713aSLionel SambucDesign Overview 94*f4a2713aSLionel Sambuc--------------- 95*f4a2713aSLionel Sambuc 96*f4a2713aSLionel SambucThe diagram below shows the significant components of the driver 97*f4a2713aSLionel Sambucarchitecture and how they relate to one another. The orange components 98*f4a2713aSLionel Sambucrepresent concrete data structures built by the driver, the green 99*f4a2713aSLionel Sambuccomponents indicate conceptually distinct stages which manipulate these 100*f4a2713aSLionel Sambucdata structures, and the blue components are important helper classes. 101*f4a2713aSLionel Sambuc 102*f4a2713aSLionel Sambuc.. image:: DriverArchitecture.png 103*f4a2713aSLionel Sambuc :align: center 104*f4a2713aSLionel Sambuc :alt: Driver Architecture Diagram 105*f4a2713aSLionel Sambuc 106*f4a2713aSLionel SambucDriver Stages 107*f4a2713aSLionel Sambuc------------- 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel SambucThe driver functionality is conceptually divided into five stages: 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc#. **Parse: Option Parsing** 112*f4a2713aSLionel Sambuc 113*f4a2713aSLionel Sambuc The command line argument strings are decomposed into arguments 114*f4a2713aSLionel Sambuc (``Arg`` instances). The driver expects to understand all available 115*f4a2713aSLionel Sambuc options, although there is some facility for just passing certain 116*f4a2713aSLionel Sambuc classes of options through (like ``-Wl,``). 117*f4a2713aSLionel Sambuc 118*f4a2713aSLionel Sambuc Each argument corresponds to exactly one abstract ``Option`` 119*f4a2713aSLionel Sambuc definition, which describes how the option is parsed along with some 120*f4a2713aSLionel Sambuc additional metadata. The Arg instances themselves are lightweight and 121*f4a2713aSLionel Sambuc merely contain enough information for clients to determine which 122*f4a2713aSLionel Sambuc option they correspond to and their values (if they have additional 123*f4a2713aSLionel Sambuc parameters). 124*f4a2713aSLionel Sambuc 125*f4a2713aSLionel Sambuc For example, a command line like "-Ifoo -I foo" would parse to two 126*f4a2713aSLionel Sambuc Arg instances (a JoinedArg and a SeparateArg instance), but each 127*f4a2713aSLionel Sambuc would refer to the same Option. 128*f4a2713aSLionel Sambuc 129*f4a2713aSLionel Sambuc Options are lazily created in order to avoid populating all Option 130*f4a2713aSLionel Sambuc classes when the driver is loaded. Most of the driver code only needs 131*f4a2713aSLionel Sambuc to deal with options by their unique ID (e.g., ``options::OPT_I``), 132*f4a2713aSLionel Sambuc 133*f4a2713aSLionel Sambuc Arg instances themselves do not generally store the values of 134*f4a2713aSLionel Sambuc parameters. In many cases, this would simply result in creating 135*f4a2713aSLionel Sambuc unnecessary string copies. Instead, Arg instances are always embedded 136*f4a2713aSLionel Sambuc inside an ArgList structure, which contains the original vector of 137*f4a2713aSLionel Sambuc argument strings. Each Arg itself only needs to contain an index into 138*f4a2713aSLionel Sambuc this vector instead of storing its values directly. 139*f4a2713aSLionel Sambuc 140*f4a2713aSLionel Sambuc The clang driver can dump the results of this stage using the 141*f4a2713aSLionel Sambuc ``-ccc-print-options`` flag (which must precede any actual command 142*f4a2713aSLionel Sambuc line arguments). For example: 143*f4a2713aSLionel Sambuc 144*f4a2713aSLionel Sambuc .. code-block:: console 145*f4a2713aSLionel Sambuc 146*f4a2713aSLionel Sambuc $ clang -ccc-print-options -Xarch_i386 -fomit-frame-pointer -Wa,-fast -Ifoo -I foo t.c 147*f4a2713aSLionel Sambuc Option 0 - Name: "-Xarch_", Values: {"i386", "-fomit-frame-pointer"} 148*f4a2713aSLionel Sambuc Option 1 - Name: "-Wa,", Values: {"-fast"} 149*f4a2713aSLionel Sambuc Option 2 - Name: "-I", Values: {"foo"} 150*f4a2713aSLionel Sambuc Option 3 - Name: "-I", Values: {"foo"} 151*f4a2713aSLionel Sambuc Option 4 - Name: "<input>", Values: {"t.c"} 152*f4a2713aSLionel Sambuc 153*f4a2713aSLionel Sambuc After this stage is complete the command line should be broken down 154*f4a2713aSLionel Sambuc into well defined option objects with their appropriate parameters. 155*f4a2713aSLionel Sambuc Subsequent stages should rarely, if ever, need to do any string 156*f4a2713aSLionel Sambuc processing. 157*f4a2713aSLionel Sambuc 158*f4a2713aSLionel Sambuc#. **Pipeline: Compilation Job Construction** 159*f4a2713aSLionel Sambuc 160*f4a2713aSLionel Sambuc Once the arguments are parsed, the tree of subprocess jobs needed for 161*f4a2713aSLionel Sambuc the desired compilation sequence are constructed. This involves 162*f4a2713aSLionel Sambuc determining the input files and their types, what work is to be done 163*f4a2713aSLionel Sambuc on them (preprocess, compile, assemble, link, etc.), and constructing 164*f4a2713aSLionel Sambuc a list of Action instances for each task. The result is a list of one 165*f4a2713aSLionel Sambuc or more top-level actions, each of which generally corresponds to a 166*f4a2713aSLionel Sambuc single output (for example, an object or linked executable). 167*f4a2713aSLionel Sambuc 168*f4a2713aSLionel Sambuc The majority of Actions correspond to actual tasks, however there are 169*f4a2713aSLionel Sambuc two special Actions. The first is InputAction, which simply serves to 170*f4a2713aSLionel Sambuc adapt an input argument for use as an input to other Actions. The 171*f4a2713aSLionel Sambuc second is BindArchAction, which conceptually alters the architecture 172*f4a2713aSLionel Sambuc to be used for all of its input Actions. 173*f4a2713aSLionel Sambuc 174*f4a2713aSLionel Sambuc The clang driver can dump the results of this stage using the 175*f4a2713aSLionel Sambuc ``-ccc-print-phases`` flag. For example: 176*f4a2713aSLionel Sambuc 177*f4a2713aSLionel Sambuc .. code-block:: console 178*f4a2713aSLionel Sambuc 179*f4a2713aSLionel Sambuc $ clang -ccc-print-phases -x c t.c -x assembler t.s 180*f4a2713aSLionel Sambuc 0: input, "t.c", c 181*f4a2713aSLionel Sambuc 1: preprocessor, {0}, cpp-output 182*f4a2713aSLionel Sambuc 2: compiler, {1}, assembler 183*f4a2713aSLionel Sambuc 3: assembler, {2}, object 184*f4a2713aSLionel Sambuc 4: input, "t.s", assembler 185*f4a2713aSLionel Sambuc 5: assembler, {4}, object 186*f4a2713aSLionel Sambuc 6: linker, {3, 5}, image 187*f4a2713aSLionel Sambuc 188*f4a2713aSLionel Sambuc Here the driver is constructing seven distinct actions, four to 189*f4a2713aSLionel Sambuc compile the "t.c" input into an object file, two to assemble the 190*f4a2713aSLionel Sambuc "t.s" input, and one to link them together. 191*f4a2713aSLionel Sambuc 192*f4a2713aSLionel Sambuc A rather different compilation pipeline is shown here; in this 193*f4a2713aSLionel Sambuc example there are two top level actions to compile the input files 194*f4a2713aSLionel Sambuc into two separate object files, where each object file is built using 195*f4a2713aSLionel Sambuc ``lipo`` to merge results built for two separate architectures. 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc .. code-block:: console 198*f4a2713aSLionel Sambuc 199*f4a2713aSLionel Sambuc $ clang -ccc-print-phases -c -arch i386 -arch x86_64 t0.c t1.c 200*f4a2713aSLionel Sambuc 0: input, "t0.c", c 201*f4a2713aSLionel Sambuc 1: preprocessor, {0}, cpp-output 202*f4a2713aSLionel Sambuc 2: compiler, {1}, assembler 203*f4a2713aSLionel Sambuc 3: assembler, {2}, object 204*f4a2713aSLionel Sambuc 4: bind-arch, "i386", {3}, object 205*f4a2713aSLionel Sambuc 5: bind-arch, "x86_64", {3}, object 206*f4a2713aSLionel Sambuc 6: lipo, {4, 5}, object 207*f4a2713aSLionel Sambuc 7: input, "t1.c", c 208*f4a2713aSLionel Sambuc 8: preprocessor, {7}, cpp-output 209*f4a2713aSLionel Sambuc 9: compiler, {8}, assembler 210*f4a2713aSLionel Sambuc 10: assembler, {9}, object 211*f4a2713aSLionel Sambuc 11: bind-arch, "i386", {10}, object 212*f4a2713aSLionel Sambuc 12: bind-arch, "x86_64", {10}, object 213*f4a2713aSLionel Sambuc 13: lipo, {11, 12}, object 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc After this stage is complete the compilation process is divided into 216*f4a2713aSLionel Sambuc a simple set of actions which need to be performed to produce 217*f4a2713aSLionel Sambuc intermediate or final outputs (in some cases, like ``-fsyntax-only``, 218*f4a2713aSLionel Sambuc there is no "real" final output). Phases are well known compilation 219*f4a2713aSLionel Sambuc steps, such as "preprocess", "compile", "assemble", "link", etc. 220*f4a2713aSLionel Sambuc 221*f4a2713aSLionel Sambuc#. **Bind: Tool & Filename Selection** 222*f4a2713aSLionel Sambuc 223*f4a2713aSLionel Sambuc This stage (in conjunction with the Translate stage) turns the tree 224*f4a2713aSLionel Sambuc of Actions into a list of actual subprocess to run. Conceptually, the 225*f4a2713aSLionel Sambuc driver performs a top down matching to assign Action(s) to Tools. The 226*f4a2713aSLionel Sambuc ToolChain is responsible for selecting the tool to perform a 227*f4a2713aSLionel Sambuc particular action; once selected the driver interacts with the tool 228*f4a2713aSLionel Sambuc to see if it can match additional actions (for example, by having an 229*f4a2713aSLionel Sambuc integrated preprocessor). 230*f4a2713aSLionel Sambuc 231*f4a2713aSLionel Sambuc Once Tools have been selected for all actions, the driver determines 232*f4a2713aSLionel Sambuc how the tools should be connected (for example, using an inprocess 233*f4a2713aSLionel Sambuc module, pipes, temporary files, or user provided filenames). If an 234*f4a2713aSLionel Sambuc output file is required, the driver also computes the appropriate 235*f4a2713aSLionel Sambuc file name (the suffix and file location depend on the input types and 236*f4a2713aSLionel Sambuc options such as ``-save-temps``). 237*f4a2713aSLionel Sambuc 238*f4a2713aSLionel Sambuc The driver interacts with a ToolChain to perform the Tool bindings. 239*f4a2713aSLionel Sambuc Each ToolChain contains information about all the tools needed for 240*f4a2713aSLionel Sambuc compilation for a particular architecture, platform, and operating 241*f4a2713aSLionel Sambuc system. A single driver invocation may query multiple ToolChains 242*f4a2713aSLionel Sambuc during one compilation in order to interact with tools for separate 243*f4a2713aSLionel Sambuc architectures. 244*f4a2713aSLionel Sambuc 245*f4a2713aSLionel Sambuc The results of this stage are not computed directly, but the driver 246*f4a2713aSLionel Sambuc can print the results via the ``-ccc-print-bindings`` option. For 247*f4a2713aSLionel Sambuc example: 248*f4a2713aSLionel Sambuc 249*f4a2713aSLionel Sambuc .. code-block:: console 250*f4a2713aSLionel Sambuc 251*f4a2713aSLionel Sambuc $ clang -ccc-print-bindings -arch i386 -arch ppc t0.c 252*f4a2713aSLionel Sambuc # "i386-apple-darwin9" - "clang", inputs: ["t0.c"], output: "/tmp/cc-Sn4RKF.s" 253*f4a2713aSLionel Sambuc # "i386-apple-darwin9" - "darwin::Assemble", inputs: ["/tmp/cc-Sn4RKF.s"], output: "/tmp/cc-gvSnbS.o" 254*f4a2713aSLionel Sambuc # "i386-apple-darwin9" - "darwin::Link", inputs: ["/tmp/cc-gvSnbS.o"], output: "/tmp/cc-jgHQxi.out" 255*f4a2713aSLionel Sambuc # "ppc-apple-darwin9" - "gcc::Compile", inputs: ["t0.c"], output: "/tmp/cc-Q0bTox.s" 256*f4a2713aSLionel Sambuc # "ppc-apple-darwin9" - "gcc::Assemble", inputs: ["/tmp/cc-Q0bTox.s"], output: "/tmp/cc-WCdicw.o" 257*f4a2713aSLionel Sambuc # "ppc-apple-darwin9" - "gcc::Link", inputs: ["/tmp/cc-WCdicw.o"], output: "/tmp/cc-HHBEBh.out" 258*f4a2713aSLionel Sambuc # "i386-apple-darwin9" - "darwin::Lipo", inputs: ["/tmp/cc-jgHQxi.out", "/tmp/cc-HHBEBh.out"], output: "a.out" 259*f4a2713aSLionel Sambuc 260*f4a2713aSLionel Sambuc This shows the tool chain, tool, inputs and outputs which have been 261*f4a2713aSLionel Sambuc bound for this compilation sequence. Here clang is being used to 262*f4a2713aSLionel Sambuc compile t0.c on the i386 architecture and darwin specific versions of 263*f4a2713aSLionel Sambuc the tools are being used to assemble and link the result, but generic 264*f4a2713aSLionel Sambuc gcc versions of the tools are being used on PowerPC. 265*f4a2713aSLionel Sambuc 266*f4a2713aSLionel Sambuc#. **Translate: Tool Specific Argument Translation** 267*f4a2713aSLionel Sambuc 268*f4a2713aSLionel Sambuc Once a Tool has been selected to perform a particular Action, the 269*f4a2713aSLionel Sambuc Tool must construct concrete Jobs which will be executed during 270*f4a2713aSLionel Sambuc compilation. The main work is in translating from the gcc style 271*f4a2713aSLionel Sambuc command line options to whatever options the subprocess expects. 272*f4a2713aSLionel Sambuc 273*f4a2713aSLionel Sambuc Some tools, such as the assembler, only interact with a handful of 274*f4a2713aSLionel Sambuc arguments and just determine the path of the executable to call and 275*f4a2713aSLionel Sambuc pass on their input and output arguments. Others, like the compiler 276*f4a2713aSLionel Sambuc or the linker, may translate a large number of arguments in addition. 277*f4a2713aSLionel Sambuc 278*f4a2713aSLionel Sambuc The ArgList class provides a number of simple helper methods to 279*f4a2713aSLionel Sambuc assist with translating arguments; for example, to pass on only the 280*f4a2713aSLionel Sambuc last of arguments corresponding to some option, or all arguments for 281*f4a2713aSLionel Sambuc an option. 282*f4a2713aSLionel Sambuc 283*f4a2713aSLionel Sambuc The result of this stage is a list of Jobs (executable paths and 284*f4a2713aSLionel Sambuc argument strings) to execute. 285*f4a2713aSLionel Sambuc 286*f4a2713aSLionel Sambuc#. **Execute** 287*f4a2713aSLionel Sambuc 288*f4a2713aSLionel Sambuc Finally, the compilation pipeline is executed. This is mostly 289*f4a2713aSLionel Sambuc straightforward, although there is some interaction with options like 290*f4a2713aSLionel Sambuc ``-pipe``, ``-pass-exit-codes`` and ``-time``. 291*f4a2713aSLionel Sambuc 292*f4a2713aSLionel SambucAdditional Notes 293*f4a2713aSLionel Sambuc---------------- 294*f4a2713aSLionel Sambuc 295*f4a2713aSLionel SambucThe Compilation Object 296*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^ 297*f4a2713aSLionel Sambuc 298*f4a2713aSLionel SambucThe driver constructs a Compilation object for each set of command line 299*f4a2713aSLionel Sambucarguments. The Driver itself is intended to be invariant during 300*f4a2713aSLionel Sambucconstruction of a Compilation; an IDE should be able to construct a 301*f4a2713aSLionel Sambucsingle long lived driver instance to use for an entire build, for 302*f4a2713aSLionel Sambucexample. 303*f4a2713aSLionel Sambuc 304*f4a2713aSLionel SambucThe Compilation object holds information that is particular to each 305*f4a2713aSLionel Sambuccompilation sequence. For example, the list of used temporary files 306*f4a2713aSLionel Sambuc(which must be removed once compilation is finished) and result files 307*f4a2713aSLionel Sambuc(which should be removed if compilation fails). 308*f4a2713aSLionel Sambuc 309*f4a2713aSLionel SambucUnified Parsing & Pipelining 310*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 311*f4a2713aSLionel Sambuc 312*f4a2713aSLionel SambucParsing and pipelining both occur without reference to a Compilation 313*f4a2713aSLionel Sambucinstance. This is by design; the driver expects that both of these 314*f4a2713aSLionel Sambucphases are platform neutral, with a few very well defined exceptions 315*f4a2713aSLionel Sambucsuch as whether the platform uses a driver driver. 316*f4a2713aSLionel Sambuc 317*f4a2713aSLionel SambucToolChain Argument Translation 318*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 319*f4a2713aSLionel Sambuc 320*f4a2713aSLionel SambucIn order to match gcc very closely, the clang driver currently allows 321*f4a2713aSLionel Sambuctool chains to perform their own translation of the argument list (into 322*f4a2713aSLionel Sambuca new ArgList data structure). Although this allows the clang driver to 323*f4a2713aSLionel Sambucmatch gcc easily, it also makes the driver operation much harder to 324*f4a2713aSLionel Sambucunderstand (since the Tools stop seeing some arguments the user 325*f4a2713aSLionel Sambucprovided, and see new ones instead). 326*f4a2713aSLionel Sambuc 327*f4a2713aSLionel SambucFor example, on Darwin ``-gfull`` gets translated into two separate 328*f4a2713aSLionel Sambucarguments, ``-g`` and ``-fno-eliminate-unused-debug-symbols``. Trying to 329*f4a2713aSLionel Sambucwrite Tool logic to do something with ``-gfull`` will not work, because 330*f4a2713aSLionel SambucTool argument translation is done after the arguments have been 331*f4a2713aSLionel Sambuctranslated. 332*f4a2713aSLionel Sambuc 333*f4a2713aSLionel SambucA long term goal is to remove this tool chain specific translation, and 334*f4a2713aSLionel Sambucinstead force each tool to change its own logic to do the right thing on 335*f4a2713aSLionel Sambucthe untranslated original arguments. 336*f4a2713aSLionel Sambuc 337*f4a2713aSLionel SambucUnused Argument Warnings 338*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^ 339*f4a2713aSLionel Sambuc 340*f4a2713aSLionel SambucThe driver operates by parsing all arguments but giving Tools the 341*f4a2713aSLionel Sambucopportunity to choose which arguments to pass on. One downside of this 342*f4a2713aSLionel Sambucinfrastructure is that if the user misspells some option, or is confused 343*f4a2713aSLionel Sambucabout which options to use, some command line arguments the user really 344*f4a2713aSLionel Sambuccared about may go unused. This problem is particularly important when 345*f4a2713aSLionel Sambucusing clang as a compiler, since the clang compiler does not support 346*f4a2713aSLionel Sambucanywhere near all the options that gcc does, and we want to make sure 347*f4a2713aSLionel Sambucusers know which ones are being used. 348*f4a2713aSLionel Sambuc 349*f4a2713aSLionel SambucTo support this, the driver maintains a bit associated with each 350*f4a2713aSLionel Sambucargument of whether it has been used (at all) during the compilation. 351*f4a2713aSLionel SambucThis bit usually doesn't need to be set by hand, as the key ArgList 352*f4a2713aSLionel Sambucaccessors will set it automatically. 353*f4a2713aSLionel Sambuc 354*f4a2713aSLionel SambucWhen a compilation is successful (there are no errors), the driver 355*f4a2713aSLionel Sambucchecks the bit and emits an "unused argument" warning for any arguments 356*f4a2713aSLionel Sambucwhich were never accessed. This is conservative (the argument may not 357*f4a2713aSLionel Sambuchave been used to do what the user wanted) but still catches the most 358*f4a2713aSLionel Sambucobvious cases. 359*f4a2713aSLionel Sambuc 360*f4a2713aSLionel SambucRelation to GCC Driver Concepts 361*f4a2713aSLionel Sambuc------------------------------- 362*f4a2713aSLionel Sambuc 363*f4a2713aSLionel SambucFor those familiar with the gcc driver, this section provides a brief 364*f4a2713aSLionel Sambucoverview of how things from the gcc driver map to the clang driver. 365*f4a2713aSLionel Sambuc 366*f4a2713aSLionel Sambuc- **Driver Driver** 367*f4a2713aSLionel Sambuc 368*f4a2713aSLionel Sambuc The driver driver is fully integrated into the clang driver. The 369*f4a2713aSLionel Sambuc driver simply constructs additional Actions to bind the architecture 370*f4a2713aSLionel Sambuc during the *Pipeline* phase. The tool chain specific argument 371*f4a2713aSLionel Sambuc translation is responsible for handling ``-Xarch_``. 372*f4a2713aSLionel Sambuc 373*f4a2713aSLionel Sambuc The one caveat is that this approach requires ``-Xarch_`` not be used 374*f4a2713aSLionel Sambuc to alter the compilation itself (for example, one cannot provide 375*f4a2713aSLionel Sambuc ``-S`` as an ``-Xarch_`` argument). The driver attempts to reject 376*f4a2713aSLionel Sambuc such invocations, and overall there isn't a good reason to abuse 377*f4a2713aSLionel Sambuc ``-Xarch_`` to that end in practice. 378*f4a2713aSLionel Sambuc 379*f4a2713aSLionel Sambuc The upside is that the clang driver is more efficient and does little 380*f4a2713aSLionel Sambuc extra work to support universal builds. It also provides better error 381*f4a2713aSLionel Sambuc reporting and UI consistency. 382*f4a2713aSLionel Sambuc 383*f4a2713aSLionel Sambuc- **Specs** 384*f4a2713aSLionel Sambuc 385*f4a2713aSLionel Sambuc The clang driver has no direct correspondent for "specs". The 386*f4a2713aSLionel Sambuc majority of the functionality that is embedded in specs is in the 387*f4a2713aSLionel Sambuc Tool specific argument translation routines. The parts of specs which 388*f4a2713aSLionel Sambuc control the compilation pipeline are generally part of the *Pipeline* 389*f4a2713aSLionel Sambuc stage. 390*f4a2713aSLionel Sambuc 391*f4a2713aSLionel Sambuc- **Toolchains** 392*f4a2713aSLionel Sambuc 393*f4a2713aSLionel Sambuc The gcc driver has no direct understanding of tool chains. Each gcc 394*f4a2713aSLionel Sambuc binary roughly corresponds to the information which is embedded 395*f4a2713aSLionel Sambuc inside a single ToolChain. 396*f4a2713aSLionel Sambuc 397*f4a2713aSLionel Sambuc The clang driver is intended to be portable and support complex 398*f4a2713aSLionel Sambuc compilation environments. All platform and tool chain specific code 399*f4a2713aSLionel Sambuc should be protected behind either abstract or well defined interfaces 400*f4a2713aSLionel Sambuc (such as whether the platform supports use as a driver driver). 401