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:
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/.
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:
Each dependency becomes a vcpkg dependency:
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.