-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
398 lines (308 loc) · 9.62 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#
# Boilerplate CMake configuration for a cross-platform library,
# including configuration for a static framework on Xcode/macOS/iOS
#
# Designed by Fletcher T. Penney
# See the LICENSE file for copyright and licensing information.
#
cmake_minimum_required(VERSION 2.9)
# ===================
# Customize This Part
# ===================
# Project Description
set (My_Project_Title "libTDP")
set (My_Project_Description "Parse CSV (and other tabular data) and export to JSON")
set (My_Project_Author "Fletcher T. Penney")
set (My_Project_Revised_Date "2021-01-04")
set (My_Project_Version_Major 1)
set (My_Project_Version_Minor 3)
set (My_Project_Version_Patch 0)
set (My_Project_Copyright_Date "2018-2021")
set (My_Project_Identifier "net.fletcherpenney.libtdp")
string(TIMESTAMP My_Build_Version "%Y.%m.%d.%H.%M")
# Source files and headers
set(src_files
src/d_string.c
src/file.c
src/lexer.c
src/parser.c
src/reader.c
src/simple_token.c
src/stack.c
)
set(public_headers
src/libTDP.h
)
set(private_headers
src/d_string.h
src/file.h
src/lexer.h
src/parser.h
src/reader.h
src/simple_token.h
src/stack.h
version.h
)
set(resources
)
set(main_storyboard
)
# Include other CMake projects?
# add_subdirectory(submodules/foo)
# Libraries for linking
# on macOS can link to frameworks using: "-framework QuartzCore", for example
set(libraries_to_link
)
# This is an ugly hack, but it seems to work.
# When linking to a framework in Xcode, we need to be able to
# search for public headers within that framework
# "$SRCROOT/build-xcode/submodules/libFoo/$<CONFIG>/libFoo.framework/.."
#
set(framework_search_paths
)
# Link to Cocoa Framework?
# set (link_to_cocoa 1)
# Use Assets for app icon set for iOS or macOS application?
# (Assets stored in resources/Assets.xcassets)
# set (use_assets 1)
# Enable ARC for Obj-C?
# set (enable_arc 1)
# =======================
# Leave the rest alone...
# =======================
# Configuration
set (My_Project_Version "${My_Project_Version_Major}.${My_Project_Version_Minor}.${My_Project_Version_Patch}")
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} )
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
project (${My_Project_Title} VERSION "${My_Project_Version}")
# 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)
else()
# If building the framework independently, we canset a deployment target
# set (CMAKE_OSX_DEPLOYMENT_TARGET "10.12")
if (CMAKE_GENERATOR MATCHES "Xcode")
set (CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD)")
endif()
endif()
# Search source directory
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/test)
# Search build directory (for version.h)
include_directories(${PROJECT_BINARY_DIR})
# Configure library/framework
add_library("${My_Project_Title}"
STATIC
${src_files}
${private_headers}
${public_headers}
${resources}
)
string (REPLACE ";" " " framework_search_paths_string "${framework_search_paths}")
set_target_properties("${My_Project_Title}"
PROPERTIES
PREFIX ""
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER "${My_Project_Identifier}"
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${My_Project_Identifier}"
XCODE_ATTRIBUTE_MARKETING_VERSION ${My_Project_Version}
RESOURCE "${resources}"
XCODE_ATTRIBUTE_FRAMEWORK_SEARCH_PATHS "${XCODE_ATTRIBUTE_FRAMEWORK_SEARCH_PATHS} ${framework_search_paths_string}"
PUBLIC_HEADER "${public_headers}"
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH[variant=Debug] YES
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH[variant=MinSizeRel] NO
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH[variant=RelWithDebInfo] NO
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH[variant=Release] NO
)
# Link to other libraries
target_link_libraries("${My_Project_Title}"
${libraries_to_link}
)
# Link to Apple Cocoa Framework?
if (link_to_cocoa)
target_link_libraries("${My_Project_Title}" "-framework Cocoa")
endif (link_to_cocoa)
# Use assets for application icons?
if (use_assets)
target_sources("${My_Project_Title}"
PRIVATE
resources/Assets.xcassets
)
set_source_files_properties(resources/Assets.xcassets
PROPERTIES
MACOSX_PACKAGE_LOCATION Resources
)
set_target_properties("${My_Project_Title}"
PROPERTIES
XCODE_ATTRIBUTE_ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon"
)
endif (use_assets)
# Enable ARC
if (enable_arc)
set_property (TARGET "${My_Project_Title}"
APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc"
)
endif (enable_arc)
# ======================
# Process Template Files
# ======================
file(READ ${PROJECT_SOURCE_DIR}/LICENSE 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"
)
# ===========================================
# 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)
set(test_files
test/CuTest.c
test/CuTest.h
${PROJECT_BINARY_DIR}/AllTests.c
)
# 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
)
if(hasParent)
# Don't create "run_tests" when we're a sub-project for something else
else()
# Only create framework if we are the parent project
set_target_properties("${My_Project_Title}"
PROPERTIES
FRAMEWORK TRUE
)
if (DEFINED TEST)
add_definitions(-DTEST)
add_executable(run_tests
${src_files}
${private_headers}
${public_headers}
${test_files}
)
# Link to other libraries
target_link_libraries(run_tests
${libraries_to_link}
)
# Link to Apple Cocoa Framework?
if (link_to_cocoa)
target_link_libraries(run_tests "-framework Cocoa")
endif (link_to_cocoa)
# Enable ARC
if (enable_arc)
set_property (TARGET run_tests
APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc"
)
endif (enable_arc)
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)
else()
# Create command line utility
add_executable(tdp
src/main.c
src/d_string.c
src/argtable3.c
)
target_link_libraries(tdp libTDP)
endif()
endif()
# ==========================
# Build Installer with CPack
# ==========================
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "${My_Project_Description}")
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")
configure_file (
"${PROJECT_SOURCE_DIR}/LICENSE"
"${PROJECT_BINARY_DIR}/LICENSE.txt"
)
configure_file (
"${PROJECT_SOURCE_DIR}/README.md"
"${PROJECT_BINARY_DIR}/README.txt"
)
set (CPACK_RESOURCE_FILE_LICENSE "${PROJECT_BINARY_DIR}/LICENSE.txt")
set (CPACK_RESOURCE_FILE_README "${PROJECT_BINARY_DIR}/README.txt")
if (DEFINED TEST)
# Don't install the test suite
else()
if (APPLE)
# Apple Builds
if (DEFINED ZIP)
set (CPACK_GENERATOR ZIP)
else (DEFINED ZIP)
set (CPACK_GENERATOR productbuild)
endif (DEFINED ZIP)
else (APPLE)
if (WIN32)
# Windows Builds
if (DEFINED ZIP)
set (CPACK_GENERATOR ZIP)
else (DEFINED ZIP)
set (CPACK_GENERATOR NSIS)
endif (DEFINED ZIP)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# Linux
set (CPACK_GENERATOR ZIP)
endif (WIN32)
endif (APPLE)
# Install Target
install (TARGETS ${My_Project_Title}
DESTINATION bin
COMPONENT Main
)
set (CPACK_COMPONENT_MAIN_DISPLAY_NAME "${My_Project_Title}")
set (CPACK_COMPONENT_MAIN_DESCRIPTION "Install ${My_Project_Title}.")
if (public_headers)
# Install Headers
install(FILES
${public_headers}
DESTINATION include/${My_Project_Title}
COMPONENT Headers
)
set (CPACK_COMPONENT_HEADERS_DISPLAY_NAME "Development")
set (CPACK_COMPONENT_HEADERS_DESCRIPTION "Install the public headers for development use.")
set (CPACK_COMPONENTS_ALL Main Headers)
else (public_headers)
set (CPACK_COMPONENTS_ALL Main)
endif (public_headers)
# Install Documentation
install(FILES
"${PROJECT_BINARY_DIR}/LICENSE.txt"
"${PROJECT_BINARY_DIR}/README.txt"
COMPONENT Docs
DESTINATION share/doc/${My_Project_Title}
)
set (CPACK_COMPONENT_DOCS_DISPLAY_NAME "Documentation")
set (CPACK_COMPONENT_DOCS_DESCRIPTION "Install README and LICENSE.")
endif (DEFINED TEST)
include(CPack)