diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bc5b9d..518387c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,10 @@ cmake_minimum_required(VERSION 2.8...3.8) +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) + message(FATAL_ERROR "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build") +endif() + # Regenerate CMakeLists.txt automatically in the root project set(CMKR_ROOT_PROJECT OFF) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) diff --git a/include/project_parser.hpp b/include/project_parser.hpp index 376322b..e86cd92 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -126,6 +126,7 @@ struct Project { std::string build_dir = "build"; std::string generator; std::string config; + bool allow_in_tree = false; Condition> project_subdirs; std::vector cppflags; std::vector cflags; diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index acb3a29..b7ff456 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -464,6 +464,14 @@ int generate_cmake(const char *path, bool root) { if (root) { cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl(); + if (!project.allow_in_tree) { + // clang-format off + cmd("if")("CMAKE_SOURCE_DIR", "STREQUAL", "CMAKE_BINARY_DIR"); + cmd("message")("FATAL_ERROR", "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build"); + cmd("endif")().endl(); + // clang-format on + } + comment("Regenerate CMakeLists.txt automatically in the root project"); cmd("set")("CMKR_ROOT_PROJECT", "OFF"); // clang-format off diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 882f6f4..9750c8a 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -79,6 +79,7 @@ Project::Project(const std::string &path, bool build) { get_optional(cmake, "generator", generator); get_optional(cmake, "config", config); get_optional(cmake, "arguments", gen_args); + get_optional(cmake, "allow-in-tree", allow_in_tree); } } else { if (toml.contains("cmake")) {