|
1 | 1 | cmake_minimum_required(VERSION 3.16) |
2 | 2 |
|
3 | | -project(ld-decode-tools) |
4 | | - |
5 | | -# Enforce out-of-source builds |
| 3 | +# Enforce out-of-source builds before project() to catch issues early |
6 | 4 | if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) |
7 | 5 | message(FATAL_ERROR "In-source builds are not allowed. Please create a build directory and run: |
8 | 6 | mkdir build |
9 | 7 | cd build |
10 | 8 | cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..") |
11 | 9 | endif() |
12 | 10 |
|
| 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 | + |
13 | 47 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules") |
14 | 48 | include(CTest) |
15 | 49 |
|
|
0 commit comments