cxx_genrule()
A cxx_genrule()
enables you to run shell commands as part of the Buck build process. A cxx_genrule()
exposes—through a set of string parameter macros and variables—information about the tools and configuration options used by the Buck environment, specifically those related to the C/C++ toolchain.
The information exposed through these tools and configuration options is a reflection of: Buck's built-in settings, the settings in .buckconfig
and .buckconfig.local
, and the result of various command-line overrides specified through the --config
command-line option.
This information is available only to the shell commands specified in the cxx_genrule
. The information is not available to other arguments of the rule.
A cxx_genrule()
can be an input to another cxx_genrule()
.
Note that if you specify the cxx_genrule
as a command-line target to buck build
, you must include a platform flavor. For example:
buck build :cxx_gr_name#iphonesimulator-x86_64
You could also just specify the default platform flavor explicitly:
buck build :cxx_gr_name#default
Arguments
name
(required) #The short name for this build target.
srcs
(defaults to[]
) #Either a list or a map of the source files which Buck makes available to the shell command at the path in the
SRCDIR
environment variable. If you specify a list, the source files are the names in the list. If you specify a map, the source files are made available as the names in the keys of the map, where the values of the map are the original source file names.cmd
(defaults toNone
) #The shell command to run to generate the output file. It is the fallback of
bash
andcmd_exe
. The shell command can access information about the buck build environment through a set of macros, parameterized macros, and variables.Macros
The following macros are available to the shell command and are accessed using the following syntax.
$(<macro>)
Example:
$(cc)
$(cc)
- Path to the C compiler.
$(cxx)
- Path to the C++ compiler.
$(cflags)
- Flags passed to the C compiler.
$(cppflags)
- Flags passed to the C preprocessor.
$(cxxflags)
- Flags passed to the C++ compiler.
$(ld)
- Path to the linker.
$(ldflags-pic)
- Flags passed to the linker for binaries that use position-independent code (PIC).
$(ldflags-pic-filter <pattern>)
- Flags passed to the linker for binaries that use position-independent code (PIC). Use the pattern parameter to specify a regular expression that matches the build targets that use these flags.
$(ldflags-shared)
- Flags passed to the linker for shared libraries, such as dynamic-link libraries (DLLs).
$(ldflags-shared-filter <pattern>)
- Flags passed to the linker for shared libraries, such as dynamic-link libraries (DLLs). Use the pattern parameter to specify a regular expression that matches the build targets that use these flags.
$(ldflags-static)
- Flags passed to the linker for statically-linked libraries.
$(ldflags-static-filter <pattern>)
- Flags passed to the linker for statically-linked libraries. Use the pattern parameter to specify a regular expression that matches the build targets that use these flags.
$(platform-name)
- The platform flavor with which this
cxx_genrule
was specified.
Parameterized Macros
It is also possible to expand references to other rules within the shell command, using the following subset of the builtin string parameter macros. Note that all build rules expanded in the command are automatically considered to be dependencies of the
genrule()
.Note that the paths returned by these macros are absolute paths. You should convert these paths to be relative paths before embedding them in, for example, a shell script or batch file. Using relative paths ensures that your builds are hermetic, that is, they are reproducible across different machine environments.
Additionally, if you embed these paths in a shell script, you should execute that script using the
sh_binary
rule and include the targets for these paths in theresources
argument of thatsh_binary
rule. These are the same targets that you pass to the string parameter macros.$(exe //path/to:target)
- Expands to the commands necessary to run the executable generated by the specified build rule. For a C++ executable, this will typically just be the name of the output executable itself, such as
main
. If the specified build rule does not generate an executable output, an exception will be thrown and the build will fail. $(location //path/to:target)
- Expands to the path of the output of the build rule. This means that you can refer to these without needing to be aware of how Buck is storing data on the disk mid-build.
Variables
Finally, Buck adds the following variables to the environment in which the shell command runs. They are accessed using the following syntax. Note the use of braces rather than parentheses.
${<variable>}
Example:
${SRCS}
${SRCS}
- A string expansion of the
srcs
argument delimited by theenvironment_expansion_separator
argument where each element ofsrcs
will be translated into an absolute path. ${SRCDIR}
- The absolute path to the to which sources are copied prior to running the command.
${OUT}
- The output file for the
genrule()
. The file specified by this variable must always be written by this command. If not, the execution of this rule will be considered a failure, halting the build process. ${TMP}
- A temporary directory which can be used for intermediate results and will not be bundled into the output.
bash
(defaults toNone
) #A platform-specific version of the shell command parameter
cmd
. It runs on Linux and UNIX systems—including OSX—on whichbash
is installed. It has a higher priority thancmd
. Thebash
argument is run with/bin/bash -c
. It has access to the same set of macros and variables as thecmd
argument.cmd_exe
(defaults toNone
) #A platform-specific version of the shell command parameter
cmd
. It runs on Windows and has a higher priority thancmd
. Thecmd_exe
argument is run withcmd.exe /c
. It has access to the same set of macros and variables as thecmd
argument.type
(defaults toNone
) #Specifies the type of this genrule. This is used for logging and is particularly useful for grouping genrules that share an underlying logical "type".
For example, if you have the following
cxx_genrule
defined in the root directory of your Buck projectcxx_genrule( name = 'cxx_gen', type = 'epilog', cmd = 'touch finish.txt; cp finish.txt $OUT', out = 'finish.txt' )
then the following
buck query
commandbuck query "attrfilter( type, 'epilog', '//...' )"
returns
//:cxx_gen
out
(required) #The name of the output file or directory. The complete path to this argument is provided to the shell command through the
OUT
environment variable.environment_expansion_separator
(defaults to" "
) #The delimiter between paths in environment variables, such as SRCS, that can contain multiple paths. It can be useful to specify this parameter if the paths could contain spaces.
enable_sandbox
(defaults toFalse
) #Whether this target should be executed in a sandbox or not.
executable
(defaults toFalse
) #Whether the output of the genrule is itself executable. Marking an output as executable makes
buck run
and$(exe ...)
macro expansion work with this target.tests
(defaults to[]
) #List of build targets that identify tests that exercise this target.
visibility
(defaults to[]
) #List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their
deps
orexported_deps
attributes. For more information, see visibility.licenses
(defaults to[]
) #Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use
buck query
.labels
(defaults to[]
) #Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using
buck query attrfilter()
.