-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
100 lines (85 loc) · 3.65 KB
/
CMakeLists.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
cmake_minimum_required(VERSION 3.20)
project(MemSed)
option(MEMSED_VENDORED_SDL3 "Statically build and link SDL3, will not require SDL3 on target system to run" ON)
option(MEMSED_PYTHON_VENV "Use a dedicated python venv instead of system python, requirements are auto-installed" ON)
# Directories
set(MEMSED_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(BUILD_DIR "${MEMSED_DIR}/build")
set(SRC_DIR "${MEMSED_DIR}/src")
set(LIB_DIR "${MEMSED_DIR}/lib")
set(FONTS_DIR "${LIB_DIR}/fonts")
set(VENDOR_DIR "${LIB_DIR}/vendor")
set(RESOURCES_DIR "${MEMSED_DIR}/resources")
set(VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/venv")
set(GLAD_DIR "${LIB_DIR}/glad")
set(GLAD_GEN "${VENDOR_DIR}/glad")
set(SDL3_DIR "${VENDOR_DIR}/sdl3")
set(IMGUI_DIR "${VENDOR_DIR}/imgui")
set(DCIMGUI_DIR "${LIB_DIR}/dcimgui")
set(DEARBINDINGS_DIR "${VENDOR_DIR}/dear_bindings")
# Export compilation DB
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(CREATE_LINK "${CMAKE_CURRENT_BINARY_DIR}" "${BUILD_DIR}/current" SYMBOLIC)
# Global compiler flags
add_compile_options(-fexceptions) # Interop with C++ exceptions
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-O0) # Better debugging experience
endif()
add_compile_options(-Werror) # Fail on any warning
# Global includes
# Allows #include <dcimgui/dcimgui.h> instead of just #include <dcimgui.h>
include_directories("${LIB_DIR}" "${VENDOR_DIR}")
# Main sources
# Globbing is discouraged but fine when keeping in mind its caveat:
# Adding new files won't be detected, need to run cmake again to detect them
# You can also `touch CMakeLists.txt`, or save it without making changes
file(GLOB_RECURSE sources "${SRC_DIR}/*.c" "${SRC_DIR}/*.h")
# Main executable
add_executable(memsed "${sources}")
# Compiler flags
target_compile_options(memsed PRIVATE -std=gnu2x) # Standard
target_compile_options(memsed PRIVATE -Wall -Wextra -Wstrict-prototypes -Wredundant-decls -Wundef) # Warnings
target_compile_options(memsed PRIVATE -fdata-sections -ffunction-sections -fno-math-errno) # Optimizations
# Python (needed for GLAD and dear_bindings)
find_package(Python COMPONENTS Interpreter REQUIRED)
if(MEMSED_PYTHON_VENV)
execute_process(COMMAND "${Python_EXECUTABLE}" -m venv "${VENV_DIR}")
set(Python_EXECUTABLE "${VENV_DIR}/bin/python")
execute_process(COMMAND "${Python_EXECUTABLE}" -m pip install -U -r "${MEMSED_DIR}/requirements.txt")
configure_file("${MEMSED_DIR}/requirements.txt" "${VENV_DIR}/.installed")
endif()
# OpenGL (via GLAD loader)
add_subdirectory("${GLAD_GEN}/cmake")
glad_add_library(glad STATIC EXCLUDE_FROM_ALL MERGE REPRODUCIBLE API gl:core=3.0 LOCATION "${GLAD_DIR}")
target_link_libraries(memsed PRIVATE glad)
# SDL3
if(MEMSED_VENDORED_SDL3)
set(SDL_STATIC ON)
add_subdirectory("${SDL3_DIR}" EXCLUDE_FROM_ALL)
else()
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
endif()
target_link_libraries(memsed PRIVATE SDL3::SDL3)
# ImGui (via dcimgui bindings)
set(DCIMGUI_BACKENDS sdl3 opengl3)
add_subdirectory("${DCIMGUI_DIR}")
target_link_libraries(dcimgui PRIVATE SDL3::SDL3)
target_link_libraries(dcimgui PRIVATE glad)
target_link_libraries(memsed PRIVATE dcimgui)
# Fonts (generate compressed C sources)
add_subdirectory("${FONTS_DIR}")
target_link_libraries(memsed PRIVATE fonts)
# Build as (almost) static executable
# Adding -static causes a segfault in SDL_CreateWindow()
target_link_libraries(memsed PRIVATE -static-libgcc -static-libstdc++)
# Add run targets
add_custom_target(run
COMMAND sudo ./memsed
DEPENDS memsed
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_custom_target(gdb
COMMAND sudo gdb -q ./memsed
DEPENDS memsed
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)