-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
222 lines (190 loc) · 7.08 KB
/
CMakeLists.txt
File metadata and controls
222 lines (190 loc) · 7.08 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
cmake_minimum_required(VERSION 3.27)
##
# @brief Main project configuration for the OpenGL_Course_Project.
# Uses separate interface libraries for compile/link flags and
# optionally applies static linking for system libraries on both
# GCC/Clang and MSVC compilers.
#
project(OpenGL_Course_Project VERSION 1.0.0)
##
# @brief Appends a debug postfix (for example, "xxxd.exe" in Debug mode).
#
set(CMAKE_DEBUG_POSTFIX d)
##
# @brief Option to link system libraries (runtime) statically or dynamically.
# If set to ON, static linking will be used for both MSVC and GCC/Clang.
#
option(STATIC_SYSTEM_LINK "Link system libraries (runtime) statically" ON)
##
# @brief Separate interface libraries to manage compile-time and link-time flags.
#
add_library(project_compile_flags INTERFACE)
add_library(project_link_flags INTERFACE)
##
# @brief Enforce C++20 features for all targets that link against project_compile_flags.
#
target_compile_features(project_compile_flags INTERFACE cxx_std_23)
##
# @brief Generator expressions to detect the compiler family for warning flags, etc.
# gcc_like_cxx = ARMClang, AppleClang, Clang, GNU, LCC
# msvc_like_cxx = MSVC
#
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
##
# @brief Additional expression to check for "real" GNU (GCC) only.
# We only apply -static-libgcc/-static-libstdc++ under actual GCC, not Clang.
#
set(real_gcc "$<STREQUAL:$<CXX_COMPILER_ID>,GNU>")
##
# @brief Common compile options:
# - For GCC/Clang: add common warning flags (-Wall, -Wextra, etc.).
# - For MSVC: add default warning level (/W3).
# - For real GCC only: -static-libgcc, -static-libstdc++.
# - For Clang only: -static-libc++ -static-libgcc (if using libc++).
# - For MSVC static runtime, /MT or /MTd must be specified at compile time.
#
target_compile_options(project_compile_flags
INTERFACE
# GCC/Clang warning flags
$<$<AND:${gcc_like_cxx},$<CONFIG:Debug>>:$<BUILD_INTERFACE:-Wall -Wextra -Wshadow -Wformat=2 -Wunused>>
$<$<AND:${gcc_like_cxx},$<CONFIG:Release>>:$<BUILD_INTERFACE:-O3>>
# Example optimization for Release builds under GCC/Clang
# $<$<AND:${gcc_like_cxx},$<CONFIG:Release>>:-O2>
# MSVC warning flags
$<$<AND:${msvc_like_cxx}>:$<BUILD_INTERFACE:-W3>>
# Only in real GCC + STATIC_SYSTEM_LINK=ON do we add -static-libgcc / -static-libstdc++
$<$<AND:${real_gcc},$<BOOL:${STATIC_SYSTEM_LINK}>>:-static-libgcc -static-libstdc++>
# MSVC static runtime for Debug/Release
#$<$<AND:${msvc_like_cxx},$<BOOL:${STATIC_SYSTEM_LINK}>,$<CONFIG:Debug>>:/MTd>
#$<$<AND:${msvc_like_cxx},$<BOOL:${STATIC_SYSTEM_LINK}>,$<CONFIG:Release>>:/MT>
)
target_compile_definitions(project_compile_flags
INTERFACE
$<$<AND:${gcc_like_cxx},$<CONFIG:Debug>>:"NDEBUG">
)
##
# @brief Common link options:
# - For GCC/Clang: use -static for fully static linking.
# - For Clang, also apply -static if desired.
# - For MSVC: /MT or /MTd at link stage, consistent with compile stage.
#
target_link_options(project_link_flags
INTERFACE
# GCC/Clang static linking (Clang generally accepts -static, so no warning here)
$<$<AND:${gcc_like_cxx},$<BOOL:${STATIC_SYSTEM_LINK}>>:-static>
# MSVC static linking for Debug/Release
#$<$<AND:${msvc_like_cxx},$<BOOL:${STATIC_SYSTEM_LINK}>,$<CONFIG:Debug>>:/MTd>
#$<$<AND:${msvc_like_cxx},$<BOOL:${STATIC_SYSTEM_LINK}>,$<CONFIG:Release>>:/MT>
)
##
# @brief Collect and copy asset files into the output directory.
#
file(GLOB ASSET_FILES
"${CMAKE_SOURCE_DIR}/assets/*"
"${CMAKE_SOURCE_DIR}/assets/**/*"
)
##
# @brief Custom command to remove old bin folder, create a new one, copy all assets,
# and then move them up one level (removing the intermediate folder).
#
add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/.copied_dir"
DEPENDS ${ASSET_FILES}
COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/bin"
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/bin"
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CMAKE_SOURCE_DIR}/assets"
"${CMAKE_BINARY_DIR}/bin/assets"
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CMAKE_BINARY_DIR}/bin/assets"
"${CMAKE_BINARY_DIR}/bin"
COMMAND ${CMAKE_COMMAND} -E remove_directory
"${CMAKE_BINARY_DIR}/bin/assets"
COMMAND ${CMAKE_COMMAND} -E touch
"${CMAKE_BINARY_DIR}/.copied_dir"
COMMENT "Copy assets/* to out/ (build-time) skipping top-level directory"
)
##
# @brief Custom target to ensure the copy_assets step runs at build time.
#
add_custom_target(
copy_assets ALL
DEPENDS "${CMAKE_BINARY_DIR}/.copied_dir"
)
# Fetch conan downloaded packages
find_package(glad REQUIRED)
find_package(glfw3 REQUIRED)
find_package(opengl_system REQUIRED)
find_package(glm REQUIRED)
find_package(assimp REQUIRED)
find_package(stb REQUIRED)
##
# @brief Add subdirectory for imgui sources.
#
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/imgui")
##
# @brief Main application executable.
#
add_executable(
${CMAKE_PROJECT_NAME}
"${CMAKE_CURRENT_SOURCE_DIR}/src/Source.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/learnopengl/custom_helper.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/learnopengl/ddsloader.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/stb_image_define.cpp"
)
#target_include_directories(
# ${CMAKE_PROJECT_NAME}
# PRIVATE
# "${CMAKE_CURRENT_SOURCE_DIR}/include"
#)
target_include_directories(
${CMAKE_PROJECT_NAME}
PRIVATE
${glad_INCLUDE_DIRS}
${glfw3_INCLUDE_DIRS}
${glm_INCLUDE_DIRS}
${stb_INCLUDE_DIRS}
${assimp_INCLUDE_DIRS}
)
##
# @brief Link libraries and interface flags to the main project executable.
#
target_link_libraries(
${CMAKE_PROJECT_NAME}
PRIVATE
project_compile_flags
project_link_flags
imgui
glad::glad glfw opengl::opengl glm::glm assimp::assimp stb::stb
)
##
# @brief Set output directories for Debug and Release builds.
#
set_target_properties(
${CMAKE_PROJECT_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY_Debug "${CMAKE_CURRENT_BINARY_DIR}/bin/Debug"
RUNTIME_OUTPUT_DIRECTORY_Release "${CMAKE_CURRENT_BINARY_DIR}/bin/Release"
)
##
# @brief Find the Doxygen package (required).
#
find_package(Doxygen)
if (DOXYGEN_FOUND)
message("Doxygen Found!")
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in
${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
@ONLY
)
make_directory("${CMAKE_CURRENT_BINARY_DIR}/doxygen")
doxygen_add_docs(
doxygen
ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generate Doxygen files."
CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile"
)
endif()