-
Notifications
You must be signed in to change notification settings - Fork 16
/
CMakeLists.txt
380 lines (279 loc) · 9.68 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
cmake_minimum_required (VERSION 2.6)
# ==================
# Define Our Project
# ==================
set (My_Project_Title "C-Template")
set (My_Project_Description "Boilerplate c project with cmake support, CuTest unit testing, and more.")
set (My_Project_Author "Fletcher T. Penney")
set (My_Project_Revised_Date "2019-01-16")
set (My_Project_Version_Major 1)
set (My_Project_Version_Minor 0)
set (My_Project_Version_Patch 9)
set (My_Project_Version "${My_Project_Version_Major}.${My_Project_Version_Minor}.${My_Project_Version_Patch}")
set (My_Project_Copyright_Date "2015-2019")
set (My_Project_Copyright "Copyright © ${My_Project_Copyright_Date} ${My_Project_Author}.")
string(TOUPPER ${My_Project_Title} My_Project_Title_Caps )
string(REGEX REPLACE " " "_" My_Project_Title_Caps ${My_Project_Title_Caps} )
project (${My_Project_Title})
# =========================
# Build Submodules (if any)
# =========================
# add_subdirectory(submodules/foo)
# =================
# Configure Project
# =================
# Search for included files here
include_directories( ${PROJECT_SOURCE_DIR}/src )
include_directories( ${PROJECT_SOURCE_DIR}/test )
include_directories(${PROJECT_BINARY_DIR})
# =================
# Macro Definitions
# =================
MACRO(ADD_PUBLIC_HEADER target filename)
# Add filename to public_header_files list, flag it as
# public header for libraries and OS X Frameworks
# This will work for creating one library/framework with public headers
# per project. If you need more than one, you will need to customize
# the workflow as appropriate, since there is only one
# public_header_files list.
# SET_TARGET_PROPERTIES(${target} PROPERTIES PUBLIC_HEADER ${filename})
LIST(APPEND public_header_files ${filename})
SET_SOURCE_FILES_PROPERTIES(
${filename}
PROPERTIES
MACOSX_PACKAGE_LOCATION
include/\${PRODUCT_NAME}
)
# Set Xcode project to configure public header location to allow
# use when this project is used in another workspace.
# NOTE: You must manually add a "Headers" build phase and add
# the desired public headers to that list for Xcode to use them.
#
# TODO: If anyone knows how to automate that in Cmake, I would be very
# grateful!!
SET_TARGET_PROPERTIES(${target} PROPERTIES
XCODE_ATTRIBUTE_PUBLIC_HEADERS_FOLDER_PATH
"include/$(TARGET_NAME)"
)
SET_TARGET_PROPERTIES(${target} PROPERTIES
XCODE_ATTRIBUTE_PRIVATE_HEADERS_FOLDER_PATH
"$(PUBLIC_HEADERS_FOLDER_PATH)/Private"
)
# Set Xcode target to include settings for OS X and iOS
SET_TARGET_PROPERTIES(${target} PROPERTIES
XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS
"macos iphonesimulator iphoneos"
)
SET_TARGET_PROPERTIES(${target} PROPERTIES
XCODE_ATTRIBUTE_VALID_ARCHITECTURES
"x86_64 i386 armv6 armv7 armv7s arm64"
)
ENDMACRO(ADD_PUBLIC_HEADER)
# The target should be an OS X Bundle with a PList
MACRO(MAKE_TARGET_BUNDLE targetname)
set_target_properties(
${targetname}
PROPERTIES
MACOSX_BUNDLE_INFO_PLIST
${PROJECT_SOURCE_DIR}/templates/plist.in
)
ENDMACRO(MAKE_TARGET_BUNDLE)
MACRO(ADD_LINKED_FRAMEWORK frame)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGES} -framework ${frame}")
ENDMACRO(ADD_LINKED_FRAMEWORK)
# ======================
# Process Template Files
# ======================
file(READ ${PROJECT_SOURCE_DIR}/LICENSE.txt My_Project_License)
string(REGEX REPLACE "\n" "\n\t" My_Project_License_Indented ${My_Project_License})
string(REGEX REPLACE "\"" "\\\\\"" My_Project_License_Escaped ${My_Project_License_Indented})
string(REGEX REPLACE "\n" "\\\\n\"\\\\\n\"" My_Project_License_Literal ${My_Project_License_Escaped})
configure_file (
"${PROJECT_SOURCE_DIR}/templates/template.c.in"
"${PROJECT_BINARY_DIR}/template.c"
)
configure_file (
"${PROJECT_SOURCE_DIR}/templates/template.h.in"
"${PROJECT_BINARY_DIR}/template.h"
)
# Update the project README, to pull in new version #, etc.
configure_file (
"${PROJECT_SOURCE_DIR}/templates/README.md.in"
"${CMAKE_CURRENT_LIST_DIR}/README.md"
)
configure_file (
"${PROJECT_SOURCE_DIR}/templates/version.h.in"
"${PROJECT_BINARY_DIR}/version.h"
)
# ============
# Source Files
# ============
# src_files are the primary files, and will be included in doxygen documentation
set(src_files
# src/foo.c
)
# Primary header files, also for doxygen documentation
set(header_files
)
# Public headers, will be installed in 'include'
# Do not manually add files here, use the ADD_PUBLIC_HEADER() macro
set(public_header_files
)
# Utility source files will not be included in doxygen
set(src_utility_files
# src/d_string.c
)
set(header_utility_files
# src/d_string.h
${PROJECT_BINARY_DIR}/version.h
)
# Generate doxygen configuration file
string(REPLACE ";" " " doxygen_src_files "${src_files}" )
string(REPLACE ";" " " doxygen_header_files "${header_files}" )
configure_file (
"${PROJECT_SOURCE_DIR}/templates/doxygen.conf.in"
"${PROJECT_BINARY_DIR}/doxygen.conf"
)
# ===========================================
# Build Test Suite with CuTest (unit testing)
# ===========================================
# from http://stackoverflow.com/questions/25199677/how-to-detect-if-current-scope-has-a-parent-in-cmake
get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
# Don't create "run_tests" when we're a sub-project for something else
else()
set(test_files
test/CuTest.c
test/CuTest.h
${PROJECT_BINARY_DIR}/AllTests.c
)
if (DEFINED TEST)
add_definitions(-DTEST)
add_executable(run_tests
${test_files}
${src_files}
${header_files}
${src_utility_files}
${header_utility_files}
)
# Process source files to look for tests to run
add_custom_command (
OUTPUT ${PROJECT_BINARY_DIR}/AllTests.c
COMMAND bash ${PROJECT_SOURCE_DIR}/test/make-tests.sh ${PROJECT_SOURCE_DIR}/src/*.c > ${PROJECT_BINARY_DIR}/AllTests.c
)
enable_testing()
add_test( test ${PROJECT_BINARY_DIR}/run_tests)
# valgrind memory testing
find_program (MEMORYCHECK_COMMAND valgrind)
SET (MEMORYCHECK_COMMAND_OPTIONS --leak-check=full --error-exitcode=1)
add_test( memory_test ${MEMORYCHECK_COMMAND} ${MEMORYCHECK_COMMAND_OPTIONS} ${PROJECT_BINARY_DIR}/run_tests)
endif()
endif()
# =======================
# Configure for Target OS
# =======================
# OS X Builds
if (APPLE)
# Configure backwards-compatible support (if your project allows it)
SET(CMAKE_OSX_DEPLOYMENT_TARGET "10.4" CACHE STRING "Deployment target for OSX" FORCE)
SET(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "10.0" CACHE STRING "Deployment target for iOS" FORCE)
# Compile for x86_64 and i386. ppc no longer supported
if(CMAKE_BUILD_TYPE MATCHES "Release")
SET (CMAKE_OSX_ARCHITECTURES x86_64;i386)
endif(CMAKE_BUILD_TYPE MATCHES "Release")
# Use PackageMaker for installers?
if (DEFINED ZIP)
set (CPACK_GENERATOR ZIP)
else (DEFINED ZIP)
set (CPACK_GENERATOR PackageMaker)
endif (DEFINED ZIP)
endif (APPLE)
# Windows Builds
if (WIN32)
# Use NSIS to generate installers?
if (DEFINED ZIP)
set (CPACK_GENERATOR ZIP)
else (DEFINED ZIP)
set (CPACK_GENERATOR NSIS)
endif (DEFINED ZIP)
# Linux Builds (not cross-compiling for Windows)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# Create zip archive
set (CPACK_GENERATOR ZIP)
# Statically link libraries -- might make the binary slightly more
# compatible across Linux distributions, for example
#
# You may wish to disable this.
#
set (CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set (BUILD_SHARED_LIBRARIES OFF)
set (CMAKE_EXE_LINKER_FLAGS "-static")
# Some libraries need to be linked on some Linux builds
if (DEFINED TEST)
# target_link_libraries(run_tests m)
endif (DEFINED TEST)
endif (WIN32)
# ==============
# Define targets
# ==============
# Create a library?
add_library(libFOO STATIC
${src_files}
${src_utility_files}
${header_files}
${header_utility_files}
)
# remove the extra "lib" from "liblibFOO"
SET_TARGET_PROPERTIES(libFOO PROPERTIES
PREFIX ""
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER net.fletcherpenney.c-template
)
# Create a command-line app?
# if (NOT DEFINED TEST)
# add_executable(main
# src/main.c
# src/d_string.c
# src/d_string.h
# ${header_files}
# )
#
# Link the library to the app?
# target_link_libraries(main libFOO)
# endif()
# Xcode settings for fat binaries
# set_target_properties(libFOO PROPERTIES XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH "NO")
# set_target_properties(main PROPERTIES XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH "NO")
# ==========================
# Build Installer with CPack
# ==========================
# You're largely on your own here
# install (FILES ${CMAKE_CURRENT_LIST_DIR}/README.md ${PROJECT_SOURCE_DIR}/LICENSE.txt
# DESTINATION .
# )
# Use something like this to install public header files (after adding them
# with the ADD_PUBLIC_HEADER() macro)
# install (FILES ${public_header_files} DESTINATION local/include/libFoo)
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "${My_Project_Description}")
set (CPACK_PACKAGE_VENDOR "${My_Project_Author}")
set (CPACK_PACKAGE_VERSION "${My_Project_Version_Major}.${My_Project_Version_Minor}.${My_Project_Version_Patch}")
set (CPACK_PACKAGE_VERSION_MAJOR "${My_Project_Version_Major}")
set (CPACK_PACKAGE_VERSION_MINOR "${My_Project_Version_Minor}")
set (CPACK_PACKAGE_VERSION_PATCH "My_Project_Version_Patch")
set (CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE.txt")
if (APPLE)
set (CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-Mac-${CPACK_PACKAGE_VERSION}")
else (APPLE)
set (CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CMAKE_SYSTEM_NAME}-${CPACK_PACKAGE_VERSION}")
endif (APPLE)
# Use some default install locations (if appropriate)
set(CPACK_SET_DESTDIR true)
if (WIN32)
set (CPACK_INSTALL_PREFIX "")
set (CPACK_NSIS_MODIFY_PATH ON)
else (WIN32)
set (CPACK_INSTALL_PREFIX /usr/local)
endif (WIN32)
set (CPACK_PACKAGE_INSTALL_DIRECTORY ${PROJECT})
include (CPack)