Slightly improve cmkr init #24

main
Duncan Ogilvie 3 years ago
parent cc2b984aa5
commit 6a825e15a0

@ -5,13 +5,9 @@
namespace cmkr {
namespace gen {
int generate_project(const char *typ);
void generate_project(const std::string &type);
int generate_cmake(const char *path, const parser::Project *parent_project = nullptr);
void generate_cmake(const char *path, const parser::Project *parent_project = nullptr);
} // namespace gen
} // namespace cmkr
int cmkr_gen_generate_project(const char *typ);
int cmkr_gen_generate_cmake(const char *path);

@ -1,45 +1,100 @@
#pragma once
static const char *hello_world = &R"lit(
static const char *cpp_executable = &R"lit(
#include <iostream>
#include <cstdlib>
int %s() {
std::cout << "Hello World!\n";
return 0;
int main() {
std::cout << "Hello from cmkr!\n";
return EXIT_SUCCESS;
}
)lit"[1]; // skip initial newline
static const char *cmake_toml = &R"lit(
[cmake]
version = "3.15"
# subdirs = []
# build-dir = ""
# cpp-flags = []
# c-flags = []
# link-flags = []
# generator = ""
# arguments = []
static const char *cpp_library = &R"lit(
#include <@name/@name.hpp>
#include <iostream>
namespace @name {
void hello() {
std::cout << "Hello from cmkr!\n";
}
} // namespace @name
)lit"[1]; // skip initial newline
static const char *hpp_library = &R"lit(
#pragma once
namespace @name {
void hello();
} // namespace @name
)lit"[1]; // skip initial newline
static const char *hpp_interface = &R"lit(
#pragma once
#include <iostream>
namespace @name {
inline void hello() {
std::cout << "Hello from cmkr!\n";
}
} // namespace @name
)lit"[1]; // skip initial newline
static const char *toml_executable = &R"lit(
# Reference: https://build-cpp.github.io/cmkr/cmake-toml
[project]
name = "%s"
version = "0.1.0"
name = "@name"
[target.@name]
type = "executable"
sources = ["src/@name/main.cpp"]
compile-features = ["cxx_std_11"]
)lit"[1]; // skip initial newline
# [find-package]
static const char *toml_library = &R"lit(
# Reference: https://build-cpp.github.io/cmkr/cmake-toml
[project]
name = "@name"
# [fetch-content]
[target.@name]
type = "@type"
sources = [
"src/@name/@name.cpp",
"include/@name/@name.hpp"
]
include-directories = ["include"]
compile-features = ["cxx_std_11"]
)lit"[1]; // skip initial newline
# [options]
static const char *toml_interface = &R"lit(
# Reference: https://build-cpp.github.io/cmkr/cmake-toml
[project]
name = "@name"
[target.%s]
type = "%s"
sources = ["src/*.cpp"]
[target.@name]
type = "interface"
include-directories = ["include"]
# alias = ""
# compile-features = []
# compile-definitions = []
# link-libraries = []
[[install]]
%s = ["%s"]
destination = "${CMAKE_INSTALL_PREFIX}/%s"
compile-features = ["cxx_std_11"]
)lit"[1]; // skip initial newline
static const char *toml_migration = &R"lit(
# Reference: https://build-cpp.github.io/cmkr/cmake-toml
[project]
name = "@name"
# TODO: define a target for each of your executables/libraries like this:
#[target.@name]
#type = "executable"
#sources = ["src/@name/*.cpp", "include/@name/*.hpp"]
#include-directories = ["include"]
#compile-features = ["cxx_std_11"]
#link-libraries = ["other-targets"]
)lit"[1]; // skip initial newline

@ -21,9 +21,7 @@ const char *handle_args(int argc, char **argv) {
throw std::runtime_error(cmkr::help::message());
std::string main_arg = args[1];
if (main_arg == "gen") {
auto ret = cmkr::gen::generate_cmake(fs::current_path().string().c_str());
if (ret)
return "CMake generation error!";
cmkr::gen::generate_cmake(fs::current_path().string().c_str());
return "CMake generation successful!";
} else if (main_arg == "help") {
return cmkr::help::message();
@ -33,18 +31,14 @@ const char *handle_args(int argc, char **argv) {
std::string type = "executable";
if (args.size() > 2)
type = args[2];
auto ret = cmkr::gen::generate_project(type.c_str());
if (ret)
return "Initialization failure!";
ret = cmkr::gen::generate_cmake(fs::current_path().string().c_str());
if (ret)
return "CMake generation error!";
cmkr::gen::generate_project(type.c_str());
cmkr::gen::generate_cmake(fs::current_path().string().c_str());
return "Directory initialized!";
} else if (main_arg == "build") {
auto ret = build::run(argc, argv);
if (ret)
return "CMake build error!";
return "CMake run completed!";
return "CMake build completed!";
} else if (main_arg == "install") {
auto ret = build::install();
if (ret)

@ -22,10 +22,9 @@ int run(int argc, char **argv) {
}
std::stringstream ss;
if (gen::generate_cmake(fs::current_path().string().c_str()))
throw std::runtime_error("CMake generation failure!");
gen::generate_cmake(fs::current_path().string().c_str());
ss << "cmake -S. -DCMKR_BUILD_SKIP_GENERATION=ON -B" << project.build_dir << " ";
ss << "cmake -DCMKR_BUILD_SKIP_GENERATION=ON -B" << project.build_dir << " ";
if (!project.generator.empty()) {
ss << "-G \"" << project.generator << "\" ";

@ -19,7 +19,7 @@
namespace cmkr {
namespace gen {
inline std::string to_upper(const std::string &str) {
static std::string to_upper(const std::string &str) {
std::string temp;
temp.reserve(str.size());
for (auto c : str) {
@ -28,16 +28,16 @@ inline std::string to_upper(const std::string &str) {
return temp;
}
template <typename... Args>
std::string format(const char *fmt, Args... args) {
auto sz = snprintf(nullptr, 0, fmt, args...) + 1;
char *buf = new char[sz];
int ret = snprintf(buf, sz, fmt, args...);
if (ret != sz - 1)
throw std::runtime_error("Error formatting string!");
std::string temp(buf, buf + sz - 1);
delete[] buf;
return temp;
static std::string format(const char *format, tsl::ordered_map<std::string, std::string> variables) {
std::string s = format;
for (const auto &itr : variables) {
size_t start_pos = 0;
while ((start_pos = s.find(itr.first, start_pos)) != std::string::npos) {
s.replace(start_pos, itr.first.length(), itr.second);
start_pos += itr.second.length();
}
}
return s;
}
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir) {
@ -89,52 +89,47 @@ static std::vector<std::string> expand_cmake_paths(const std::vector<std::string
return result;
}
int generate_project(const char *str) {
fs::create_directory("src");
fs::create_directory("include");
const auto dir_name = fs::current_path().stem().string();
std::string mainbuf;
std::string installed;
std::string target;
std::string dest;
if (!strcmp(str, "executable")) {
mainbuf = format(hello_world, "main");
installed = "targets";
target = dir_name;
dest = "bin";
} else if (!strcmp(str, "static") || !strcmp(str, "shared") || !strcmp(str, "library")) {
mainbuf = format(hello_world, "test");
installed = "targets";
target = dir_name;
dest = "lib";
} else if (!strcmp(str, "interface")) {
installed = "files";
target = "include/*.h";
dest = "include/" + dir_name;
} else {
throw std::runtime_error("Unknown project type " + std::string(str) +
"! Supported types are: executable, library, shared, static, interface");
static void create_file(const fs::path &path, const std::string &contents) {
if (!path.parent_path().empty()) {
fs::create_directories(path.parent_path());
}
const auto tomlbuf = format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str, installed.c_str(), target.c_str(), dest.c_str());
if (strcmp(str, "interface")) {
std::ofstream ofs("src/main.cpp", std::ios::binary);
if (ofs.is_open()) {
ofs << mainbuf;
}
ofs.flush();
ofs.close();
std::ofstream ofs(path, std::ios::binary);
if (!ofs) {
throw std::runtime_error("Failed to create " + path.string());
}
ofs << contents;
}
std::ofstream ofs2("cmake.toml", std::ios::binary);
if (ofs2.is_open()) {
ofs2 << tomlbuf;
void generate_project(const std::string &type) {
const auto name = fs::current_path().stem().string();
if (fs::exists(fs::current_path() / "cmake.toml")) {
throw std::runtime_error("Cannot initialize a project when cmake.toml already exists!");
}
ofs2.flush();
ofs2.close();
return 0;
tsl::ordered_map<std::string, std::string> variables = {
{"@name", name},
{"@type", type},
};
if (!fs::is_empty(fs::current_path())) {
create_file("cmake.toml", format(toml_migration, variables));
puts("Generated migration cmake.toml in existing project directory...");
return;
}
if (type == "executable") {
create_file("cmake.toml", format(toml_executable, variables));
create_file("src/" + name + "/main.cpp", format(cpp_executable, variables));
} else if (type == "static" || type == "shared" || type == "library") {
create_file("cmake.toml", format(toml_library, variables));
create_file("src/" + name + "/" + name + ".cpp", format(cpp_library, variables));
create_file("include/" + name + "/" + name + ".hpp", format(hpp_library, variables));
} else if (type == "interface") {
create_file("cmake.toml", format(toml_interface, variables));
create_file("include/" + name + "/" + name + ".hpp", format(hpp_interface, variables));
} else {
throw std::runtime_error("Unknown project type " + type + "! Supported types are: executable, library, shared, static, interface");
}
}
struct CommandEndl {
@ -440,7 +435,7 @@ static std::string vcpkg_escape_identifier(const std::string &name) {
return escaped;
}
int generate_cmake(const char *path, const parser::Project *parent_project) {
void generate_cmake(const char *path, const parser::Project *parent_project) {
if (!fs::exists(fs::path(path) / "cmake.toml")) {
throw std::runtime_error("No cmake.toml found!");
}
@ -494,14 +489,7 @@ int generate_cmake(const char *path, const parser::Project *parent_project) {
fs::path cmkr_include(project.cmkr_include);
if (!project.cmkr_include.empty() && !fs::exists(cmkr_include) && cmkr_include.is_relative()) {
if (!cmkr_include.parent_path().empty()) {
fs::create_directories(cmkr_include.parent_path());
}
std::ofstream ofs(cmkr_include.string(), std::ios::binary);
if (!ofs) {
throw std::runtime_error("Failed to create " + project.cmkr_include);
}
ofs.write(resources::cmkr, strlen(resources::cmkr));
create_file(cmkr_include, resources::cmkr);
}
}
@ -910,12 +898,7 @@ int generate_cmake(const char *path, const parser::Project *parent_project) {
}();
if (should_regenerate) {
std::ofstream ofs(list_path, std::ios::binary);
if (ofs.is_open()) {
ofs << ss.str();
} else {
throw std::runtime_error("Failed to write " + list_path.string());
}
create_file(list_path, ss.str());
}
auto generate_subdir = [path, &project](const fs::path &sub) {
@ -941,28 +924,6 @@ int generate_cmake(const char *path, const parser::Project *parent_project) {
for (const auto &subdir : project.subdirs) {
generate_subdir(subdir.name);
}
return 0;
}
} // namespace gen
} // namespace cmkr
int cmkr_gen_generate_project(const char *typ) {
try {
return cmkr::gen::generate_project(typ);
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::InitError);
}
}
int cmkr_gen_generate_cmake(const char *path) {
try {
return cmkr::gen::generate_cmake(path);
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::GenerationError);
}
}

Loading…
Cancel
Save