xref: /aosp_15_r20/external/llvm/docs/CMakePrimer.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker============
2*9880d681SAndroid Build Coastguard WorkerCMake Primer
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 Worker.. warning::
9*9880d681SAndroid Build Coastguard Worker   Disclaimer: This documentation is written by LLVM project contributors `not`
10*9880d681SAndroid Build Coastguard Worker   anyone affiliated with the CMake project. This document may contain
11*9880d681SAndroid Build Coastguard Worker   inaccurate terminology, phrasing, or technical details. It is provided with
12*9880d681SAndroid Build Coastguard Worker   the best intentions.
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard WorkerIntroduction
16*9880d681SAndroid Build Coastguard Worker============
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard WorkerThe LLVM project and many of the core projects built on LLVM build using CMake.
19*9880d681SAndroid Build Coastguard WorkerThis document aims to provide a brief overview of CMake for developers modifying
20*9880d681SAndroid Build Coastguard WorkerLLVM projects or building their own projects on top of LLVM.
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard WorkerThe official CMake language references is available in the cmake-language
23*9880d681SAndroid Build Coastguard Workermanpage and `cmake-language online documentation
24*9880d681SAndroid Build Coastguard Worker<https://cmake.org/cmake/help/v3.4/manual/cmake-language.7.html>`_.
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker10,000 ft View
27*9880d681SAndroid Build Coastguard Worker==============
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard WorkerCMake is a tool that reads script files in its own language that describe how a
30*9880d681SAndroid Build Coastguard Workersoftware project builds. As CMake evaluates the scripts it constructs an
31*9880d681SAndroid Build Coastguard Workerinternal representation of the software project. Once the scripts have been
32*9880d681SAndroid Build Coastguard Workerfully processed, if there are no errors, CMake will generate build files to
33*9880d681SAndroid Build Coastguard Workeractually build the project. CMake supports generating build files for a variety
34*9880d681SAndroid Build Coastguard Workerof command line build tools as well as for popular IDEs.
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard WorkerWhen a user runs CMake it performs a variety of checks similar to how autoconf
37*9880d681SAndroid Build Coastguard Workerworked historically. During the checks and the evaluation of the build
38*9880d681SAndroid Build Coastguard Workerdescription scripts CMake caches values into the CMakeCache. This is useful
39*9880d681SAndroid Build Coastguard Workerbecause it allows the build system to skip long-running checks during
40*9880d681SAndroid Build Coastguard Workerincremental development. CMake caching also has some drawbacks, but that will be
41*9880d681SAndroid Build Coastguard Workerdiscussed later.
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard WorkerScripting Overview
44*9880d681SAndroid Build Coastguard Worker==================
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard WorkerCMake's scripting language has a very simple grammar. Every language construct
47*9880d681SAndroid Build Coastguard Workeris a command that matches the pattern _name_(_args_). Commands come in three
48*9880d681SAndroid Build Coastguard Workerprimary types: language-defined (commands implemented in C++ in CMake), defined
49*9880d681SAndroid Build Coastguard Workerfunctions, and defined macros. The CMake distribution also contains a suite of
50*9880d681SAndroid Build Coastguard WorkerCMake modules that contain definitions for useful functionality.
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard WorkerThe example below is the full CMake build for building a C++ "Hello World"
53*9880d681SAndroid Build Coastguard Workerprogram. The example uses only CMake language-defined functions.
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker   cmake_minimum_required(VERSION 3.2)
58*9880d681SAndroid Build Coastguard Worker   project(HelloWorld)
59*9880d681SAndroid Build Coastguard Worker   add_executable(HelloWorld HelloWorld.cpp)
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard WorkerThe CMake language provides control flow constructs in the form of foreach loops
62*9880d681SAndroid Build Coastguard Workerand if blocks. To make the example above more complicated you could add an if
63*9880d681SAndroid Build Coastguard Workerblock to define "APPLE" when targeting Apple platforms:
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker   cmake_minimum_required(VERSION 3.2)
68*9880d681SAndroid Build Coastguard Worker   project(HelloWorld)
69*9880d681SAndroid Build Coastguard Worker   add_executable(HelloWorld HelloWorld.cpp)
70*9880d681SAndroid Build Coastguard Worker   if(APPLE)
71*9880d681SAndroid Build Coastguard Worker     target_compile_definitions(HelloWorld PUBLIC APPLE)
72*9880d681SAndroid Build Coastguard Worker   endif()
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard WorkerVariables, Types, and Scope
75*9880d681SAndroid Build Coastguard Worker===========================
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard WorkerDereferencing
78*9880d681SAndroid Build Coastguard Worker-------------
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard WorkerIn CMake variables are "stringly" typed. All variables are represented as
81*9880d681SAndroid Build Coastguard Workerstrings throughout evaluation. Wrapping a variable in ``${}`` dereferences it
82*9880d681SAndroid Build Coastguard Workerand results in a literal substitution of the name for the value. CMake refers to
83*9880d681SAndroid Build Coastguard Workerthis as "variable evaluation" in their documentation. Dereferences are performed
84*9880d681SAndroid Build Coastguard Worker*before* the command being called receives the arguments. This means
85*9880d681SAndroid Build Coastguard Workerdereferencing a list results in multiple separate arguments being passed to the
86*9880d681SAndroid Build Coastguard Workercommand.
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard WorkerVariable dereferences can be nested and be used to model complex data. For
89*9880d681SAndroid Build Coastguard Workerexample:
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker   set(var_name var1)
94*9880d681SAndroid Build Coastguard Worker   set(${var_name} foo) # same as "set(var1 foo)"
95*9880d681SAndroid Build Coastguard Worker   set(${${var_name}}_var bar) # same as "set(foo_var bar)"
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard WorkerDereferencing an unset variable results in an empty expansion. It is a common
98*9880d681SAndroid Build Coastguard Workerpattern in CMake to conditionally set variables knowing that it will be used in
99*9880d681SAndroid Build Coastguard Workercode paths that the variable isn't set. There are examples of this throughout
100*9880d681SAndroid Build Coastguard Workerthe LLVM CMake build system.
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard WorkerAn example of variable empty expansion is:
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker   if(APPLE)
107*9880d681SAndroid Build Coastguard Worker     set(extra_sources Apple.cpp)
108*9880d681SAndroid Build Coastguard Worker   endif()
109*9880d681SAndroid Build Coastguard Worker   add_executable(HelloWorld HelloWorld.cpp ${extra_sources})
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard WorkerIn this example the ``extra_sources`` variable is only defined if you're
112*9880d681SAndroid Build Coastguard Workertargeting an Apple platform. For all other targets the ``extra_sources`` will be
113*9880d681SAndroid Build Coastguard Workerevaluated as empty before add_executable is given its arguments.
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard WorkerOne big "Gotcha" with variable dereferencing is that ``if`` commands implicitly
116*9880d681SAndroid Build Coastguard Workerdereference values. This has some unexpected results. For example:
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker   if("${SOME_VAR}" STREQUAL "MSVC")
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard WorkerIn this code sample MSVC will be implicitly dereferenced, which will result in
123*9880d681SAndroid Build Coastguard Workerthe if command comparing the value of the dereferenced variables ``SOME_VAR``
124*9880d681SAndroid Build Coastguard Workerand ``MSVC``. A common workaround to this solution is to prepend strings being
125*9880d681SAndroid Build Coastguard Workercompared with an ``x``.
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker   if("x${SOME_VAR}" STREQUAL "xMSVC")
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard WorkerThis works because while ``MSVC`` is a defined variable, ``xMSVC`` is not. This
132*9880d681SAndroid Build Coastguard Workerpattern is uncommon, but it does occur in LLVM's CMake scripts.
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker.. note::
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker   Once the LLVM project upgrades its minimum CMake version to 3.1 or later we
137*9880d681SAndroid Build Coastguard Worker   can prevent this behavior by setting CMP0054 to new. For more information on
138*9880d681SAndroid Build Coastguard Worker   CMake policies please see the cmake-policies manpage or the `cmake-policies
139*9880d681SAndroid Build Coastguard Worker   online documentation
140*9880d681SAndroid Build Coastguard Worker   <https://cmake.org/cmake/help/v3.4/manual/cmake-policies.7.html>`_.
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard WorkerLists
143*9880d681SAndroid Build Coastguard Worker-----
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard WorkerIn CMake lists are semi-colon delimited strings, and it is strongly advised that
146*9880d681SAndroid Build Coastguard Workeryou avoid using semi-colons in lists; it doesn't go smoothly. A few examples of
147*9880d681SAndroid Build Coastguard Workerdefining lists:
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker   # Creates a list with members a, b, c, and d
152*9880d681SAndroid Build Coastguard Worker   set(my_list a b c d)
153*9880d681SAndroid Build Coastguard Worker   set(my_list "a;b;c;d")
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker   # Creates a string "a b c d"
156*9880d681SAndroid Build Coastguard Worker   set(my_string "a b c d")
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard WorkerLists of Lists
159*9880d681SAndroid Build Coastguard Worker--------------
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard WorkerOne of the more complicated patterns in CMake is lists of lists. Because a list
162*9880d681SAndroid Build Coastguard Workercannot contain an element with a semi-colon to construct a list of lists you
163*9880d681SAndroid Build Coastguard Workermake a list of variable names that refer to other lists. For example:
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker   set(list_of_lists a b c)
168*9880d681SAndroid Build Coastguard Worker   set(a 1 2 3)
169*9880d681SAndroid Build Coastguard Worker   set(b 4 5 6)
170*9880d681SAndroid Build Coastguard Worker   set(c 7 8 9)
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard WorkerWith this layout you can iterate through the list of lists printing each value
173*9880d681SAndroid Build Coastguard Workerwith the following code:
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker   foreach(list_name IN LISTS list_of_lists)
178*9880d681SAndroid Build Coastguard Worker     foreach(value IN LISTS ${list_name})
179*9880d681SAndroid Build Coastguard Worker       message(${value})
180*9880d681SAndroid Build Coastguard Worker     endforeach()
181*9880d681SAndroid Build Coastguard Worker   endforeach()
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard WorkerYou'll notice that the inner foreach loop's list is doubly dereferenced. This is
184*9880d681SAndroid Build Coastguard Workerbecause the first dereference turns ``list_name`` into the name of the sub-list
185*9880d681SAndroid Build Coastguard Worker(a, b, or c in the example), then the second dereference is to get the value of
186*9880d681SAndroid Build Coastguard Workerthe list.
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard WorkerThis pattern is used throughout CMake, the most common example is the compiler
189*9880d681SAndroid Build Coastguard Workerflags options, which CMake refers to using the following variable expansions:
190*9880d681SAndroid Build Coastguard WorkerCMAKE_${LANGUAGE}_FLAGS and CMAKE_${LANGUAGE}_FLAGS_${CMAKE_BUILD_TYPE}.
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard WorkerOther Types
193*9880d681SAndroid Build Coastguard Worker-----------
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard WorkerVariables that are cached or specified on the command line can have types
196*9880d681SAndroid Build Coastguard Workerassociated with them. The variable's type is used by CMake's UI tool to display
197*9880d681SAndroid Build Coastguard Workerthe right input field. The variable's type generally doesn't impact evaluation.
198*9880d681SAndroid Build Coastguard WorkerOne of the few examples is PATH variables, which CMake does have some special
199*9880d681SAndroid Build Coastguard Workerhandling for. You can read more about the special handling in `CMake's set
200*9880d681SAndroid Build Coastguard Workerdocumentation
201*9880d681SAndroid Build Coastguard Worker<https://cmake.org/cmake/help/v3.5/command/set.html#set-cache-entry>`_.
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard WorkerScope
204*9880d681SAndroid Build Coastguard Worker-----
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard WorkerCMake inherently has a directory-based scoping. Setting a variable in a
207*9880d681SAndroid Build Coastguard WorkerCMakeLists file, will set the variable for that file, and all subdirectories.
208*9880d681SAndroid Build Coastguard WorkerVariables set in a CMake module that is included in a CMakeLists file will be
209*9880d681SAndroid Build Coastguard Workerset in the scope they are included from, and all subdirectories.
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard WorkerWhen a variable that is already set is set again in a subdirectory it overrides
212*9880d681SAndroid Build Coastguard Workerthe value in that scope and any deeper subdirectories.
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard WorkerThe CMake set command provides two scope-related options. PARENT_SCOPE sets a
215*9880d681SAndroid Build Coastguard Workervariable into the parent scope, and not the current scope. The CACHE option sets
216*9880d681SAndroid Build Coastguard Workerthe variable in the CMakeCache, which results in it being set in all scopes. The
217*9880d681SAndroid Build Coastguard WorkerCACHE option will not set a variable that already exists in the CACHE unless the
218*9880d681SAndroid Build Coastguard WorkerFORCE option is specified.
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard WorkerIn addition to directory-based scope, CMake functions also have their own scope.
221*9880d681SAndroid Build Coastguard WorkerThis means variables set inside functions do not bleed into the parent scope.
222*9880d681SAndroid Build Coastguard WorkerThis is not true of macros, and it is for this reason LLVM prefers functions
223*9880d681SAndroid Build Coastguard Workerover macros whenever reasonable.
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker.. note::
226*9880d681SAndroid Build Coastguard Worker  Unlike C-based languages, CMake's loop and control flow blocks do not have
227*9880d681SAndroid Build Coastguard Worker  their own scopes.
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard WorkerControl Flow
230*9880d681SAndroid Build Coastguard Worker============
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard WorkerCMake features the same basic control flow constructs you would expect in any
233*9880d681SAndroid Build Coastguard Workerscripting language, but there are a few quarks because, as with everything in
234*9880d681SAndroid Build Coastguard WorkerCMake, control flow constructs are commands.
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard WorkerIf, ElseIf, Else
237*9880d681SAndroid Build Coastguard Worker----------------
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker.. note::
240*9880d681SAndroid Build Coastguard Worker  For the full documentation on the CMake if command go
241*9880d681SAndroid Build Coastguard Worker  `here <https://cmake.org/cmake/help/v3.4/command/if.html>`_. That resource is
242*9880d681SAndroid Build Coastguard Worker  far more complete.
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard WorkerIn general CMake if blocks work the way you'd expect:
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker  if(<condition>)
249*9880d681SAndroid Build Coastguard Worker    .. do stuff
250*9880d681SAndroid Build Coastguard Worker  elseif(<condition>)
251*9880d681SAndroid Build Coastguard Worker    .. do other stuff
252*9880d681SAndroid Build Coastguard Worker  else()
253*9880d681SAndroid Build Coastguard Worker    .. do other other stuff
254*9880d681SAndroid Build Coastguard Worker  endif()
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard WorkerThe single most important thing to know about CMake's if blocks coming from a C
257*9880d681SAndroid Build Coastguard Workerbackground is that they do not have their own scope. Variables set inside
258*9880d681SAndroid Build Coastguard Workerconditional blocks persist after the ``endif()``.
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard WorkerLoops
261*9880d681SAndroid Build Coastguard Worker-----
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard WorkerThe most common form of the CMake ``foreach`` block is:
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker  foreach(var ...)
268*9880d681SAndroid Build Coastguard Worker    .. do stuff
269*9880d681SAndroid Build Coastguard Worker  endforeach()
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard WorkerThe variable argument portion of the ``foreach`` block can contain dereferenced
272*9880d681SAndroid Build Coastguard Workerlists, values to iterate, or a mix of both:
273*9880d681SAndroid Build Coastguard Worker
274*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker  foreach(var foo bar baz)
277*9880d681SAndroid Build Coastguard Worker    message(${var})
278*9880d681SAndroid Build Coastguard Worker  endforeach()
279*9880d681SAndroid Build Coastguard Worker  # prints:
280*9880d681SAndroid Build Coastguard Worker  #  foo
281*9880d681SAndroid Build Coastguard Worker  #  bar
282*9880d681SAndroid Build Coastguard Worker  #  baz
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker  set(my_list 1 2 3)
285*9880d681SAndroid Build Coastguard Worker  foreach(var ${my_list})
286*9880d681SAndroid Build Coastguard Worker    message(${var})
287*9880d681SAndroid Build Coastguard Worker  endforeach()
288*9880d681SAndroid Build Coastguard Worker  # prints:
289*9880d681SAndroid Build Coastguard Worker  #  1
290*9880d681SAndroid Build Coastguard Worker  #  2
291*9880d681SAndroid Build Coastguard Worker  #  3
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker  foreach(var ${my_list} out_of_bounds)
294*9880d681SAndroid Build Coastguard Worker    message(${var})
295*9880d681SAndroid Build Coastguard Worker  endforeach()
296*9880d681SAndroid Build Coastguard Worker  # prints:
297*9880d681SAndroid Build Coastguard Worker  #  1
298*9880d681SAndroid Build Coastguard Worker  #  2
299*9880d681SAndroid Build Coastguard Worker  #  3
300*9880d681SAndroid Build Coastguard Worker  #  out_of_bounds
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard WorkerThere is also a more modern CMake foreach syntax. The code below is equivalent
303*9880d681SAndroid Build Coastguard Workerto the code above:
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker  foreach(var IN ITEMS foo bar baz)
308*9880d681SAndroid Build Coastguard Worker    message(${var})
309*9880d681SAndroid Build Coastguard Worker  endforeach()
310*9880d681SAndroid Build Coastguard Worker  # prints:
311*9880d681SAndroid Build Coastguard Worker  #  foo
312*9880d681SAndroid Build Coastguard Worker  #  bar
313*9880d681SAndroid Build Coastguard Worker  #  baz
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard Worker  set(my_list 1 2 3)
316*9880d681SAndroid Build Coastguard Worker  foreach(var IN LISTS my_list)
317*9880d681SAndroid Build Coastguard Worker    message(${var})
318*9880d681SAndroid Build Coastguard Worker  endforeach()
319*9880d681SAndroid Build Coastguard Worker  # prints:
320*9880d681SAndroid Build Coastguard Worker  #  1
321*9880d681SAndroid Build Coastguard Worker  #  2
322*9880d681SAndroid Build Coastguard Worker  #  3
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker  foreach(var IN LISTS my_list ITEMS out_of_bounds)
325*9880d681SAndroid Build Coastguard Worker    message(${var})
326*9880d681SAndroid Build Coastguard Worker  endforeach()
327*9880d681SAndroid Build Coastguard Worker  # prints:
328*9880d681SAndroid Build Coastguard Worker  #  1
329*9880d681SAndroid Build Coastguard Worker  #  2
330*9880d681SAndroid Build Coastguard Worker  #  3
331*9880d681SAndroid Build Coastguard Worker  #  out_of_bounds
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard WorkerSimilar to the conditional statements, these generally behave how you would
334*9880d681SAndroid Build Coastguard Workerexpect, and they do not have their own scope.
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard WorkerCMake also supports ``while`` loops, although they are not widely used in LLVM.
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard WorkerModules, Functions and Macros
339*9880d681SAndroid Build Coastguard Worker=============================
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard WorkerModules
342*9880d681SAndroid Build Coastguard Worker-------
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard WorkerModules are CMake's vehicle for enabling code reuse. CMake modules are just
345*9880d681SAndroid Build Coastguard WorkerCMake script files. They can contain code to execute on include as well as
346*9880d681SAndroid Build Coastguard Workerdefinitions for commands.
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard WorkerIn CMake macros and functions are universally referred to as commands, and they
349*9880d681SAndroid Build Coastguard Workerare the primary method of defining code that can be called multiple times.
350*9880d681SAndroid Build Coastguard Worker
351*9880d681SAndroid Build Coastguard WorkerIn LLVM we have several CMake modules that are included as part of our
352*9880d681SAndroid Build Coastguard Workerdistribution for developers who don't build our project from source. Those
353*9880d681SAndroid Build Coastguard Workermodules are the fundamental pieces needed to build LLVM-based projects with
354*9880d681SAndroid Build Coastguard WorkerCMake. We also rely on modules as a way of organizing the build system's
355*9880d681SAndroid Build Coastguard Workerfunctionality for maintainability and re-use within LLVM projects.
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard WorkerArgument Handling
358*9880d681SAndroid Build Coastguard Worker-----------------
359*9880d681SAndroid Build Coastguard Worker
360*9880d681SAndroid Build Coastguard WorkerWhen defining a CMake command handling arguments is very useful. The examples
361*9880d681SAndroid Build Coastguard Workerin this section will all use the CMake ``function`` block, but this all applies
362*9880d681SAndroid Build Coastguard Workerto the ``macro`` block as well.
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard WorkerCMake commands can have named arguments, but all commands are implicitly
365*9880d681SAndroid Build Coastguard Workervariable argument. If the command has named arguments they are required and must
366*9880d681SAndroid Build Coastguard Workerbe specified at every call site. Below is a trivial example of providing a
367*9880d681SAndroid Build Coastguard Workerwrapper function for CMake's built in function ``add_dependencies``.
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker   function(add_deps target)
372*9880d681SAndroid Build Coastguard Worker     add_dependencies(${target} ${ARGV})
373*9880d681SAndroid Build Coastguard Worker   endfunction()
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard WorkerThis example defines a new macro named ``add_deps`` which takes a required first
376*9880d681SAndroid Build Coastguard Workerargument, and just calls another function passing through the first argument and
377*9880d681SAndroid Build Coastguard Workerall trailing arguments. When variable arguments are present CMake defines them
378*9880d681SAndroid Build Coastguard Workerin a list named ``ARGV``, and the count of the arguments is defined in ``ARGN``.
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard WorkerCMake provides a module ``CMakeParseArguments`` which provides an implementation
381*9880d681SAndroid Build Coastguard Workerof advanced argument parsing. We use this all over LLVM, and it is recommended
382*9880d681SAndroid Build Coastguard Workerfor any function that has complex argument-based behaviors or optional
383*9880d681SAndroid Build Coastguard Workerarguments. CMake's official documentation for the module is in the
384*9880d681SAndroid Build Coastguard Worker``cmake-modules`` manpage, and is also available at the
385*9880d681SAndroid Build Coastguard Worker`cmake-modules online documentation
386*9880d681SAndroid Build Coastguard Worker<https://cmake.org/cmake/help/v3.4/module/CMakeParseArguments.html>`_.
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker.. note::
389*9880d681SAndroid Build Coastguard Worker  As of CMake 3.5 the cmake_parse_arguments command has become a native command
390*9880d681SAndroid Build Coastguard Worker  and the CMakeParseArguments module is empty and only left around for
391*9880d681SAndroid Build Coastguard Worker  compatibility.
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard WorkerFunctions Vs Macros
394*9880d681SAndroid Build Coastguard Worker-------------------
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard WorkerFunctions and Macros look very similar in how they are used, but there is one
397*9880d681SAndroid Build Coastguard Workerfundamental difference between the two. Functions have their own scope, and
398*9880d681SAndroid Build Coastguard Workermacros don't. This means variables set in macros will bleed out into the calling
399*9880d681SAndroid Build Coastguard Workerscope. That makes macros suitable for defining very small bits of functionality
400*9880d681SAndroid Build Coastguard Workeronly.
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard WorkerThe other difference between CMake functions and macros is how arguments are
403*9880d681SAndroid Build Coastguard Workerpassed. Arguments to macros are not set as variables, instead dereferences to
404*9880d681SAndroid Build Coastguard Workerthe parameters are resolved across the macro before executing it. This can
405*9880d681SAndroid Build Coastguard Workerresult in some unexpected behavior if using unreferenced variables. For example:
406*9880d681SAndroid Build Coastguard Worker
407*9880d681SAndroid Build Coastguard Worker.. code-block:: cmake
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker   macro(print_list my_list)
410*9880d681SAndroid Build Coastguard Worker     foreach(var IN LISTS my_list)
411*9880d681SAndroid Build Coastguard Worker       message("${var}")
412*9880d681SAndroid Build Coastguard Worker     endforeach()
413*9880d681SAndroid Build Coastguard Worker   endmacro()
414*9880d681SAndroid Build Coastguard Worker
415*9880d681SAndroid Build Coastguard Worker   set(my_list a b c d)
416*9880d681SAndroid Build Coastguard Worker   set(my_list_of_numbers 1 2 3 4)
417*9880d681SAndroid Build Coastguard Worker   print_list(my_list_of_numbers)
418*9880d681SAndroid Build Coastguard Worker   # prints:
419*9880d681SAndroid Build Coastguard Worker   # a
420*9880d681SAndroid Build Coastguard Worker   # b
421*9880d681SAndroid Build Coastguard Worker   # c
422*9880d681SAndroid Build Coastguard Worker   # d
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard WorkerGenerally speaking this issue is uncommon because it requires using
425*9880d681SAndroid Build Coastguard Workernon-dereferenced variables with names that overlap in the parent scope, but it
426*9880d681SAndroid Build Coastguard Workeris important to be aware of because it can lead to subtle bugs.
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard WorkerLLVM Project Wrappers
429*9880d681SAndroid Build Coastguard Worker=====================
430*9880d681SAndroid Build Coastguard Worker
431*9880d681SAndroid Build Coastguard WorkerLLVM projects provide lots of wrappers around critical CMake built-in commands.
432*9880d681SAndroid Build Coastguard WorkerWe use these wrappers to provide consistent behaviors across LLVM components
433*9880d681SAndroid Build Coastguard Workerand to reduce code duplication.
434*9880d681SAndroid Build Coastguard Worker
435*9880d681SAndroid Build Coastguard WorkerWe generally (but not always) follow the convention that commands prefaced with
436*9880d681SAndroid Build Coastguard Worker``llvm_`` are intended to be used only as building blocks for other commands.
437*9880d681SAndroid Build Coastguard WorkerWrapper commands that are intended for direct use are generally named following
438*9880d681SAndroid Build Coastguard Workerwith the project in the middle of the command name (i.e. ``add_llvm_executable``
439*9880d681SAndroid Build Coastguard Workeris the wrapper for ``add_executable``). The LLVM ``add_*`` wrapper functions are
440*9880d681SAndroid Build Coastguard Workerall defined in ``AddLLVM.cmake`` which is installed as part of the LLVM
441*9880d681SAndroid Build Coastguard Workerdistribution. It can be included and used by any LLVM sub-project that requires
442*9880d681SAndroid Build Coastguard WorkerLLVM.
443*9880d681SAndroid Build Coastguard Worker
444*9880d681SAndroid Build Coastguard Worker.. note::
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard Worker   Not all LLVM projects require LLVM for all use cases. For example compiler-rt
447*9880d681SAndroid Build Coastguard Worker   can be built without LLVM, and the compiler-rt sanitizer libraries are used
448*9880d681SAndroid Build Coastguard Worker   with GCC.
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard WorkerUseful Built-in Commands
451*9880d681SAndroid Build Coastguard Worker========================
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard WorkerCMake has a bunch of useful built-in commands. This document isn't going to
454*9880d681SAndroid Build Coastguard Workergo into details about them because The CMake project has excellent
455*9880d681SAndroid Build Coastguard Workerdocumentation. To highlight a few useful functions see:
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker* `add_custom_command <https://cmake.org/cmake/help/v3.4/command/add_custom_command.html>`_
458*9880d681SAndroid Build Coastguard Worker* `add_custom_target <https://cmake.org/cmake/help/v3.4/command/add_custom_target.html>`_
459*9880d681SAndroid Build Coastguard Worker* `file <https://cmake.org/cmake/help/v3.4/command/file.html>`_
460*9880d681SAndroid Build Coastguard Worker* `list <https://cmake.org/cmake/help/v3.4/command/list.html>`_
461*9880d681SAndroid Build Coastguard Worker* `math <https://cmake.org/cmake/help/v3.4/command/math.html>`_
462*9880d681SAndroid Build Coastguard Worker* `string <https://cmake.org/cmake/help/v3.4/command/string.html>`_
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard WorkerThe full documentation for CMake commands is in the ``cmake-commands`` manpage
465*9880d681SAndroid Build Coastguard Workerand available on `CMake's website <https://cmake.org/cmake/help/v3.4/manual/cmake-commands.7.html>`_
466