mirror of
https://github.com/build-cpp/cmkr
synced 2026-06-08 13:22:55 +00:00
Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 153bdbe591 | |||
| 97f1c5c1cc | |||
| 5b6d9c3826 | |||
| a90988b81a | |||
| 3a1298f4e8 | |||
| 534f955827 | |||
| ef537ce084 | |||
| 9cdd0f7344 | |||
| e69cf4d2b9 | |||
| e98a906231 | |||
| 9a82f8c796 | |||
| 13255c68cf | |||
| 5768460827 | |||
| 50d4a905b6 | |||
| f957cec2dc | |||
| 232e49e087 | |||
| 7408d42160 | |||
| 1596a8143d | |||
| 9b0f18ee94 | |||
| 887086dc05 | |||
| edc8e1e02e | |||
| 1f6e31e0ef | |||
| 4de1500bb3 | |||
| 85370968f8 | |||
| 06a4c04df6 | |||
| fef2a0215b | |||
| fc74a5e802 | |||
| e48d402e21 | |||
| f6a245618e | |||
| c713606fd4 | |||
| fe9b6587a9 | |||
| a2f8d9e5c9 | |||
| c7925e7110 | |||
| c778a5fe2b | |||
| 16b2cbbb57 | |||
| 2f5c7823be | |||
| a29b6a8aaa | |||
| 5ac8b15c9a | |||
| e7d9faa6bb | |||
| 6f8b07a801 | |||
| e8c6ccb698 | |||
| 539ebc8f4b | |||
| 37ca385673 | |||
| 8ae029dab1 | |||
| fb16cc34b1 | |||
| 47703871e1 | |||
| a8d6b15dcb | |||
| 87b3c7ec6c | |||
| d905be1d13 | |||
| 8d13ccbeaa | |||
| bcbc9d2b20 | |||
| 05324f593b | |||
| 2b7ee72e86 | |||
| 61d5e64d87 | |||
| eed1e38407 | |||
| 61851d2bee | |||
| c0d665095e | |||
| 6c1e388391 |
+1
-1
@@ -10,7 +10,7 @@ AlignTrailingComments: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
- name: Test
|
||||
run: |
|
||||
cd build/tests
|
||||
ctest -C ${{ env.BUILD_TYPE }}
|
||||
ctest -C ${{ env.BUILD_TYPE }} --verbose
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v2
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
string: ${{ runner.os }}
|
||||
|
||||
- name: Compress artifacts
|
||||
uses: papeloto/action-zip@v1
|
||||
uses: vimtor/action-zip@v1
|
||||
with:
|
||||
files: install/bin/
|
||||
dest: ${{ github.event.repository.name }}-${{ steps.osname.outputs.lowercase }}.zip
|
||||
|
||||
Generated
+1
-2
@@ -25,7 +25,7 @@ project(cmkr
|
||||
LANGUAGES
|
||||
CXX
|
||||
VERSION
|
||||
0.2.4
|
||||
0.2.14
|
||||
DESCRIPTION
|
||||
"CMakeLists generator from TOML"
|
||||
)
|
||||
@@ -86,7 +86,6 @@ list(APPEND cmkr_SOURCES
|
||||
"include/arguments.hpp"
|
||||
"include/build.hpp"
|
||||
"include/cmake_generator.hpp"
|
||||
"include/enum_helper.hpp"
|
||||
"include/error.hpp"
|
||||
"include/fs.hpp"
|
||||
"include/help.hpp"
|
||||
|
||||
@@ -69,3 +69,5 @@ arguments:
|
||||
- https://github.com/ToruNiina/toml11
|
||||
- https://github.com/mpark/variant
|
||||
- https://www.svgrepo.com/svg/192268/hammer
|
||||
- https://github.com/can1357 for buying `cmkr.build` ❤️
|
||||
- https://github.com/JustasMasiulis for fixing the dark theme ❤️
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ cmkr-include = false
|
||||
|
||||
[project]
|
||||
name = "cmkr"
|
||||
version = "0.2.4"
|
||||
version = "0.2.14"
|
||||
description = "CMakeLists generator from TOML"
|
||||
languages = ["CXX"]
|
||||
subdirs = ["third_party", "tests"]
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
if(NOT CMAKE_SCRIPT_MODE_FILE)
|
||||
message(FATAL_ERROR "Usage: cmake -P bump_version.cmake [1.2.3]")
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake.toml")
|
||||
message(FATAL_ERROR "Cannot find cmake.toml")
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake")
|
||||
message(FATAL_ERROR "Cannot find cmkr.cmake")
|
||||
endif()
|
||||
|
||||
# Validate branch
|
||||
find_package(Git REQUIRED)
|
||||
execute_process(COMMAND "${GIT_EXECUTABLE}" branch --show-current OUTPUT_VARIABLE GIT_BRANCH)
|
||||
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
|
||||
if(NOT GIT_BRANCH STREQUAL "main")
|
||||
message(FATAL_ERROR "You need to be on the main branch, you are on: ${GIT_BRANCH}")
|
||||
endif()
|
||||
|
||||
file(READ "${CMAKE_SOURCE_DIR}/cmake.toml" CMAKE_TOML)
|
||||
string(FIND "${CMAKE_TOML}" "[project]" PROJECT_INDEX)
|
||||
string(SUBSTRING "${CMAKE_TOML}" ${PROJECT_INDEX} -1 CMAKE_TOML_PROJECT)
|
||||
set(SEMVER_REGEX "([0-9]+)\\.([0-9]+)\\.([0-9]+)")
|
||||
set(VERSION_REGEX "version = \"${SEMVER_REGEX}\"")
|
||||
if(CMAKE_TOML_PROJECT MATCHES "${VERSION_REGEX}")
|
||||
set(MAJOR "${CMAKE_MATCH_1}")
|
||||
set(MINOR "${CMAKE_MATCH_2}")
|
||||
set(PATCH "${CMAKE_MATCH_3}")
|
||||
set(OLDVERSION "${MAJOR}.${MINOR}.${PATCH}")
|
||||
else()
|
||||
message(FATAL_ERROR "Failed to match semantic version in cmake.toml")
|
||||
endif()
|
||||
|
||||
if(CMAKE_ARGV3)
|
||||
if(NOT CMAKE_ARGV3 MATCHES "${SEMVER_REGEX}")
|
||||
message(FATAL_ERROR "Invalid semantic version number '${CMAKE_ARGV3}'")
|
||||
endif()
|
||||
set(NEWVERSION "${CMAKE_ARGV3}")
|
||||
else()
|
||||
math(EXPR NEWPATCH "${PATCH} + 1")
|
||||
set(NEWVERSION "${MAJOR}.${MINOR}.${NEWPATCH}")
|
||||
endif()
|
||||
|
||||
message(STATUS "Version ${OLDVERSION} -> ${NEWVERSION}")
|
||||
|
||||
find_program(CMKR_EXECUTABLE "cmkr" PATHS "${CMAKE_SOURCE_DIR}/build" PATH_SUFFIXES Debug Release RelWithDebInfo MinSizeRel NO_CACHE REQUIRED)
|
||||
message(STATUS "Found cmkr: ${CMKR_EXECUTABLE}")
|
||||
|
||||
# Replace version in cmake.toml
|
||||
string(REPLACE "version = \"${OLDVERSION}\"" "version = \"${NEWVERSION}\"" CMAKE_TOML "${CMAKE_TOML}")
|
||||
file(CONFIGURE
|
||||
OUTPUT "${CMAKE_SOURCE_DIR}/cmake.toml"
|
||||
CONTENT "${CMAKE_TOML}"
|
||||
@ONLY
|
||||
NEWLINE_STYLE LF
|
||||
)
|
||||
|
||||
# Run cmkr gen
|
||||
execute_process(COMMAND "${CMKR_EXECUTABLE}" gen RESULT_VARIABLE CMKR_EXEC_RESULT)
|
||||
if(NOT CMKR_EXEC_RESULT EQUAL 0)
|
||||
message(FATAL_ERROR "cmkr gen failed (exit code ${CMKR_EXEC_RESULT})")
|
||||
endif()
|
||||
|
||||
# Replace version in cmkr.cmake
|
||||
file(READ "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake" CMKR_CMAKE)
|
||||
string(REGEX REPLACE "CMKR_TAG \"[^\"]+\"" "CMKR_TAG \"v${NEWVERSION}\"" CMKR_CMAKE "${CMKR_CMAKE}")
|
||||
file(CONFIGURE
|
||||
OUTPUT "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake"
|
||||
CONTENT "${CMKR_CMAKE}"
|
||||
@ONLY
|
||||
NEWLINE_STYLE LF
|
||||
)
|
||||
|
||||
# Print git commands
|
||||
message(STATUS "Git commands to create new version:\ngit commit -a -m \"Bump to ${NEWVERSION}\"\ngit tag v${NEWVERSION}\ngit push origin main v${NEWVERSION}")
|
||||
+42
-11
@@ -2,7 +2,8 @@ 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.4" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
|
||||
set(CMKR_TAG "v0.2.14" 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
|
||||
if(CMAKE_SCRIPT_MODE_FILE)
|
||||
@@ -15,6 +16,7 @@ endif()
|
||||
set(CMKR_EXECUTABLE "" CACHE FILEPATH "cmkr executable")
|
||||
set(CMKR_SKIP_GENERATION OFF CACHE BOOL "skip automatic cmkr generation")
|
||||
set(CMKR_BUILD_TYPE "Debug" CACHE STRING "cmkr build configuration")
|
||||
mark_as_advanced(CMKR_REPO CMKR_TAG CMKR_COMMIT_HASH CMKR_EXECUTABLE CMKR_SKIP_GENERATION CMKR_BUILD_TYPE)
|
||||
|
||||
# Disable cmkr if generation is disabled
|
||||
if(DEFINED ENV{CI} OR CMKR_SKIP_GENERATION OR CMKR_BUILD_SKIP_GENERATION)
|
||||
@@ -56,22 +58,41 @@ else()
|
||||
endif()
|
||||
|
||||
# Use cached cmkr if found
|
||||
if(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}")
|
||||
if(DEFINED ENV{CMKR_CACHE})
|
||||
set(CMKR_DIRECTORY_PREFIX "$ENV{CMKR_CACHE}")
|
||||
string(REPLACE "\\" "/" CMKR_DIRECTORY_PREFIX "${CMKR_DIRECTORY_PREFIX}")
|
||||
if(NOT CMKR_DIRECTORY_PREFIX MATCHES "\\/$")
|
||||
set(CMKR_DIRECTORY_PREFIX "${CMKR_DIRECTORY_PREFIX}/")
|
||||
endif()
|
||||
# Build in release mode for the cache
|
||||
set(CMKR_BUILD_TYPE "Release")
|
||||
else()
|
||||
set(CMKR_DIRECTORY_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/_cmkr_")
|
||||
endif()
|
||||
set(CMKR_DIRECTORY "${CMKR_DIRECTORY_PREFIX}${CMKR_TAG}")
|
||||
set(CMKR_CACHED_EXECUTABLE "${CMKR_DIRECTORY}/bin/${CMKR_EXECUTABLE_NAME}")
|
||||
|
||||
# Helper function to check if a string starts with a prefix
|
||||
# Cannot use MATCHES, see: https://github.com/build-cpp/cmkr/issues/61
|
||||
function(cmkr_startswith str prefix result)
|
||||
string(LENGTH "${prefix}" prefix_length)
|
||||
string(LENGTH "${str}" str_length)
|
||||
if(prefix_length LESS_EQUAL str_length)
|
||||
string(SUBSTRING "${str}" 0 ${prefix_length} str_prefix)
|
||||
if(prefix STREQUAL str_prefix)
|
||||
set("${result}" ON PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
set("${result}" OFF PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Handle upgrading logic
|
||||
if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
|
||||
if(CMKR_EXECUTABLE MATCHES "^${CMAKE_CURRENT_BINARY_DIR}/_cmkr")
|
||||
if(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}")
|
||||
if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
|
||||
cmkr_startswith("${CMKR_EXECUTABLE}" "${CMAKE_CURRENT_BINARY_DIR}/_cmkr" CMKR_STARTSWITH_BUILD)
|
||||
cmkr_startswith("${CMKR_EXECUTABLE}" "${CMKR_DIRECTORY_PREFIX}" CMKR_STARTSWITH_CACHE)
|
||||
if(CMKR_STARTSWITH_BUILD)
|
||||
if(DEFINED ENV{CMKR_CACHE})
|
||||
message(AUTHOR_WARNING "[cmkr] Switching to cached cmkr: '${CMKR_CACHED_EXECUTABLE}'")
|
||||
if(EXISTS "${CMKR_CACHED_EXECUTABLE}")
|
||||
set(CMKR_EXECUTABLE "${CMKR_CACHED_EXECUTABLE}" CACHE FILEPATH "Full path to cmkr executable" FORCE)
|
||||
@@ -82,7 +103,7 @@ if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
|
||||
message(AUTHOR_WARNING "[cmkr] Upgrading '${CMKR_EXECUTABLE}' to '${CMKR_CACHED_EXECUTABLE}'")
|
||||
unset(CMKR_EXECUTABLE CACHE)
|
||||
endif()
|
||||
elseif(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}" AND CMKR_EXECUTABLE MATCHES "^${CMKR_DIRECTORY_PREFIX}")
|
||||
elseif(DEFINED ENV{CMKR_CACHE} AND CMKR_STARTSWITH_CACHE)
|
||||
message(AUTHOR_WARNING "[cmkr] Upgrading cached '${CMKR_EXECUTABLE}' to '${CMKR_CACHED_EXECUTABLE}'")
|
||||
unset(CMKR_EXECUTABLE CACHE)
|
||||
endif()
|
||||
@@ -98,7 +119,7 @@ elseif(NOT CMKR_EXECUTABLE AND EXISTS "${CMKR_CACHED_EXECUTABLE}")
|
||||
else()
|
||||
set(CMKR_EXECUTABLE "${CMKR_CACHED_EXECUTABLE}" CACHE FILEPATH "Full path to cmkr executable" FORCE)
|
||||
message(VERBOSE "[cmkr] Bootstrapping '${CMKR_EXECUTABLE}'")
|
||||
|
||||
|
||||
message(STATUS "[cmkr] Fetching cmkr...")
|
||||
if(EXISTS "${CMKR_DIRECTORY}")
|
||||
cmkr_exec("${CMAKE_COMMAND}" -E rm -rf "${CMKR_DIRECTORY}")
|
||||
@@ -112,6 +133,16 @@ else()
|
||||
${CMKR_REPO}
|
||||
"${CMKR_DIRECTORY}"
|
||||
)
|
||||
if(CMKR_COMMIT_HASH)
|
||||
execute_process(
|
||||
COMMAND "${GIT_EXECUTABLE}" checkout -q "${CMKR_COMMIT_HASH}"
|
||||
RESULT_VARIABLE CMKR_EXEC_RESULT
|
||||
WORKING_DIRECTORY "${CMKR_DIRECTORY}"
|
||||
)
|
||||
if(NOT CMKR_EXEC_RESULT EQUAL 0)
|
||||
message(FATAL_ERROR "Tag '${CMKR_TAG}' hash is not '${CMKR_COMMIT_HASH}'")
|
||||
endif()
|
||||
endif()
|
||||
message(STATUS "[cmkr] Building cmkr (using system compiler)...")
|
||||
cmkr_exec("${CMAKE_COMMAND}"
|
||||
--no-warn-unused-cli
|
||||
@@ -202,18 +233,18 @@ macro(cmkr)
|
||||
# Copy the now-generated CMakeLists.txt to CMakerLists.txt
|
||||
# This is done because you cannot include() a file you are currently in
|
||||
configure_file(CMakeLists.txt "${CMKR_TEMP_FILE}" COPYONLY)
|
||||
|
||||
|
||||
# Add the macro required for the hack at the start of the cmkr macro
|
||||
set_source_files_properties("${CMKR_TEMP_FILE}" PROPERTIES
|
||||
CMKR_CURRENT_LIST_FILE "${CMAKE_CURRENT_LIST_FILE}"
|
||||
)
|
||||
|
||||
|
||||
# 'Execute' the newly-generated CMakeLists.txt
|
||||
include("${CMKR_TEMP_FILE}")
|
||||
|
||||
|
||||
# Delete the generated file
|
||||
file(REMOVE "${CMKR_TEMP_FILE}")
|
||||
|
||||
|
||||
# Do not execute the rest of the original CMakeLists.txt
|
||||
return()
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
cmkr.build
|
||||
@@ -3,6 +3,7 @@ gem "minima", "~> 2.5"
|
||||
gem "github-pages", group: :jekyll_plugins
|
||||
|
||||
gem "jekyll-remote-theme"
|
||||
gem "jekyll-sitemap", "~> 1.4.0"
|
||||
|
||||
platforms :mingw, :x64_mingw, :mswin, :jruby do
|
||||
gem "tzinfo", "~> 1.2"
|
||||
|
||||
@@ -271,6 +271,7 @@ PLATFORMS
|
||||
DEPENDENCIES
|
||||
github-pages
|
||||
jekyll-remote-theme
|
||||
jekyll-sitemap (~> 1.4.0)
|
||||
just-the-docs
|
||||
minima (~> 2.5)
|
||||
tzinfo (~> 1.2)
|
||||
|
||||
+5
-2
@@ -2,6 +2,7 @@ title: cmkr
|
||||
|
||||
plugins:
|
||||
- jekyll-remote-theme
|
||||
- jekyll-sitemap
|
||||
|
||||
# Automatically deduce dark theme preference https://github.com/pmarsceill/just-the-docs/pull/464
|
||||
remote_theme: build-cpp/just-the-docs@light-switch
|
||||
@@ -9,7 +10,7 @@ search_enabled: true
|
||||
color_scheme: light-or-dark
|
||||
heading_anchors: true
|
||||
aux_links:
|
||||
"cmkr":
|
||||
"GitHub":
|
||||
- "https://github.com/build-cpp/cmkr"
|
||||
aux_links_new_tab: true
|
||||
|
||||
@@ -18,4 +19,6 @@ gh_edit_link_text: "Edit this page on GitHub."
|
||||
gh_edit_repository: "https://github.com/build-cpp/cmkr"
|
||||
gh_edit_branch: "main"
|
||||
gh_edit_view_mode: "edit"
|
||||
gh_edit_source: docs
|
||||
gh_edit_source: docs
|
||||
|
||||
production_url : https://cmkr.build
|
||||
+7
-2
@@ -70,6 +70,9 @@ linux = "CMAKE_SYSTEM_NAME MATCHES \"Linux\""
|
||||
gcc = "CMAKE_CXX_COMPILER_ID STREQUAL \"GNU\" OR CMAKE_C_COMPILER_ID STREQUAL \"GNU\""
|
||||
clang = "CMAKE_CXX_COMPILER_ID MATCHES \"Clang\" OR CMAKE_C_COMPILER_ID MATCHES \"Clang\""
|
||||
msvc = "MSVC"
|
||||
root = "CMKR_ROOT_PROJECT"
|
||||
x64 = "CMAKE_SIZEOF_VOID_P EQUAL 8"
|
||||
x32 = "CMAKE_SIZEOF_VOID_P EQUAL 4"
|
||||
```
|
||||
|
||||
## Subdirectories
|
||||
@@ -197,7 +200,9 @@ working-directory = "mytest-dir"
|
||||
condition = "mycondition"
|
||||
targets = ["mytarget", "mytest"]
|
||||
destination = ["bin"]
|
||||
component = "mycomponent"
|
||||
files = ["content/my.png"]
|
||||
dirs = [""]
|
||||
configs = [""]
|
||||
dirs = ["include"]
|
||||
configs = ["Release", "Debug"]
|
||||
optional = false
|
||||
```
|
||||
@@ -19,6 +19,7 @@ description = "Header-only library"
|
||||
[target.mylib]
|
||||
type = "interface"
|
||||
include-directories = ["include"]
|
||||
compile-features = ["cxx_std_11"]
|
||||
|
||||
[target.example]
|
||||
type = "executable"
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
# Automatically generated from tests/templates/cmake.toml - DO NOT EDIT
|
||||
layout: default
|
||||
title: Target templates
|
||||
permalink: /examples/templates
|
||||
parent: Examples
|
||||
nav_order: 7
|
||||
---
|
||||
|
||||
# Target templates
|
||||
|
||||
To avoid repeating yourself in targets you can create your own target type (template). All properties of the template are inherited when used as a target type.
|
||||
|
||||
```toml
|
||||
[project]
|
||||
name = "templates"
|
||||
description = "Target templates"
|
||||
|
||||
[template.app]
|
||||
type = "executable"
|
||||
sources = ["src/templates.cpp"]
|
||||
compile-definitions = ["IS_APP"]
|
||||
|
||||
# Unlike interface targets you can also inherit properties
|
||||
[template.app.properties]
|
||||
CXX_STANDARD = "11"
|
||||
CXX_STANDARD_REQUIRED = true
|
||||
|
||||
[target.app-a]
|
||||
type = "app"
|
||||
compile-definitions = ["APP_A"]
|
||||
|
||||
[target.app-b]
|
||||
type = "app"
|
||||
compile-definitions = ["APP_B"]
|
||||
```
|
||||
|
||||
**Note**: In most cases you probably want to use an [interface](/examples/interface) target instead.
|
||||
|
||||
<sup><sub>This page was automatically generated from [tests/templates/cmake.toml](https://github.com/build-cpp/cmkr/tree/main/tests/templates/cmake.toml).</sub></sup>
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
layout: null
|
||||
permalink: /getting-started/
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;url=https://cmkr.build">
|
||||
</head>
|
||||
</html>
|
||||
@@ -1,69 +0,0 @@
|
||||
// https://codereview.stackexchange.com/a/14315
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
// This is the type that will hold all the strings.
|
||||
// Each enumeration type will declare its own specialization.
|
||||
// Any enum that does not have a specialization will generate a compiler error
|
||||
// indicating that there is no definition of this variable (as there should be
|
||||
// be no definition of a generic version).
|
||||
template <typename T>
|
||||
struct enumStrings {
|
||||
static char const *data[];
|
||||
};
|
||||
|
||||
// This is a utility type.
|
||||
// Created automatically. Should not be used directly.
|
||||
template <typename T>
|
||||
struct enumRefHolder {
|
||||
T &enumVal;
|
||||
enumRefHolder(T &enumVal) : enumVal(enumVal) {}
|
||||
};
|
||||
template <typename T>
|
||||
struct enumConstRefHolder {
|
||||
T const &enumVal;
|
||||
enumConstRefHolder(T const &enumVal) : enumVal(enumVal) {}
|
||||
};
|
||||
|
||||
// The next two functions do the actual work of reading/writing an
|
||||
// enum as a string.
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &str, enumConstRefHolder<T> const &data) {
|
||||
return str << enumStrings<T>::data[data.enumVal];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::istream &operator>>(std::istream &str, enumRefHolder<T> const &data) {
|
||||
std::string value;
|
||||
str >> value;
|
||||
|
||||
// These two can be made easier to read in C++11
|
||||
// using std::begin() and std::end()
|
||||
//
|
||||
static auto begin = std::begin(enumStrings<T>::data);
|
||||
static auto end = std::end(enumStrings<T>::data);
|
||||
|
||||
auto find = std::find(begin, end, value);
|
||||
if (find != end) {
|
||||
data.enumVal = static_cast<T>(std::distance(begin, find));
|
||||
} else {
|
||||
throw std::invalid_argument("");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// This is the public interface:
|
||||
// use the ability of function to deduce their template type without
|
||||
// being explicitly told to create the correct type of enumRefHolder<T>
|
||||
template <typename T>
|
||||
enumConstRefHolder<T> enumToString(T const &e) {
|
||||
return enumConstRefHolder<T>(e);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enumRefHolder<T> enumFromString(T &e) {
|
||||
return enumRefHolder<T>(e);
|
||||
}
|
||||
@@ -56,11 +56,16 @@ enum TargetType {
|
||||
target_interface,
|
||||
target_custom,
|
||||
target_object,
|
||||
target_template,
|
||||
target_last,
|
||||
};
|
||||
|
||||
extern const char *targetTypeNames[target_last];
|
||||
|
||||
struct Target {
|
||||
std::string name;
|
||||
TargetType type = {};
|
||||
TargetType type = target_last;
|
||||
std::string type_name;
|
||||
|
||||
ConditionVector headers;
|
||||
ConditionVector sources;
|
||||
@@ -100,6 +105,12 @@ struct Target {
|
||||
ConditionVector include_after;
|
||||
};
|
||||
|
||||
struct Template {
|
||||
Target outline;
|
||||
std::string add_function;
|
||||
bool pass_sources_to_add_function = false;
|
||||
};
|
||||
|
||||
struct Test {
|
||||
std::string name;
|
||||
std::string condition;
|
||||
@@ -116,11 +127,14 @@ struct Install {
|
||||
std::vector<std::string> dirs;
|
||||
std::vector<std::string> configs;
|
||||
std::string destination;
|
||||
std::string component;
|
||||
bool optional = false;
|
||||
};
|
||||
|
||||
struct Subdir {
|
||||
std::string name;
|
||||
std::string condition;
|
||||
|
||||
Condition<std::string> cmake_before;
|
||||
Condition<std::string> cmake_after;
|
||||
ConditionVector include_before;
|
||||
@@ -131,6 +145,11 @@ struct Content {
|
||||
std::string name;
|
||||
std::string condition;
|
||||
tsl::ordered_map<std::string, std::string> arguments;
|
||||
|
||||
Condition<std::string> cmake_before;
|
||||
Condition<std::string> cmake_after;
|
||||
ConditionVector include_before;
|
||||
ConditionVector include_after;
|
||||
};
|
||||
|
||||
struct Project {
|
||||
@@ -160,6 +179,7 @@ struct Project {
|
||||
std::vector<Package> packages;
|
||||
Vcpkg vcpkg;
|
||||
std::vector<Content> contents;
|
||||
std::vector<Template> templates;
|
||||
std::vector<Target> targets;
|
||||
std::vector<Test> tests;
|
||||
std::vector<Install> installs;
|
||||
|
||||
+209
-91
@@ -6,21 +6,13 @@
|
||||
#include "fs.hpp"
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace cmkr {
|
||||
namespace gen {
|
||||
|
||||
static std::string to_upper(const std::string &str) {
|
||||
std::string temp;
|
||||
temp.reserve(str.size());
|
||||
for (auto c : str) {
|
||||
temp.push_back(::toupper(c));
|
||||
}
|
||||
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) {
|
||||
@@ -33,7 +25,7 @@ static std::string format(const char *format, tsl::ordered_map<std::string, std:
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir) {
|
||||
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir, bool root_project) {
|
||||
std::vector<std::string> temp;
|
||||
|
||||
auto extract_suffix = [](const fs::path &base, const fs::path &full) {
|
||||
@@ -46,6 +38,10 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
|
||||
auto stem = name.filename().stem().string();
|
||||
auto ext = name.extension();
|
||||
|
||||
if (root_project && stem == "**" && name == name.filename()) {
|
||||
throw std::runtime_error("Recursive globbing not allowed in project root: " + name.string());
|
||||
}
|
||||
|
||||
if (stem == "*") {
|
||||
for (const auto &f : fs::directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) {
|
||||
if (!f.is_directory() && f.path().extension() == ext) {
|
||||
@@ -70,11 +66,11 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
|
||||
return temp;
|
||||
}
|
||||
|
||||
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir) {
|
||||
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir, bool root_project) {
|
||||
// TODO: add duplicate checking
|
||||
std::vector<std::string> result;
|
||||
for (const auto &src : sources) {
|
||||
auto expanded = expand_cmake_path(src, toml_dir);
|
||||
auto expanded = expand_cmake_path(src, toml_dir, root_project);
|
||||
for (const auto &f : expanded) {
|
||||
result.push_back(f);
|
||||
}
|
||||
@@ -93,8 +89,24 @@ static void create_file(const fs::path &path, const std::string &contents) {
|
||||
ofs << contents;
|
||||
}
|
||||
|
||||
// CMake target name rules: https://cmake.org/cmake/help/latest/policy/CMP0037.html [A-Za-z0-9_.+\-]
|
||||
// TOML bare keys: non-empty strings composed only of [A-Za-z0-9_-]
|
||||
// We replace all non-TOML bare key characters with _
|
||||
static std::string escape_project_name(const std::string &name) {
|
||||
std::string escaped;
|
||||
escaped.reserve(name.length());
|
||||
for (auto ch : name) {
|
||||
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
|
||||
escaped += ch;
|
||||
} else {
|
||||
escaped += '_';
|
||||
}
|
||||
}
|
||||
return escaped;
|
||||
}
|
||||
|
||||
void generate_project(const std::string &type) {
|
||||
const auto name = fs::current_path().stem().string();
|
||||
const auto name = escape_project_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!");
|
||||
}
|
||||
@@ -130,13 +142,17 @@ void generate_project(const std::string &type) {
|
||||
|
||||
struct CommandEndl {
|
||||
std::stringstream &ss;
|
||||
CommandEndl(std::stringstream &ss) : ss(ss) {}
|
||||
void endl() { ss << '\n'; }
|
||||
CommandEndl(std::stringstream &ss) : ss(ss) {
|
||||
}
|
||||
void endl() {
|
||||
ss << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
struct RawArg {
|
||||
RawArg() = default;
|
||||
RawArg(std::string arg) : arg(std::move(arg)) {}
|
||||
RawArg(std::string arg) : arg(std::move(arg)) {
|
||||
}
|
||||
|
||||
std::string arg;
|
||||
};
|
||||
@@ -152,7 +168,8 @@ struct Command {
|
||||
std::string post_comment;
|
||||
|
||||
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)) {}
|
||||
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {
|
||||
}
|
||||
|
||||
~Command() noexcept(false) {
|
||||
if (!generated) {
|
||||
@@ -161,11 +178,16 @@ struct Command {
|
||||
}
|
||||
|
||||
static std::string quote(const std::string &str) {
|
||||
// Don't quote arguments that don't need quoting
|
||||
if (str.find(' ') == std::string::npos && str.find('\"') == std::string::npos && str.find('/') == std::string::npos &&
|
||||
str.find(';') == std::string::npos) {
|
||||
return str;
|
||||
// Quote an empty string
|
||||
if (str.empty()) {
|
||||
return "\"\"";
|
||||
}
|
||||
// Don't quote arguments that don't need quoting
|
||||
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#unquoted-argument
|
||||
// NOTE: Normally '/' does not require quoting according to the documentation but this has been the case here
|
||||
// previously, so for backwards compatibility its still here.
|
||||
if (str.find_first_of("()#\"\\'> |/;") == str.npos)
|
||||
return str;
|
||||
std::string result;
|
||||
result += "\"";
|
||||
for (char ch : str) {
|
||||
@@ -316,7 +338,8 @@ static std::string tolf(const std::string &str) {
|
||||
};
|
||||
|
||||
struct Generator {
|
||||
Generator(const parser::Project &project) : project(project) {}
|
||||
Generator(const parser::Project &project) : project(project) {
|
||||
}
|
||||
Generator(const Generator &) = delete;
|
||||
|
||||
const parser::Project &project;
|
||||
@@ -342,7 +365,9 @@ struct Generator {
|
||||
return CommandEndl(ss);
|
||||
}
|
||||
|
||||
void endl() { ss << '\n'; }
|
||||
void endl() {
|
||||
ss << '\n';
|
||||
}
|
||||
|
||||
void inject_includes(const std::vector<std::string> &includes) {
|
||||
if (!includes.empty()) {
|
||||
@@ -397,6 +422,14 @@ 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); });
|
||||
}
|
||||
|
||||
void conditional_cmake(const parser::Condition<std::string> &cmake) {
|
||||
handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
}
|
||||
};
|
||||
|
||||
struct ConditionScope {
|
||||
@@ -471,6 +504,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
throw std::runtime_error("No cmake.toml found!");
|
||||
}
|
||||
|
||||
// Root project doesn't have a parent
|
||||
auto root_project = parent_project == nullptr;
|
||||
|
||||
parser::Project project(parent_project, path, false);
|
||||
Generator gen(project);
|
||||
|
||||
@@ -479,16 +515,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
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); };
|
||||
auto inject_cmake = [&gen](const std::string &cmake) { gen.inject_cmake(cmake); };
|
||||
|
||||
std::string cmkr_url = "https://github.com/build-cpp/cmkr";
|
||||
comment("This file is automatically generated from cmake.toml - DO NOT EDIT");
|
||||
comment("See " + cmkr_url + " for more information");
|
||||
endl();
|
||||
|
||||
// Root project doesn't have a parent
|
||||
if (parent_project == nullptr) {
|
||||
if (root_project) {
|
||||
cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl();
|
||||
|
||||
if (!project.allow_in_tree) {
|
||||
@@ -561,7 +594,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
if (!project.options.empty()) {
|
||||
comment("Options");
|
||||
for (const auto &opt : project.options) {
|
||||
cmd("option")(opt.name, opt.comment, opt.val ? "ON" : "OFF");
|
||||
cmd("option")(opt.name, RawArg(Command::quote(opt.comment)), opt.val ? "ON" : "OFF");
|
||||
}
|
||||
endl();
|
||||
}
|
||||
@@ -587,8 +620,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
endl();
|
||||
}
|
||||
|
||||
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); });
|
||||
gen.conditional_includes(project.include_before);
|
||||
gen.conditional_cmake(project.cmake_before);
|
||||
|
||||
if (!project.project_name.empty()) {
|
||||
auto languages = std::make_pair("LANGUAGES", project.project_languages);
|
||||
@@ -597,17 +630,19 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
cmd("project")(project.project_name, languages, version, description).endl();
|
||||
}
|
||||
|
||||
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); });
|
||||
gen.conditional_includes(project.include_after);
|
||||
gen.conditional_cmake(project.cmake_after);
|
||||
|
||||
if (!project.vcpkg.packages.empty()) {
|
||||
// Allow the user to specify a url or derive it from the version
|
||||
auto url = project.vcpkg.url;
|
||||
auto version_name = url;
|
||||
if (url.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/" + project.vcpkg.version + ".tar.gz";
|
||||
version_name = project.vcpkg.version;
|
||||
}
|
||||
|
||||
// Show a nicer error than vcpkg when specifying an invalid package name
|
||||
@@ -621,10 +656,14 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
// clang-format off
|
||||
cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG");
|
||||
cmd("include")("FetchContent");
|
||||
cmd("message")("STATUS", "Fetching vcpkg...");
|
||||
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")...");
|
||||
cmd("FetchContent_Declare")("vcpkg", "URL", url);
|
||||
cmd("FetchContent_MakeAvailable")("vcpkg");
|
||||
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
|
||||
// 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("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
|
||||
cmd("endif")();
|
||||
cmd("endif")();
|
||||
endl();
|
||||
// clang-format on
|
||||
@@ -688,7 +727,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
|
||||
ofs << " ],\n";
|
||||
ofs << " \"description\": \"" << escape(project.project_description) << "\",\n";
|
||||
ofs << " \"name\": \"" << escape(project.project_name) << "\",\n";
|
||||
ofs << " \"name\": \"" << escape(vcpkg_escape_identifier(project.project_name)) << "\",\n";
|
||||
ofs << R"( "version-string": "")" << '\n';
|
||||
ofs << "}\n";
|
||||
}
|
||||
@@ -697,6 +736,10 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
cmd("include")("FetchContent").endl();
|
||||
for (const auto &content : project.contents) {
|
||||
ConditionScope cs(gen, content.condition);
|
||||
|
||||
gen.conditional_includes(content.include_before);
|
||||
gen.conditional_cmake(content.cmake_before);
|
||||
|
||||
std::string version_info = "";
|
||||
if (content.arguments.contains("GIT_TAG")) {
|
||||
version_info = " (" + content.arguments.at("GIT_TAG") + ")";
|
||||
@@ -704,12 +747,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
version_info = " (" + content.arguments.at("SVN_REVISION") + ")";
|
||||
}
|
||||
cmd("message")("STATUS", "Fetching " + content.name + version_info + "...");
|
||||
ss << "FetchContent_Declare(\n\t" << content.name << "\n";
|
||||
for (const auto &arg : content.arguments) {
|
||||
ss << "\t" << arg.first << "\n\t\t" << arg.second << "\n";
|
||||
}
|
||||
ss << ")\n";
|
||||
cmd("FetchContent_Declare")(content.name, content.arguments);
|
||||
cmd("FetchContent_MakeAvailable")(content.name).endl();
|
||||
|
||||
gen.conditional_includes(content.include_after);
|
||||
gen.conditional_cmake(content.cmake_after);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -754,14 +796,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
for (const auto &subdir : project.subdirs) {
|
||||
ConditionScope cs(gen, subdir.condition);
|
||||
|
||||
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); });
|
||||
gen.conditional_includes(subdir.include_before);
|
||||
gen.conditional_cmake(subdir.cmake_before);
|
||||
|
||||
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); });
|
||||
gen.conditional_includes(subdir.include_after);
|
||||
gen.conditional_cmake(subdir.cmake_after);
|
||||
}
|
||||
|
||||
if (!project.targets.empty()) {
|
||||
@@ -771,22 +812,51 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
}
|
||||
|
||||
const auto &target = project.targets[i];
|
||||
const parser::Template *tmplate = nullptr;
|
||||
std::unique_ptr<ConditionScope> tmplate_cs{};
|
||||
|
||||
comment("Target " + target.name);
|
||||
|
||||
// Check if this target is using a template.
|
||||
if (target.type == parser::target_template) {
|
||||
for (const auto &t : project.templates) {
|
||||
if (target.type_name == t.outline.name) {
|
||||
tmplate = &t;
|
||||
tmplate_cs = std::unique_ptr<ConditionScope>(new ConditionScope(gen, tmplate->outline.condition));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ConditionScope cs(gen, target.condition);
|
||||
|
||||
cmd("set")("CMKR_TARGET", target.name);
|
||||
|
||||
gen.handle_condition(target.include_before,
|
||||
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
if (tmplate != nullptr) {
|
||||
gen.conditional_includes(tmplate->outline.include_before);
|
||||
gen.conditional_cmake(tmplate->outline.cmake_before);
|
||||
}
|
||||
|
||||
gen.conditional_includes(target.include_before);
|
||||
gen.conditional_cmake(target.cmake_before);
|
||||
|
||||
auto sources_var = target.name + "_SOURCES";
|
||||
|
||||
bool added_toml = false;
|
||||
cmd("set")(sources_var, RawArg("\"\"")).endl();
|
||||
|
||||
if (tmplate != nullptr) {
|
||||
gen.handle_condition(tmplate->outline.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
|
||||
auto sources = expand_cmake_paths(condition_sources, path, root_project);
|
||||
if (sources.empty()) {
|
||||
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
|
||||
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
|
||||
}
|
||||
cmd("list")("APPEND", sources_var, sources);
|
||||
});
|
||||
}
|
||||
|
||||
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
|
||||
auto sources = expand_cmake_paths(condition_sources, path);
|
||||
auto sources = expand_cmake_paths(condition_sources, path, root_project);
|
||||
if (sources.empty()) {
|
||||
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
|
||||
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
|
||||
@@ -798,66 +868,91 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
cmd("list")("APPEND", sources_var, sources);
|
||||
});
|
||||
|
||||
if (!added_toml && target.type != parser::target_interface) {
|
||||
auto target_type = target.type;
|
||||
|
||||
if (tmplate != nullptr) {
|
||||
target_type = tmplate->outline.type;
|
||||
}
|
||||
|
||||
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 + "}");
|
||||
|
||||
std::string add_command;
|
||||
std::string target_type;
|
||||
std::string target_type_string;
|
||||
std::string target_scope;
|
||||
switch (target.type) {
|
||||
|
||||
switch (target_type) {
|
||||
case parser::target_executable:
|
||||
add_command = "add_executable";
|
||||
target_type = "";
|
||||
target_type_string = "";
|
||||
target_scope = "PRIVATE";
|
||||
break;
|
||||
case parser::target_library:
|
||||
add_command = "add_library";
|
||||
target_type = "";
|
||||
target_type_string = "";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case parser::target_shared:
|
||||
add_command = "add_library";
|
||||
target_type = "SHARED";
|
||||
target_type_string = "SHARED";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case parser::target_static:
|
||||
add_command = "add_library";
|
||||
target_type = "STATIC";
|
||||
target_type_string = "STATIC";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case parser::target_interface:
|
||||
add_command = "add_library";
|
||||
target_type = "INTERFACE";
|
||||
target_type_string = "INTERFACE";
|
||||
target_scope = "INTERFACE";
|
||||
break;
|
||||
case parser::target_custom:
|
||||
// TODO: add proper support, this is hacky
|
||||
add_command = "add_custom_target";
|
||||
target_type = "SOURCES";
|
||||
target_type_string = "SOURCES";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
case parser::target_object:
|
||||
// NOTE: This is properly supported since 3.12
|
||||
add_command = "add_library";
|
||||
target_type = "OBJECT";
|
||||
target_type_string = "OBJECT";
|
||||
target_scope = "PUBLIC";
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("Unimplemented enum value");
|
||||
}
|
||||
|
||||
cmd(add_command)(target.name, target_type).endl();
|
||||
// Handle custom add commands from templates.
|
||||
if (tmplate != nullptr && !tmplate->add_function.empty()) {
|
||||
add_command = tmplate->add_function;
|
||||
target_type_string = ""; // TODO: let templates supply options to the add_command here?
|
||||
|
||||
// clang-format off
|
||||
cmd("if")(sources_var);
|
||||
cmd("target_sources")(target.name, target.type == parser::target_interface ? "INTERFACE" : "PRIVATE", "${" + sources_var + "}");
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
if (tmplate->pass_sources_to_add_function) {
|
||||
cmd(add_command)(target.name, target_type_string, "${" + sources_var + "}");
|
||||
} else {
|
||||
cmd(add_command)(target.name, target_type_string).endl();
|
||||
|
||||
// clang-format off
|
||||
cmd("if")(sources_var);
|
||||
cmd("target_sources")(target.name, target_type == parser::target_interface ? "INTERFACE" : "PRIVATE", "${" + sources_var + "}");
|
||||
cmd("endif")().endl();
|
||||
// clang-format on
|
||||
}
|
||||
} else {
|
||||
cmd(add_command)(target.name, target_type_string).endl();
|
||||
|
||||
// clang-format off
|
||||
cmd("if")(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 == parser::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");
|
||||
@@ -879,39 +974,57 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
[&](const std::string &, const std::vector<std::string> &args) { cmd(command)(target.name, scope, args); });
|
||||
};
|
||||
|
||||
target_cmd("target_compile_definitions", target.compile_definitions, target_scope);
|
||||
target_cmd("target_compile_definitions", target.private_compile_definitions, "PRIVATE");
|
||||
auto gen_target_cmds = [&](const parser::Target &t) {
|
||||
target_cmd("target_compile_definitions", t.compile_definitions, target_scope);
|
||||
target_cmd("target_compile_definitions", t.private_compile_definitions, "PRIVATE");
|
||||
|
||||
target_cmd("target_compile_features", target.compile_features, target_scope);
|
||||
target_cmd("target_compile_features", target.private_compile_features, "PRIVATE");
|
||||
target_cmd("target_compile_features", t.compile_features, target_scope);
|
||||
target_cmd("target_compile_features", t.private_compile_features, "PRIVATE");
|
||||
|
||||
target_cmd("target_compile_options", target.compile_options, target_scope);
|
||||
target_cmd("target_compile_options", target.private_compile_options, "PRIVATE");
|
||||
target_cmd("target_compile_options", t.compile_options, target_scope);
|
||||
target_cmd("target_compile_options", t.private_compile_options, "PRIVATE");
|
||||
|
||||
target_cmd("target_include_directories", target.include_directories, target_scope);
|
||||
target_cmd("target_include_directories", target.private_include_directories, "PRIVATE");
|
||||
target_cmd("target_include_directories", t.include_directories, target_scope);
|
||||
target_cmd("target_include_directories", t.private_include_directories, "PRIVATE");
|
||||
|
||||
target_cmd("target_link_directories", target.link_directories, target_scope);
|
||||
target_cmd("target_link_directories", target.private_link_directories, "PRIVATE");
|
||||
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", target.link_libraries, target_scope);
|
||||
target_cmd("target_link_libraries", target.private_link_libraries, "PRIVATE");
|
||||
target_cmd("target_link_libraries", t.link_libraries, target_scope);
|
||||
target_cmd("target_link_libraries", t.private_link_libraries, "PRIVATE");
|
||||
|
||||
target_cmd("target_link_options", target.link_options, target_scope);
|
||||
target_cmd("target_link_options", target.private_link_options, "PRIVATE");
|
||||
target_cmd("target_link_options", t.link_options, target_scope);
|
||||
target_cmd("target_link_options", t.private_link_options, "PRIVATE");
|
||||
|
||||
target_cmd("target_precompile_headers", target.precompile_headers, target_scope);
|
||||
target_cmd("target_precompile_headers", target.private_precompile_headers, "PRIVATE");
|
||||
target_cmd("target_precompile_headers", t.precompile_headers, target_scope);
|
||||
target_cmd("target_precompile_headers", t.private_precompile_headers, "PRIVATE");
|
||||
};
|
||||
|
||||
if (!target.properties.empty()) {
|
||||
gen.handle_condition(target.properties, [&](const std::string &, const tsl::ordered_map<std::string, std::string> &properties) {
|
||||
if (tmplate != nullptr) {
|
||||
gen_target_cmds(tmplate->outline);
|
||||
}
|
||||
|
||||
gen_target_cmds(target);
|
||||
|
||||
if (!target.properties.empty() || (tmplate != nullptr && !tmplate->outline.properties.empty())) {
|
||||
auto props = target.properties;
|
||||
|
||||
if (tmplate != nullptr) {
|
||||
props.insert(tmplate->outline.properties.begin(), tmplate->outline.properties.end());
|
||||
}
|
||||
|
||||
gen.handle_condition(props, [&](const std::string &, const tsl::ordered_map<std::string, std::string> &properties) {
|
||||
cmd("set_target_properties")(target.name, "PROPERTIES", properties);
|
||||
});
|
||||
}
|
||||
|
||||
gen.handle_condition(target.include_after,
|
||||
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
|
||||
gen.handle_condition(target.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
|
||||
gen.conditional_includes(target.include_after);
|
||||
gen.conditional_cmake(target.cmake_after);
|
||||
|
||||
if (tmplate != nullptr) {
|
||||
gen.conditional_includes(tmplate->outline.include_after);
|
||||
gen.conditional_cmake(tmplate->outline.cmake_after);
|
||||
}
|
||||
|
||||
cmd("unset")("CMKR_TARGET");
|
||||
cmd("unset")("CMKR_SOURCES");
|
||||
@@ -943,7 +1056,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
auto dirs = std::make_pair("DIRS", inst.dirs);
|
||||
std::vector<std::string> files_data;
|
||||
if (!inst.files.empty()) {
|
||||
files_data = expand_cmake_paths(inst.files, path);
|
||||
files_data = expand_cmake_paths(inst.files, path, root_project);
|
||||
if (files_data.empty()) {
|
||||
throw std::runtime_error("[[install]] files wildcard did not resolve to any files");
|
||||
}
|
||||
@@ -951,9 +1064,14 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
|
||||
auto files = std::make_pair("FILES", inst.files);
|
||||
auto configs = std::make_pair("CONFIGURATIONS", inst.configs);
|
||||
auto destination = std::make_pair("DESTINATION", inst.destination);
|
||||
auto component = std::make_pair("COMPONENT", inst.targets.empty() ? "" : inst.targets.front());
|
||||
auto component_name = inst.component;
|
||||
if (component_name.empty() && !inst.targets.empty()) {
|
||||
component_name = inst.targets.front();
|
||||
}
|
||||
auto component = std::make_pair("COMPONENT", component_name);
|
||||
auto optional = inst.optional ? "OPTIONAL" : "";
|
||||
ConditionScope cs(gen, inst.condition);
|
||||
cmd("install")(targets, dirs, files, configs, destination, component);
|
||||
cmd("install")(targets, dirs, files, configs, destination, component, optional);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-4
@@ -1,15 +1,21 @@
|
||||
#include "error.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
namespace cmkr {
|
||||
namespace error {
|
||||
|
||||
Status::Status(Code ec) noexcept : ec_(ec) {}
|
||||
Status::Status(Code ec) noexcept : ec_(ec) {
|
||||
}
|
||||
|
||||
Status::operator int() const noexcept { return static_cast<int>(ec_); }
|
||||
Status::operator int() const noexcept {
|
||||
return static_cast<int>(ec_);
|
||||
}
|
||||
|
||||
Status::Code Status::code() const noexcept { return ec_; }
|
||||
Status::Code Status::code() const noexcept {
|
||||
return ec_;
|
||||
}
|
||||
|
||||
} // namespace error
|
||||
} // namespace cmkr
|
||||
@@ -20,6 +26,6 @@ static const char *err_string[] = {
|
||||
};
|
||||
|
||||
const char *cmkr_error_status(int i) {
|
||||
assert(i >= 0 && i < (sizeof(err_string) / sizeof(*(err_string))));
|
||||
assert(i >= 0 && static_cast<size_t>(i) < (sizeof(err_string) / sizeof(*(err_string))));
|
||||
return err_string[i];
|
||||
}
|
||||
|
||||
+9
-3
@@ -4,7 +4,9 @@
|
||||
namespace cmkr {
|
||||
namespace help {
|
||||
|
||||
const char *version() noexcept { return "cmkr version " CMKR_VERSION; }
|
||||
const char *version() noexcept {
|
||||
return "cmkr version " CMKR_VERSION;
|
||||
}
|
||||
|
||||
const char *message() noexcept {
|
||||
return R"lit(
|
||||
@@ -22,6 +24,10 @@ arguments:
|
||||
} // namespace help
|
||||
} // namespace cmkr
|
||||
|
||||
const char *cmkr_help_version(void) { return cmkr::help::version(); }
|
||||
const char *cmkr_help_version(void) {
|
||||
return cmkr::help::version();
|
||||
}
|
||||
|
||||
const char *cmkr_help_message(void) { return cmkr::help::message(); }
|
||||
const char *cmkr_help_message(void) {
|
||||
return cmkr::help::message();
|
||||
}
|
||||
+256
-128
@@ -1,39 +1,28 @@
|
||||
#include "project_parser.hpp"
|
||||
|
||||
#include "enum_helper.hpp"
|
||||
#include "fs.hpp"
|
||||
#include <deque>
|
||||
#include <stdexcept>
|
||||
#include <toml.hpp>
|
||||
#include <tsl/ordered_map.h>
|
||||
|
||||
template <>
|
||||
const char *enumStrings<cmkr::parser::TargetType>::data[] = {"executable", "library", "shared", "static", "interface", "custom", "object"};
|
||||
#include <tsl/ordered_set.h>
|
||||
|
||||
namespace cmkr {
|
||||
namespace parser {
|
||||
|
||||
using TomlBasicValue = toml::basic_value<toml::discard_comments, tsl::ordered_map, std::vector>;
|
||||
const char *targetTypeNames[target_last] = {"executable", "library", "shared", "static", "interface", "custom", "object", "template"};
|
||||
|
||||
template <typename EnumType>
|
||||
static EnumType to_enum(const std::string &str, const std::string &help_name) {
|
||||
EnumType value;
|
||||
try {
|
||||
std::stringstream ss(str);
|
||||
ss >> enumFromString(value);
|
||||
} catch (std::invalid_argument &) {
|
||||
std::string supported;
|
||||
for (const auto &s : enumStrings<EnumType>::data) {
|
||||
if (!supported.empty()) {
|
||||
supported += ", ";
|
||||
}
|
||||
supported += s;
|
||||
static TargetType parse_targetType(const std::string &name) {
|
||||
for (int i = 0; i < target_last; i++) {
|
||||
if (name == targetTypeNames[i]) {
|
||||
return static_cast<TargetType>(i);
|
||||
}
|
||||
throw std::runtime_error("Unknown " + help_name + "'" + str + "'! Supported types are: " + supported);
|
||||
}
|
||||
return value;
|
||||
return target_last;
|
||||
}
|
||||
|
||||
using TomlBasicValue = toml::basic_value<toml::discard_comments, tsl::ordered_map, std::vector>;
|
||||
|
||||
static std::string format_key_error(const std::string &error, const toml::key &ky, const TomlBasicValue &value) {
|
||||
auto loc = value.location();
|
||||
auto line_number_str = std::to_string(loc.line());
|
||||
@@ -42,7 +31,7 @@ static std::string format_key_error(const std::string &error, const toml::key &k
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << "[error] " << error << '\n';
|
||||
oss << " --> " << loc.file_name() << '\n';
|
||||
oss << " --> " << loc.file_name() << ':' << loc.line() << '\n';
|
||||
|
||||
oss << std::string(line_width + 2, ' ') << "|\n";
|
||||
oss << ' ' << line_number_str << " | " << line_str << '\n';
|
||||
@@ -55,19 +44,20 @@ static std::string format_key_error(const std::string &error, const toml::key &k
|
||||
if (key_start != std::string::npos) {
|
||||
oss << std::string(key_start + 1, ' ') << std::string(ky.length(), '~');
|
||||
}
|
||||
oss << '\n';
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
class TomlChecker {
|
||||
const TomlBasicValue &m_v;
|
||||
tsl::ordered_map<toml::key, bool> m_visited;
|
||||
tsl::ordered_map<toml::key, bool> m_conditionVisited;
|
||||
tsl::ordered_set<toml::key> m_visited;
|
||||
tsl::ordered_set<toml::key> m_conditionVisited;
|
||||
|
||||
public:
|
||||
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {}
|
||||
TomlChecker(const TomlBasicValue &v) : m_v(v) {}
|
||||
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {
|
||||
}
|
||||
TomlChecker(const TomlBasicValue &v) : m_v(v) {
|
||||
}
|
||||
TomlChecker(const TomlChecker &) = delete;
|
||||
TomlChecker(TomlChecker &&) = delete;
|
||||
|
||||
@@ -90,7 +80,7 @@ class TomlChecker {
|
||||
// Handle visiting logic
|
||||
for (const auto &itr : destination) {
|
||||
if (!itr.first.empty()) {
|
||||
m_conditionVisited.emplace(itr.first, true);
|
||||
m_conditionVisited.emplace(itr.first);
|
||||
}
|
||||
}
|
||||
visit(ky);
|
||||
@@ -121,7 +111,13 @@ class TomlChecker {
|
||||
return toml::find(m_v, ky);
|
||||
}
|
||||
|
||||
void visit(const toml::key &ky) { m_visited.emplace(ky, true); }
|
||||
void visit(const toml::key &ky) {
|
||||
m_visited.emplace(ky);
|
||||
}
|
||||
|
||||
bool visisted(const toml::key &ky) const {
|
||||
return m_visited.contains(ky);
|
||||
}
|
||||
|
||||
void check(const tsl::ordered_map<std::string, std::string> &conditions) const {
|
||||
for (const auto &itr : m_v.as_table()) {
|
||||
@@ -156,14 +152,22 @@ class TomlChecker {
|
||||
};
|
||||
|
||||
class TomlCheckerRoot {
|
||||
const TomlBasicValue &m_root;
|
||||
std::deque<TomlChecker> m_checkers;
|
||||
tsl::ordered_set<toml::key> m_visisted;
|
||||
bool m_checked = false;
|
||||
|
||||
public:
|
||||
TomlCheckerRoot() = default;
|
||||
TomlCheckerRoot(const TomlBasicValue &root) : m_root(root) {
|
||||
}
|
||||
TomlCheckerRoot(const TomlCheckerRoot &) = delete;
|
||||
TomlCheckerRoot(TomlCheckerRoot &&) = delete;
|
||||
|
||||
bool contains(const toml::key &ky) {
|
||||
m_visisted.emplace(ky);
|
||||
return m_root.contains(ky);
|
||||
}
|
||||
|
||||
TomlChecker &create(const TomlBasicValue &v) {
|
||||
m_checkers.emplace_back(v);
|
||||
return m_checkers.back();
|
||||
@@ -174,7 +178,14 @@ class TomlCheckerRoot {
|
||||
return m_checkers.back();
|
||||
}
|
||||
|
||||
void check(const tsl::ordered_map<std::string, std::string> &conditions) {
|
||||
void check(const tsl::ordered_map<std::string, std::string> &conditions, bool check_root) {
|
||||
if (check_root) {
|
||||
for (const auto &itr : m_root.as_table()) {
|
||||
if (!m_visisted.contains(itr.first)) {
|
||||
throw std::runtime_error(format_key_error("Unknown key '" + itr.first + "'", itr.first, itr.second));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto &checker : m_checkers) {
|
||||
checker.check(conditions);
|
||||
}
|
||||
@@ -184,13 +195,16 @@ class TomlCheckerRoot {
|
||||
Project::Project(const Project *parent, 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!");
|
||||
throw std::runtime_error("File not found '" + toml_path.string() + "'");
|
||||
}
|
||||
const auto toml = toml::parse<toml::discard_comments, tsl::ordered_map, std::vector>(toml_path.string());
|
||||
if (toml.size() == 0) {
|
||||
throw std::runtime_error("Empty TOML '" + toml_path.string() + "'");
|
||||
}
|
||||
|
||||
TomlCheckerRoot checker;
|
||||
TomlCheckerRoot checker(toml);
|
||||
|
||||
if (toml.contains("cmake")) {
|
||||
if (checker.contains("cmake")) {
|
||||
auto &cmake = checker.create(toml, "cmake");
|
||||
|
||||
cmake.required("version", cmake_version);
|
||||
@@ -222,7 +236,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
|
||||
// Skip the rest of the parsing when building
|
||||
if (build) {
|
||||
checker.check(conditions);
|
||||
checker.check(conditions, false);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -236,18 +250,22 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
conditions["gcc"] = R"cmake(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")cmake";
|
||||
conditions["clang"] = R"cmake(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "Clang")cmake";
|
||||
conditions["msvc"] = R"cmake(MSVC)cmake";
|
||||
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";
|
||||
} else {
|
||||
conditions = parent->conditions;
|
||||
templates = parent->templates;
|
||||
}
|
||||
|
||||
if (toml.contains("conditions")) {
|
||||
if (checker.contains("conditions")) {
|
||||
auto conds = toml::find<decltype(conditions)>(toml, "conditions");
|
||||
for (const auto &cond : conds) {
|
||||
conditions[cond.first] = cond.second;
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("project")) {
|
||||
if (checker.contains("project")) {
|
||||
auto &project = checker.create(toml, "project");
|
||||
project.required("name", project_name);
|
||||
project.optional("version", project_version);
|
||||
@@ -260,7 +278,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
project.optional("subdirs", project_subdirs);
|
||||
}
|
||||
|
||||
if (toml.contains("subdir")) {
|
||||
if (checker.contains("subdir")) {
|
||||
const auto &subs = toml::find(toml, "subdir").as_table();
|
||||
for (const auto &itr : subs) {
|
||||
Subdir subdir;
|
||||
@@ -277,7 +295,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("settings")) {
|
||||
if (checker.contains("settings")) {
|
||||
using set_map = tsl::ordered_map<std::string, TomlBasicValue>;
|
||||
const auto &sets = toml::find<set_map>(toml, "settings");
|
||||
for (const auto &itr : sets) {
|
||||
@@ -306,7 +324,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("options")) {
|
||||
if (checker.contains("options")) {
|
||||
using opts_map = tsl::ordered_map<std::string, TomlBasicValue>;
|
||||
const auto &opts = toml::find<opts_map>(toml, "options");
|
||||
for (const auto &itr : opts) {
|
||||
@@ -324,7 +342,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("find-package")) {
|
||||
if (checker.contains("find-package")) {
|
||||
using pkg_map = tsl::ordered_map<std::string, TomlBasicValue>;
|
||||
const auto &pkgs = toml::find<pkg_map>(toml, "find-package");
|
||||
for (const auto &itr : pkgs) {
|
||||
@@ -345,136 +363,243 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: perform checking here
|
||||
if (toml.contains("fetch-content")) {
|
||||
if (checker.contains("fetch-content")) {
|
||||
const auto &fc = toml::find(toml, "fetch-content").as_table();
|
||||
for (const auto &itr : fc) {
|
||||
Content content;
|
||||
content.name = itr.first;
|
||||
|
||||
auto &c = checker.create(itr.second);
|
||||
c.optional("condition", content.condition);
|
||||
c.optional("cmake-before", content.cmake_before);
|
||||
c.optional("cmake-after", content.cmake_after);
|
||||
c.optional("include-before", content.include_before);
|
||||
c.optional("include-after", content.include_after);
|
||||
|
||||
for (const auto &argItr : itr.second.as_table()) {
|
||||
auto key = argItr.first;
|
||||
if (key == "condition") {
|
||||
content.condition = argItr.second.as_string();
|
||||
continue;
|
||||
std::string value;
|
||||
if (argItr.second.is_array()) {
|
||||
for (const auto &list_val : argItr.second.as_array()) {
|
||||
if (!value.empty()) {
|
||||
value += ';';
|
||||
}
|
||||
value += list_val.as_string();
|
||||
}
|
||||
} else if (argItr.second.is_boolean()) {
|
||||
value = argItr.second.as_boolean() ? "ON" : "OFF";
|
||||
} else {
|
||||
value = argItr.second.as_string();
|
||||
}
|
||||
|
||||
auto is_cmake_arg = [](const std::string &s) {
|
||||
for (auto c : s) {
|
||||
if (!(std::isdigit(c) || std::isupper(c) || c == '_')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// https://cmake.org/cmake/help/latest/command/string.html#supported-hash-algorithms
|
||||
tsl::ordered_set<std::string> hash_algorithms = {
|
||||
"md5", "sha1", "sha224", "sha256", "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512",
|
||||
};
|
||||
|
||||
auto key = argItr.first;
|
||||
if (key == "git") {
|
||||
key = "GIT_REPOSITORY";
|
||||
} else if (key == "tag") {
|
||||
key = "GIT_TAG";
|
||||
} else if (key == "shallow") {
|
||||
key = "GIT_SHALLOW";
|
||||
} else if (key == "svn") {
|
||||
key = "SVN_REPOSITORY";
|
||||
} else if (key == "rev") {
|
||||
key = "SVN_REVISION";
|
||||
} else if (key == "url") {
|
||||
key = "URL";
|
||||
} else if (hash_algorithms.contains(key)) {
|
||||
std::string algo;
|
||||
for (auto c : key) {
|
||||
algo.push_back(std::toupper(c));
|
||||
}
|
||||
key = "URL_HASH";
|
||||
value = algo + "=" + value;
|
||||
} else if (key == "hash") {
|
||||
key = "URL_HASH";
|
||||
} else {
|
||||
// don't change arg
|
||||
} else if (is_cmake_arg(key)) {
|
||||
// allow passthrough of ExternalProject options
|
||||
} else if (!c.visisted(key)) {
|
||||
throw std::runtime_error(format_key_error("Unknown key '" + argItr.first + "'", argItr.first, argItr.second));
|
||||
}
|
||||
content.arguments.emplace(key, argItr.second.as_string());
|
||||
|
||||
// Make sure not to emit keys like "condition" in the FetchContent call
|
||||
if (!c.visisted(key)) {
|
||||
content.arguments.emplace(key, value);
|
||||
}
|
||||
|
||||
c.visit(argItr.first);
|
||||
}
|
||||
contents.emplace_back(std::move(content));
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("bin")) {
|
||||
if (checker.contains("bin")) {
|
||||
throw std::runtime_error("[[bin]] has been renamed to [[target]]");
|
||||
}
|
||||
|
||||
if (toml.contains("target")) {
|
||||
const auto &ts = toml::find(toml, "target").as_table();
|
||||
auto parse_target = [&](const std::string &name, TomlChecker &t, bool isTemplate) {
|
||||
Target target;
|
||||
target.name = name;
|
||||
|
||||
for (const auto &itr : ts) {
|
||||
const auto &value = itr.second;
|
||||
t.required("type", target.type_name);
|
||||
target.type = parse_targetType(target.type_name);
|
||||
|
||||
Target target;
|
||||
target.name = itr.first;
|
||||
// Users cannot set this target type
|
||||
if (target.type == target_template) {
|
||||
target.type = target_last;
|
||||
}
|
||||
|
||||
auto &t = checker.create(value);
|
||||
std::string type;
|
||||
t.required("type", type);
|
||||
target.type = to_enum<TargetType>(type, "target type");
|
||||
|
||||
t.optional("headers", target.headers);
|
||||
t.optional("sources", target.sources);
|
||||
|
||||
t.optional("compile-definitions", target.compile_definitions);
|
||||
t.optional("private-compile-definitions", target.private_compile_definitions);
|
||||
|
||||
t.optional("compile-features", target.compile_features);
|
||||
t.optional("private-compile-features", target.private_compile_features);
|
||||
|
||||
t.optional("compile-options", target.compile_options);
|
||||
t.optional("private-compile-options", target.private_compile_options);
|
||||
|
||||
t.optional("include-directories", target.include_directories);
|
||||
t.optional("private-include-directories", target.private_include_directories);
|
||||
|
||||
t.optional("link-directories", target.link_directories);
|
||||
t.optional("private-link-directories", target.private_link_directories);
|
||||
|
||||
t.optional("link-libraries", target.link_libraries);
|
||||
t.optional("private-link-libraries", target.private_link_libraries);
|
||||
|
||||
t.optional("link-options", target.link_options);
|
||||
t.optional("private-link-options", target.private_link_options);
|
||||
|
||||
t.optional("precompile-headers", target.precompile_headers);
|
||||
t.optional("private-precompile-headers", target.private_precompile_headers);
|
||||
|
||||
if (!target.headers.empty()) {
|
||||
auto &sources = target.sources.nth(0).value();
|
||||
const auto &headers = target.headers.nth(0)->second;
|
||||
sources.insert(sources.end(), headers.begin(), headers.end());
|
||||
if (!isTemplate && target.type == target_last) {
|
||||
for (const auto &tmplate : templates) {
|
||||
if (target.type_name == tmplate.outline.name) {
|
||||
target.type = target_template;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.optional("condition", target.condition);
|
||||
t.optional("alias", target.alias);
|
||||
if (target.type == target_last) {
|
||||
std::string error = "Unknown target type '" + target.type_name + "'\n";
|
||||
error += "Available types:\n";
|
||||
for (std::string type_name : targetTypeNames) {
|
||||
if (type_name != "template") {
|
||||
error += " - " + type_name + "\n";
|
||||
}
|
||||
}
|
||||
if (!isTemplate && !templates.empty()) {
|
||||
error += "Available templates:\n";
|
||||
for (const auto &tmplate : templates) {
|
||||
error += " - " + tmplate.outline.name + "\n";
|
||||
}
|
||||
}
|
||||
error.pop_back(); // Remove last newline
|
||||
throw std::runtime_error(format_key_error(error, target.type_name, t.find("type")));
|
||||
}
|
||||
|
||||
if (t.contains("properties")) {
|
||||
auto store_property = [&target](const toml::key &k, const TomlBasicValue &v, const std::string &condition) {
|
||||
if (v.is_array()) {
|
||||
std::string property_list;
|
||||
for (const auto &list_val : v.as_array()) {
|
||||
if (!property_list.empty()) {
|
||||
property_list += ';';
|
||||
}
|
||||
property_list += list_val.as_string();
|
||||
t.optional("headers", target.headers);
|
||||
t.optional("sources", target.sources);
|
||||
|
||||
t.optional("compile-definitions", target.compile_definitions);
|
||||
t.optional("private-compile-definitions", target.private_compile_definitions);
|
||||
|
||||
t.optional("compile-features", target.compile_features);
|
||||
t.optional("private-compile-features", target.private_compile_features);
|
||||
|
||||
t.optional("compile-options", target.compile_options);
|
||||
t.optional("private-compile-options", target.private_compile_options);
|
||||
|
||||
t.optional("include-directories", target.include_directories);
|
||||
t.optional("private-include-directories", target.private_include_directories);
|
||||
|
||||
t.optional("link-directories", target.link_directories);
|
||||
t.optional("private-link-directories", target.private_link_directories);
|
||||
|
||||
t.optional("link-libraries", target.link_libraries);
|
||||
t.optional("private-link-libraries", target.private_link_libraries);
|
||||
|
||||
t.optional("link-options", target.link_options);
|
||||
t.optional("private-link-options", target.private_link_options);
|
||||
|
||||
t.optional("precompile-headers", target.precompile_headers);
|
||||
t.optional("private-precompile-headers", target.private_precompile_headers);
|
||||
|
||||
if (!target.headers.empty()) {
|
||||
auto &sources = target.sources.nth(0).value();
|
||||
const auto &headers = target.headers.nth(0)->second;
|
||||
sources.insert(sources.end(), headers.begin(), headers.end());
|
||||
}
|
||||
|
||||
t.optional("condition", target.condition);
|
||||
t.optional("alias", target.alias);
|
||||
|
||||
if (t.contains("properties")) {
|
||||
auto store_property = [&target](const toml::key &k, const TomlBasicValue &v, const std::string &condition) {
|
||||
if (v.is_array()) {
|
||||
std::string property_list;
|
||||
for (const auto &list_val : v.as_array()) {
|
||||
if (!property_list.empty()) {
|
||||
property_list += ';';
|
||||
}
|
||||
target.properties[condition][k] = property_list;
|
||||
} else if (v.is_boolean()) {
|
||||
target.properties[condition][k] = v.as_boolean() ? "ON" : "OFF";
|
||||
} else {
|
||||
target.properties[condition][k] = v.as_string();
|
||||
property_list += list_val.as_string();
|
||||
}
|
||||
};
|
||||
target.properties[condition][k] = property_list;
|
||||
} else if (v.is_boolean()) {
|
||||
target.properties[condition][k] = v.as_boolean() ? "ON" : "OFF";
|
||||
} else {
|
||||
target.properties[condition][k] = v.as_string();
|
||||
}
|
||||
};
|
||||
|
||||
const auto &props = t.find("properties").as_table();
|
||||
for (const auto &propKv : props) {
|
||||
const auto &k = propKv.first;
|
||||
const auto &v = propKv.second;
|
||||
if (v.is_table()) {
|
||||
for (const auto &condKv : v.as_table()) {
|
||||
store_property(condKv.first, condKv.second, k);
|
||||
}
|
||||
} else {
|
||||
store_property(k, v, "");
|
||||
const auto &props = t.find("properties").as_table();
|
||||
for (const auto &propKv : props) {
|
||||
const auto &k = propKv.first;
|
||||
const auto &v = propKv.second;
|
||||
if (v.is_table()) {
|
||||
for (const auto &condKv : v.as_table()) {
|
||||
store_property(condKv.first, condKv.second, k);
|
||||
}
|
||||
} else {
|
||||
store_property(k, v, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.optional("cmake-before", target.cmake_before);
|
||||
t.optional("cmake-after", target.cmake_after);
|
||||
t.optional("include-before", target.include_before);
|
||||
t.optional("include-after", target.include_after);
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
if (checker.contains("template")) {
|
||||
const auto &ts = toml::find(toml, "template").as_table();
|
||||
for (const auto &itr : ts) {
|
||||
auto &t = checker.create(itr.second);
|
||||
const auto &name = itr.first;
|
||||
|
||||
for (const auto &type_name : targetTypeNames) {
|
||||
if (name == type_name) {
|
||||
throw std::runtime_error(format_key_error("Reserved template name '" + name + "'", name, itr.second));
|
||||
}
|
||||
}
|
||||
|
||||
t.optional("cmake-before", target.cmake_before);
|
||||
t.optional("cmake-after", target.cmake_after);
|
||||
t.optional("include-before", target.include_before);
|
||||
t.optional("include-after", target.include_after);
|
||||
for (const auto &tmplate : templates) {
|
||||
if (name == tmplate.outline.name) {
|
||||
throw std::runtime_error(format_key_error("Template '" + name + "' already defined", name, itr.second));
|
||||
}
|
||||
}
|
||||
|
||||
targets.push_back(target);
|
||||
Template tmplate;
|
||||
tmplate.outline = parse_target(name, t, true);
|
||||
|
||||
t.optional("add-function", tmplate.add_function);
|
||||
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_add_function);
|
||||
|
||||
templates.push_back(tmplate);
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("test")) {
|
||||
if (checker.contains("target")) {
|
||||
const auto &ts = toml::find(toml, "target").as_table();
|
||||
for (const auto &itr : ts) {
|
||||
auto &t = checker.create(itr.second);
|
||||
targets.push_back(parse_target(itr.first, t, false));
|
||||
}
|
||||
}
|
||||
|
||||
if (checker.contains("test")) {
|
||||
const auto &ts = toml::find(toml, "test").as_array();
|
||||
for (const auto &value : ts) {
|
||||
auto &t = checker.create(value);
|
||||
@@ -489,7 +614,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("install")) {
|
||||
if (checker.contains("install")) {
|
||||
const auto &is = toml::find(toml, "install").as_array();
|
||||
for (const auto &value : is) {
|
||||
auto &i = checker.create(value);
|
||||
@@ -500,14 +625,17 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
i.optional("dirs", inst.dirs);
|
||||
i.optional("configs", inst.configs);
|
||||
i.required("destination", inst.destination);
|
||||
i.optional("component", inst.component);
|
||||
i.optional("optional", inst.optional);
|
||||
installs.push_back(inst);
|
||||
}
|
||||
}
|
||||
|
||||
if (toml.contains("vcpkg")) {
|
||||
if (checker.contains("vcpkg")) {
|
||||
auto &v = checker.create(toml, "vcpkg");
|
||||
v.optional("url", vcpkg.url);
|
||||
v.optional("version", vcpkg.version);
|
||||
|
||||
for (const auto &p : v.find("packages").as_array()) {
|
||||
Vcpkg::Package package;
|
||||
const auto &package_str = p.as_string().str;
|
||||
@@ -530,7 +658,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
|
||||
}
|
||||
}
|
||||
|
||||
checker.check(conditions);
|
||||
checker.check(conditions, true);
|
||||
}
|
||||
|
||||
bool is_root_path(const std::string &path) {
|
||||
|
||||
Generated
+17
-7
@@ -14,7 +14,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/basic"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -24,7 +24,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/interface"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -34,7 +34,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/fetch-content"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -44,7 +44,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/conditions"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -54,7 +54,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/vcpkg"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -64,7 +64,7 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cxx-standard"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
@@ -74,7 +74,17 @@ add_test(
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/globbing"
|
||||
COMMAND
|
||||
$<TARGET_FILE:cmkr>
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
add_test(
|
||||
NAME
|
||||
templates
|
||||
WORKING_DIRECTORY
|
||||
"${CMAKE_CURRENT_LIST_DIR}/templates"
|
||||
COMMAND
|
||||
"$<TARGET_FILE:cmkr>"
|
||||
build
|
||||
)
|
||||
|
||||
|
||||
+13
-7
@@ -1,41 +1,47 @@
|
||||
[[test]]
|
||||
name = "basic"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "basic"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "interface"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "interface"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "fetch-content"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "fetch-content"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "conditions"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "conditions"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "vcpkg"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "vcpkg"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "cxx-standard"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "cxx-standard"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "globbing"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
working-directory = "globbing"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
[[test]]
|
||||
name = "templates"
|
||||
working-directory = "templates"
|
||||
command = "$<TARGET_FILE:cmkr>"
|
||||
arguments = ["build"]
|
||||
|
||||
@@ -5,6 +5,7 @@ description = "Header-only library"
|
||||
[target.mylib]
|
||||
type = "interface"
|
||||
include-directories = ["include"]
|
||||
compile-features = ["cxx_std_11"]
|
||||
|
||||
[target.example]
|
||||
type = "executable"
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# To avoid repeating yourself in targets you can create your own target type (template). All properties of the template are inherited when used as a target type.
|
||||
|
||||
[project]
|
||||
name = "templates"
|
||||
description = "Target templates"
|
||||
|
||||
[template.app]
|
||||
type = "executable"
|
||||
sources = ["src/templates.cpp"]
|
||||
compile-definitions = ["IS_APP"]
|
||||
|
||||
# Unlike interface targets you can also inherit properties
|
||||
[template.app.properties]
|
||||
CXX_STANDARD = "11"
|
||||
CXX_STANDARD_REQUIRED = true
|
||||
|
||||
[target.app-a]
|
||||
type = "app"
|
||||
compile-definitions = ["APP_A"]
|
||||
|
||||
[target.app-b]
|
||||
type = "app"
|
||||
compile-definitions = ["APP_B"]
|
||||
|
||||
# **Note**: In most cases you probably want to use an [interface](/examples/interface) target instead.
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <cstdio>
|
||||
|
||||
#if !defined(IS_APP)
|
||||
#error Something went wrong with the template
|
||||
#endif // IS_APP
|
||||
|
||||
int main() {
|
||||
#if defined(APP_A)
|
||||
puts("Hello from app A!");
|
||||
#elif defined(APP_B)
|
||||
puts("Hello from app B!");
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user