-
Notifications
You must be signed in to change notification settings - Fork 68
/
CMakeLists.txt
270 lines (213 loc) · 7.71 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
add_library(BlitHalSDL STATIC
DefaultMetadata.cpp
File.cpp
Input.cpp
JPEG.cpp
Main.cpp
Multiplayer.cpp
Renderer.cpp
Audio.cpp
System.cpp
)
function (find_sdl_lib lib_name header_name)
string(TOUPPER ${lib_name} VAR_PREFIX)
# new enough SDL_image/_net have cmake config support
find_package(${lib_name} QUIET)
if(TARGET ${lib_name}::${lib_name})
set(${VAR_PREFIX}_LIBRARY ${lib_name}::${lib_name} PARENT_SCOPE)
set(${VAR_PREFIX}_INCLUDE_DIR "" PARENT_SCOPE)
if(WIN32)
get_property(LIB_DLL TARGET ${lib_name}::${lib_name} PROPERTY IMPORTED_LOCATION)
set(${VAR_PREFIX}_DLL ${LIB_DLL} PARENT_SCOPE)
endif()
return()
endif()
message("find_package(${lib_name}) failed, trying manual search...")
find_path(${VAR_PREFIX}_INCLUDE_DIR ${header_name}
HINTS ${SDL2_DIR} ${SDL2_DIR}/../../../
PATH_SUFFIXES SDL2 include/SDL2 include
)
find_library(${VAR_PREFIX}_LIBRARY
NAMES ${lib_name}
HINTS ${SDL2_DIR} ${SDL2_DIR}/../../../
PATH_SUFFIXES lib
)
if(NOT ${VAR_PREFIX}_INCLUDE_DIR OR NOT ${VAR_PREFIX}_LIBRARY)
message(FATAL_ERROR "${lib_name} not found!")
endif()
endfunction()
if(EMSCRIPTEN)
target_compile_options(BlitHalSDL
PRIVATE -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_NET=2
)
set(SDL2_LIBRARIES "-sUSE_SDL=2")
set(SDL2_IMAGE_LIBRARY "-sUSE_SDL_IMAGE=2")
set(SDL2_NET_LIBRARY "-sUSE_SDL_NET=2")
set(EMSCRIPTEN_SHELL ${CMAKE_CURRENT_SOURCE_DIR}/emscripten-shell.html PARENT_SCOPE)
else()
# fallback guess for SDL location on Windows
if(WIN32 AND NOT SDL2_DIR AND 32BLIT_DIR)
set(SDL2_DIR "${32BLIT_DIR}/vs/sdl")
endif()
if(NOT TARGET SDL2::SDL2 AND NOT TARGET SDL2::SDL2-static)
find_package(SDL2 REQUIRED)
endif()
if(SDL2_FRAMEWORK_PATH)
set(SDL2_FRAMEWORK_PATH ${SDL2_FRAMEWORK_PATH} PARENT_SCOPE)
endif()
# If SDL2 was built using CMake, the generated configuration files define SDL2::* targets instead of the SDL2_* variables
if(TARGET SDL2::SDL2)
if(NOT TARGET SDL2::SDL2main)
add_library(SDL2::SDL2main IMPORTED INTERFACE) # 2.24.0 is missing this target for macOS
endif()
set(SDL2_LIBRARIES SDL2::SDL2main SDL2::SDL2)
# handle SDL2 built with only a static library
elseif(TARGET SDL2::SDL2-static)
set(SDL2_LIBRARIES SDL2::SDL2main SDL2::SDL2-static)
else()
target_include_directories(BlitHalSDL
PRIVATE ${SDL2_INCLUDE_DIRS}
)
endif()
# find SDL2_image
find_sdl_lib(SDL2_image SDL_image.h)
find_sdl_lib(SDL2_net SDL_net.h)
target_include_directories(BlitHalSDL
PRIVATE ${SDL2_IMAGE_INCLUDE_DIR} ${SDL2_NET_INCLUDE_DIR}
)
endif()
target_link_libraries(BlitHalSDL PUBLIC BlitEngine ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARY} ${SDL2_NET_LIBRARY})
# copy SDL2 dlls to build/install dir for windows users
set(DLLS_TO_COPY)
if(SDL2_DLL)
list(APPEND DLLS_TO_COPY ${SDL2_DLL})
endif()
if(SDL2_IMAGE_DLL)
list(APPEND DLLS_TO_COPY ${SDL2_IMAGE_DLL})
endif()
if(SDL2_NET_DLL)
list(APPEND DLLS_TO_COPY ${SDL2_NET_DLL})
endif()
set(DLLS_TO_COPY ${DLLS_TO_COPY} PARENT_SCOPE)
# copy once for install
install(FILES ${DLLS_TO_COPY} DESTINATION bin)
if(DEFINED VIDEO_CAPTURE AND VIDEO_CAPTURE)
find_path(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h)
find_library(AVCODEC_LIBRARY avcodec)
find_path(AVFORMAT_INCLUDE_DIR libavformat/avformat.h)
find_library(AVFORMAT_LIBRARY avformat)
find_path(AVUTIL_INCLUDE_DIR libavutil/avutil.h)
find_library(AVUTIL_LIBRARY avutil)
find_library(SWRESAMPLE_LIBRARY swresample)
find_library(SWSCALE_LIBRARY swscale)
target_include_directories(BlitHalSDL PRIVATE ${AVCODEC_INCLUDE_DIR} ${AVFORMAT_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR})
target_link_libraries(BlitHalSDL PUBLIC ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY} ${SWRESAMPLE_LIBRARY} ${SWSCALE_LIBRARY})
target_sources(BlitHalSDL PRIVATE VideoCapture.cpp VideoCaptureFfmpeg.cpp)
target_compile_definitions(BlitHalSDL
PRIVATE
-DVIDEO_CAPTURE
-D__STDC_CONSTANT_MACROS
)
endif()
function(blit_executable NAME SOURCES)
message(STATUS "Processing ${NAME}")
if(ANDROID)
add_library(${NAME} SHARED ${SOURCES} ${ARGN})
else()
add_executable(${NAME} MACOSX_BUNDLE ${SOURCES} ${ARGN})
endif()
install(TARGETS ${NAME}
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
BUNDLE DESTINATION bin
)
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
target_link_libraries(${NAME} BlitHalSDL)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL Darwin)
target_link_libraries(${NAME} -Wl,-force_load BlitHalSDL)
else()
target_link_libraries(${NAME} -Wl,--whole-archive BlitHalSDL -Wl,--no-whole-archive)
endif()
# copy dlls to build dir for dev
foreach(DLL ${DLLS_TO_COPY})
add_custom_command(TARGET ${NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${DLL}"
$<TARGET_FILE_DIR:${NAME}>
)
endforeach()
if(EMSCRIPTEN)
set_target_properties(${NAME} PROPERTIES
SUFFIX ".html"
LINK_FLAGS "-sENVIRONMENT=web -sSDL2_IMAGE_FORMATS=['jpg'] --shell-file ${EMSCRIPTEN_SHELL} -s'EXPORTED_RUNTIME_METHODS=[\"addRunDependency\", \"removeRunDependency\"]' -sTOTAL_STACK=1MB"
LINK_DEPENDS ${EMSCRIPTEN_SHELL}
)
target_link_libraries(${NAME} "-lidbfs.js") # include the persistent IndexedDB-based filesystem
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.js ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.wasm
DESTINATION bin
)
endif()
if(APPLE)
set(SEARCH_DIRS)
# assuming all frameworks installed in the same place
if(SDL2_FRAMEWORK_PATH)
get_filename_component(FW_PARENT_DIR ${SDL2_FRAMEWORK_PATH} DIRECTORY)
list(APPEND SEARCH_DIRS ${FW_PARENT_DIR})
endif()
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fixup.cmake
CONTENT "
include(BundleUtilities)
fixup_bundle(\"\${CMAKE_INSTALL_PREFIX}/bin/$<TARGET_FILE_NAME:${NAME}>.app\" \"\" \"${SEARCH_DIRS}\")
"
)
install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/fixup.cmake)
endif()
endfunction()
function(blit_executable_int_flash NAME SOURCES)
blit_executable(${NAME} ${SOURCES} ${ARGN})
endfunction()
function(blit_metadata TARGET FILE)
if(NOT EXISTS ${FILE})
set(FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
endif()
# cause cmake to reconfigure whenever the asset list changes
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${FILE})
# parse the metadata to variables
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -m ttblit cmake --config ${FILE} --cmake ${CMAKE_CURRENT_BINARY_DIR}/metadata.cmake
RESULT_VARIABLE TOOL_RESULT
)
if(${TOOL_RESULT})
message(FATAL_ERROR "Reading metadata config failed!\n")
endif()
include(${CMAKE_CURRENT_BINARY_DIR}/metadata.cmake)
if(APPLE)
set_target_properties(${TARGET} PROPERTIES
OUTPUT_NAME ${METADATA_TITLE}
MACOSX_BUNDLE_BUNDLE_NAME ${METADATA_TITLE}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${METADATA_VERSION}
MACOSX_BUNDLE_LONG_VERSION_STRING ${METADATA_VERSION}
MACOSX_BUNDLE_COPYRIGHT "${METADATA_AUTHOR}"
MACOSX_BUNDLE_ICON_FILE icon
)
set(ICON ${CMAKE_CURRENT_BINARY_DIR}/icon.icns)
add_custom_command(
OUTPUT ${ICON}
COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && ${PYTHON_EXECUTABLE} -m ttblit metadata --force --config ${FILE} --icns ${ICON}
DEPENDS ${METADATA_DEPENDS} ${FILE}
)
target_sources(${TARGET} PRIVATE ${ICON})
set_source_files_properties(${ICON} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
endif()
string(CONFIGURE "
const char *metadata_title = \"\${METADATA_TITLE}\";
const char *metadata_author = \"\${METADATA_AUTHOR}\";
const char *metadata_description = \"\${METADATA_DESCRIPTION}\";
const char *metadata_version = \"\${METADATA_VERSION}\";
const char *metadata_url = \"\${METADATA_URL}\";
const char *metadata_category = \"\${METADATA_CATEGORY}\";"
METADATA_CONTENT ESCAPE_QUOTES
)
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_metadata.cpp CONTENT "${METADATA_CONTENT}")
target_sources(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_metadata.cpp)
endfunction()