Skip to content

Commit 5b1b8a4

Browse files
committed
Add additional checks into cmake to force out-of-source builds
1 parent 231e1eb commit 5b1b8a4

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

CMakeLists.txt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
cmake_minimum_required(VERSION 3.16)
22

3-
project(ld-decode-tools)
4-
5-
# Enforce out-of-source builds
3+
# Enforce out-of-source builds before project() to catch issues early
64
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
75
message(FATAL_ERROR "In-source builds are not allowed. Please create a build directory and run:
86
mkdir build
97
cd build
108
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..")
119
endif()
1210

11+
# Also prevent building directly in subdirectories of the source tree
12+
# Exception: Allow 'build' and 'build-*' directories as they are gitignored
13+
get_filename_component(REAL_SOURCE_DIR "${CMAKE_SOURCE_DIR}" REALPATH)
14+
get_filename_component(REAL_BINARY_DIR "${CMAKE_BINARY_DIR}" REALPATH)
15+
get_filename_component(BUILD_DIR_NAME "${CMAKE_BINARY_DIR}" NAME)
16+
file(RELATIVE_PATH REL_PATH "${REAL_SOURCE_DIR}" "${REAL_BINARY_DIR}")
17+
18+
# Check if build directory is inside source tree (rel path doesn't start with ..)
19+
# but allow 'build' or 'build-*' directory names
20+
if(NOT REL_PATH MATCHES "^\\.\\." AND
21+
NOT BUILD_DIR_NAME STREQUAL "build" AND
22+
NOT BUILD_DIR_NAME MATCHES "^build-")
23+
message(FATAL_ERROR "Building within the source tree (except 'build/' or 'build-*/') is not allowed.
24+
Build directory: ${REAL_BINARY_DIR}
25+
Source directory: ${REAL_SOURCE_DIR}
26+
27+
Please use one of these approaches:
28+
29+
1. Standard build directory (recommended):
30+
mkdir build
31+
cd build
32+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
33+
34+
2. Named build directory:
35+
mkdir build-release
36+
cd build-release
37+
cmake -DCMAKE_BUILD_TYPE=Release ..
38+
39+
3. Out-of-tree build:
40+
mkdir ../ld-decode-build
41+
cd ../ld-decode-build
42+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ld-decode")
43+
endif()
44+
45+
project(ld-decode-tools)
46+
1347
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
1448
include(CTest)
1549

0 commit comments

Comments
 (0)