mirror of
https://github.com/build-cpp/cmkr
synced 2026-06-08 13:22:55 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 264e4ace18 | |||
| b6ab584a1a | |||
| 3cd84a9708 | |||
| 9932e501ce | |||
| 1b37dd76a5 | |||
| 82f2a11311 | |||
| eaf03eb785 | |||
| 8ea5b77ba5 | |||
| a12b3ae021 |
Generated
+6
-6
@@ -77,22 +77,22 @@ set(CMKR_TARGET cmkr)
|
||||
set(cmkr_SOURCES "")
|
||||
|
||||
list(APPEND cmkr_SOURCES
|
||||
"src/args.cpp"
|
||||
"src/arguments.cpp"
|
||||
"src/build.cpp"
|
||||
"src/cmake.cpp"
|
||||
"src/cmake_generator.cpp"
|
||||
"src/error.cpp"
|
||||
"src/gen.cpp"
|
||||
"src/help.cpp"
|
||||
"src/main.cpp"
|
||||
"include/args.hpp"
|
||||
"src/project_parser.cpp"
|
||||
"include/arguments.hpp"
|
||||
"include/build.hpp"
|
||||
"include/cmake.hpp"
|
||||
"include/cmake_generator.hpp"
|
||||
"include/enum_helper.hpp"
|
||||
"include/error.hpp"
|
||||
"include/fs.hpp"
|
||||
"include/gen.hpp"
|
||||
"include/help.hpp"
|
||||
"include/literals.hpp"
|
||||
"include/project_parser.hpp"
|
||||
)
|
||||
|
||||
list(APPEND cmkr_SOURCES
|
||||
|
||||
+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 "archive_af3807ca" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
|
||||
set(CMKR_TAG "archive_3cd84a97" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
|
||||
|
||||
# Set these from the command line to customize for development/debugging purposes
|
||||
set(CMKR_EXECUTABLE "" CACHE FILEPATH "cmkr executable")
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <vector>
|
||||
|
||||
namespace cmkr {
|
||||
namespace cmake {
|
||||
namespace parser {
|
||||
|
||||
struct Setting {
|
||||
std::string name;
|
||||
@@ -84,6 +84,7 @@ struct Target {
|
||||
ConditionVector precompile_headers;
|
||||
ConditionVector private_precompile_headers;
|
||||
|
||||
std::string condition;
|
||||
std::string alias;
|
||||
Condition<tsl::ordered_map<std::string, std::string>> properties;
|
||||
|
||||
@@ -109,14 +110,23 @@ struct Install {
|
||||
std::string destination;
|
||||
};
|
||||
|
||||
struct CMake {
|
||||
struct Subdir {
|
||||
std::string name;
|
||||
std::string condition;
|
||||
Condition<std::string> cmake_before;
|
||||
Condition<std::string> cmake_after;
|
||||
ConditionVector include_before;
|
||||
ConditionVector include_after;
|
||||
};
|
||||
|
||||
struct Project {
|
||||
// This is the CMake version required to use all cmkr versions.
|
||||
std::string cmake_version = "3.15";
|
||||
std::string cmkr_include = "cmkr.cmake";
|
||||
std::string build_dir = "build";
|
||||
std::string generator;
|
||||
std::string config;
|
||||
Condition<std::vector<std::string>> subdirs;
|
||||
Condition<std::vector<std::string>> project_subdirs;
|
||||
std::vector<std::string> cppflags;
|
||||
std::vector<std::string> cflags;
|
||||
std::vector<std::string> linkflags;
|
||||
@@ -139,9 +149,12 @@ struct CMake {
|
||||
std::vector<Test> tests;
|
||||
std::vector<Install> installs;
|
||||
tsl::ordered_map<std::string, std::string> conditions;
|
||||
std::vector<Subdir> subdirs;
|
||||
|
||||
CMake(const std::string &path, bool build);
|
||||
Project(const std::string &path, bool build);
|
||||
};
|
||||
|
||||
} // namespace cmake
|
||||
bool is_root_path(const std::string &path);
|
||||
|
||||
} // namespace parser
|
||||
} // namespace cmkr
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "args.hpp"
|
||||
#include "arguments.hpp"
|
||||
#include "build.hpp"
|
||||
#include "gen.hpp"
|
||||
#include "cmake_generator.hpp"
|
||||
#include "help.hpp"
|
||||
|
||||
#include "fs.hpp"
|
||||
+19
-19
@@ -1,7 +1,7 @@
|
||||
#include "build.hpp"
|
||||
#include "cmake.hpp"
|
||||
#include "project_parser.hpp"
|
||||
#include "error.hpp"
|
||||
#include "gen.hpp"
|
||||
#include "cmake_generator.hpp"
|
||||
|
||||
#include "fs.hpp"
|
||||
#include <sstream>
|
||||
@@ -14,10 +14,10 @@ namespace cmkr {
|
||||
namespace build {
|
||||
|
||||
int run(int argc, char **argv) {
|
||||
cmake::CMake cmake(".", true);
|
||||
parser::Project project(".", true);
|
||||
if (argc > 2) {
|
||||
for (int i = 2; i < argc; ++i) {
|
||||
cmake.build_args.push_back(argv[i]);
|
||||
project.build_args.push_back(argv[i]);
|
||||
}
|
||||
}
|
||||
std::stringstream ss;
|
||||
@@ -25,22 +25,22 @@ int run(int argc, char **argv) {
|
||||
if (gen::generate_cmake(fs::current_path().string().c_str()))
|
||||
throw std::runtime_error("CMake generation failure!");
|
||||
|
||||
ss << "cmake -S. -B" << cmake.build_dir << " ";
|
||||
ss << "cmake -S. -B" << project.build_dir << " ";
|
||||
|
||||
if (!cmake.generator.empty()) {
|
||||
ss << "-G \"" << cmake.generator << "\" ";
|
||||
if (!project.generator.empty()) {
|
||||
ss << "-G \"" << project.generator << "\" ";
|
||||
}
|
||||
if (!cmake.config.empty()) {
|
||||
ss << "-DCMAKE_BUILD_TYPE=" << cmake.config << " ";
|
||||
if (!project.config.empty()) {
|
||||
ss << "-DCMAKE_BUILD_TYPE=" << project.config << " ";
|
||||
}
|
||||
if (!cmake.gen_args.empty()) {
|
||||
for (const auto &arg : cmake.gen_args) {
|
||||
if (!project.gen_args.empty()) {
|
||||
for (const auto &arg : project.gen_args) {
|
||||
ss << "-D" << arg << " ";
|
||||
}
|
||||
}
|
||||
ss << "&& cmake --build " << cmake.build_dir << " --parallel";
|
||||
ss << "&& cmake --build " << project.build_dir << " --parallel";
|
||||
if (argc > 2) {
|
||||
for (const auto &arg : cmake.build_args) {
|
||||
for (const auto &arg : project.build_args) {
|
||||
ss << " " << arg;
|
||||
}
|
||||
}
|
||||
@@ -50,17 +50,17 @@ int run(int argc, char **argv) {
|
||||
|
||||
int clean() {
|
||||
bool ret = false;
|
||||
cmake::CMake cmake(".", true);
|
||||
if (fs::exists(cmake.build_dir)) {
|
||||
ret = fs::remove_all(cmake.build_dir);
|
||||
fs::create_directory(cmake.build_dir);
|
||||
parser::Project project(".", true);
|
||||
if (fs::exists(project.build_dir)) {
|
||||
ret = fs::remove_all(project.build_dir);
|
||||
fs::create_directory(project.build_dir);
|
||||
}
|
||||
return !ret;
|
||||
}
|
||||
|
||||
int install() {
|
||||
cmake::CMake cmake(".", false);
|
||||
auto cmd = "cmake --install " + cmake.build_dir;
|
||||
parser::Project project(".", false);
|
||||
auto cmd = "cmake --install " + project.build_dir;
|
||||
return ::system(cmd.c_str());
|
||||
}
|
||||
} // namespace build
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "gen.hpp"
|
||||
#include "cmake.hpp"
|
||||
#include "cmake_generator.hpp"
|
||||
#include "error.hpp"
|
||||
#include "literals.hpp"
|
||||
#include "project_parser.hpp"
|
||||
|
||||
#include "fs.hpp"
|
||||
#include <cassert>
|
||||
@@ -158,8 +158,10 @@ struct Command {
|
||||
bool first_arg = true;
|
||||
bool had_newline = false;
|
||||
bool generated = false;
|
||||
std::string post_comment;
|
||||
|
||||
Command(std::stringstream &ss, int depth, const std::string &command) : ss(ss), depth(depth), command(command) {}
|
||||
Command(std::stringstream &ss, int depth, std::string command, std::string post_comment)
|
||||
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {}
|
||||
|
||||
~Command() {
|
||||
if (!generated) {
|
||||
@@ -303,7 +305,11 @@ struct Command {
|
||||
(void)std::initializer_list<bool>{print_arg(values)...};
|
||||
if (had_newline)
|
||||
ss << '\n' << indent(depth);
|
||||
ss << ")\n";
|
||||
ss << ")";
|
||||
if (!post_comment.empty()) {
|
||||
ss << " # " << post_comment;
|
||||
}
|
||||
ss << "\n";
|
||||
return CommandEndl(ss);
|
||||
}
|
||||
};
|
||||
@@ -319,25 +325,25 @@ static std::string tolf(const std::string &str) {
|
||||
};
|
||||
|
||||
struct Generator {
|
||||
Generator(cmake::CMake &cmake) : cmake(cmake) {}
|
||||
Generator(parser::Project &project) : project(project) {}
|
||||
Generator(const Generator &) = delete;
|
||||
|
||||
cmake::CMake &cmake;
|
||||
parser::Project &project;
|
||||
std::stringstream ss;
|
||||
int indent = 0;
|
||||
|
||||
Command cmd(const std::string &command) {
|
||||
Command cmd(const std::string &command, const std::string &post_comment = "") {
|
||||
if (command.empty())
|
||||
throw std::invalid_argument("command cannot be empty");
|
||||
if (command == "if") {
|
||||
indent++;
|
||||
return Command(ss, indent - 1, command);
|
||||
return Command(ss, indent - 1, command, post_comment);
|
||||
} else if (command == "else" || command == "elseif") {
|
||||
return Command(ss, indent - 1, command);
|
||||
return Command(ss, indent - 1, command, post_comment);
|
||||
} else if (command == "endif") {
|
||||
indent--;
|
||||
}
|
||||
return Command(ss, indent, command);
|
||||
return Command(ss, indent, command, post_comment);
|
||||
}
|
||||
|
||||
CommandEndl comment(const std::string &comment) {
|
||||
@@ -381,16 +387,16 @@ struct Generator {
|
||||
}
|
||||
|
||||
template <typename T, typename Lambda>
|
||||
void handle_condition(const cmake::Condition<T> &value, const Lambda &fn) {
|
||||
void handle_condition(const parser::Condition<T> &value, const Lambda &fn) {
|
||||
if (!value.empty()) {
|
||||
for (const auto &itr : value) {
|
||||
const auto &condition = itr.first;
|
||||
if (!condition.empty()) {
|
||||
if (cmake.conditions.count(condition) == 0) {
|
||||
if (project.conditions.count(condition) == 0) {
|
||||
// TODO: somehow print line number information here?
|
||||
throw std::runtime_error("Unknown condition '" + condition + "'");
|
||||
}
|
||||
cmd("if")(RawArg(cmake.conditions[condition]));
|
||||
cmd("if", condition)(RawArg(project.conditions[condition]));
|
||||
}
|
||||
|
||||
if (!itr.second.empty()) {
|
||||
@@ -436,12 +442,12 @@ int generate_cmake(const char *path, bool root) {
|
||||
throw std::runtime_error("No cmake.toml found!");
|
||||
}
|
||||
|
||||
cmake::CMake cmake(path, false);
|
||||
Generator gen(cmake);
|
||||
parser::Project project(path, false);
|
||||
Generator gen(project);
|
||||
|
||||
// Helper lambdas for more convenient CMake generation
|
||||
auto &ss = gen.ss;
|
||||
auto cmd = [&gen](const std::string &comment) { return gen.cmd(comment); };
|
||||
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 inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); };
|
||||
@@ -453,7 +459,7 @@ int generate_cmake(const char *path, bool root) {
|
||||
endl();
|
||||
|
||||
if (root) {
|
||||
cmd("cmake_minimum_required")("VERSION", cmake.cmake_version).endl();
|
||||
cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl();
|
||||
|
||||
comment("Regenerate CMakeLists.txt automatically in the root project");
|
||||
cmd("set")("CMKR_ROOT_PROJECT", "OFF");
|
||||
@@ -462,7 +468,7 @@ int generate_cmake(const char *path, bool root) {
|
||||
cmd("set")("CMKR_ROOT_PROJECT", "ON").endl();
|
||||
|
||||
comment("Bootstrap cmkr");
|
||||
cmd("include")(cmake.cmkr_include, "OPTIONAL", "RESULT_VARIABLE", "CMKR_INCLUDE_RESULT");
|
||||
cmd("include")(project.cmkr_include, "OPTIONAL", "RESULT_VARIABLE", "CMKR_INCLUDE_RESULT");
|
||||
cmd("if")("CMKR_INCLUDE_RESULT");
|
||||
cmd("cmkr")();
|
||||
cmd("endif")().endl();
|
||||
@@ -481,48 +487,48 @@ int generate_cmake(const char *path, bool root) {
|
||||
// clang-format on
|
||||
|
||||
// TODO: remove support and replace with global compile-features
|
||||
if (!cmake.cppflags.empty()) {
|
||||
if (!project.cppflags.empty()) {
|
||||
ss << "set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} \"";
|
||||
for (const auto &flag : cmake.cppflags) {
|
||||
for (const auto &flag : project.cppflags) {
|
||||
ss << flag << " ";
|
||||
}
|
||||
ss << "\")\n\n";
|
||||
}
|
||||
|
||||
// TODO: remove support and replace with global compile-features
|
||||
if (!cmake.cflags.empty()) {
|
||||
if (!project.cflags.empty()) {
|
||||
ss << "set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} \"";
|
||||
for (const auto &flag : cmake.cflags) {
|
||||
for (const auto &flag : project.cflags) {
|
||||
ss << flag << " ";
|
||||
}
|
||||
ss << "\")\n\n";
|
||||
}
|
||||
|
||||
// TODO: remove support and replace with global linker-flags
|
||||
if (!cmake.linkflags.empty()) {
|
||||
if (!project.linkflags.empty()) {
|
||||
ss << "set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} \"";
|
||||
for (const auto &flag : cmake.linkflags) {
|
||||
for (const auto &flag : project.linkflags) {
|
||||
ss << flag << " ";
|
||||
}
|
||||
ss << "\")\n\n";
|
||||
}
|
||||
|
||||
gen.handle_condition(cmake.include_before, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(cmake.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
gen.handle_condition(project.include_before, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(project.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
|
||||
if (!cmake.project_name.empty()) {
|
||||
auto languages = std::make_pair("LANGUAGES", cmake.project_languages);
|
||||
auto version = std::make_pair("VERSION", cmake.project_version);
|
||||
auto description = std::make_pair("DESCRIPTION", cmake.project_description);
|
||||
cmd("project")(cmake.project_name, languages, version, description).endl();
|
||||
if (!project.project_name.empty()) {
|
||||
auto languages = std::make_pair("LANGUAGES", project.project_languages);
|
||||
auto version = std::make_pair("VERSION", project.project_version);
|
||||
auto description = std::make_pair("DESCRIPTION", project.project_description);
|
||||
cmd("project")(project.project_name, languages, version, description).endl();
|
||||
}
|
||||
|
||||
gen.handle_condition(cmake.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(cmake.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
gen.handle_condition(project.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(project.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
|
||||
if (!cmake.contents.empty()) {
|
||||
if (!project.contents.empty()) {
|
||||
cmd("include")("FetchContent").endl();
|
||||
for (const auto &dep : cmake.contents) {
|
||||
for (const auto &dep : project.contents) {
|
||||
cmd("message")("STATUS", "Fetching " + dep.first + "...");
|
||||
ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
|
||||
for (const auto &arg : dep.second) {
|
||||
@@ -549,14 +555,14 @@ int generate_cmake(const char *path, bool root) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmake.vcpkg.packages.empty()) {
|
||||
if (!project.vcpkg.packages.empty()) {
|
||||
// Allow the user to specify a url or derive it from the version
|
||||
auto url = cmake.vcpkg.url;
|
||||
auto url = project.vcpkg.url;
|
||||
if (url.empty()) {
|
||||
if (cmake.vcpkg.version.empty()) {
|
||||
if (project.vcpkg.version.empty()) {
|
||||
throw std::runtime_error("You need either [vcpkg].version or [vcpkg].url");
|
||||
}
|
||||
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/" + cmake.vcpkg.version + ".tar.gz";
|
||||
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/" + project.vcpkg.version + ".tar.gz";
|
||||
}
|
||||
|
||||
// CMake to bootstrap vcpkg and download the packages
|
||||
@@ -577,11 +583,11 @@ int generate_cmake(const char *path, bool root) {
|
||||
j["$cmkr"] = "This file is automatically generated from cmake.toml - DO NOT EDIT";
|
||||
j["$cmkr-url"] = cmkr_url;
|
||||
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
|
||||
j["name"] = vcpkg_escape_identifier(cmake.project_name);
|
||||
j["version-string"] = cmake.project_version;
|
||||
j["dependencies"] = cmake.vcpkg.packages;
|
||||
if (!cmake.project_description.empty()) {
|
||||
j["description"] = cmake.project_description;
|
||||
j["name"] = vcpkg_escape_identifier(project.project_name);
|
||||
j["version-string"] = project.project_version;
|
||||
j["dependencies"] = project.vcpkg.packages;
|
||||
if (!project.project_description.empty()) {
|
||||
j["description"] = project.project_description;
|
||||
}
|
||||
|
||||
std::ofstream ofs("vcpkg.json");
|
||||
@@ -591,9 +597,9 @@ int generate_cmake(const char *path, bool root) {
|
||||
ofs << std::setw(2) << j << std::endl;
|
||||
}
|
||||
|
||||
if (!cmake.packages.empty()) {
|
||||
if (!project.packages.empty()) {
|
||||
comment("Packages");
|
||||
for (const auto &dep : cmake.packages) {
|
||||
for (const auto &dep : project.packages) {
|
||||
auto version = dep.version;
|
||||
if (version == "*")
|
||||
version.clear();
|
||||
@@ -604,17 +610,17 @@ int generate_cmake(const char *path, bool root) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmake.options.empty()) {
|
||||
if (!project.options.empty()) {
|
||||
comment("Options");
|
||||
for (const auto &opt : cmake.options) {
|
||||
for (const auto &opt : project.options) {
|
||||
cmd("option")(opt.name, opt.comment, opt.val ? "ON" : "OFF");
|
||||
}
|
||||
endl();
|
||||
}
|
||||
|
||||
if (!cmake.settings.empty()) {
|
||||
if (!project.settings.empty()) {
|
||||
comment("Settings");
|
||||
for (const auto &set : cmake.settings) {
|
||||
for (const auto &set : project.settings) {
|
||||
std::string set_val;
|
||||
if (set.val.index() == 1) {
|
||||
set_val = mpark::get<1>(set.val);
|
||||
@@ -633,30 +639,65 @@ int generate_cmake(const char *path, bool root) {
|
||||
endl();
|
||||
}
|
||||
|
||||
// generate_cmake is called on the subdirectories recursively later
|
||||
if (!cmake.subdirs.empty()) {
|
||||
gen.handle_condition(cmake.subdirs, [&](const std::string &, const std::vector<std::string> &subdirs) {
|
||||
for (const auto &dir : subdirs) {
|
||||
// clang-format off
|
||||
comment(dir);
|
||||
cmd("set")("CMKR_CMAKE_FOLDER", "${CMAKE_FOLDER}");
|
||||
cmd("if")("CMAKE_FOLDER");
|
||||
cmd("set")("CMAKE_FOLDER", "${CMAKE_FOLDER}/" + dir);
|
||||
cmd("else")();
|
||||
cmd("set")("CMAKE_FOLDER", dir);
|
||||
cmd("endif")();
|
||||
// clang-format on
|
||||
auto add_subdir = [&](const std::string &dir) {
|
||||
// clang-format off
|
||||
comment(dir);
|
||||
cmd("set")("CMKR_CMAKE_FOLDER", "${CMAKE_FOLDER}");
|
||||
cmd("if")("CMAKE_FOLDER");
|
||||
cmd("set")("CMAKE_FOLDER", "${CMAKE_FOLDER}/" + dir);
|
||||
cmd("else")();
|
||||
cmd("set")("CMAKE_FOLDER", dir);
|
||||
cmd("endif")();
|
||||
// clang-format on
|
||||
|
||||
cmd("add_subdirectory")(dir);
|
||||
cmd("set")("CMAKE_FOLDER", "${CMKR_CMAKE_FOLDER}").endl();
|
||||
cmd("add_subdirectory")(dir);
|
||||
cmd("set")("CMAKE_FOLDER", "${CMKR_CMAKE_FOLDER}").endl();
|
||||
};
|
||||
|
||||
// generate_cmake is called on the subdirectories recursively later
|
||||
if (!project.project_subdirs.empty()) {
|
||||
gen.handle_condition(project.project_subdirs, [&](const std::string &, const std::vector<std::string> &subdirs) {
|
||||
for (const auto &dir : subdirs) {
|
||||
add_subdir(dir);
|
||||
}
|
||||
});
|
||||
endl();
|
||||
}
|
||||
for (const auto &subdir : project.subdirs) {
|
||||
if (!subdir.condition.empty()) {
|
||||
const auto &condition = subdir.condition;
|
||||
if (project.conditions.count(condition) == 0) {
|
||||
throw std::runtime_error("Unknown condition '" + condition + "' for [subdir." + subdir.name + "]");
|
||||
}
|
||||
gen.cmd("if", condition)(RawArg(project.conditions[condition]));
|
||||
}
|
||||
|
||||
if (!cmake.targets.empty()) {
|
||||
for (const auto &target : cmake.targets) {
|
||||
gen.handle_condition(subdir.include_before,
|
||||
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(subdir.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
|
||||
add_subdir(subdir.name);
|
||||
|
||||
gen.handle_condition(subdir.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(subdir.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
|
||||
if (!subdir.condition.empty()) {
|
||||
cmd("endif")();
|
||||
}
|
||||
}
|
||||
|
||||
if (!project.targets.empty()) {
|
||||
for (const auto &target : project.targets) {
|
||||
comment("Target " + target.name);
|
||||
|
||||
if (!target.condition.empty()) {
|
||||
const auto &condition = target.condition;
|
||||
if (project.conditions.count(condition) == 0) {
|
||||
throw std::runtime_error("Unknown condition '" + condition + "' for [target." + target.name + "]");
|
||||
}
|
||||
gen.cmd("if", condition)(RawArg(project.conditions[condition]));
|
||||
}
|
||||
|
||||
cmd("set")("CMKR_TARGET", target.name);
|
||||
|
||||
gen.handle_condition(target.include_before,
|
||||
@@ -680,7 +721,7 @@ int generate_cmake(const char *path, bool root) {
|
||||
cmd("list")("APPEND", sources_var, sources);
|
||||
});
|
||||
|
||||
if (!added_toml && target.type != cmake::target_interface) {
|
||||
if (!added_toml && target.type != parser::target_interface) {
|
||||
cmd("list")("APPEND", sources_var, std::vector<std::string>{"cmake.toml"}).endl();
|
||||
}
|
||||
cmd("set")("CMKR_SOURCES", "${" + sources_var + "}");
|
||||
@@ -689,32 +730,32 @@ int generate_cmake(const char *path, bool root) {
|
||||
std::string target_type;
|
||||
std::string target_scope;
|
||||
switch (target.type) {
|
||||
case cmake::target_executable:
|
||||
case parser::target_executable:
|
||||
add_command = "add_executable";
|
||||
target_type = "";
|
||||
target_scope = "PRIVATE";
|
||||
break;
|
||||
case cmake::target_library:
|
||||
case parser::target_library:
|
||||
add_command = "add_library";
|
||||
target_type = "";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case cmake::target_shared:
|
||||
case parser::target_shared:
|
||||
add_command = "add_library";
|
||||
target_type = "SHARED";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case cmake::target_static:
|
||||
case parser::target_static:
|
||||
add_command = "add_library";
|
||||
target_type = "STATIC";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case cmake::target_interface:
|
||||
case parser::target_interface:
|
||||
add_command = "add_library";
|
||||
target_type = "INTERFACE";
|
||||
target_scope = "INTERFACE";
|
||||
break;
|
||||
case cmake::target_custom:
|
||||
case parser::target_custom:
|
||||
// TODO: add proper support, this is hacky
|
||||
add_command = "add_custom_target";
|
||||
target_type = "SOURCES";
|
||||
@@ -728,12 +769,12 @@ int generate_cmake(const char *path, bool root) {
|
||||
|
||||
// clang-format off
|
||||
cmd("if")(sources_var);
|
||||
cmd("target_sources")(target.name, target.type == cmake::target_interface ? "INTERFACE" : "PRIVATE", "${" + sources_var + "}");
|
||||
cmd("target_sources")(target.name, target.type == parser::target_interface ? "INTERFACE" : "PRIVATE", "${" + sources_var + "}");
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
|
||||
// The first executable target will become the Visual Studio startup project
|
||||
if (target.type == cmake::target_executable) {
|
||||
if (target.type == parser::target_executable) {
|
||||
cmd("get_directory_property")("CMKR_VS_STARTUP_PROJECT", "DIRECTORY", "${PROJECT_SOURCE_DIR}", "DEFINITION", "VS_STARTUP_PROJECT");
|
||||
// clang-format off
|
||||
cmd("if")("NOT", "CMKR_VS_STARTUP_PROJECT");
|
||||
@@ -750,7 +791,7 @@ int generate_cmake(const char *path, bool root) {
|
||||
cmd("add_library")(target.alias, "ALIAS", target.name);
|
||||
}
|
||||
|
||||
auto target_cmd = [&](const char *command, const cmake::ConditionVector &cargs, const std::string &scope) {
|
||||
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); });
|
||||
};
|
||||
@@ -791,13 +832,18 @@ int generate_cmake(const char *path, bool root) {
|
||||
|
||||
cmd("unset")("CMKR_TARGET");
|
||||
cmd("unset")("CMKR_SOURCES");
|
||||
|
||||
if (!target.condition.empty()) {
|
||||
cmd("endif")();
|
||||
}
|
||||
|
||||
endl();
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmake.tests.empty()) {
|
||||
if (!project.tests.empty()) {
|
||||
cmd("enable_testing")().endl();
|
||||
for (const auto &test : cmake.tests) {
|
||||
for (const auto &test : project.tests) {
|
||||
auto name = std::make_pair("NAME", test.name);
|
||||
auto configurations = std::make_pair("CONFIGURATIONS", test.configurations);
|
||||
auto dir = test.working_directory;
|
||||
@@ -811,8 +857,8 @@ int generate_cmake(const char *path, bool root) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmake.installs.empty()) {
|
||||
for (const auto &inst : cmake.installs) {
|
||||
if (!project.installs.empty()) {
|
||||
for (const auto &inst : project.installs) {
|
||||
auto targets = std::make_pair("TARGETS", inst.targets);
|
||||
auto dirs = std::make_pair("DIRS", inst.dirs);
|
||||
std::vector<std::string> files_data;
|
||||
@@ -855,12 +901,28 @@ int generate_cmake(const char *path, bool root) {
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &itr : cmake.subdirs) {
|
||||
for (const auto &sub : itr.second) {
|
||||
auto subpath = fs::path(path) / fs::path(sub);
|
||||
if (fs::exists(subpath / "cmake.toml"))
|
||||
generate_cmake(subpath.string().c_str(), false);
|
||||
auto generate_subdir = [path](const fs::path &sub) {
|
||||
// Skip generating for subdirectories that have a cmake.toml with a [project] in it
|
||||
fs::path subpath;
|
||||
for (const auto &p : sub) {
|
||||
subpath /= p;
|
||||
if (parser::is_root_path((path / subpath).string())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
subpath = path / sub;
|
||||
if (fs::exists(subpath / "cmake.toml")) {
|
||||
generate_cmake(subpath.string().c_str(), false);
|
||||
}
|
||||
};
|
||||
for (const auto &itr : project.project_subdirs) {
|
||||
for (const auto &sub : itr.second) {
|
||||
generate_subdir(sub);
|
||||
}
|
||||
}
|
||||
for (const auto &subdir : project.subdirs) {
|
||||
generate_subdir(subdir.name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "args.hpp"
|
||||
#include "arguments.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "cmake.hpp"
|
||||
#include "project_parser.hpp"
|
||||
|
||||
#include "enum_helper.hpp"
|
||||
#include "fs.hpp"
|
||||
@@ -7,10 +7,10 @@
|
||||
#include <tsl/ordered_map.h>
|
||||
|
||||
template <>
|
||||
const char *enumStrings<cmkr::cmake::TargetType>::data[] = {"executable", "library", "shared", "static", "interface", "custom"};
|
||||
const char *enumStrings<cmkr::parser::TargetType>::data[] = {"executable", "library", "shared", "static", "interface", "custom"};
|
||||
|
||||
namespace cmkr {
|
||||
namespace cmake {
|
||||
namespace parser {
|
||||
|
||||
using TomlBasicValue = toml::basic_value<toml::preserve_comments, tsl::ordered_map, std::vector>;
|
||||
|
||||
@@ -61,11 +61,12 @@ static void get_optional(const TomlBasicValue &v, const toml::key &ky, T &destin
|
||||
}
|
||||
}
|
||||
|
||||
CMake::CMake(const std::string &path, bool build) {
|
||||
if (!fs::exists(fs::path(path) / "cmake.toml")) {
|
||||
Project::Project(const std::string &path, bool build) {
|
||||
const auto toml_path = fs::path(path) / "cmake.toml";
|
||||
if (!fs::exists(toml_path)) {
|
||||
throw std::runtime_error("No cmake.toml was found!");
|
||||
}
|
||||
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>((fs::path(path) / "cmake.toml").string());
|
||||
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
|
||||
if (build) {
|
||||
if (toml.contains("cmake")) {
|
||||
const auto &cmake = toml::find(toml, "cmake");
|
||||
@@ -99,7 +100,21 @@ CMake::CMake(const std::string &path, bool build) {
|
||||
get_optional(project, "cmake-after", cmake_after);
|
||||
get_optional(project, "include-before", include_before);
|
||||
get_optional(project, "include-after", include_after);
|
||||
get_optional(project, "subdirs", subdirs);
|
||||
get_optional(project, "subdirs", project_subdirs);
|
||||
}
|
||||
|
||||
if (toml.contains("subdir")) {
|
||||
const auto &subs = toml::find(toml, "subdir").as_table();
|
||||
for (const auto &sub : subs) {
|
||||
Subdir subdir;
|
||||
subdir.name = sub.first;
|
||||
get_optional(sub.second, "condition", subdir.condition);
|
||||
get_optional(sub.second, "cmake-before", subdir.cmake_before);
|
||||
get_optional(sub.second, "cmake-after", subdir.cmake_after);
|
||||
get_optional(sub.second, "include-before", subdir.include_before);
|
||||
get_optional(sub.second, "include-after", subdir.include_after);
|
||||
subdirs.push_back(subdir);
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("settings")) {
|
||||
@@ -214,6 +229,10 @@ CMake::CMake(const std::string &path, bool build) {
|
||||
sources.insert(sources.end(), headers.begin(), headers.end());
|
||||
}
|
||||
|
||||
if (t.contains("condition")) {
|
||||
target.condition = toml::find(t, "condition").as_string();
|
||||
}
|
||||
|
||||
if (t.contains("alias")) {
|
||||
target.alias = toml::find(t, "alias").as_string();
|
||||
}
|
||||
@@ -307,5 +326,15 @@ CMake::CMake(const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace cmake
|
||||
|
||||
bool is_root_path(const std::string &path) {
|
||||
const auto toml_path = fs::path(path) / "cmake.toml";
|
||||
if (!fs::exists(toml_path)) {
|
||||
return false;
|
||||
}
|
||||
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
|
||||
return toml.contains("project");
|
||||
}
|
||||
|
||||
} // namespace parser
|
||||
} // namespace cmkr
|
||||
Reference in New Issue
Block a user