-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
71 lines (57 loc) · 2.62 KB
/
Copy pathCMakeLists.txt
File metadata and controls
71 lines (57 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
cmake_minimum_required(VERSION 3.10)
# Projektname dynamisch aus dem Ordnernamen ableiten
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${PROJECT_NAME} C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: Debug Release." FORCE)
endif()
# Compiler warnings and build-type specific flags (for GCC/Clang)
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-Wall -Wextra)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Debug build: Enabling sanitizers.")
add_compile_options(-fsanitize=address,leak,undefined -fno-omit-frame-pointer -g)
add_link_options(-fsanitize=address,leak,undefined)
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
message(STATUS "Release build: Enabling optimizations.")
add_compile_options(-O3 -DNDEBUG)
endif()
endif()
# Alle C-Dateien im Spielsrc sammeln
file(GLOB_RECURSE SOURCES "src/*.c")
list(FILTER SOURCES EXCLUDE REGEX "src/main\\.c$")
# --- Objekt-Bibliotheken erstellen ---
# Für jede Quelldatei eine eigene "OBJECT"-Bibliothek erstellen.
set(PROJECT_OBJECTS "")
foreach(SOURCE_FILE ${SOURCES})
# Erstelle einen Namen relativ zum Projektverzeichnis, z.B. "src/core/widget.c"
# Erstelle einen eindeutigen Namen für die Objekt-Bibliothek, z.B. aus "src/core/widget.c" wird "obj_core_widget"
string(REPLACE "/" "_" LIB_NAME_RAW ${SOURCE_FILE})
string(REPLACE ".c" "" LIB_NAME_RAW ${LIB_NAME_RAW})
set(LIB_NAME "obj_${LIB_NAME_RAW}")
add_library(${LIB_NAME} OBJECT ${SOURCE_FILE})
# Wichtig: Jede Objekt-Bibliothek braucht die richtigen Include-Pfade.
# PUBLIC sorgt dafür, dass der Pfad sowohl für die Kompilierung des Objekts selbst als auch für alle Ziele, die es verwenden, gilt.
target_include_directories(${LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
list(APPEND PROJECT_OBJECTS ${LIB_NAME})
endforeach()
# --- Haupt-Executable erstellen ---
add_executable(${PROJECT_NAME} src/main.c)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_OBJECTS})
add_compile_definitions(_XOPEN_SOURCE=700)
# --- symlink zum data Ordner erstellen
execute_process(
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_SOURCE_DIR}/data
${CMAKE_BINARY_DIR}/data
)
# CTest-Unterstützung aktivieren
enable_testing()
# --- Add tools ---
add_subdirectory(tools)
# --- Add tests ---
add_subdirectory(tests)