require cmake 3.14 or higher

self-hosting
MoAlyousef 4 years ago
parent c20781f970
commit 11b3061d47

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.14)
project(cmkr VERSION 0.1.0) project(cmkr VERSION 0.1.0)

@ -3,7 +3,7 @@
A CMakeLists.txt generator from TOML. Still WIP. A CMakeLists.txt generator from TOML. Still WIP.
## Building ## Building
cmkr requires a C++17 compiler, cmake and git. It depends on toml11 by ToruNiina, which is added as a git submodule. cmkr requires a C++17 compiler, cmake >= 3.14 and git. It depends on toml11 by ToruNiina, which is added as a git submodule.
``` ```
git clone https://github.com/moalyousef/cmkr git clone https://github.com/moalyousef/cmkr
cd cmkr cd cmkr
@ -16,7 +16,7 @@ cmake --build bin
cmkr parses cmake.toml files (using toml11) at the project directory. A basic hello world format with the minimum required fields: cmkr parses cmake.toml files (using toml11) at the project directory. A basic hello world format with the minimum required fields:
```toml ```toml
[cmake] [cmake]
minimum_required = "3.0" minimum = "3.14"
[project] [project]
name = "app" name = "app"
@ -31,7 +31,7 @@ sources = ["src/main.cpp"]
This project's cmake.toml: This project's cmake.toml:
```toml ```toml
[cmake] [cmake]
minimum_required = "3.0" minimum = "3.14"
[project] [project]
name = "cmkr" name = "cmkr"
@ -41,40 +41,43 @@ version = "0.1.0"
name = "cmkrlib" name = "cmkrlib"
type = "static" type = "static"
sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"]
include_dirs = ["vendor"] include-dirs = ["vendor"]
features = ["cxx_std_17"] features = ["cxx_std_17"]
[[bin]] [[bin]]
name = "cmkr" name = "cmkr"
type = "exe" type = "exe"
sources = ["src/main.cpp"] sources = ["src/main.cpp"]
link_libs = ["cmkrlib"] link-libs = ["cmkrlib"]
``` ```
Currently supported fields: Currently supported fields:
```toml ```toml
[cmake] # required for top-level project [cmake] # required for top-level project
minimum_required = "3.0" # required minimum = "3.14" # required
cpp_flags = [] # optional cpp-flags = [] # optional
c_flags = [] # optional c-flags = [] # optional
link_flags = [] # optional link-flags = [] # optional
subdirs = [] # optional subdirs = [] # optional
[project] # required per project [project] # required per project
name = "app" # required name = "app" # required
version = "0.1.0" # required version = "0.1.0" # required
[dependencies] # optional, runs find_package, use "*" to ignore version [find] # optional, runs find_package, use "*" to ignore version
boost = "1.74.0" # optional boost = "1.74.0" # optional
[fetch] # optional, runs fetchContent
toml11 = { git = "https://github.com/ToruNiina/toml11", git-tag = "v3.5.0" } # optional
[[bin]] # required, can define several binaries [[bin]] # required, can define several binaries
name = "app" # required name = "app" # required
type = "exe" # required (exe || shared || static) type = "exe" # required (exe || shared || static)
sources = ["src/main.cpp"] # required sources = ["src/main.cpp"] # required
include_dirs = [] # optional include-dirs = [] # optional
features = [] # optional features = [] # optional
defines = [] # optional defines = [] # optional
link_libs = [] # optional link-libs = [] # optional
``` ```
The cmkr executable can be run from the command-line: The cmkr executable can be run from the command-line:

@ -1,5 +1,5 @@
[cmake] [cmake]
minimum_required = "3.0" minimum = "3.14"
[project] [project]
name = "cmkr" name = "cmkr"
@ -9,12 +9,12 @@ version = "0.1.0"
name = "cmkrlib" name = "cmkrlib"
type = "static" type = "static"
sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"]
include_dirs = ["vendor"] include-dirs = ["vendor"]
features = ["cxx_std_17"] features = ["cxx_std_17"]
[[bin]] [[bin]]
name = "cmkr" name = "cmkr"
type = "exe" type = "exe"
sources = ["src/main.cpp"] sources = ["src/main.cpp"]
link_libs = ["cmkrlib"] link-libs = ["cmkrlib"]

@ -39,7 +39,7 @@ void generate_project(const char *str) {
std::ofstream ofs2("cmake.toml"); std::ofstream ofs2("cmake.toml");
if (ofs2.is_open()) { if (ofs2.is_open()) {
ofs2 << "[cmake]\nminimum_required = \"3.0\"\n\n[project]\nname = \"" ofs2 << "[cmake]\nminimum = \"3.14\"\n\n[project]\nname = \""
<< dir_name.string() << dir_name.string()
<< "\"\nversion = " << "\"\nversion = "
"\"0.1.0\"\n\n[[bin]]\nname = \"" "\"0.1.0\"\n\n[[bin]]\nname = \""
@ -58,7 +58,7 @@ void generate_project(const char *str) {
std::ofstream ofs2("cmake.toml"); std::ofstream ofs2("cmake.toml");
if (ofs2.is_open()) { if (ofs2.is_open()) {
ofs2 << "[cmake]\nminimum_required = \"3.0\"\n\n[project]\nname = \"" ofs2 << "[cmake]\nminimum = \"3.14\"\n\n[project]\nname = \""
<< dir_name.string() << dir_name.string()
<< "\"\nversion = " << "\"\nversion = "
"\"0.1.0\"\n\n[[bin]]\nname = \"" "\"0.1.0\"\n\n[[bin]]\nname = \""
@ -79,30 +79,30 @@ void generate_cmake(const char *path) {
const auto toml = toml::parse((fs::path(path) / "cmake.toml").string()); const auto toml = toml::parse((fs::path(path) / "cmake.toml").string());
if (toml.contains("cmake")) { if (toml.contains("cmake")) {
const auto &cmake = toml::find(toml, "cmake"); const auto &cmake = toml::find(toml, "cmake");
const std::string cmake_min = toml::find(cmake, "minimum_required").as_string(); const std::string cmake_min = toml::find(cmake, "minimum").as_string();
ss << "cmake_minimum_required(VERSION " << cmake_min << ")\n\n"; ss << "cmake_minimum_required(VERSION " << cmake_min << ")\n\n";
if (cmake.contains("cpp_flags")) { if (cmake.contains("cpp-flags")) {
ss << "set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}"; ss << "set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}";
const auto flags = toml::find(cmake, "cpp_flags").as_array(); const auto flags = toml::find(cmake, "cpp-flags").as_array();
for (const auto &flag : flags) { for (const auto &flag : flags) {
ss << " " << flag; ss << " " << flag;
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (cmake.contains("c_flags")) { if (cmake.contains("c-flags")) {
ss << "set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS}"; ss << "set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS}";
const auto flags = toml::find(cmake, "c_flags").as_array(); const auto flags = toml::find(cmake, "c-flags").as_array();
for (const auto &flag : flags) { for (const auto &flag : flags) {
ss << " " << flag; ss << " " << flag;
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (cmake.contains("link_flags")) { if (cmake.contains("link-flags")) {
ss << "set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}"; ss << "set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}";
const auto flags = toml::find(cmake, "link_flags").as_array(); const auto flags = toml::find(cmake, "link-flags").as_array();
for (const auto &flag : flags) { for (const auto &flag : flags) {
ss << " " << flag; ss << " " << flag;
} }
@ -127,9 +127,9 @@ void generate_cmake(const char *path) {
ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n"; ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n";
} }
if (toml.contains("dependencies")) { if (toml.contains("find")) {
std::map<std::string, std::string> deps = std::map<std::string, std::string> deps =
toml::find<std::map<std::string, std::string>>(toml, "dependencies"); toml::find<std::map<std::string, std::string>>(toml, "find");
for (const auto &dep : deps) { for (const auto &dep : deps) {
ss << "find_package(" << dep.first; ss << "find_package(" << dep.first;
if (dep.second != "*") { if (dep.second != "*") {
@ -140,6 +140,20 @@ void generate_cmake(const char *path) {
} }
} }
if (toml.contains("fetch")) {
std::map<std::string, std::map<std::string, std::string>> deps =
toml::find<std::map<std::string, std::map<std::string, std::string>>>(toml, "fetch");
ss << "include(FetchContent)\n\n";
for (const auto &dep : deps) {
ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
for (const auto &arg: dep.second) {
ss << "\t" << arg.first << " " << arg.second << "\n";
}
ss << "\t)\n\n"
<< "FetchContent_MakeAvailable("<< dep.first << ")\n\n";
}
}
ss << "\nset(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n\n"; ss << "\nset(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n\n";
if (toml.contains("bin")) { if (toml.contains("bin")) {
@ -170,8 +184,8 @@ void generate_cmake(const char *path) {
<< add_command << "(" << bin_name << " " << bin_type << " ${" << add_command << "(" << bin_name << " " << bin_type << " ${"
<< detail::to_upper(bin_name) << "_SOURCES})\n\n"; << detail::to_upper(bin_name) << "_SOURCES})\n\n";
if (bin.contains("include_dirs")) { if (bin.contains("include-dirs")) {
const auto includes = toml::find(bin, "include_dirs").as_array(); const auto includes = toml::find(bin, "include-dirs").as_array();
ss << "target_include_directories(" << bin_name << " PUBLIC\n\t"; ss << "target_include_directories(" << bin_name << " PUBLIC\n\t";
for (const auto &inc : includes) { for (const auto &inc : includes) {
ss << inc << "\n\t"; ss << inc << "\n\t";
@ -179,8 +193,8 @@ void generate_cmake(const char *path) {
ss << ")\n\n"; ss << ")\n\n";
} }
if (bin.contains("link_libs")) { if (bin.contains("link-libs")) {
const auto libraries = toml::find(bin, "link_libs").as_array(); const auto libraries = toml::find(bin, "link-libs").as_array();
ss << "target_link_libraries(" << bin_name << " PUBLIC\n\t"; ss << "target_link_libraries(" << bin_name << " PUBLIC\n\t";
for (const auto &l : libraries) { for (const auto &l : libraries) {
ss << l << "\n\t"; ss << l << "\n\t";

Loading…
Cancel
Save