Merge pull request #101 from build-cpp/minor-cleanup

Cleanup
main
Duncan Ogilvie 2 years ago committed by GitHub
commit 240fafc102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -72,7 +72,7 @@ PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60 PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right PointerAlignment: Right
ReflowComments: true ReflowComments: true
SortIncludes: true SortIncludes: false
SortUsingDeclarations: true SortUsingDeclarations: true
SpaceAfterCStyleCast: false SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true SpaceAfterTemplateKeyword: true

@ -10,13 +10,20 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [windows-2019, macos-10.15, ubuntu-20.04] os: [windows-2022, macos-11, ubuntu-20.04]
env: env:
BUILD_TYPE: Release BUILD_TYPE: 'Release'
CMAKE_GENERATOR: 'Ninja'
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Install Ninja
uses: seanmiddleditch/gha-setup-ninja@6263846cf3c17009dfc81604efabae16044fc074 # master
- name: Visual Studio Development Environment
uses: ilammy/msvc-dev-cmd@cec98b9d092141f74527d0afa6feb2af698cfe89 # v1.12.1
- name: Tag cmkr.cmake - name: Tag cmkr.cmake
if: ${{ startsWith(github.ref, 'refs/tags/') }} if: ${{ startsWith(github.ref, 'refs/tags/') }}
run: cmake -P "cmake/replace_tag.cmake" run: cmake -P "cmake/replace_tag.cmake"

@ -0,0 +1,27 @@
name: lint
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: clang-format
id: clang-format
uses: DoozyX/clang-format-lint-action@c3b2c943e924028b93a707a5b1b017976ab8d50c # v0.15
with:
exclude: './third_party'
extensions: 'c,h,cpp,hpp'
clangFormatVersion: 15
style: file
- name: clang-format instructions
if: ${{ failure() && steps.clang-format.outcome == 'failure' }}
run: |
# Instructions for fixing the formatting errors
echo -e "\n\033[0;31mTo fix the formatting, run:\nclang-format -style=file -i \$(git ls-files \"*.c\" \"*.h\" \"*.cpp\" \"*.hpp\")\033[0m\n"
exit 1

3
.gitignore vendored

@ -9,4 +9,5 @@ build*/
.idea/ .idea/
cmake-build*/ cmake-build*/
CMakeLists.txt.user CMakeLists.txt.user
.vscode/ .vscode/
.DS_Store

2
CMakeLists.txt generated

@ -61,14 +61,12 @@ set(cmkr_SOURCES
"src/arguments.cpp" "src/arguments.cpp"
"src/build.cpp" "src/build.cpp"
"src/cmake_generator.cpp" "src/cmake_generator.cpp"
"src/error.cpp"
"src/help.cpp" "src/help.cpp"
"src/main.cpp" "src/main.cpp"
"src/project_parser.cpp" "src/project_parser.cpp"
"include/arguments.hpp" "include/arguments.hpp"
"include/build.hpp" "include/build.hpp"
"include/cmake_generator.hpp" "include/cmake_generator.hpp"
"include/error.hpp"
"include/fs.hpp" "include/fs.hpp"
"include/help.hpp" "include/help.hpp"
"include/literals.hpp" "include/literals.hpp"

@ -11,9 +11,3 @@ int install();
} // namespace build } // namespace build
} // namespace cmkr } // namespace cmkr
int cmkr_build_run(int argc, char **argv);
int cmkr_build_clean();
int cmkr_build_install();

@ -1,27 +0,0 @@
#pragma once
namespace cmkr {
namespace error {
struct Status {
enum class Code {
Success = 0,
RuntimeError,
InitError,
GenerationError,
BuildError,
CleanError,
InstallError,
};
Status(Code ec) noexcept;
operator int() const noexcept;
Code code() const noexcept;
private:
Code ec_ = Code::Success;
};
} // namespace error
} // namespace cmkr
const char *cmkr_error_status_string(int);

@ -1,8 +1,6 @@
#pragma once #pragma once
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || \ #if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || (defined(__cplusplus) && __cplusplus >= 201703L)) && defined(__has_include)
(defined(__cplusplus) && __cplusplus >= 201703L)) && \
defined(__has_include)
#if __has_include(<filesystem>) && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500) #if __has_include(<filesystem>) && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500)
#define GHC_USE_STD_FS #define GHC_USE_STD_FS
#include <filesystem> #include <filesystem>

@ -9,7 +9,3 @@ const char *message() noexcept;
} // namespace help } // namespace help
} // namespace cmkr } // namespace cmkr
const char *cmkr_help_version(void);
const char *cmkr_help_message(void);

@ -1,10 +1,11 @@
#pragma once #pragma once
#include <mpark/variant.hpp> #include <vector>
#include <string> #include <string>
#include <mpark/variant.hpp>
#include <tsl/ordered_map.h> #include <tsl/ordered_map.h>
#include <tsl/ordered_set.h> #include <tsl/ordered_set.h>
#include <vector>
namespace cmkr { namespace cmkr {
namespace parser { namespace parser {
@ -47,6 +48,10 @@ struct Vcpkg {
}; };
std::vector<Package> packages; std::vector<Package> packages;
bool enabled() const {
return !packages.empty();
}
}; };
enum TargetType { enum TargetType {

@ -2,10 +2,8 @@
#include "build.hpp" #include "build.hpp"
#include "cmake_generator.hpp" #include "cmake_generator.hpp"
#include "help.hpp" #include "help.hpp"
#include "fs.hpp" #include "fs.hpp"
#include <exception>
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>

@ -1,14 +1,10 @@
#include "build.hpp" #include "build.hpp"
#include "cmake_generator.hpp" #include "cmake_generator.hpp"
#include "error.hpp"
#include "project_parser.hpp" #include "project_parser.hpp"
#include "fs.hpp" #include "fs.hpp"
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <sstream> #include <sstream>
#include <stdexcept>
#include <system_error>
namespace cmkr { namespace cmkr {
namespace build { namespace build {
@ -17,7 +13,7 @@ int run(int argc, char **argv) {
parser::Project project(nullptr, ".", true); parser::Project project(nullptr, ".", true);
if (argc > 2) { if (argc > 2) {
for (int i = 2; i < argc; ++i) { for (int i = 2; i < argc; ++i) {
project.build_args.push_back(argv[i]); project.build_args.emplace_back(argv[i]);
} }
} }
std::stringstream ss; std::stringstream ss;
@ -48,13 +44,13 @@ int run(int argc, char **argv) {
} }
int clean() { int clean() {
bool ret = false; bool success = false;
parser::Project project(nullptr, ".", true); parser::Project project(nullptr, ".", true);
if (fs::exists(project.build_dir)) { if (fs::exists(project.build_dir)) {
ret = fs::remove_all(project.build_dir); success = fs::remove_all(project.build_dir);
fs::create_directory(project.build_dir); fs::create_directory(project.build_dir);
} }
return !ret; return success ? EXIT_SUCCESS : EXIT_FAILURE;
} }
int install() { int install() {
@ -64,33 +60,3 @@ int install() {
} }
} // namespace build } // namespace build
} // namespace cmkr } // namespace cmkr
int cmkr_build_run(int argc, char **argv) {
try {
return cmkr::build::run(argc, argv);
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::BuildError);
}
}
int cmkr_build_clean(void) {
try {
return cmkr::build::clean();
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::CleanError);
}
}
int cmkr_build_install(void) {
try {
return cmkr::build::install();
} catch (const std::system_error &e) {
return e.code().value();
} catch (...) {
return cmkr::error::Status(cmkr::error::Status::Code::InstallError);
}
}

@ -1,15 +1,14 @@
#include "cmake_generator.hpp" #include "cmake_generator.hpp"
#include "error.hpp"
#include "literals.hpp" #include "literals.hpp"
#include <resources/cmkr.hpp> #include <resources/cmkr.hpp>
#include "fs.hpp" #include "fs.hpp"
#include "project_parser.hpp" #include "project_parser.hpp"
#include <cstdio> #include <cstdio>
#include <fstream>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <fstream>
namespace cmkr { namespace cmkr {
namespace gen { namespace gen {
@ -43,7 +42,7 @@ static tsl::ordered_map<std::string, std::vector<std::string>> known_languages =
{"Swift", {".swift"}}, {"Swift", {".swift"}},
}; };
static std::string format(const char *format, tsl::ordered_map<std::string, std::string> variables) { static std::string format(const char *format, const tsl::ordered_map<std::string, std::string> &variables) {
std::string s = format; std::string s = format;
for (const auto &itr : variables) { for (const auto &itr : variables) {
size_t start_pos = 0; size_t start_pos = 0;
@ -249,7 +248,7 @@ void generate_project(const std::string &type) {
struct CommandEndl { struct CommandEndl {
std::stringstream &ss; std::stringstream &ss;
CommandEndl(std::stringstream &ss) : ss(ss) { explicit CommandEndl(std::stringstream &ss) : ss(ss) {
} }
void endl() { void endl() {
ss << '\n'; ss << '\n';
@ -258,7 +257,7 @@ struct CommandEndl {
struct RawArg { struct RawArg {
RawArg() = default; RawArg() = default;
RawArg(std::string arg) : arg(std::move(arg)) { explicit RawArg(std::string arg) : arg(std::move(arg)) {
} }
std::string arg; std::string arg;
@ -293,7 +292,7 @@ struct Command {
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#unquoted-argument // 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 // 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. // previously, so for backwards compatibility its still here.
if (str.find_first_of("()#\"\\'> |/;") == str.npos) if (str.find_first_of("()#\"\\'> |/;") == std::string::npos)
return str; return str;
std::string result; std::string result;
result += "\""; result += "\"";
@ -442,10 +441,10 @@ static std::string tolf(const std::string &str) {
} }
} }
return result; return result;
}; }
struct Generator { struct Generator {
Generator(const parser::Project &project, const fs::path &path) : project(project), path(path) { Generator(const parser::Project &project, fs::path path) : project(project), path(std::move(path)) {
} }
Generator(const Generator &) = delete; Generator(const Generator &) = delete;
@ -786,7 +785,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
gen.conditional_includes(project.include_after); gen.conditional_includes(project.include_after);
gen.conditional_cmake(project.cmake_after); gen.conditional_cmake(project.cmake_after);
if (!project.vcpkg.packages.empty()) { if (project.vcpkg.enabled()) {
if (!is_root_project) {
throw std::runtime_error("[vcpkg] is only supported in the root project");
}
// Allow the user to specify a url or derive it from the version // Allow the user to specify a url or derive it from the version
auto url = project.vcpkg.url; auto url = project.vcpkg.url;
auto version_name = url; auto version_name = url;
@ -799,7 +802,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
} }
// Show a nicer error than vcpkg when specifying an invalid package name // Show a nicer error than vcpkg when specifying an invalid package name
for (const auto &package : project.vcpkg.packages) { const auto &packages = project.vcpkg.packages;
for (const auto &package : packages) {
if (!vcpkg_valid_identifier(package.name)) { if (!vcpkg_valid_identifier(package.name)) {
throw std::runtime_error("Invalid [vcpkg].packages name '" + package.name + "' (needs to be lowercase alphanumeric)"); throw std::runtime_error("Invalid [vcpkg].packages name '" + package.name + "' (needs to be lowercase alphanumeric)");
} }
@ -809,14 +813,20 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
// clang-format off // clang-format off
cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG"); cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG");
cmd("include")("FetchContent"); cmd("include")("FetchContent");
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")..."); comment("Fix warnings about DOWNLOAD_EXTRACT_TIMESTAMP");
cmd("FetchContent_Declare")("vcpkg", "URL", url); // clang-format off
// Not using FetchContent_MakeAvailable here in case vcpkg adds CMakeLists.txt cmd("if")("POLICY", "CMP0135");
cmd("FetchContent_GetProperties")("vcpkg"); cmd("cmake_policy")("SET", "CMP0135", "NEW");
cmd("if")("NOT", "vcpkg_POPULATED");
cmd("FetchContent_Populate")("vcpkg");
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
cmd("endif")(); cmd("endif")();
// clang-format on
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")...");
cmd("FetchContent_Declare")("vcpkg", "URL", url);
// 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")(); cmd("endif")();
endl(); endl();
// clang-format on // clang-format on
@ -833,7 +843,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
"dependencies": [ "dependencies": [
)"; )";
const auto &packages = project.vcpkg.packages;
for (size_t i = 0; i < packages.size(); i++) { for (size_t i = 0; i < packages.size(); i++) {
const auto &package = packages[i]; const auto &package = packages[i];
const auto &features = package.features; const auto &features = package.features;
@ -887,13 +896,21 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
if (!project.contents.empty()) { if (!project.contents.empty()) {
cmd("include")("FetchContent").endl(); cmd("include")("FetchContent").endl();
if (!project.root()->vcpkg.enabled()) {
comment("Fix warnings about DOWNLOAD_EXTRACT_TIMESTAMP");
// clang-format off
cmd("if")("POLICY", "CMP0135");
cmd("cmake_policy")("SET", "CMP0135", "NEW");
cmd("endif")();
// clang-format on
}
for (const auto &content : project.contents) { for (const auto &content : project.contents) {
ConditionScope cs(gen, content.condition); ConditionScope cs(gen, content.condition);
gen.conditional_includes(content.include_before); gen.conditional_includes(content.include_before);
gen.conditional_cmake(content.cmake_before); gen.conditional_cmake(content.cmake_before);
std::string version_info = ""; std::string version_info;
if (content.arguments.contains("GIT_TAG")) { if (content.arguments.contains("GIT_TAG")) {
version_info = " (" + content.arguments.at("GIT_TAG") + ")"; version_info = " (" + content.arguments.at("GIT_TAG") + ")";
} else if (content.arguments.contains("SVN_REVISION")) { } else if (content.arguments.contains("SVN_REVISION")) {

@ -1,31 +0,0 @@
#include "error.hpp"
#include <cassert>
#include <cstddef>
namespace cmkr {
namespace error {
Status::Status(Code ec) noexcept : ec_(ec) {
}
Status::operator int() const noexcept {
return static_cast<int>(ec_);
}
Status::Code Status::code() const noexcept {
return ec_;
}
} // namespace error
} // namespace cmkr
// strings for cmkr::error::Status::Code
static const char *err_string[] = {
"Success", "Runtime error", "Initialization error", "CMake generation error", "Build error", "Clean error", "Install error",
};
const char *cmkr_error_status(int i) {
assert(i >= 0 && static_cast<size_t>(i) < (sizeof(err_string) / sizeof(*(err_string))));
return err_string[i];
}

@ -23,11 +23,3 @@ arguments:
} }
} // namespace help } // namespace help
} // namespace cmkr } // namespace cmkr
const char *cmkr_help_version(void) {
return cmkr::help::version();
}
const char *cmkr_help_message(void) {
return cmkr::help::message();
}

@ -36,7 +36,7 @@ static std::string format_key_message(const std::string &message, const toml::ke
auto loc = value.location(); auto loc = value.location();
auto line_number_str = std::to_string(loc.line()); auto line_number_str = std::to_string(loc.line());
auto line_width = line_number_str.length(); auto line_width = line_number_str.length();
auto line_str = loc.line_str(); const auto &line_str = loc.line_str();
std::ostringstream oss; std::ostringstream oss;
oss << message << "\n"; oss << message << "\n";
@ -73,7 +73,7 @@ class TomlChecker {
public: public:
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) { TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {
} }
TomlChecker(const TomlBasicValue &v) : m_v(v) { explicit TomlChecker(const TomlBasicValue &v) : m_v(v) {
} }
TomlChecker(const TomlChecker &) = delete; TomlChecker(const TomlChecker &) = delete;
TomlChecker(TomlChecker &&) = delete; TomlChecker(TomlChecker &&) = delete;
@ -175,7 +175,7 @@ class TomlCheckerRoot {
bool m_checked = false; bool m_checked = false;
public: public:
TomlCheckerRoot(const TomlBasicValue &root) : m_root(root) { explicit TomlCheckerRoot(const TomlBasicValue &root) : m_root(root) {
} }
TomlCheckerRoot(const TomlCheckerRoot &) = delete; TomlCheckerRoot(const TomlCheckerRoot &) = delete;
TomlCheckerRoot(TomlCheckerRoot &&) = delete; TomlCheckerRoot(TomlCheckerRoot &&) = delete;

@ -51,4 +51,4 @@ condition = "msvc"
name = "msvc-runtime" name = "msvc-runtime"
working-directory = "msvc-runtime" working-directory = "msvc-runtime"
command = "$<TARGET_FILE:cmkr>" command = "$<TARGET_FILE:cmkr>"
arguments = ["build"] arguments = ["build"]

@ -1 +1,2 @@
int main() { } int main() {
}

@ -1,3 +1,4 @@
#include <Windows.h> #include <Windows.h>
void foo() { } void foo() {
}

@ -1,8 +1,7 @@
#include <cstdio> #include <cstdio>
#include <tuple> #include <tuple>
int main() int main() {
{
auto tpl = std::make_tuple(1, 2); auto tpl = std::make_tuple(1, 2);
printf("Hello from C++11 %d\n", std::get<0>(tpl)); printf("Hello from C++11 %d\n", std::get<0>(tpl));
} }

@ -1,6 +1,5 @@
#include <fmt/core.h> #include <fmt/core.h>
int main() int main() {
{
fmt::print("Hello, world!\n"); fmt::print("Hello, world!\n");
} }

@ -2,7 +2,6 @@
#include <string> #include <string>
namespace mylib namespace mylib {
{
std::string message(); std::string message();
} }

@ -1,6 +1,5 @@
#include <mylib/mylib.hpp> #include <mylib/mylib.hpp>
std::string mylib::message() std::string mylib::message() {
{
return "cmkr is awesome!"; return "cmkr is awesome!";
} }

@ -1,4 +1,5 @@
namespace mylib namespace mylib {
{ static const char *version() {
static const char* version() { return "v1.0"; } return "v1.0";
}
} // namespace mylib } // namespace mylib

@ -2,7 +2,6 @@
#include "mylib/mylib.hpp" #include "mylib/mylib.hpp"
int main() int main() {
{
printf("mylib version: %s\n", mylib::version()); printf("mylib version: %s\n", mylib::version());
} }

@ -1,6 +1,5 @@
#include <fmt/core.h> #include <fmt/core.h>
int main() int main() {
{
fmt::print("Hello, world!\n"); fmt::print("Hello, world!\n");
} }

@ -0,0 +1,3 @@
# Disable clang-format in this folder
DisableFormat: true
SortIncludes: Never
Loading…
Cancel
Save