blob: 041aedf9e16db74779b9219bc09ca967fd87e891 [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkov23f522a2008-12-13 17:51:47 +00004..
5 This file was automatically generated by rst2html.
6 Please do not edit directly!
7 The ReST source lives in the directory 'tools/llvmc/doc'.
Mikhail Glushenkovd5652032008-12-13 02:28:58 +00008
9.. contents::
10
11.. raw:: html
12
13 <div class="doc_author">
14 <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
15 </div>
16
17Introduction
18============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000019
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000020LLVMC is a generic compiler driver, designed to be customizable and
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000021extensible. It plays the same role for LLVM as the ``gcc`` program does for
22GCC - LLVMC's job is essentially to transform a set of input files into a set of
23targets depending on configuration rules and user options. What makes LLVMC
24different is that these transformation rules are completely customizable - in
25fact, LLVMC knows nothing about the specifics of transformation (even the
26command-line options are mostly not hard-coded) and regards the transformation
27structure as an abstract graph. The structure of this graph is described in
28high-level TableGen code, from which an efficient C++ representation is
29automatically derived. This makes it possible to adapt LLVMC for other
30purposes - for example, as a build tool for game resources.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000032Because LLVMC employs TableGen_ as its configuration language, you
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000033need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000034
Mikhail Glushenkov4aecec12009-06-17 02:56:08 +000035.. _TableGen: http://llvm.org/docs/TableGenFundamentals.html
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000036
37
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000038Compiling with ``llvmc``
39========================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000040
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000041LLVMC tries hard to be as compatible with ``gcc`` as possible,
42although there are some small differences. Most of the time, however,
43you shouldn't be able to notice them::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000044
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000045 $ # This works as expected:
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000046 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000047 $ ./a.out
48 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4aecec12009-06-17 02:56:08 +000050One nice feature of LLVMC is that one doesn't have to distinguish between
51different compilers for different languages (think ``g++`` vs. ``gcc``) - the
52right toolchain is chosen automatically based on input language names (which
53are, in turn, determined from file extensions). If you want to force files
54ending with ".c" to compile as C++, use the ``-x`` option, just like you would
55do it with ``gcc``::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000056
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000057 $ # hello.c is really a C++ file
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000058 $ llvmc -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000059 $ ./a.out
60 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000061
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000062On the other hand, when using LLVMC as a linker to combine several C++
63object files you should provide the ``--linker`` option since it's
64impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000065
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000066 $ llvmc -c hello.cpp
67 $ llvmc hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000068 [A lot of link-time errors skipped]
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000069 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000070 $ ./a.out
71 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000072
Mikhail Glushenkove8e4d582009-06-30 00:16:22 +000073By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is also
74possible to choose the ``clang`` compiler with the ``-clang`` option.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +000075
Mikhail Glushenkov83237482008-10-15 09:29:13 +000076
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000077Predefined options
78==================
79
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000080LLVMC has some built-in options that can't be overridden in the TableGen code:
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000081
82* ``-o FILE`` - Output file name.
83
84* ``-x LANGUAGE`` - Specify the language of the following input files
85 until the next -x option.
86
87* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
88
Mikhail Glushenkov294f5072009-06-25 18:20:44 +000089* ``--save-temps`` - Write temporary files to the current directory and do not
90 delete them on exit. This option can also take an argument: the
91 ``--save-temps=obj`` switch will write files into the directory specified with
92 the ``-o`` option. The ``--save-temps=cwd`` and ``--save-temps`` switches are
93 both synonyms for the default behaviour.
94
Mikhail Glushenkov09826e32009-07-11 19:27:40 +000095* ``--temp-dir DIRECTORY`` - Store temporary files in the given directory. This
96 directory is deleted on exit unless ``--save-temps`` is specified. If
97 ``--save-temps=obj`` is also specified, ``--temp-dir`` is given the
98 precedence.
Mikhail Glushenkov792f1822009-07-09 19:39:16 +000099
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000100* ``--check-graph`` - Check the compilation for common errors like mismatched
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000101 output/input language names, multiple default edges and cycles. Exit with code
102 zero if no errors were found, and return the number of found errors
103 otherwise. Hidden option, useful for debugging.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000104
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000105* ``--view-graph`` - Show a graphical representation of the compilation graph
106 and exit. Requires that you have ``dot`` and ``gv`` programs installed. Hidden
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000107 option, useful for debugging.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000108
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000109* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the current
110 directory with the compilation graph description in Graphviz format (identical
Mikhail Glushenkov530f3992009-06-16 00:13:52 +0000111 to the file used by the ``--view-graph`` option). The ``-o`` option can be
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000112 used to set the output file name. Hidden option, useful for debugging.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000113
Mikhail Glushenkov73296102008-05-30 06:29:17 +0000114* ``--help``, ``--help-hidden``, ``--version`` - These options have
115 their standard meaning.
116
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000117Compiling LLVMC-based drivers
118=============================
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000119
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000120It's easiest to start working on your own LLVMC driver by copying the skeleton
121project which lives under ``$LLVMC_DIR/examples/Skeleton``::
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000122
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000123 $ cd $LLVMC_DIR/examples
124 $ cp -r Skeleton MyDriver
125 $ cd MyDriver
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000126 $ ls
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000127 AutoGenerated.td Hooks.cpp Main.cpp Makefile
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000128
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000129As you can see, our basic driver consists of only three files (not counting the
130build script). ``AutoGenerated.td`` contains TableGen description of the
131compilation graph; its format is documented in the following
132sections. ``Hooks.cpp`` is an empty file that should be used for hook
133definitions (see `below`__). ``Main.cpp`` is just a helper used to compile the
134auto-generated C++ code produced from TableGen source.
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000135
136__ hooks_
137
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000138The first thing that you should do is to change the ``LLVMC_BASED_DRIVER``
139variable in the ``Makefile``::
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000140
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000141 LLVMC_BASED_DRIVER=MyDriver
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000142
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000143It can also be a good idea to put your TableGen code into a file with a less
144generic name::
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000145
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000146 $ touch MyDriver.td
147 $ vim AutoGenerated.td
148 [...]
149 include "MyDriver.td"
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000150
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000151If you have more than one TableGen source file, they all should be included from
152``AutoGenerated.td``, since this file is used by the build system to generate
153C++ code.
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000154
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000155To build your driver, just ``cd`` to its source directory and run ``make``. The
156resulting executable will be put into ``$LLVM_OBJ_DIR/$(BuildMode)/bin``.
Mikhail Glushenkov530f3992009-06-16 00:13:52 +0000157
158If you're compiling LLVM with different source and object directories, then you
159must perform the following additional steps before running ``make``::
160
161 # LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
162 # LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000163 $ mkdir $LLVMC_OBJ_DIR/examples/MyDriver/
164 $ cp $LLVMC_SRC_DIR/examples/MyDriver/Makefile \
165 $LLVMC_OBJ_DIR/examples/MyDriver/
166 $ cd $LLVMC_OBJ_DIR/examples/MyDriver
Mikhail Glushenkov530f3992009-06-16 00:13:52 +0000167 $ make
168
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000169
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000170Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000171========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000172
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000173Each TableGen configuration file should include the common definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000174
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000175 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000176
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000177Internally, LLVMC stores information about possible source transformations in
178form of a graph. Nodes in this graph represent tools, and edges between two
179nodes represent a transformation path. A special "root" node is used to mark
180entry points for the transformations. LLVMC also assigns a weight to each edge
181(more on this later) to choose between several alternative edges.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000182
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000183The definition of the compilation graph (see file ``llvmc/src/Base.td`` for an
184example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000185
186 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000187 Edge<"root", "llvm_gcc_c">,
188 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000189 ...
190
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000191 Edge<"llvm_gcc_c", "llc">,
192 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000193 ...
194
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000195 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
196 (inc_weight))>,
197 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
198 (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000199 ...
200
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000201 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000202 (case (input_languages_contain "c++"), (inc_weight),
203 (or (parameter_equals "linker", "g++"),
204 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000205 ...
206
207 ]>;
208
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000209As you can see, the edges can be either default or optional, where optional
210edges are differentiated by an additional ``case`` expression used to calculate
211the weight of this edge. Notice also that we refer to tools via their names (as
212strings). This makes it possible to add edges to an existing compilation graph
213without having to know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000214
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000215The default edges are assigned a weight of 1, and optional edges get a weight of
2160 + 2*N where N is the number of tests that evaluated to true in the ``case``
217expression. It is also possible to provide an integer parameter to
218``inc_weight`` and ``dec_weight`` - in this case, the weight is increased (or
219decreased) by the provided value instead of the default 2. Default weight of an
220optional edge can be changed by using the ``default`` clause of the ``case``
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000221construct.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000222
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000223When passing an input file through the graph, LLVMC picks the edge with the
224maximum weight. To avoid ambiguity, there should be only one default edge
225between two nodes (with the exception of the root node, which gets a special
226treatment - there you are allowed to specify one default edge *per language*).
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000227
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000228When multiple compilation graphs are defined, they are merged together. Multiple
229edges with the same end nodes are not allowed (i.e. the graph is not a
230multigraph), and will lead to a compile-time error.
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000231
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000232To get a visual representation of the compilation graph (useful for debugging),
233run ``llvmc --view-graph``. You will need ``dot`` and ``gsview`` installed for
234this to work properly.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000235
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000236Describing options
237==================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000238
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000239Command-line options supported by the driver are defined by using an
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000240``OptionList``::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000241
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000242 def Options : OptionList<[
243 (switch_option "E", (help "Help string")),
244 (alias_option "quiet", "q")
245 ...
246 ]>;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000247
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000248As you can see, the option list is just a list of DAGs, where each DAG is an
249option description consisting of the option name and some properties. More than
250one option list can be defined (they are all merged together in the end), which
251can be handy if one wants to separate option groups syntactically.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000252
253* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000255 - ``switch_option`` - a simple boolean switch without arguments, for example
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000256 ``-O2`` or ``-time``. At most one occurrence is allowed by default.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000258 - ``parameter_option`` - option that takes one argument, for example
259 ``-std=c99``. It is also allowed to use spaces instead of the equality
260 sign: ``-std c99``. At most one occurrence is allowed.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000261
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000262 - ``parameter_list_option`` - same as the above, but more than one option
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000263 occurrence is allowed.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000265 - ``prefix_option`` - same as the parameter_option, but the option name and
266 argument do not have to be separated. Example: ``-ofile``. This can be also
267 specified as ``-o file``; however, ``-o=file`` will be parsed incorrectly
268 (``=file`` will be interpreted as option value). At most one occurrence is
269 allowed.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000270
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000271 - ``prefix_list_option`` - same as the above, but more than one occurrence of
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000272 the option is allowed; example: ``-lm -lpthread``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000274 - ``alias_option`` - a special option type for creating aliases. Unlike other
275 option types, aliases are not allowed to have any properties besides the
Mikhail Glushenkov4d5a2992010-11-22 17:10:20 +0000276 aliased option name.
277 Usage example: ``(alias_option "preprocess", "E")``
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000278
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000279 - ``switch_list_option`` - like ``switch_option`` with the ``zero_or_more``
280 property, but remembers how many times the switch was turned on. Useful
281 mostly for forwarding. Example: when ``-foo`` is a switch option (with the
282 ``zero_or_more`` property), the command ``driver -foo -foo`` is forwarded
283 as ``some-tool -foo``, but when ``-foo`` is a switch list, the same command
284 is forwarded as ``some-tool -foo -foo``.
285
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000286
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000287* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000288
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000289 - ``help`` - help string associated with this option. Used for ``--help``
290 output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000291
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000292 - ``required`` - this option must be specified exactly once (or, in case of
293 the list options without the ``multi_val`` property, at least
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000294 once). Incompatible with ``optional`` and ``one_or_more``.
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000295
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000296 - ``optional`` - the option can be specified either zero times or exactly
297 once. The default for switch options. Useful only for list options in
298 conjunction with ``multi_val``. Incompatible with ``required``,
299 ``zero_or_more`` and ``one_or_more``.
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000300
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000301 - ``one_or_more`` - the option must be specified at least once. Can be useful
302 to allow switch options be both obligatory and be specified multiple
303 times. For list options is useful only in conjunction with ``multi_val``;
304 for ordinary it is synonymous with ``required``. Incompatible with
305 ``required``, ``optional`` and ``zero_or_more``.
306
307 - ``zero_or_more`` - the option can be specified zero or more times. Useful
308 to allow a single switch option to be specified more than
309 once. Incompatible with ``required``, ``optional`` and ``one_or_more``.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000310
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000311 - ``hidden`` - the description of this option will not appear in
312 the ``--help`` output (but will appear in the ``--help-hidden``
313 output).
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000314
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000315 - ``really_hidden`` - the option will not be mentioned in any help
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000316 output.
317
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000318 - ``comma_separated`` - Indicates that any commas specified for an option's
319 value should be used to split the value up into multiple values for the
320 option. This property is valid only for list options. In conjunction with
321 ``forward_value`` can be used to implement option forwarding in style of
322 gcc's ``-Wa,``.
323
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000324 - ``multi_val n`` - this option takes *n* arguments (can be useful in some
325 special cases). Usage example: ``(parameter_list_option "foo", (multi_val
Mikhail Glushenkov4e613872009-10-21 02:13:52 +0000326 3))``; the command-line syntax is '-foo a b c'. Only list options can have
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000327 this attribute; you can, however, use the ``one_or_more``, ``optional``
Mikhail Glushenkov4e613872009-10-21 02:13:52 +0000328 and ``required`` properties.
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000329
Mikhail Glushenkovdad78202009-07-07 16:09:29 +0000330 - ``init`` - this option has a default value, either a string (if it is a
Mikhail Glushenkov68d475a2009-12-15 03:03:37 +0000331 parameter), or a boolean (if it is a switch; as in C++, boolean constants
332 are called ``true`` and ``false``). List options can't have ``init``
333 attribute.
334 Usage examples: ``(switch_option "foo", (init true))``; ``(prefix_option
335 "bar", (init "baz"))``.
Mikhail Glushenkovdad78202009-07-07 16:09:29 +0000336
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000337.. _case:
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000338
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000339Conditional evaluation
340======================
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000341
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000342The 'case' construct is the main means by which programmability is achieved in
343LLVMC. It can be used to calculate edge weights, program actions and modify the
344shell commands to be executed. The 'case' expression is designed after the
345similarly-named construct in functional languages and takes the form ``(case
346(test_1), statement_1, (test_2), statement_2, ... (test_N), statement_N)``. The
347statements are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000348
349Examples::
350
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000351 // Edge weight calculation
352
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000353 // Increases edge weight by 5 if "-A" is provided on the
354 // command-line, and by 5 more if "-B" is also provided.
355 (case
356 (switch_on "A"), (inc_weight 5),
357 (switch_on "B"), (inc_weight 5))
358
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000359
360 // Tool command line specification
361
362 // Evaluates to "cmdline1" if the option "-A" is provided on the
363 // command line; to "cmdline2" if "-B" is provided;
364 // otherwise to "cmdline3".
365
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000366 (case
367 (switch_on "A"), "cmdline1",
368 (switch_on "B"), "cmdline2",
369 (default), "cmdline3")
370
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000371Note the slight difference in 'case' expression handling in contexts of edge
372weights and command line specification - in the second example the value of the
373``"B"`` switch is never checked when switch ``"A"`` is enabled, and the whole
374expression always evaluates to ``"cmdline1"`` in that case.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000375
376Case expressions can also be nested, i.e. the following is legal::
377
378 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
379 (default), ...)
380
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000381You should, however, try to avoid doing that because it hurts readability. It is
382usually better to split tool descriptions and/or use TableGen inheritance
383instead.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000384
385* Possible tests are:
386
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000387 - ``switch_on`` - Returns true if a given command-line switch is provided by
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000388 the user. Can be given multiple arguments, in that case ``(switch_on "foo",
389 "bar", "baz")`` is equivalent to ``(and (switch_on "foo"), (switch_on
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000390 "bar"), (switch_on "baz"))``.
391 Example: ``(switch_on "opt")``.
392
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000393 - ``any_switch_on`` - Given a number of switch options, returns true if any of
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000394 the switches is turned on.
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000395 Example: ``(any_switch_on "foo", "bar", "baz")`` is equivalent to ``(or
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000396 (switch_on "foo"), (switch_on "bar"), (switch_on "baz"))``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000397
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000398 - ``parameter_equals`` - Returns true if a command-line parameter (first
399 argument) equals a given value (second argument).
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000400 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000401
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000402 - ``element_in_list`` - Returns true if a command-line parameter list (first
403 argument) contains a given value (second argument).
Mikhail Glushenkov4227c0f2009-12-01 09:19:09 +0000404 Example: ``(element_in_list "l", "pthread")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000405
406 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000407 belongs to the current input language set.
408 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000409
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000410 - ``in_language`` - Evaluates to true if the input file language is equal to
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000411 the argument. At the moment works only with ``command`` and ``actions`` (on
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000412 non-join nodes).
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000413 Example: ``(in_language "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000414
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000415 - ``not_empty`` - Returns true if a given option (which should be either a
416 parameter or a parameter list) is set by the user. Like ``switch_on``, can
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000417 be also given multiple arguments.
418 Examples: ``(not_empty "o")``, ``(not_empty "o", "l")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000419
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000420 - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000421 the provided options.
422 Example: ``(any_not_empty "foo", "bar", "baz")`` is equivalent to ``(or
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000423 (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``.
424
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000425 - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000426 X))``. Can be given multiple arguments.
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000427
428 - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000429 the provided options.
430 Example: ``(any_empty "foo", "bar", "baz")`` is equivalent to ``(or
431 (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``.
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000432
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +0000433 - ``single_input_file`` - Returns true if there was only one input file
434 provided on the command-line. Used without arguments:
435 ``(single_input_file)``.
436
437 - ``multiple_input_files`` - Equivalent to ``(not (single_input_file))`` (the
438 case of zero input files is considered an error).
439
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000440 - ``default`` - Always evaluates to true. Should always be the last
441 test in the ``case`` expression.
442
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000443 - ``and`` - A standard logical combinator that returns true iff all of
Mikhail Glushenkovd66e8de2009-09-28 01:16:07 +0000444 its arguments return true. Used like this: ``(and (test1), (test2),
445 ... (testN))``. Nesting of ``and`` and ``or`` is allowed, but not
446 encouraged.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000447
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000448 - ``or`` - A logical combinator that returns true iff any of its arguments
449 return true.
450 Example: ``(or (test1), (test2), ... (testN))``.
Mikhail Glushenkovd66e8de2009-09-28 01:16:07 +0000451
452 - ``not`` - Standard unary logical combinator that negates its
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000453 argument.
454 Example: ``(not (or (test1), (test2), ... (testN)))``.
Mikhail Glushenkovd66e8de2009-09-28 01:16:07 +0000455
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000456
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000457Writing a tool description
458==========================
459
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000460As was said earlier, nodes in the compilation graph represent tools, which are
461described separately. A tool definition looks like this (taken from the
462``llvmc/src/Base.td`` file)::
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000463
464 def llvm_gcc_cpp : Tool<[
465 (in_language "c++"),
466 (out_language "llvm-assembler"),
467 (output_suffix "bc"),
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000468 (command "llvm-g++ -c -emit-llvm"),
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000469 (sink)
470 ]>;
471
472This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000473``llvm-g++``. As you can see, a tool definition is just a list of properties;
474most of them should be self-explanatory. The ``sink`` property means that this
475tool should be passed all command-line options that aren't mentioned in the
476option list.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000477
478The complete list of all currently implemented tool properties follows.
479
480* Possible tool properties:
481
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000482 - ``in_language`` - input language name. Can be given multiple arguments, in
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000483 case the tool supports multiple input languages. Used for typechecking and
484 mapping file extensions to tools.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000485
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000486 - ``out_language`` - output language name. Multiple output languages are
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000487 allowed. Used for typechecking the compilation graph.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000488
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000489 - ``output_suffix`` - output file suffix. Can also be changed dynamically, see
490 documentation on `actions`__.
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000491
492__ actions_
493
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000494 - ``command`` - the actual command used to run the tool. You can use output
495 redirection with ``>``, hook invocations (``$CALL``), environment variables
496 (via ``$ENV``) and the ``case`` construct.
497
498 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a list of
499 input files and joins them together. Used for linkers.
500
501 - ``sink`` - all command-line options that are not handled by other tools are
502 passed to this tool.
503
504 - ``actions`` - A single big ``case`` expression that specifies how this tool
505 reacts on command-line options (described in more detail `below`__).
506
507__ actions_
508
509 - ``out_file_option``, ``in_file_option`` - Options appended to the
510 ``command`` string to designate output and input files. Default values are
511 ``"-o"`` and ``""``, respectively.
512
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000513.. _actions:
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000514
515Actions
516-------
517
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000518A tool often needs to react to command-line options, and this is precisely what
519the ``actions`` property is for. The next example illustrates this feature::
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000520
521 def llvm_gcc_linker : Tool<[
522 (in_language "object-code"),
523 (out_language "executable"),
524 (output_suffix "out"),
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000525 (command "llvm-gcc"),
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000526 (join),
527 (actions (case (not_empty "L"), (forward "L"),
528 (not_empty "l"), (forward "l"),
529 (not_empty "dummy"),
530 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
531 ]>;
532
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000533The ``actions`` tool property is implemented on top of the omnipresent ``case``
534expression. It associates one or more different *actions* with given
535conditions - in the example, the actions are ``forward``, which forwards a given
536option unchanged, and ``append_cmd``, which appends a given string to the tool
537execution command. Multiple actions can be associated with a single condition by
538using a list of actions (used in the example to append some dummy options). The
539same ``case`` construct can also be used in the ``cmd_line`` property to modify
540the tool command line.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000541
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000542The "join" property used in the example means that this tool behaves like a
543linker.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000544
545The list of all possible actions follows.
546
547* Possible actions:
548
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000549 - ``append_cmd`` - Append a string to the tool invocation command.
550 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``.
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000551
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000552 - ``error`` - Exit with error.
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000553 Example: ``(error "Mixing -c and -S is not allowed!")``.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000554
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000555 - ``warning`` - Print a warning.
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000556 Example: ``(warning "Specifying both -O1 and -O2 is meaningless!")``.
557
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000558 - ``forward`` - Forward the option unchanged.
559 Example: ``(forward "Wall")``.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000560
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000561 - ``forward_as`` - Change the option's name, but forward the argument
562 unchanged.
Mikhail Glushenkove89331b2009-05-06 01:41:19 +0000563 Example: ``(forward_as "O0", "--disable-optimization")``.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000564
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000565 - ``forward_value`` - Forward only option's value. Cannot be used with switch
566 options (since they don't have values), but works fine with lists.
567 Example: ``(forward_value "Wa,")``.
568
569 - ``forward_transformed_value`` - As above, but applies a hook to the
570 option's value before forwarding (see `below`__). When
571 ``forward_transformed_value`` is applied to a list
572 option, the hook must have signature
573 ``std::string hooks::HookName (const std::vector<std::string>&)``.
574 Example: ``(forward_transformed_value "m", "ConvertToMAttr")``.
575
576 __ hooks_
577
578 - ``output_suffix`` - Modify the output suffix of this tool.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000579 Example: ``(output_suffix "i")``.
580
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000581 - ``stop_compilation`` - Stop compilation after this tool processes its
582 input. Used without arguments.
583 Example: ``(stop_compilation)``.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000584
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000585
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000586Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000587============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000588
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000589If you are adding support for a new language to LLVMC, you'll need to modify the
590language map, which defines mappings from file extensions to language names. It
591is used to choose the proper toolchain(s) for a given input file set. Language
592map definition looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000593
594 def LanguageMap : LanguageMap<
595 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
596 LangToSuffixes<"c", ["c"]>,
597 ...
598 ]>;
599
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000600For example, without those definitions the following command wouldn't work::
601
602 $ llvmc hello.cpp
603 llvmc: Unknown suffix: cpp
604
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +0000605The language map entries are needed only for the tools that are linked from the
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000606root node. A tool can have multiple output languages.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000607
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000608Option preprocessor
609===================
610
611It is sometimes useful to run error-checking code before processing the
612compilation graph. For example, if optimization options "-O1" and "-O2" are
613implemented as switches, we might want to output a warning if the user invokes
614the driver with both of these options enabled.
615
616The ``OptionPreprocessor`` feature is reserved specially for these
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000617occasions. Example (adapted from ``llvm/src/Base.td.in``)::
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000618
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000619
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +0000620 def Preprocess : OptionPreprocessor<
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000621 (case (not (any_switch_on "O0", "O1", "O2", "O3")),
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +0000622 (set_option "O2"),
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000623 (and (switch_on "O3"), (any_switch_on "O0", "O1", "O2")),
624 (unset_option "O0", "O1", "O2"),
625 (and (switch_on "O2"), (any_switch_on "O0", "O1")),
626 (unset_option "O0", "O1"),
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +0000627 (and (switch_on "O1"), (switch_on "O0")),
628 (unset_option "O0"))
629 >;
630
631Here, ``OptionPreprocessor`` is used to unset all spurious ``-O`` options so
632that they are not forwarded to the compiler. If no optimization options are
633specified, ``-O2`` is enabled.
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000634
635``OptionPreprocessor`` is basically a single big ``case`` expression, which is
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000636evaluated only once right after the driver is started. The only allowed actions
Mikhail Glushenkove0b65702009-12-23 12:49:30 +0000637in ``OptionPreprocessor`` are ``error``, ``warning``, and two special actions:
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +0000638``unset_option`` and ``set_option``. As their names suggest, they can be used to
Mikhail Glushenkove0b65702009-12-23 12:49:30 +0000639set or unset a given option. To set an option with ``set_option``, use the
640two-argument form: ``(set_option "parameter", VALUE)``. Here, ``VALUE`` can be
641either a string, a string list, or a boolean constant.
642
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000643For convenience, ``set_option`` and ``unset_option`` also work with multiple
644arguments. That is, instead of ``[(unset_option "A"), (unset_option "B")]`` you
645can use ``(unset_option "A", "B")``. Obviously, ``(set_option "A", "B")`` is
646only valid if both ``A`` and ``B`` are switches.
Mikhail Glushenkov3e4102e2009-10-25 01:44:11 +0000647
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000648
649More advanced topics
650====================
651
652.. _hooks:
653
654Hooks and environment variables
655-------------------------------
656
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000657Normally, LLVMC searches for programs in the system ``PATH``. Sometimes, this is
658not sufficient: for example, we may want to specify tool paths or names in the
659configuration file. This can be achieved via the hooks mechanism. To write your
660own hooks, add their definitions to the ``Hooks.cpp`` or drop a ``.cpp`` file
661into your driver directory. Hooks should live in the ``hooks`` namespace and
662have the signature ``std::string hooks::MyHookName ([const char* Arg0 [ const
663char* Arg2 [, ...]]])``. They can be used from the ``command`` tool property::
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000664
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000665 (command "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000666
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000667To pass arguments to hooks, use the following syntax::
668
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000669 (command "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2")
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000670
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000671It is also possible to use environment variables in the same manner::
672
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000673 (command "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000674
675To change the command line string based on user-provided options use
676the ``case`` expression (documented `above`__)::
677
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000678 (command
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000679 (case
680 (switch_on "E"),
681 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
682 (default),
683 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
684
685__ case_
686
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000687Debugging
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000688---------
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000689
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000690When writing LLVMC-based drivers, it can be useful to get a visual view of the
691resulting compilation graph. This can be achieved via the command line option
692``--view-graph`` (which assumes that Graphviz_ and Ghostview_ are
693installed). There is also a ``--write-graph`` option that creates a Graphviz
694source file (``compilation-graph.dot``) in the current directory.
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000695
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000696Another useful ``llvmc`` option is ``--check-graph``. It checks the compilation
697graph for common errors like mismatched output/input language names, multiple
698default edges and cycles. When invoked with ``--check-graph``, ``llvmc`` doesn't
699perform any compilation tasks and returns the number of encountered errors as
700its status code. In the future, these checks will be performed at compile-time
701and this option will disappear.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000702
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000703.. _Graphviz: http://www.graphviz.org/
704.. _Ghostview: http://pages.cs.wisc.edu/~ghost/
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000705
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000706Conditioning on the executable name
707-----------------------------------
708
709For now, the executable name (the value passed to the driver in ``argv[0]``) is
710accessible only in the C++ code (i.e. hooks). Use the following code::
711
712 namespace llvmc {
713 extern const char* ProgramName;
714 }
715
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000716 namespace hooks {
717
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000718 std::string MyHook() {
719 //...
720 if (strcmp(ProgramName, "mydriver") == 0) {
721 //...
722
723 }
724
Mikhail Glushenkovb59b0f82009-12-07 18:26:11 +0000725 } // end namespace hooks
726
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000727In general, you're encouraged not to make the behaviour dependent on the
728executable file name, and use command-line switches instead. See for example how
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000729the ``llvmc`` program behaves when it needs to choose the correct linker options
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000730(think ``g++`` vs. ``gcc``).
731
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000732.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000733
734 <hr />
735 <address>
736 <a href="http://jigsaw.w3.org/css-validator/check/referer">
737 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
738 alt="Valid CSS" /></a>
739 <a href="http://validator.w3.org/check?uri=referer">
740 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
741 alt="Valid XHTML 1.0 Transitional"/></a>
742
743 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
744 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
745
746 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
747 </address>