• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1add_custom_target
2-----------------
3
4Add a target with no output so it will always be built.
5
6.. code-block:: cmake
7
8  add_custom_target(Name [ALL] [command1 [args1...]]
9                    [COMMAND command2 [args2...] ...]
10                    [DEPENDS depend depend depend ... ]
11                    [BYPRODUCTS [files...]]
12                    [WORKING_DIRECTORY dir]
13                    [COMMENT comment]
14                    [JOB_POOL job_pool]
15                    [VERBATIM] [USES_TERMINAL]
16                    [COMMAND_EXPAND_LISTS]
17                    [SOURCES src1 [src2...]])
18
19Adds a target with the given name that executes the given commands.
20The target has no output file and is *always considered out of date*
21even if the commands try to create a file with the name of the target.
22Use the :command:`add_custom_command` command to generate a file with
23dependencies.  By default nothing depends on the custom target.  Use
24the :command:`add_dependencies` command to add dependencies to or
25from other targets.
26
27The options are:
28
29``ALL``
30  Indicate that this target should be added to the default build
31  target so that it will be run every time (the command cannot be
32  called ``ALL``).
33
34``BYPRODUCTS``
35  .. versionadded:: 3.2
36
37  Specify the files the command is expected to produce but whose
38  modification time may or may not be updated on subsequent builds.
39  If a byproduct name is a relative path it will be interpreted
40  relative to the build tree directory corresponding to the
41  current source directory.
42  Each byproduct file will be marked with the :prop_sf:`GENERATED`
43  source file property automatically.
44
45  Explicit specification of byproducts is supported by the
46  :generator:`Ninja` generator to tell the ``ninja`` build tool
47  how to regenerate byproducts when they are missing.  It is
48  also useful when other build rules (e.g. custom commands)
49  depend on the byproducts.  Ninja requires a build rule for any
50  generated file on which another rule depends even if there are
51  order-only dependencies to ensure the byproducts will be
52  available before their dependents build.
53
54  The :ref:`Makefile Generators` will remove ``BYPRODUCTS`` and other
55  :prop_sf:`GENERATED` files during ``make clean``.
56
57  .. versionadded:: 3.20
58    Arguments to ``BYPRODUCTS`` may use a restricted set of
59    :manual:`generator expressions <cmake-generator-expressions(7)>`.
60    :ref:`Target-dependent expressions <Target-Dependent Queries>` are not
61    permitted.
62
63``COMMAND``
64  Specify the command-line(s) to execute at build time.
65  If more than one ``COMMAND`` is specified they will be executed in order,
66  but *not* necessarily composed into a stateful shell or batch script.
67  (To run a full script, use the :command:`configure_file` command or the
68  :command:`file(GENERATE)` command to create it, and then specify
69  a ``COMMAND`` to launch it.)
70
71  If ``COMMAND`` specifies an executable target name (created by the
72  :command:`add_executable` command), it will automatically be replaced
73  by the location of the executable created at build time if either of
74  the following is true:
75
76  * The target is not being cross-compiled (i.e. the
77    :variable:`CMAKE_CROSSCOMPILING` variable is not set to true).
78  * .. versionadded:: 3.6
79      The target is being cross-compiled and an emulator is provided (i.e.
80      its :prop_tgt:`CROSSCOMPILING_EMULATOR` target property is set).
81      In this case, the contents of :prop_tgt:`CROSSCOMPILING_EMULATOR` will be
82      prepended to the command before the location of the target executable.
83
84  If neither of the above conditions are met, it is assumed that the
85  command name is a program to be found on the ``PATH`` at build time.
86
87  Arguments to ``COMMAND`` may use
88  :manual:`generator expressions <cmake-generator-expressions(7)>`.
89  Use the :genex:`TARGET_FILE` generator expression to refer to the location
90  of a target later in the command line (i.e. as a command argument rather
91  than as the command to execute).
92
93  Whenever one of the following target based generator expressions are used as
94  a command to execute or is mentioned in a command argument, a target-level
95  dependency will be added automatically so that the mentioned target will be
96  built before this custom target (see policy :policy:`CMP0112`).
97
98    * ``TARGET_FILE``
99    * ``TARGET_LINKER_FILE``
100    * ``TARGET_SONAME_FILE``
101    * ``TARGET_PDB_FILE``
102
103  The command and arguments are optional and if not specified an empty
104  target will be created.
105
106``COMMENT``
107  Display the given message before the commands are executed at
108  build time.
109
110``DEPENDS``
111  Reference files and outputs of custom commands created with
112  :command:`add_custom_command` command calls in the same directory
113  (``CMakeLists.txt`` file).  They will be brought up to date when
114  the target is built.
115
116  .. versionchanged:: 3.16
117    A target-level dependency is added if any dependency is a byproduct
118    of a target or any of its build events in the same directory to ensure
119    the byproducts will be available before this target is built.
120
121  Use the :command:`add_dependencies` command to add dependencies
122  on other targets.
123
124``COMMAND_EXPAND_LISTS``
125  .. versionadded:: 3.8
126
127  Lists in ``COMMAND`` arguments will be expanded, including those
128  created with
129  :manual:`generator expressions <cmake-generator-expressions(7)>`,
130  allowing ``COMMAND`` arguments such as
131  ``${CC} "-I$<JOIN:$<TARGET_PROPERTY:foo,INCLUDE_DIRECTORIES>,;-I>" foo.cc``
132  to be properly expanded.
133
134``JOB_POOL``
135  .. versionadded:: 3.15
136
137  Specify a :prop_gbl:`pool <JOB_POOLS>` for the :generator:`Ninja`
138  generator. Incompatible with ``USES_TERMINAL``, which implies
139  the ``console`` pool.
140  Using a pool that is not defined by :prop_gbl:`JOB_POOLS` causes
141  an error by ninja at build time.
142
143``SOURCES``
144  Specify additional source files to be included in the custom target.
145  Specified source files will be added to IDE project files for
146  convenience in editing even if they have no build rules.
147
148``VERBATIM``
149  All arguments to the commands will be escaped properly for the
150  build tool so that the invoked command receives each argument
151  unchanged.  Note that one level of escapes is still used by the
152  CMake language processor before ``add_custom_target`` even sees
153  the arguments.  Use of ``VERBATIM`` is recommended as it enables
154  correct behavior.  When ``VERBATIM`` is not given the behavior
155  is platform specific because there is no protection of
156  tool-specific special characters.
157
158``USES_TERMINAL``
159  .. versionadded:: 3.2
160
161  The command will be given direct access to the terminal if possible.
162  With the :generator:`Ninja` generator, this places the command in
163  the ``console`` :prop_gbl:`pool <JOB_POOLS>`.
164
165``WORKING_DIRECTORY``
166  Execute the command with the given current working directory.
167  If it is a relative path it will be interpreted relative to the
168  build tree directory corresponding to the current source directory.
169
170  .. versionadded:: 3.13
171    Arguments to ``WORKING_DIRECTORY`` may use
172    :manual:`generator expressions <cmake-generator-expressions(7)>`.
173
174Ninja Multi-Config
175^^^^^^^^^^^^^^^^^^
176
177.. versionadded:: 3.20
178
179  ``add_custom_target`` supports the :generator:`Ninja Multi-Config`
180  generator's cross-config capabilities. See the generator documentation
181  for more information.
182