-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
320 lines (264 loc) · 11.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
cmake_minimum_required(VERSION 3.12)
if(POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif()
function(_arp_disable_warnings target)
if(NOT TARGET "${target}")
message(WARNING "Target '${target}' does not exist")
return()
endif()
get_target_property(TARGET_COMPILER_FLAGS "${target}" COMPILE_OPTIONS)
if(TARGET_COMPILER_FLAGS)
# remove any present warning flags
if(MSVC)
list(FILTER TARGET_COMPILER_FLAGS EXCLUDE REGEX "^(/W[0-4])|(\\$<\\$<(.*)>:/W[0-4]>)$")
elseif(GCC OR CLANG)
list(FILTER TARGET_COMPILER_FLAGS EXCLUDE REGEX "^(-W)|(\\$<\\$<(.*)>:-W(.*)>$)")
else()
message(WARNING "Unknown compiler, unable to disable warnings for dependencies")
return()
endif()
# assign the modified flags back to the target
set_target_properties("${target}" PROPERTIES COMPILE_OPTIONS "${TARGET_COMPILER_FLAGS}")
endif()
# add flag to disable all warnings
if(MSVC)
target_compile_options(${target} PRIVATE "/W0")
elseif(GCC OR CLANG)
target_compile_options(${target} PRIVATE "-w")
endif()
endfunction()
project(arp VERSION 1.0.0
LANGUAGES C)
if("${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" STREQUAL "10.0.17763.0")
message(FATAL_ERROR "Compilation against Windows SDK version 10.0.17763 is broken due to a bug in the SDK headers. \
Please use a different SDK version.")
endif()
set(LIBARP_VERSION_SUFFIX "-dev")
set(LIBARP_VERSION "${CMAKE_PROJECT_VERSION}${LIBARP_VERSION_SUFFIX}" CACHE STRING "" FORCE)
set(LIBRARY_BASE_NAME arp)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
if(WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_SHARED_MODULE_PREFIX "")
set(CMAKE_IMPORT_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_IMPORT_LIBRARY_SUFFIX ".dll.lib")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
endif()
set(LIBARP_USER_MAPPINGS "" CACHE STRING "Path to user-supplied media type mappings")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(DEF_USE_SYSTEM_ZLIB OFF)
else()
set(DEF_USE_SYSTEM_ZLIB ON)
endif()
option(LIBARP_FEATURE_PACK "Compile with support for packing data" ON)
option(LIBARP_FEATURE_UNPACK "Compile with support for unpacking data" ON)
option(LIBARP_FEATURE_DEFLATE "Compile with support for DEFLATE compression" ON)
option(USE_SYSTEM_ZLIB "Use system-provided zlib library and headers" "${DEF_USE_SYSTEM_ZLIB}")
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed.")
endif()
set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(IS_TARGET_X86 0)
if(MSVC AND CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64")
set(IS_TARGET_X86 1)
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
set(IS_TARGET_X86 1)
endif()
# include dir for generated headers which must be copied (configs)
set(TMP_INCLUDE_DIR "${PROJECT_BINARY_DIR}/include.tmp")
set(EXT_INCLUDE_DIRS "")
set(EXT_LIBS "")
set(EXT_LIBS_STATIC "")
set(EXT_DEPS "")
set(EXT_DEPS_STATIC "")
if(${LIBARP_FEATURE_DEFLATE})
add_definitions("-DFEATURE_DEFLATE")
if(USE_SYSTEM_ZLIB)
message("Using system-provided zlib")
find_package(ZLIB REQUIRED)
list(APPEND EXT_INCLUDE_DIRS "${ZLIB_INCLUDE_DIRS}")
list(APPEND EXT_LIBS "${ZLIB_LIBRARIES}")
list(APPEND EXT_LIBS_STATIC "${ZLIB_LIBRARIES}")
else()
message("Using local zlib build")
set(ZLIB_TARGET "zlib")
set(ZLIB_STATIC_TARGET "zlibstatic")
if(NOT TARGET "${ZLIB_TARGET}")
set(ZLIB_SOURCE_DIR "${PROJECT_SOURCE_DIR}/libs/zlib")
set(ZLIB_INCLUDE_DIRS "${ZLIB_SOURCE_DIR};${TMP_INCLUDE_DIR}/zlib")
configure_file("${ZLIB_SOURCE_DIR}/zconf.h.in" "${TMP_INCLUDE_DIR}/zlib/zconf.h")
set(ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory("${ZLIB_SOURCE_DIR}")
if(TARGET minigzip)
set_target_properties(minigzip example
PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
endif()
if(TARGET minigzip64)
set_target_properties(minigzip64 example64
PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
endif()
_arp_disable_warnings("${ZLIB_TARGET}")
_arp_disable_warnings("${ZLIB_STATIC_TARGET}")
else()
message("'${ZLIB_TARGET}' target already exists, reusing it")
endif()
list(APPEND EXT_INCLUDE_DIRS "${ZLIB_INCLUDE_DIRS}")
list(APPEND EXT_LIBS "${ZLIB_TARGET}")
list(APPEND EXT_LIBS_STATIC "${ZLIB_STATIC_TARGET}")
endif()
endif()
if(WIN32)
list(APPEND EXT_LIBS "shlwapi")
list(APPEND EXT_LIBS_STATIC "shlwapi")
elseif(UNIX AND NOT APPLE)
list(APPEND EXT_LIBS "m")
list(APPEND EXT_LIBS_STATIC "m")
endif()
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(PACK_SRC_DIR "${SRC_DIR}/pack")
set(UNPACK_SRC_DIR "${SRC_DIR}/unpack")
set(UTIL_SRC_DIR "${SRC_DIR}/util")
set(INC_DIR "${PROJECT_SOURCE_DIR}/include")
set(TOOLING_DIR "${PROJECT_SOURCE_DIR}/tooling")
set(TEMP_INC_DIR "${PROJECT_BINARY_DIR}/include.tmp")
set(TEMP_SRC_DIR "${PROJECT_BINARY_DIR}/src.tmp")
set(GENERATED_SRC_DIR "${TEMP_SRC_DIR}/internal/generated")
set(GENERATED_INC_DIR "${TEMP_INC_DIR}/internal/generated")
# generate resource sources
# first we generate a master media type map
message("Generating resource sources")
execute_process(COMMAND "ruby" "${TOOLING_DIR}/generate_mt_mappings.rb" "${LIBARP_USER_MAPPINGS}"
WORKING_DIRECTORY "${TOOLING_DIR}"
RESULT_VARIABLE CMD_RES)
if(CMD_RES)
message(FATAL_ERROR " generate_mt_mappings.rb: ${CMD_RES}")
endif()
# then we generate the actual source files
file(MAKE_DIRECTORY "${GENERATED_SRC_DIR}")
file(MAKE_DIRECTORY "${GENERATED_INC_DIR}")
execute_process(COMMAND "ruby" "${TOOLING_DIR}/abacus/abacus.rb"
"${TOOLING_DIR}/output/media_types.csv"
"-h ${GENERATED_INC_DIR}/"
"-c ${GENERATED_SRC_DIR}/"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE CMD_RES)
if(CMD_RES)
message(FATAL_ERROR " abacus.rb: ${CMD_RES}")
endif()
# source files
file(GLOB_RECURSE GENERATED_C_FILES ${GENERATED_SRC_DIR}/*.c)
list(APPEND C_FILES "${GENERATED_C_FILES}")
file(GLOB_RECURSE UTIL_C_FILES ${UTIL_SRC_DIR}/*.c)
list(APPEND C_FILES "${UTIL_C_FILES}")
if(${LIBARP_FEATURE_PACK})
file(GLOB_RECURSE PACK_C_FILES ${PACK_SRC_DIR}/*.c)
list(APPEND C_FILES "${PACK_C_FILES}")
endif()
if(${LIBARP_FEATURE_UNPACK})
file(GLOB_RECURSE UNPACK_C_FILES ${UNPACK_SRC_DIR}/*.c)
list(APPEND C_FILES "${UNPACK_C_FILES}")
endif()
file(GLOB_RECURSE H_FILES ${SRC_DIR}/*.h)
set(DIST_DIR "${PROJECT_BINARY_DIR}/dist")
set(LIB_OUT_DIR "${DIST_DIR}/lib")
set(LIBRARY_OUTPUT_PATH "${LIB_OUT_DIR}")
# configure the copy headers task
set(HDR_TARGET "${PROJECT_NAME}_headers")
set(HDR_OUT_DIR "${DIST_DIR}/include")
add_custom_target(${HDR_TARGET})
add_custom_command(TARGET ${HDR_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
make_directory ${HDR_OUT_DIR})
add_custom_command(TARGET ${HDR_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_directory "${INC_DIR}/${PROJECT_NAME}/" "${HDR_OUT_DIR}/${PROJECT_NAME}/")
add_library("${PROJECT_NAME}" SHARED ${C_FILES} ${H_FILES})
add_library("${PROJECT_NAME}_static" STATIC ${C_FILES} ${H_FILES})
target_include_directories("${PROJECT_NAME}" PUBLIC "${INC_DIR};${TEMP_INC_DIR}")
target_include_directories("${PROJECT_NAME}_static" PUBLIC "${INC_DIR};${TEMP_INC_DIR}")
target_include_directories("${PROJECT_NAME}" PUBLIC "${EXT_INCLUDE_DIRS}")
target_include_directories("${PROJECT_NAME}_static" PUBLIC "${EXT_INCLUDE_DIRS}")
target_link_libraries("${PROJECT_NAME}" "${EXT_LIBS}")
target_link_libraries("${PROJECT_NAME}_static" "${EXT_LIBS_STATIC}")
# set the C standard
set_target_properties("${PROJECT_NAME}" PROPERTIES C_STANDARD 11)
set_target_properties("${PROJECT_NAME}" PROPERTIES C_STANDARD_REQUIRED ON)
set_target_properties("${PROJECT_NAME}" PROPERTIES C_EXTENSIONS OFF)
set_target_properties("${PROJECT_NAME}_static" PROPERTIES C_STANDARD 11)
set_target_properties("${PROJECT_NAME}_static" PROPERTIES C_STANDARD_REQUIRED ON)
set_target_properties("${PROJECT_NAME}_static" PROPERTIES C_EXTENSIONS OFF)
# enable PIC
set_target_properties("${PROJECT_NAME}" PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties("${PROJECT_NAME}_static" PROPERTIES POSITION_INDEPENDENT_CODE ON)
# export all symbols (required on Windows)
set_target_properties("${PROJECT_NAME}" PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
set_target_properties("${PROJECT_NAME}_static" PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
# set target name
set_target_properties("${PROJECT_NAME}" PROPERTIES OUTPUT_NAME ${LIBRARY_BASE_NAME})
set_target_properties("${PROJECT_NAME}_static" PROPERTIES OUTPUT_NAME ${LIBRARY_BASE_NAME})
# set shared library parameters
set_target_properties("${PROJECT_NAME}" PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties("${PROJECT_NAME}_static" PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties("${PROJECT_NAME}" PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})
set_target_properties("${PROJECT_NAME}" PROPERTIES DEBUG_POSTFIX "d")
set_target_properties("${PROJECT_NAME}_static" PROPERTIES DEBUG_POSTFIX "d")
target_compile_definitions("${PROJECT_NAME}" PUBLIC "$<$<CONFIG:DEBUG>:LIBARP_DEBUG>")
target_compile_definitions("${PROJECT_NAME}_static" PUBLIC "$<$<CONFIG:DEBUG>:LIBARP_DEBUG>")
if(MSVC)
add_compile_definitions("_CRT_SECURE_NO_WARNINGS" "_CRT_NONSTDC_NO_WARNINGS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4 /wd4244 /wd4267")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Od")
if(CMAKE_BUILD_TYPE MATCHES "Debug")
if(MSVC_VERSION LESS 1911)
set_property(TARGET "${PROJECT_NAME}" APPEND PROPERTY LINK_FLAGS /DEBUG)
set_property(TARGET "${PROJECT_NAME}_static" APPEND PROPERTY LINK_FLAGS /DEBUG)
else()
set_property(TARGET "${PROJECT_NAME}" APPEND PROPERTY LINK_FLAGS /DEBUG)
set_property(TARGET "${PROJECT_NAME}_static" APPEND PROPERTY LINK_FLAGS /DEBUG)
endif()
endif()
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /O2")
else()
add_compile_definitions("_POSIX_C_SOURCE=200809L" "_XOPEN_SOURCE=500")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wextra -Winit-self -Wuninitialized -Wmissing-declarations \
-Wconversion -Wno-error=conversion -Wno-error=sign-conversion -pedantic -pedantic-errors")
if(IS_TARGET_X86)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2")
endif()
if(MINGW)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb")
endif()
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
endif()
# make the copy headers target a dependency to force it to run
add_dependencies("${PROJECT_NAME}" ${HDR_TARGET})
add_dependencies("${PROJECT_NAME}_static" ${HDR_TARGET})
# copy dependency output
function(copy_dep_output DEP_TARGET)
add_custom_command(TARGET "${PROJECT_NAME}" POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"$<TARGET_FILE:${DEP_TARGET}>"
"${DIST_DIR}/lib/$<IF:$<BOOL:${MSVC}>,$<CONFIG>/,>$<TARGET_FILE_NAME:${DEP_TARGET}>"
COMMENT "Copying '${DEP_TARGET}' dist output to output directory")
add_custom_command(TARGET "${PROJECT_NAME}_static" POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"$<TARGET_FILE:${DEP_TARGET}>"
"${DIST_DIR}/lib/$<IF:$<BOOL:${MSVC}>,$<CONFIG>/,>$<TARGET_FILE_NAME:${DEP_TARGET}>"
COMMENT "Copying '${DEP_TARGET}' dist output to output directory")
if(WIN32)
add_custom_command(TARGET "${PROJECT_NAME}" POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"$<TARGET_LINKER_FILE:${DEP_TARGET}>"
"${DIST_DIR}/lib/$<IF:$<BOOL:${MSVC}>,$<CONFIG>/,>$<TARGET_LINKER_FILE_NAME:${DEP_TARGET}>"
COMMENT "Copying '${DEP_TARGET}' linker output to output directory")
endif()
endfunction()
if(LIBARP_FEATURE_DEFLATE AND NOT USE_SYSTEM_ZLIB)
copy_dep_output("${ZLIB_TARGET}")
endif()