Restructure project

vcpkg-wip
Duncan Ogilvie 4 years ago
parent f929cf5f80
commit 173c5e0f58

@ -16,61 +16,75 @@ project(cmkr VERSION ${cmkr_PROJECT_VERSION})
include(FetchContent) include(FetchContent)
message(STATUS "Fetching filesystem...")
FetchContent_Declare( FetchContent_Declare(
filesystem filesystem
GIT_REPOSITORY https://github.com/gulrak/filesystem GIT_REPOSITORY https://github.com/gulrak/filesystem
) )
FetchContent_MakeAvailable(filesystem) FetchContent_MakeAvailable(filesystem)
message(STATUS "Fetching mpark_variant...")
FetchContent_Declare( FetchContent_Declare(
mpark_variant mpark_variant
URL https://github.com/mpark/variant/archive/v1.4.0.tar.gz URL https://github.com/mpark/variant/archive/v1.4.0.tar.gz
) )
FetchContent_MakeAvailable(mpark_variant) FetchContent_MakeAvailable(mpark_variant)
message(STATUS "Fetching toml11...")
FetchContent_Declare( FetchContent_Declare(
toml11 toml11
GIT_REPOSITORY https://github.com/ToruNiina/toml11 GIT_REPOSITORY https://github.com/ToruNiina/toml11
) )
FetchContent_MakeAvailable(toml11) FetchContent_MakeAvailable(toml11)
set(CMKRLIB_SOURCES set(CMKRLIB_SOURCES
src/cmake.cpp src/cmkrlib/args.cpp
src/gen.cpp src/cmkrlib/build.cpp
src/help.cpp src/cmkrlib/cmake.cpp
src/build.cpp src/cmkrlib/error.cpp
src/error.cpp src/cmkrlib/gen.cpp
src/cmkrlib/help.cpp
src/cmkrlib/cmake.hpp
src/cmkrlib/fs.hpp
include/args.h
include/build.h
include/error.h
include/gen.h
include/help.h
include/literals.h
cmake.toml
) )
add_library(cmkrlib STATIC ${CMKRLIB_SOURCES}) add_library(cmkrlib STATIC ${CMKRLIB_SOURCES})
source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${CMKRLIB_SOURCES})
target_include_directories(cmkrlib PUBLIC target_include_directories(cmkrlib PUBLIC
include include
) )
target_link_libraries(cmkrlib PUBLIC target_link_libraries(cmkrlib PUBLIC
toml11::toml11 toml11::toml11
ghc_filesystem ghc_filesystem
mpark_variant mpark_variant
) )
target_compile_features(cmkrlib PUBLIC target_compile_features(cmkrlib PUBLIC
cxx_std_11 cxx_std_11
) )
set(CMKR_SOURCES set(CMKR_SOURCES
src/main.cpp src/main.cpp
src/args.cpp cmake.toml
) )
add_executable(cmkr ${CMKR_SOURCES}) add_executable(cmkr ${CMKR_SOURCES})
source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${CMKR_SOURCES})
target_link_libraries(cmkr PUBLIC target_link_libraries(cmkr PUBLIC
cmkrlib cmkrlib
) )
install( install(
TARGETS cmkr TARGETS cmkr

@ -13,7 +13,7 @@ mpark_variant = { url = "https://github.com/mpark/variant/archive/v1.4.0.tar.gz"
[[target]] [[target]]
name = "cmkrlib" name = "cmkrlib"
type = "static" type = "static"
sources = ["src/cmake.cpp", "src/gen.cpp", "src/help.cpp", "src/build.cpp", "src/error.cpp"] sources = ["src/cmkrlib/*.cpp", "src/cmkrlib/*.hpp", "include/*.h"]
include-directories = ["include"] include-directories = ["include"]
compile-features = ["cxx_std_11"] compile-features = ["cxx_std_11"]
link-libraries = ["toml11::toml11", "ghc_filesystem", "mpark_variant"] link-libraries = ["toml11::toml11", "ghc_filesystem", "mpark_variant"]
@ -21,7 +21,7 @@ link-libraries = ["toml11::toml11", "ghc_filesystem", "mpark_variant"]
[[target]] [[target]]
name = "cmkr" name = "cmkr"
type = "executable" type = "executable"
sources = ["src/main.cpp", "src/args.cpp"] sources = ["src/main.cpp"]
link-libraries = ["cmkrlib"] link-libraries = ["cmkrlib"]
[[install]] [[install]]

@ -9,7 +9,6 @@ endif()
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.15)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(example_PROJECT_VERSION 0.1.0) set(example_PROJECT_VERSION 0.1.0)
@ -17,9 +16,12 @@ project(example VERSION ${example_PROJECT_VERSION})
set(EXAMPLE_SOURCES set(EXAMPLE_SOURCES
src/example.cpp src/example.cpp
cmake.toml
) )
add_executable(example ${EXAMPLE_SOURCES}) add_executable(example ${EXAMPLE_SOURCES})
source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${EXAMPLE_SOURCES})

@ -1,16 +1,15 @@
#pragma once #pragma once
const char *hello_world = R"lit( static const char *hello_world = R"lit(
#include <iostream> #include <iostream>
int %s() { int %s() {
std::cout << "Hello World!\n"; std::cout << "Hello World!\n";
return 0; return 0;
} }
)lit" + 1; // skip initial newline
)lit"; static const char *cmake_toml = R"lit(
const char *cmake_toml = R"lit(
[cmake] [cmake]
minimum = "3.15" minimum = "3.15"
# subdirs = [] # subdirs = []
@ -44,5 +43,4 @@ include-directories = ["include"]
[[install]] [[install]]
%s = ["%s"] %s = ["%s"]
destination = "${CMAKE_INSTALL_PREFIX}/%s" destination = "${CMAKE_INSTALL_PREFIX}/%s"
)lit" + 1; // skip initial newline
)lit";

@ -218,7 +218,7 @@ CMake::CMake(const std::string &path, bool build) {
target.properties = toml::find<prop_map>(t, "properties"); target.properties = toml::find<prop_map>(t, "properties");
} }
binaries.push_back(target); targets.push_back(target);
} }
} }

@ -73,7 +73,7 @@ struct CMake {
std::vector<Option> options; std::vector<Option> options;
std::vector<Package> packages; std::vector<Package> packages;
std::map<std::string, std::map<std::string, std::string>> contents; std::map<std::string, std::map<std::string, std::string>> contents;
std::vector<Target> binaries; std::vector<Target> targets;
std::vector<Test> tests; std::vector<Test> tests;
std::vector<Install> installs; std::vector<Install> installs;
CMake(const std::string &path, bool build); CMake(const std::string &path, bool build);

@ -40,14 +40,19 @@ std::string format(const char *fmt, Args... args) {
static std::vector<std::string> expand_cmake_path(const fs::path &p) { static std::vector<std::string> expand_cmake_path(const fs::path &p) {
std::vector<std::string> temp; std::vector<std::string> temp;
if (p.filename().stem().string() == "*") { auto stem = p.filename().stem().string();
auto ext = p.extension(); auto ext = p.extension();
for (const auto &f : fs::recursive_directory_iterator( if (stem == "*") {
for (const auto &f : fs::directory_iterator(
p.parent_path(), fs::directory_options::follow_directory_symlink)) { p.parent_path(), fs::directory_options::follow_directory_symlink)) {
if (f.is_directory()) { if (!f.is_directory() && f.path().extension() == ext) {
continue; temp.push_back(f.path().string());
}
} }
if (f.path().extension() == ext) { } else if (stem == "**") {
for (const auto &f : fs::recursive_directory_iterator(
p.parent_path(), fs::directory_options::follow_directory_symlink)) {
if (!f.is_directory() && f.path().extension() == ext) {
temp.push_back(f.path().string()); temp.push_back(f.path().string());
} }
} }
@ -193,6 +198,7 @@ int generate_cmake(const char *path) {
if (!cmake.contents.empty()) { if (!cmake.contents.empty()) {
ss << "include(FetchContent)\n\n"; ss << "include(FetchContent)\n\n";
for (const auto &dep : cmake.contents) { for (const auto &dep : cmake.contents) {
ss << "message(STATUS \"Fetching " << dep.first << "...\")\n";
ss << "FetchContent_Declare(\n\t" << dep.first << "\n"; ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
for (const auto &arg : dep.second) { for (const auto &arg : dep.second) {
std::string first_arg = arg.first; std::string first_arg = arg.first;
@ -213,7 +219,7 @@ int generate_cmake(const char *path) {
} }
ss << "\t" << first_arg << " " << arg.second << "\n"; ss << "\t" << first_arg << " " << arg.second << "\n";
} }
ss << ")\n\n" ss << ")\n"
<< "FetchContent_MakeAvailable(" << dep.first << ")\n\n"; << "FetchContent_MakeAvailable(" << dep.first << ")\n\n";
} }
} }
@ -249,87 +255,98 @@ int generate_cmake(const char *path) {
} }
} }
if (!cmake.binaries.empty()) { if (!cmake.targets.empty()) {
for (const auto &bin : cmake.binaries) { for (const auto &target : cmake.targets) {
std::string bin_type;
std::string add_command; std::string add_command;
if (bin.type == "executable") { std::string target_type;
bin_type = ""; if (target.type == "executable") {
add_command = "add_executable"; add_command = "add_executable";
} else if (bin.type == "shared" || bin.type == "static" || target_type = "";
bin.type == "interface") { } else if (target.type == "shared" || target.type == "static" ||
bin_type = detail::to_upper(bin.type); target.type == "interface") {
add_command = "add_library"; add_command = "add_library";
} else if (bin.type == "library") { target_type = detail::to_upper(target.type);
bin_type = ""; } else if (target.type == "library") {
add_command = "add_library"; add_command = "add_library";
target_type = "";
} else { } else {
throw std::runtime_error( throw std::runtime_error(
"Unknown binary type " + bin.type + "Unknown binary type " + target.type +
"! Supported types are: executable, library, shared, static, interface"); "! Supported types are: executable, library, shared, static, interface");
} }
if (!bin.sources.empty()) { if (!target.sources.empty()) {
ss << "set(" << detail::to_upper(bin.name) << "_SOURCES\n"; ss << "set(" << detail::to_upper(target.name) << "_SOURCES\n";
for (const auto &src : bin.sources) { int sources_added = 0;
for (const auto &src : target.sources) {
auto path = fs::path(src); auto path = fs::path(src);
auto expanded = detail::expand_cmake_path(path); auto expanded = detail::expand_cmake_path(path);
for (const auto &f : expanded) { for (const auto &f : expanded) {
ss << "\t" << f << "\n"; ss << "\t" << f << "\n";
sources_added++;
}
} }
if (sources_added == 0) {
throw std::runtime_error(target.name + " sources wildcard found 0 files");
}
if (target.type != "interface") {
ss << "\tcmake.toml\n";
} }
ss << ")\n\n"; ss << ")\n\n";
} }
ss << add_command << "(" << bin.name; ss << add_command << "(" << target.name;
if (!bin_type.empty()) if (!target_type.empty()) {
ss << " " << bin_type; ss << " " << target_type;
}
if (!bin.sources.empty()) { if (!target.sources.empty()) {
ss << " ${" << detail::to_upper(bin.name) << "_SOURCES})\n\n"; ss << " ${" << detail::to_upper(target.name) << "_SOURCES})\n\n";
ss << "source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${"
<< detail::to_upper(target.name) << "_SOURCES})\n\n";
} else { } else {
ss << ")\n\n"; ss << ")\n\n";
} }
if (!bin.alias.empty()) { if (!target.alias.empty()) {
ss << "add_library(" << bin.alias << " ALIAS " << bin.name << ")\n\n"; ss << "add_library(" << target.alias << " ALIAS " << target.name << ")\n\n";
} }
if (!bin.include_directories.empty()) { if (!target.include_directories.empty()) {
ss << "target_include_directories(" << bin.name << " PUBLIC\n\t"; ss << "target_include_directories(" << target.name << " PUBLIC\n";
for (const auto &inc : bin.include_directories) { for (const auto &inc : target.include_directories) {
ss << fs::path(inc).string() << "\n\t"; ss << "\t" << inc << "\n";
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (!bin.link_libraries.empty()) { if (!target.link_libraries.empty()) {
ss << "target_link_libraries(" << bin.name << " PUBLIC\n\t"; ss << "target_link_libraries(" << target.name << " PUBLIC\n";
for (const auto &l : bin.link_libraries) { for (const auto &l : target.link_libraries) {
ss << l << "\n\t"; ss << "\t" << l << "\n";
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (!bin.compile_features.empty()) { if (!target.compile_features.empty()) {
ss << "target_compile_features(" << bin.name << " PUBLIC\n\t"; ss << "target_compile_features(" << target.name << " PUBLIC\n";
for (const auto &feat : bin.compile_features) { for (const auto &feat : target.compile_features) {
ss << feat << "\n\t"; ss << "\t" << feat << "\n";
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (!bin.compile_definitions.empty()) { if (!target.compile_definitions.empty()) {
ss << "target_add_definitions(" << bin.name << " PUBLIC\n\t"; ss << "target_compile_definitions(" << target.name << " PUBLIC\n";
for (const auto &def : bin.compile_definitions) { for (const auto &def : target.compile_definitions) {
ss << def << "\n\t"; ss << "\t" << def << "\n";
} }
ss << ")\n\n"; ss << ")\n\n";
} }
if (!bin.properties.empty()) { if (!target.properties.empty()) {
ss << "set_target_properties(" << bin.name << " PROPERTIES\n"; ss << "set_target_properties(" << target.name << " PROPERTIES\n";
for (const auto &prop : bin.properties) { for (const auto &prop : target.properties) {
ss << "\t" << prop.first << " " << prop.second << "\n"; ss << "\t" << prop.first << " " << prop.second << "\n";
} }
ss << ")\n\n"; ss << ")\n\n";
@ -368,10 +385,17 @@ int generate_cmake(const char *path) {
} }
if (!inst.files.empty()) { if (!inst.files.empty()) {
ss << "\tFILES "; ss << "\tFILES ";
int files_added = 0;
for (const auto &file : inst.files) { for (const auto &file : inst.files) {
auto path = detail::expand_cmake_path(fs::path(file)); auto path = detail::expand_cmake_path(fs::path(file));
for (const auto &f : path) for (const auto &f : path) {
ss << f << " "; ss << f << " ";
files_added++;
}
}
if (files_added == 0) {
throw std::runtime_error(
"[[install]] files wildcard did not resolve to any files");
} }
} }
if (!inst.configs.empty()) { if (!inst.configs.empty()) {
Loading…
Cancel
Save