The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Importing an existing project

Most projects already have a build. cx init --from reads a CMakeLists.txt, a vcpkg.json, or both, and drafts a cx.toml from them:

$ cx init --from CMakeLists.txt --from vcpkg.json --dry-run # print it, write nothing
$ cx init --from CMakeLists.txt --from vcpkg.json
warning: CMakeLists.txt:21: warning flags were not imported: cx enables warnings itself (`[build] warnings`)
warning: CMakeLists.txt:40: `configure_file(...)` was not imported; generate the file with `[[generate]]` or `build.cmake`
Created `geotool` package from `CMakeLists.txt`, `vcpkg.json`; 2 notes above, also at the end of cx.toml
$ cx build

Like cx init, it never replaces an existing cx.toml, adds /target to .gitignore and runs git init (unless --vcs none or the directory is already in a repository). It writes no sources. --name and --std override what the files say; --lib, --bin and --c can't be combined with --from, since the imported build says what the package is.

Nothing is run or evaluated: cx reads the files. What it can't translate faithfully, it doesn't guess. It prints a warning for it and lists it at the end of the new cx.toml, with the file and line, so you can finish the job by hand. The CMake build keeps working next to cx, which only writes to target/.

From a CMakeLists.txt

cx reads the top CMakeLists.txt and follows add_subdirectory(). Variables set with set(), list(APPEND) and option() are expanded, as are ${PROJECT_NAME}, ${PROJECT_VERSION} and ${CMAKE_CURRENT_SOURCE_DIR}.

CMake cx.toml
project(name VERSION 1.2 DESCRIPTION ... LANGUAGES C) [package] name, version = "1.2.0", description, language
CMAKE_CXX_STANDARD, cxx_std_20, CXX_STANDARD, -std=c++20 standard (the newest any target asks for)
find_package(ZLIB 1.2 REQUIRED COMPONENTS ...) zlib = { system = "ZLIB", version = "1.2", components = [...] }
FetchContent_Declare(fmt GIT_REPOSITORY ... GIT_TAG 11.2.0) fmt = { git = "...", tag = "11.2.0" } (a commit hash becomes rev, main/master a branch)
FetchContent_Declare(x URL https://github.com/o/r/archive/refs/tags/v1.tar.gz) x = { git = "https://github.com/o/r", tag = "v1" }
CPMAddPackage("gh:o/r@1.2.3"), CPMAddPackage(NAME ... OPTIONS "A ON") r = { git = "https://github.com/o/r", tag = "v1.2.3", options = ["A=ON"] }
add_subdirectory(third_party/x) with its own project() x = { path = "third_party/x" }, a plain CMake dependency
set(SPDLOG_FMT_EXTERNAL ON CACHE ...) options = ["SPDLOG_FMT_EXTERNAL=ON"] on the dependency spdlog
target_link_libraries(app fmt::fmt ZLIB::ZLIB m) targets of the dependencies that provide them; m to [build] link-libraries
target_compile_definitions, add_compile_definitions [build] defines
target_compile_options, target_link_options [build] flags, link-flags (warning, optimization and debug flags are left to cx and the profiles)
target_include_directories(... third_party) [build] include-dirs (include/ is cx's own)
add_library(... SHARED) [lib] kind = "shared"
add_executable(tool tools/main.cpp tools/opts.cpp) [[bin]] name = "tool" path = "tools/main.cpp" (a main.* brings its directory along)
if(WIN32), if(UNIX), if(APPLE), if(NOT ...) around find_package/FetchContent [target.'cfg(windows)'.dependencies] and so on
dependencies only tests use, if(BUILD_TESTING) blocks, Catch2/GoogleTest/doctest [dev-dependencies]

These are reported instead:

  • Sources outside cx's layout. cx builds the library from src/** and tests from tests/. A library source in lib/, a file(GLOB) of another directory or a test in test/ is listed, with where it should go.
  • More than one library. A cx package has one; the sources are merged, and the note suggests a workspace with one member each.
  • Conditions cx can't read. Anything inside if(SOME_OPTION), elseif(), foreach() or while(). Platform-specific build settings (target_compile_definitions inside if(WIN32)).
  • Build steps. configure_file, add_custom_command and add_custom_target (see Code generation and build scripts), target_precompile_headers, and the project's own functions and macros.
  • Generator expressions other than $<BUILD_INTERFACE:...>, and variables cx couldn't expand.

From a vcpkg.json

Each dependency becomes a vcpkg dependency:

{ "name": "demo", "version": "1.2.0",
"dependencies": [ "fmt", { "name": "curl", "default-features": false, "features": ["ssl"] },
{ "name": "wil", "platform": "windows" } ] }
[dependencies]
curl = { vcpkg = true, package = "CURL", features = ["ssl"], default-features = false, targets = ["CURL::libcurl"] }
fmt = { vcpkg = true, targets = ["fmt::fmt"] }
[target."cfg(windows)".dependencies]
# TODO: check the find_package name (`package`) and `targets`; cx guesses `wil` and `wil::wil`
wil = { vcpkg = true }
  • Package fields: name, version (or version-semver, version-string, version-date, made semver), description and license.
  • find_package names and targets: cx knows them for common ports (fmt, spdlog, nlohmann-json, zlib, openssl, curl, Catch2, GoogleTest, Boost libraries, ...). For others it writes a # TODO above the line: check package and targets in the port's usage notes.
  • Test frameworks (catch2, gtest, doctest, benchmark) go to [dev-dependencies].
  • platform: windows, linux, osx, android, ios, freebsd, openbsd, x64 and arm64, combined with !, &, | and parentheses, become cfg(...). A dependency whose expression uses anything else (uwp, static, ...) is left out and reported.
  • Versions: cx installs ports with the versions of your vcpkg checkout, so version>=, overrides and builtin-baseline aren't imported: a version>= becomes a comment, the others notes.
  • Host tools ("host": true) are left out: cx runs tools from [build-dependencies] or PATH.

Both together

With --from CMakeLists.txt --from vcpkg.json, the CMake file gives the package, its targets and settings, and how each dependency is linked; the vcpkg manifest says which of the find_package() dependencies come from vcpkg. A port replaces the find_package it provides (zlib for find_package(ZLIB)), and ports CMake doesn't use are added as they are. With only --from CMakeLists.txt, cx mentions a vcpkg.json lying next to it.

Then build: cx build, cx test. Compare with the CMake build, and work through the notes at the end of cx.toml.