The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Code generation and build scripts

Many projects generate code while they build: protobuf stubs, parsers, embedded resources, version headers. cx supports this in two ways: declarative generators for the common cases, and a build.cmake script for everything else.

Generators

[[generate]]
command = ["protoc", "--cpp_out={out}", "proto/api.proto"]
inputs = ["proto/api.proto"]
outputs = ["api.pb.h", "api.pb.cc"]
[[generate]]
command = ["tools/embed.sh", "assets/logo.png", "{out}/logo.hpp"]
inputs = ["assets/**", "tools/embed.sh"]
outputs = ["logo.hpp"]
Key Meaning
command the program and its arguments. {out} is the output directory, {root} the package directory. It runs in the package directory, never through a shell
inputs files it reads, as globs relative to the package. When one changes, the generator runs again; otherwise it doesn't
outputs files it writes, relative to {out}
  • Where outputs go: target/cx/gen/<package>/, which is added to the include path. A library's generated headers are part of its public interface, so dependents see them too.
  • Generated sources: .c, .cc, .cpp and .cxx outputs become sources of the package's library. A package can consist of nothing but generated code.
  • The tool: a program looked up in PATH, or a file of the package (tools/embed.sh). A tool that isn't there fails when CMake configures, naming the generator and the tool.
  • Packaging: cx package includes the generator inputs and a tool file of the package.

Tools from packages: build-dependencies

A generator can run a binary built from a cx package instead of one in PATH, so the tool's version is pinned in cx.lock like everything else:

[build-dependencies]
embedder = "^1.2" # a cx package with a binary
[[generate]]
command = ["{bin:embedder/embed}", "assets/logo.png", "{out}/logo.hpp"]
inputs = ["assets/logo.png"]
outputs = ["logo.hpp"]
  • {bin:<package>/<binary>} names one of the build-dependency's binaries (src/main.* is the binary named after the package). It can appear in any argument. The binary is built before the generator runs, never as part of the default build, and never linked into anything.
  • Resolution: build-dependencies are solved together with the other dependencies (one version per package) and recorded in cx.lock. A package that is both a library you link and a tool is declared once, in [dependencies]. Index entries mark them build = true.
  • Cross-compiling: with --target, the tools are built for the host first, in target/host/<profile>/, with the host compiler, and the target build runs those.
  • cx add NAME --build adds one, and cx tree lists them under [build-dependencies]. They can't be optional, public or system packages.

build.cmake

For anything the generators can't express, a package can have a build.cmake next to its cx.toml. It's plain CMake, included after the package's targets are defined, with these variables:

Variable Value
CX_PACKAGE_NAME the package name
CX_PACKAGE_DIR its directory
CX_LIB_TARGET its library target (empty if it has none)
CX_GEN_DIR target/cx/gen/<package>/, for generated files
CX_FEATURES the enabled features
CX_FEATURE_<NAME> ON for each enabled feature (upper case, - and + become _)
# build.cmake
find_package(BISON REQUIRED)
bison_target(parser grammar.y "${CX_GEN_DIR}/parser.cpp")
target_sources(${CX_LIB_TARGET} PRIVATE ${BISON_parser_OUTPUTS})
if(CX_FEATURE_FAST)
target_compile_definitions(${CX_LIB_TARGET} PRIVATE FAST_PARSER=1)
endif()

These variables are the whole contract. A script that uses cx's internal target names or other variables of the generated project may break with any cx release.

Dependencies with build scripts

Generators and build.cmake run code that comes with the package. For dependencies from registries and git:

  • The index says so: a published package with generators or a build.cmake has build-script = true in its index entry.
  • cx points it out once: the first build that uses such a dependency (per version) prints a note naming it.
  • They're never taken from the artifact cache, since their output isn't determined by their sources alone (see Build caches).

Generators are package-local: they can't use the outputs of a dependency's generators.