mirror of
https://github.com/build-cpp/cmkr
synced 2026-06-08 13:22:55 +00:00
Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1249af365 | |||
| ea2ff31b5a | |||
| a32eb66df4 | |||
| 9b85833240 | |||
| ccb3d0b9ef | |||
| 49cdab39bd | |||
| e318139314 | |||
| 0f2a9a55fb | |||
| 38cec2d5d1 | |||
| 3caf593438 | |||
| c94dbd6416 | |||
| a1eaf601c6 | |||
| 10320c8e55 | |||
| 45e12c8d98 | |||
| e12ebc060d | |||
| c6e3fc159f | |||
| dc6f306d9c | |||
| e08bd5a311 | |||
| 068977e2f5 | |||
| 73147747a4 | |||
| a64fa9cc34 | |||
| eb6ccb39ee | |||
| 91dd8ce51f | |||
| efbfca27bd | |||
| 78bcf2acf7 | |||
| 83559cc5d4 | |||
| 5d85cc83e3 | |||
| dea41d80b5 | |||
| b52cf5b241 | |||
| e9480a625f | |||
| 13a7a8788a | |||
| 45ca74099c | |||
| ae3d73896e | |||
| 57042565d1 | |||
| d02ee3ed24 | |||
| 4a15d2ba55 | |||
| 87c0295f93 | |||
| a608e6b778 | |||
| 523f5f4a8a | |||
| 510cf71af5 | |||
| 87d90138b4 | |||
| 50615e0c27 | |||
| 42ed3048b3 | |||
| b0a1d79a42 | |||
| 5882bc082d | |||
| a90b3c6095 | |||
| 0a887c08f8 | |||
| 9f2cfe4083 | |||
| 15ad1dc6a7 | |||
| dabffeaed9 | |||
| 59fe5c38b8 | |||
| dcdc7ac410 | |||
| 88377a8bfb | |||
| 3a9685be82 | |||
| 5576789c18 |
@@ -13,6 +13,7 @@ AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortLambdasOnASingleLine: None
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
|
||||
@@ -14,7 +14,7 @@ jobs:
|
||||
|
||||
- name: clang-format
|
||||
id: clang-format
|
||||
uses: DoozyX/clang-format-lint-action@c3b2c943e924028b93a707a5b1b017976ab8d50c # v0.15
|
||||
uses: DoozyX/clang-format-lint-action@c71d0bf4e21876ebec3e5647491186f8797fde31 # v0.18.2
|
||||
with:
|
||||
exclude: './third_party'
|
||||
extensions: 'c,h,cpp,hpp'
|
||||
|
||||
Generated
+2
-2
@@ -1,7 +1,7 @@
|
||||
# This file is automatically generated from cmake.toml - DO NOT EDIT
|
||||
# See https://github.com/build-cpp/cmkr for more information
|
||||
|
||||
cmake_minimum_required(VERSION 2.8...3.8)
|
||||
cmake_minimum_required(VERSION 2.8...3.31)
|
||||
|
||||
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
|
||||
message(FATAL_ERROR "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build")
|
||||
@@ -22,7 +22,7 @@ project(cmkr
|
||||
LANGUAGES
|
||||
CXX
|
||||
VERSION
|
||||
0.2.31
|
||||
0.2.44
|
||||
DESCRIPTION
|
||||
"CMakeLists generator from TOML"
|
||||
)
|
||||
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
[cmake]
|
||||
version = "2.8...3.8"
|
||||
version = "2.8...3.31"
|
||||
cmkr-include = false
|
||||
|
||||
[project]
|
||||
name = "cmkr"
|
||||
version = "0.2.31"
|
||||
version = "0.2.44"
|
||||
description = "CMakeLists generator from TOML"
|
||||
languages = ["CXX"]
|
||||
include-after = [
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ include_guard()
|
||||
|
||||
# Change these defaults to point to your infrastructure if desired
|
||||
set(CMKR_REPO "https://github.com/build-cpp/cmkr" CACHE STRING "cmkr git repository" FORCE)
|
||||
set(CMKR_TAG "v0.2.31" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
|
||||
set(CMKR_TAG "v0.2.44" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
|
||||
set(CMKR_COMMIT_HASH "" CACHE STRING "cmkr git commit hash (optional)" FORCE)
|
||||
|
||||
# To bootstrap/generate a cmkr project: cmake -P cmkr.cmake
|
||||
|
||||
+55
-13
@@ -109,6 +109,13 @@ clang-any = "CMAKE_CXX_COMPILER_ID MATCHES \"Clang\" OR CMAKE_C_COMPILER_ID MATC
|
||||
root = "CMKR_ROOT_PROJECT"
|
||||
x64 = "CMAKE_SIZEOF_VOID_P EQUAL 8"
|
||||
x32 = "CMAKE_SIZEOF_VOID_P EQUAL 4"
|
||||
android = "ANDROID"
|
||||
apple = "APPLE"
|
||||
bsd = "BSD"
|
||||
cygwin = "CYGWIN"
|
||||
ios = "IOS"
|
||||
xcode = "XCODE"
|
||||
wince = "WINCE"
|
||||
```
|
||||
|
||||
## Subdirectories
|
||||
@@ -153,14 +160,20 @@ Variables emit a [`set`](https://cmake.org/cmake/help/latest/command/set.html) a
|
||||
|
||||
```toml
|
||||
[vcpkg]
|
||||
version = "2024.03.25"
|
||||
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.03.25.tar.gz"
|
||||
version = "2024.11.16"
|
||||
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.11.16.tar.gz"
|
||||
packages = ["fmt", "zlib"]
|
||||
overlay-ports = ["my-ports"]
|
||||
overlay-triplets = ["my-triplets"]
|
||||
```
|
||||
|
||||
The vcpkg `version` will automatically generate the `url` from the [official repository](https://github.com/microsoft/vcpkg/releases). For a custom registry you can specify your own `url` (and omit the `version`). You can browse available packages on [vcpkg.io](https://vcpkg.io/en/packages.html).
|
||||
The vcpkg `version` will automatically generate the `url` from the [official repository](https://github.com/microsoft/vcpkg/releases). For a custom registry you can specify your own `url` (and omit the `version`). You can browse available packages on [vcpkg.io](https://vcpkg.io/en/packages.html) or [vcpkg.link](https://vcpkg.link).
|
||||
|
||||
To specify package features you can use the following syntax: `imgui[docking-experimental,freetype,sdl2-binding,opengl3-binding]`.
|
||||
To specify package features you can use the following syntax: `imgui[docking-experimental,freetype,sdl2-binding,opengl3-binding]`. To disable the [default features](https://learn.microsoft.com/en-us/vcpkg/concepts/default-features) you can do: `cpp-httplib[core,openssl]`
|
||||
|
||||
The `overlay-ports` feature allows you to embed vcpkg ports inside your project, without having to fork the main vcpkg registry or creating a custom registry. You can find more information in the relevant [documentation](https://learn.microsoft.com/en-us/vcpkg/concepts/overlay-ports). The `overlay-triplets` feature allows you to customize triplets and change the default behavior (for example always preferring static libraries on Windows). To specify both in one go you can do `[vcpkg].overlay = "vcpkg-overlay"`.
|
||||
|
||||
For a concrete example of all the features, take a look at the [vcpkg_template](https://github.com/build-cpp/vcpkg_template) repository.
|
||||
|
||||
## Packages
|
||||
|
||||
@@ -208,28 +221,28 @@ Table keys that match CMake variable names (`[A-Z_]+`) will be passed to the [`F
|
||||
[target.mytarget]
|
||||
condition = "mycondition"
|
||||
alias = "mytarget::mytarget"
|
||||
type = "static" # executable, shared (DLL), static, interface, object, library, custom
|
||||
type = "static" # executable, library, shared (DLL), static, interface, object, custom
|
||||
headers = ["src/mytarget.h"]
|
||||
sources = ["src/mytarget.cpp"]
|
||||
msvc-runtime = "" # dynamic (implicit default), static
|
||||
|
||||
# The keys below match the target_xxx CMake commands
|
||||
# Keys prefixed with private- will get PRIVATE visibility
|
||||
compile-definitions = [""]
|
||||
compile-definitions = [""] # preprocessor define (-DFOO)
|
||||
private-compile-definitions = [""]
|
||||
compile-features = [""]
|
||||
compile-features = [""] # C++ standard version (cxx_std_20)
|
||||
private-compile-features = [""]
|
||||
compile-options = [""]
|
||||
compile-options = [""] # compiler flags
|
||||
private-compile-options = [""]
|
||||
include-directories = [""]
|
||||
include-directories = [""] # include paths/directories
|
||||
private-include-directories = [""]
|
||||
link-directories = [""]
|
||||
link-directories = [""] # library directories
|
||||
private-link-directories = [""]
|
||||
link-libraries = [""]
|
||||
link-libraries = [""] # dependencies
|
||||
private-link-libraries = [""]
|
||||
link-options = [""]
|
||||
link-options = [""] # linker flags
|
||||
private-link-options = [""]
|
||||
precompile-headers = [""]
|
||||
precompile-headers = [""] # precompiled headers
|
||||
private-precompile-headers = [""]
|
||||
|
||||
cmake-before = """
|
||||
@@ -248,6 +261,35 @@ CXX_STANDARD_REQUIRED = true
|
||||
FOLDER = "MyFolder"
|
||||
```
|
||||
|
||||
A table mapping the cmkr features to the relevant CMake construct and the relevant documentation pages:
|
||||
|
||||
| cmkr | CMake construct | Description |
|
||||
| ---- | ----- | ----------- |
|
||||
| `alias` | [Alias Libraries](https://cmake.org/cmake/help/latest/command/add_library.html#alias-libraries) | Create an [alias target](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#alias-targets), used for namespacing or clarity. |
|
||||
| `sources` | [`target_sources`](https://cmake.org/cmake/help/latest/command/target_sources.html) | Source files (`PRIVATE` except `interface` targets). |
|
||||
| `headers` | [`target_sources`](https://cmake.org/cmake/help/latest/command/target_sources.html) | For readability (and future packaging). |
|
||||
| `msvc-runtime` | [`MSVC_RUNTIME_LIBRARY`](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html) | The [CMP0091](https://cmake.org/cmake/help/latest/policy/CMP0091.html) policy is set automatically. |
|
||||
| `compile-definitions` | [`target_compile_definitions`](https://cmake.org/cmake/help/latest/command/target_compile_definitions.html) | Adds a macro definition (define, `-DMYMACRO=XXX`). |
|
||||
| `compile-features` | [`target_compile_features`](https://cmake.org/cmake/help/latest/command/target_compile_features.html) | Specifies the C++ standard version (`cxx_std_20`). |
|
||||
| `compile-options` | [`target_compile_options`](https://cmake.org/cmake/help/latest/command/target_compile_options.html) | Adds compiler flags. |
|
||||
| `include-directories` | [`target_include_directories`](https://cmake.org/cmake/help/latest/command/target_include_directories.html) | Adds include directories. |
|
||||
| `link-directories` | [`target_link_directories`](https://cmake.org/cmake/help/latest/command/target_link_directories.html) | Adds library directories. |
|
||||
| `link-libraries` | [`target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) | Adds library dependencies. Use `::mylib` to make sure a target exists. |
|
||||
| `link-options` | [`target_link_options`](https://cmake.org/cmake/help/latest/command/target_link_options.html) | Adds linker flags. |
|
||||
| `precompile-headers` | [`target_precompile_headers`](https://cmake.org/cmake/help/latest/command/target_precompile_headers.html) | Specifies precompiled headers. |
|
||||
| `properties` | [`set_target_properties`](https://cmake.org/cmake/help/latest/command/set_target_properties.html) | See [properties on targets](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets) for more information. |
|
||||
|
||||
The default [visibility](/basics) is as follows:
|
||||
|
||||
| Type | Visibility |
|
||||
| ------------ | ----------- |
|
||||
| `executable` | `PRIVATE` |
|
||||
| `library` | `PUBLIC` |
|
||||
| `shared` | `PUBLIC` |
|
||||
| `static` | `PUBLIC` |
|
||||
| `object` | `PUBLIC` |
|
||||
| `interface` | `INTERFACE` |
|
||||
|
||||
## Templates
|
||||
|
||||
To avoid repeating yourself you can create your own target type and use it in your targets:
|
||||
|
||||
@@ -9,7 +9,7 @@ nav_order: 9
|
||||
|
||||
# Compiler flags
|
||||
|
||||
Example project that sets compiler/linker flags for various platforms.
|
||||
Example project that sets compiler flags and preprocessor defines for various platforms.
|
||||
|
||||
```toml
|
||||
[project]
|
||||
@@ -20,11 +20,14 @@ description = "Compiler flags"
|
||||
type = "executable"
|
||||
sources = ["src/main.cpp"]
|
||||
msvc.compile-options = ["/W2"]
|
||||
msvc.compile-definitions = ["PLATFORM=\"msvc\""]
|
||||
gcc.compile-options = ["-Wall"]
|
||||
gcc.compile-definitions = ["PLATFORM=\"gcc\""]
|
||||
clang.compile-options = ["-Wall"]
|
||||
clang.compile-definitions = ["PLATFORM=\"clang\""]
|
||||
```
|
||||
|
||||
The `hello` target uses [conditions](/cmake-toml#conditions) to set different compiler flags depending on the platform. See the [targets](/cmake-toml/#targets) documentation for other things you can set.
|
||||
The `hello` target uses [conditions](/cmake-toml#conditions) to set different compiler flags and definitions depending on the platform. See the [targets](/cmake-toml/#targets) documentation for other things you can set.
|
||||
|
||||
_Note_: In general you only want to specify flags _required_ to compile your code without errors.
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ nav_order: 4
|
||||
|
||||
# Dependencies from vcpkg
|
||||
|
||||
Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
|
||||
Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://github.com/microsoft/vcpkg) and links an `example` target to it:
|
||||
|
||||
```toml
|
||||
[project]
|
||||
@@ -17,13 +17,12 @@ name = "vcpkg"
|
||||
description = "Dependencies from vcpkg"
|
||||
|
||||
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
|
||||
# See https://vcpkg.io/en/packages.html for available packages
|
||||
# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages
|
||||
[vcpkg]
|
||||
version = "2024.03.25"
|
||||
version = "2024.11.16"
|
||||
packages = ["fmt"]
|
||||
|
||||
[find-package]
|
||||
fmt = {}
|
||||
[find-package.fmt]
|
||||
|
||||
[target.example]
|
||||
type = "executable"
|
||||
|
||||
@@ -45,10 +45,15 @@ struct Vcpkg {
|
||||
struct Package {
|
||||
std::string name;
|
||||
std::vector<std::string> features;
|
||||
bool default_features = true;
|
||||
};
|
||||
|
||||
std::vector<Package> packages;
|
||||
|
||||
// https://github.com/Microsoft/vcpkg-docs/blob/main/vcpkg/users/buildsystems/cmake-integration.md
|
||||
std::vector<std::string> overlay_ports;
|
||||
std::vector<std::string> overlay_triplets;
|
||||
|
||||
bool enabled() const {
|
||||
return !packages.empty();
|
||||
}
|
||||
@@ -155,7 +160,7 @@ struct Content {
|
||||
Condition<std::string> cmake_after;
|
||||
ConditionVector include_before;
|
||||
ConditionVector include_after;
|
||||
bool system;
|
||||
bool system = false;
|
||||
std::string subdir;
|
||||
};
|
||||
|
||||
|
||||
+116
-41
@@ -289,6 +289,10 @@ struct RawArg {
|
||||
}
|
||||
|
||||
std::string arg;
|
||||
|
||||
bool empty() const {
|
||||
return arg.empty();
|
||||
}
|
||||
};
|
||||
|
||||
// Credit: JustMagic
|
||||
@@ -406,7 +410,7 @@ struct Command {
|
||||
}
|
||||
|
||||
bool print_arg(const RawArg &arg) {
|
||||
if (arg.arg.empty()) {
|
||||
if (arg.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -558,11 +562,15 @@ struct Generator {
|
||||
}
|
||||
|
||||
void conditional_includes(const parser::ConditionVector &include) {
|
||||
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) {
|
||||
inject_includes(includes);
|
||||
});
|
||||
}
|
||||
|
||||
void conditional_cmake(const parser::Condition<std::string> &cmake) {
|
||||
handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
handle_condition(cmake, [this](const std::string &, const std::string &cmake) {
|
||||
inject_cmake(cmake);
|
||||
});
|
||||
}
|
||||
|
||||
bool if_condition(const std::string &condition) {
|
||||
@@ -577,7 +585,7 @@ struct Generator {
|
||||
}
|
||||
cmd("if", "NOTE: unnamed condition")(RawArg(cmake_condition(condition)));
|
||||
} else {
|
||||
cmd("if", condition)(RawArg(found->second));
|
||||
cmd("if", condition)(RawArg(cmake_condition(found->second)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -719,9 +727,15 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
|
||||
// Helper lambdas for more convenient CMake generation
|
||||
auto &ss = gen.ss;
|
||||
auto cmd = [&gen](const std::string &command) { return gen.cmd(command); };
|
||||
auto comment = [&gen](const std::string &comment) { return gen.comment(comment); };
|
||||
auto endl = [&gen]() { gen.endl(); };
|
||||
auto cmd = [&gen](const std::string &command) {
|
||||
return gen.cmd(command);
|
||||
};
|
||||
auto comment = [&gen](const std::string &comment) {
|
||||
return gen.comment(comment);
|
||||
};
|
||||
auto endl = [&gen]() {
|
||||
gen.endl();
|
||||
};
|
||||
|
||||
std::string cmkr_url = "https://github.com/build-cpp/cmkr";
|
||||
comment("This file is automatically generated from cmake.toml - DO NOT EDIT");
|
||||
@@ -731,7 +745,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
if (is_root_project) {
|
||||
cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl();
|
||||
|
||||
// clang-format on
|
||||
if (!project.allow_in_tree) {
|
||||
// clang-format off
|
||||
cmd("if")("CMAKE_SOURCE_DIR", "STREQUAL", "CMAKE_BINARY_DIR");
|
||||
@@ -740,6 +753,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
if (project.project_msvc_runtime != parser::msvc_last) {
|
||||
comment("Enable support for MSVC_RUNTIME_LIBRARY");
|
||||
cmd("cmake_policy")("SET", "CMP0091", "NEW").endl();
|
||||
}
|
||||
|
||||
cmd("set")("CMKR_ROOT_PROJECT", "OFF");
|
||||
// clang-format off
|
||||
cmd("if")("CMAKE_CURRENT_SOURCE_DIR", "STREQUAL", "CMAKE_SOURCE_DIR");
|
||||
@@ -757,16 +775,14 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
cmd("set_property")("GLOBAL", "PROPERTY", "USE_FOLDERS", "ON").endl();
|
||||
|
||||
comment("Create a configure-time dependency on cmake.toml to improve IDE support");
|
||||
cmd("configure_file")("cmake.toml", "cmake.toml", "COPYONLY");
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
if (project.cmake_minimum_version(3, 0)) {
|
||||
cmd("set_property")("DIRECTORY", "APPEND", "PROPERTY", "CMAKE_CONFIGURE_DEPENDS", "cmake.toml");
|
||||
} else {
|
||||
cmd("configure_file")("cmake.toml", "cmake.toml", "COPYONLY");
|
||||
}
|
||||
|
||||
if (project.project_msvc_runtime != parser::msvc_last) {
|
||||
comment("Enable support for MSVC_RUNTIME_LIBRARY");
|
||||
cmd("cmake_policy")("SET", "CMP0091", "NEW");
|
||||
|
||||
// clang-format off
|
||||
cmd("if")("NOT", "DEFINED", "CMAKE_MSVC_RUNTIME_LIBRARY");
|
||||
if (project.project_msvc_runtime != parser::msvc_last) {
|
||||
cmd("if")("NOT", "DEFINED", "CMAKE_MSVC_RUNTIME_LIBRARY");
|
||||
switch (project.project_msvc_runtime) {
|
||||
case parser::msvc_dynamic:
|
||||
cmd("set")("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL");
|
||||
@@ -777,21 +793,24 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
}
|
||||
cmd("endif")().endl();
|
||||
}
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
|
||||
fs::path cmkr_include(project.cmkr_include);
|
||||
if (!project.cmkr_include.empty() && !fs::exists(cmkr_include) && cmkr_include.is_relative()) {
|
||||
create_file(cmkr_include, resources::cmkr);
|
||||
}
|
||||
} else {
|
||||
// clang-format off
|
||||
comment("Create a configure-time dependency on cmake.toml to improve IDE support");
|
||||
cmd("if")("CMKR_ROOT_PROJECT");
|
||||
if (project.cmake_minimum_version(3, 0)) {
|
||||
cmd("set_property")("DIRECTORY", "APPEND", "PROPERTY", "CMAKE_CONFIGURE_DEPENDS", "cmake.toml");
|
||||
} else {
|
||||
cmd("configure_file")("cmake.toml", "cmake.toml", "COPYONLY");
|
||||
}
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// TODO: remove support and replace with global compile-features
|
||||
@@ -856,6 +875,27 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
endl();
|
||||
}
|
||||
|
||||
if (project.vcpkg.enabled()) {
|
||||
comment("vcpkg settings");
|
||||
auto emit_overlay = [&cmd](const std::string &name, const std::vector<std::string> &overlay) {
|
||||
if (!overlay.empty()) {
|
||||
std::vector<std::string> set_args;
|
||||
for (const auto &directory : overlay) {
|
||||
if (!fs::path(directory).is_relative()) {
|
||||
throw std::runtime_error("[vcpkg] overlay is not a relative path: " + directory);
|
||||
}
|
||||
if (!fs::is_directory(directory)) {
|
||||
throw std::runtime_error("[vcpkg] overlay is not a directory: " + directory);
|
||||
}
|
||||
set_args.emplace_back("${CMAKE_CURRENT_SOURCE_DIR}/" + directory);
|
||||
}
|
||||
cmd("set")(name, set_args);
|
||||
}
|
||||
};
|
||||
emit_overlay("VCPKG_OVERLAY_PORTS", project.vcpkg.overlay_ports);
|
||||
emit_overlay("VCPKG_OVERLAY_TRIPLETS", project.vcpkg.overlay_triplets);
|
||||
}
|
||||
|
||||
gen.conditional_includes(project.include_before);
|
||||
gen.conditional_cmake(project.cmake_before);
|
||||
|
||||
@@ -900,6 +940,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
}
|
||||
}
|
||||
|
||||
tsl::ordered_map<std::string, std::string> vcpkg_args = {
|
||||
{"URL", url},
|
||||
{"SUBBUILD_DIR", "CMakeFiles/vcpkg-subbuild"},
|
||||
{"SOURCE_DIR", "vcpkg"},
|
||||
{"BINARY_DIR", "CMakeFiles/vcpkg-build"},
|
||||
};
|
||||
|
||||
// CMake to bootstrap vcpkg and download the packages
|
||||
// clang-format off
|
||||
cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG");
|
||||
@@ -909,16 +956,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
cmd("cmake_policy")("SET", "CMP0135", "NEW");
|
||||
cmd("endif")();
|
||||
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")...");
|
||||
cmd("FetchContent_Declare")("vcpkg", "URL", url);
|
||||
// Not using FetchContent_MakeAvailable here in case vcpkg adds CMakeLists.txt
|
||||
cmd("FetchContent_GetProperties")("vcpkg");
|
||||
cmd("if")("NOT", "vcpkg_POPULATED");
|
||||
cmd("FetchContent_Populate")("vcpkg");
|
||||
cmd("if")("CMAKE_HOST_SYSTEM_NAME", "STREQUAL", "Darwin", "AND", "CMAKE_OSX_ARCHITECTURES", "STREQUAL", RawArg("\"\""));
|
||||
cmd("set")("CMAKE_OSX_ARCHITECTURES", "${CMAKE_HOST_SYSTEM_PROCESSOR}", "CACHE", "STRING", RawArg("\"\""), "FORCE");
|
||||
cmd("endif")();
|
||||
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
|
||||
cmd("FetchContent_Declare")("vcpkg", vcpkg_args);
|
||||
cmd("FetchContent_MakeAvailable")("vcpkg").endl();
|
||||
cmd("if")("CMAKE_HOST_SYSTEM_NAME", "STREQUAL", "Darwin", "AND", "CMAKE_OSX_ARCHITECTURES", "STREQUAL", RawArg("\"\""));
|
||||
cmd("set")("CMAKE_OSX_ARCHITECTURES", "${CMAKE_HOST_SYSTEM_PROCESSOR}", "CACHE", "STRING", RawArg("\"\""), "FORCE");
|
||||
cmd("endif")();
|
||||
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
|
||||
cmd("endif")();
|
||||
endl();
|
||||
// clang-format on
|
||||
@@ -931,7 +974,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
ofs << R"({
|
||||
"$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT",
|
||||
"$cmkr-url": "https://github.com/build-cpp/cmkr",
|
||||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
|
||||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
|
||||
"dependencies": [
|
||||
)";
|
||||
|
||||
@@ -952,17 +995,20 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
throw std::runtime_error("Invalid vcpkg package feature '" + feature + "', name is reserved");
|
||||
}
|
||||
}
|
||||
if (features.empty()) {
|
||||
if (features.empty() && package.default_features) {
|
||||
ofs << " \"" << package.name << '\"';
|
||||
} else {
|
||||
ofs << " {\n";
|
||||
ofs << " \"name\": \"" << package.name << "\",\n";
|
||||
if (!package.default_features) {
|
||||
ofs << " \"default-features\": false,\n";
|
||||
}
|
||||
ofs << " \"features\": [";
|
||||
for (size_t j = 0; j < features.size(); j++) {
|
||||
const auto &feature = features[j];
|
||||
ofs << '\"' << feature << '\"';
|
||||
if (j + 1 < features.size()) {
|
||||
ofs << ',';
|
||||
ofs << ", ";
|
||||
}
|
||||
}
|
||||
ofs << "]\n";
|
||||
@@ -988,7 +1034,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
ofs << " ],\n";
|
||||
ofs << " \"description\": \"" << escape(project.project_description) << "\",\n";
|
||||
ofs << " \"name\": \"" << escape(vcpkg_escape_identifier(project.project_name)) << "\",\n";
|
||||
ofs << R"( "version-string": "")" << '\n';
|
||||
ofs << " \"version-string\": \"none\"\n";
|
||||
ofs << "}\n";
|
||||
}
|
||||
|
||||
@@ -1086,6 +1132,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
if (project_languages.empty())
|
||||
project_languages = {"C", "CXX"};
|
||||
|
||||
// Reference: https://gitlab.kitware.com/cmake/cmake/-/issues/24340#note_1304703
|
||||
project_languages.push_back("RC");
|
||||
|
||||
// All acceptable extensions based off our given languages.
|
||||
tsl::ordered_set<std::string> project_extensions;
|
||||
for (const auto &language : project_languages) {
|
||||
@@ -1110,7 +1159,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
for (size_t i = 0; i < project.targets.size(); i++) {
|
||||
const auto &target = project.targets[i];
|
||||
|
||||
auto throw_target_error = [&target](const std::string &message) { throw std::runtime_error("[target." + target.name + "] " + message); };
|
||||
auto throw_target_error = [&target](const std::string &message) {
|
||||
throw std::runtime_error("[target." + target.name + "] " + message);
|
||||
};
|
||||
|
||||
const parser::Template *tmplate = nullptr;
|
||||
std::unique_ptr<ConditionScope> tmplate_cs{};
|
||||
@@ -1349,8 +1400,29 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
}
|
||||
|
||||
auto target_cmd = [&](const char *command, const parser::ConditionVector &cargs, const std::string &scope) {
|
||||
gen.handle_condition(cargs,
|
||||
[&](const std::string &, const std::vector<std::string> &args) { cmd(command)(target.name, scope, args); });
|
||||
gen.handle_condition(cargs, [&](const std::string &, const std::vector<std::string> &args) {
|
||||
cmd(command)(target.name, scope, args);
|
||||
});
|
||||
};
|
||||
|
||||
auto link_libraries = [&](const parser::ConditionVector &cargs, const std::string &scope) {
|
||||
gen.handle_condition(cargs, [&](const std::string &, const std::vector<std::string> &args) {
|
||||
std::vector<std::string> targs;
|
||||
for (const std::string &arg : args) {
|
||||
if (arg.find("::") == 0) {
|
||||
auto library = arg.substr(2);
|
||||
// clang-format off
|
||||
cmd("if")("NOT", "TARGET", library);
|
||||
cmd("message")("FATAL_ERROR", "Target \"" + library + "\" referenced by \"" + target.name + "\" does not exist!");
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
targs.push_back(std::move(library));
|
||||
} else {
|
||||
targs.push_back(arg);
|
||||
}
|
||||
}
|
||||
cmd("target_link_libraries")(target.name, scope, targs);
|
||||
});
|
||||
};
|
||||
|
||||
auto gen_target_cmds = [&](const parser::Target &t) {
|
||||
@@ -1369,8 +1441,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
target_cmd("target_link_directories", t.link_directories, target_scope);
|
||||
target_cmd("target_link_directories", t.private_link_directories, "PRIVATE");
|
||||
|
||||
target_cmd("target_link_libraries", t.link_libraries, target_scope);
|
||||
target_cmd("target_link_libraries", t.private_link_libraries, "PRIVATE");
|
||||
link_libraries(t.link_libraries, target_scope);
|
||||
link_libraries(t.private_link_libraries, "PRIVATE");
|
||||
|
||||
target_cmd("target_link_options", t.link_options, target_scope);
|
||||
target_cmd("target_link_options", t.private_link_options, "PRIVATE");
|
||||
@@ -1393,14 +1465,17 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
}
|
||||
|
||||
gen.handle_condition(props, [&](const std::string &, const tsl::ordered_map<std::string, std::string> &properties) {
|
||||
tsl::ordered_map<std::string, RawArg> raw_properties;
|
||||
for (const auto &propItr : properties) {
|
||||
if (propItr.first == "MSVC_RUNTIME_LIBRARY") {
|
||||
if (project_root->project_msvc_runtime == parser::msvc_last) {
|
||||
throw_target_error("You cannot set msvc-runtime without setting the root [project].msvc-runtime");
|
||||
}
|
||||
}
|
||||
// NOTE: We need to make sure that empty strings do not get omitted
|
||||
raw_properties.emplace(propItr.first, Command::quote(propItr.second));
|
||||
}
|
||||
cmd("set_target_properties")(target.name, "PROPERTIES", properties);
|
||||
cmd("set_target_properties")(target.name, "PROPERTIES", raw_properties);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+57
-19
@@ -274,6 +274,13 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
conditions["root"] = R"cmake(CMKR_ROOT_PROJECT)cmake";
|
||||
conditions["x64"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 8)cmake";
|
||||
conditions["x32"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 4)cmake";
|
||||
conditions["android"] = R"cmake(ANDROID)cmake";
|
||||
conditions["apple"] = R"cmake(APPLE)cmake";
|
||||
conditions["bsd"] = R"cmake(BSD)cmake";
|
||||
conditions["cygwin"] = R"cmake(CYGWIN)cmake";
|
||||
conditions["ios"] = R"cmake(IOS)cmake";
|
||||
conditions["xcode"] = R"cmake(XCODE)cmake";
|
||||
conditions["wince"] = R"cmake(WINCE)cmake";
|
||||
} else {
|
||||
conditions = parent->conditions;
|
||||
templates = parent->templates;
|
||||
@@ -432,6 +439,9 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
}
|
||||
options.push_back(o);
|
||||
|
||||
// Add a condition matching the option name
|
||||
conditions.emplace(o.name, o.name);
|
||||
|
||||
// Add an implicit condition for the option
|
||||
auto ncondition = normalize(o.name);
|
||||
if (ncondition.find(nproject_prefix) == 0) {
|
||||
@@ -540,6 +550,14 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
algo.push_back(ch);
|
||||
}
|
||||
key = "URL_HASH";
|
||||
if (value.empty()) {
|
||||
throw_key_error("Empty hash value", argItr.first, argItr.second);
|
||||
}
|
||||
for (char c : value) {
|
||||
if (!std::isxdigit(c)) {
|
||||
throw_key_error("Hash value must be a hex string", argItr.first, argItr.second);
|
||||
}
|
||||
}
|
||||
value = algo + "=" + value;
|
||||
} else if (key == "hash") {
|
||||
key = "URL_HASH";
|
||||
@@ -638,29 +656,28 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
// Add support for relative paths for (private-)link-libraries
|
||||
const auto fix_relative_paths = [&name, &path](ConditionVector &libraries, const char *key) {
|
||||
for (const auto &library_entries : libraries) {
|
||||
for (auto &library_path : libraries[library_entries.first]) {
|
||||
for (auto &library : libraries[library_entries.first]) {
|
||||
// Skip processing paths with potential CMake macros in them (this check isn't perfect)
|
||||
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variable-references
|
||||
if ((library_path.find("${") != std::string::npos || library_path.find("$ENV{") != std::string::npos ||
|
||||
library_path.find("$CACHE{") != std::string::npos) &&
|
||||
library_path.find('}') != std::string::npos) {
|
||||
if ((library.find("${") != std::string::npos || library.find("$ENV{") != std::string::npos ||
|
||||
library.find("$CACHE{") != std::string::npos) &&
|
||||
library.find('}') != std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip paths that don't contain backwards or forwards slashes
|
||||
if (library_path.find_first_of(R"(\/)") == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the new file path exists, otherwise emit an error
|
||||
const auto expected_library_file_path = fs::path{path} / library_path;
|
||||
if (!fs::exists(expected_library_file_path)) {
|
||||
auto library_path = fs::path(path) / library;
|
||||
if (fs::exists(library_path)) {
|
||||
if (!fs::is_directory(library_path)) {
|
||||
// If the file path is relative (and not a directory), prepend ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
library.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
|
||||
}
|
||||
} else if (library.find_first_of(R"(\/)") != std::string::npos) {
|
||||
// Error if the path contains a directory separator and the file doesn't exist
|
||||
throw std::runtime_error("Attempted to link against a library file that doesn't exist for target \"" + name + "\" in \"" +
|
||||
key + "\": " + library_path);
|
||||
key + "\": " + library);
|
||||
} else {
|
||||
// NOTE: We cannot check if system libraries exist, so we leave them as-is
|
||||
}
|
||||
|
||||
// Prepend ${CMAKE_CURRENT_SOURCE_DIR} to the path
|
||||
library_path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -715,6 +732,8 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
property_list += list_val.as_string();
|
||||
}
|
||||
target.properties[condition][k] = property_list;
|
||||
} else if (v.is_integer()) {
|
||||
target.properties[condition][k] = std::to_string(v.as_integer());
|
||||
} else if (v.is_boolean()) {
|
||||
target.properties[condition][k] = v.as_boolean() ? "ON" : "OFF";
|
||||
} else {
|
||||
@@ -831,13 +850,33 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
|
||||
std::istringstream feature_stream{features};
|
||||
std::string feature;
|
||||
while (std::getline(feature_stream, feature, ',')) {
|
||||
package.features.emplace_back(feature);
|
||||
// Disable default features with package-name[core,feature1]
|
||||
if (feature == "core") {
|
||||
package.default_features = false;
|
||||
} else {
|
||||
package.features.emplace_back(feature);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw_key_error("Invalid package name '" + package_str + "'", "packages", p);
|
||||
}
|
||||
vcpkg.packages.emplace_back(std::move(package));
|
||||
}
|
||||
|
||||
if (v.contains("overlay")) {
|
||||
std::string overlay;
|
||||
v.optional("overlay", overlay);
|
||||
vcpkg.overlay_triplets = vcpkg.overlay_ports = {overlay};
|
||||
if (v.contains("overlay-ports")) {
|
||||
throw_key_error("[vcpkg].overlay was already specified", "overlay-ports", v.find("overlay-ports"));
|
||||
}
|
||||
if (v.contains("overlay-triplets")) {
|
||||
throw_key_error("[vcpkg].overlay was already specified", "overlay-triplets", v.find("overlay-triplets"));
|
||||
}
|
||||
} else {
|
||||
v.optional("overlay-ports", vcpkg.overlay_ports);
|
||||
v.optional("overlay-triplets", vcpkg.overlay_triplets);
|
||||
}
|
||||
}
|
||||
|
||||
checker.check(conditions, true);
|
||||
@@ -870,9 +909,8 @@ bool Project::cmake_minimum_version(int major, int minor) const {
|
||||
}
|
||||
|
||||
bool Project::is_condition_name(const std::string &name) {
|
||||
auto is_named_condition = true;
|
||||
for (auto ch : name) {
|
||||
if (!(ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z'))) {
|
||||
if (!std::isalnum(ch) && ch != '-' && ch != '_') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
# These will be generated by cmkr, so no point in tracking them
|
||||
**/CMakeLists.txt
|
||||
**/cmkr.cmake
|
||||
**/cmkr.cmake
|
||||
**/vcpkg.json
|
||||
@@ -1,4 +1,4 @@
|
||||
# Example project that sets compiler/linker flags for various platforms.
|
||||
# Example project that sets compiler flags and preprocessor defines for various platforms.
|
||||
|
||||
[project]
|
||||
name = "compile-options"
|
||||
@@ -8,8 +8,11 @@ description = "Compiler flags"
|
||||
type = "executable"
|
||||
sources = ["src/main.cpp"]
|
||||
msvc.compile-options = ["/W2"]
|
||||
msvc.compile-definitions = ["PLATFORM=\"msvc\""]
|
||||
gcc.compile-options = ["-Wall"]
|
||||
gcc.compile-definitions = ["PLATFORM=\"gcc\""]
|
||||
clang.compile-options = ["-Wall"]
|
||||
clang.compile-definitions = ["PLATFORM=\"clang\""]
|
||||
|
||||
# The `hello` target uses [conditions](/cmake-toml#conditions) to set different compiler flags depending on the platform. See the [targets](/cmake-toml/#targets) documentation for other things you can set.
|
||||
# The `hello` target uses [conditions](/cmake-toml#conditions) to set different compiler flags and definitions depending on the platform. See the [targets](/cmake-toml/#targets) documentation for other things you can set.
|
||||
# _Note_: In general you only want to specify flags _required_ to compile your code without errors.
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
|
||||
# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://github.com/microsoft/vcpkg) and links an `example` target to it:
|
||||
|
||||
[project]
|
||||
name = "vcpkg"
|
||||
description = "Dependencies from vcpkg"
|
||||
|
||||
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
|
||||
# See https://vcpkg.io/en/packages.html for available packages
|
||||
# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages
|
||||
[vcpkg]
|
||||
version = "2024.03.25"
|
||||
version = "2024.11.16"
|
||||
packages = ["fmt"]
|
||||
|
||||
[find-package]
|
||||
fmt = {}
|
||||
[find-package.fmt]
|
||||
|
||||
[target.example]
|
||||
type = "executable"
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT",
|
||||
"$cmkr-url": "https://github.com/build-cpp/cmkr",
|
||||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
|
||||
"dependencies": [
|
||||
"fmt"
|
||||
],
|
||||
"description": "Dependencies from vcpkg",
|
||||
"name": "vcpkg",
|
||||
"version-string": ""
|
||||
}
|
||||
Reference in New Issue
Block a user