-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
194 lines (175 loc) · 6.41 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
# This source file forms part of libsxbp, a library which generates
# experimental 2D spiral-like shapes based on input binary data.
#
# This is the CMake build file for libsxbp.
# It requires CMake v3.0 or greater.
#
#
#
# Copyright (C) 2016, Joshua Saxby [email protected]
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# begin basic metadata
cmake_minimum_required(VERSION 3.0)
project(sxbp VERSION 0.27.0 LANGUAGES C)
# set default C standard to use (C99) if not already set
if(NOT DEFINED LIBSXBP_C_STANDARD)
set(LIBSXBP_C_STANDARD "99")
endif()
# if env variable LIBSXBP_C_STANDARD is set and valid, override version
if(DEFINED ENV{LIBSXBP_C_STANDARD})
# not a very robust regex but ok for most purposes
if("$ENV{LIBSXBP_C_STANDARD}" MATCHES "(99|11)")
set(LIBSXBP_C_STANDARD "$ENV{LIBSXBP_C_STANDARD}")
endif()
endif()
message(STATUS "[sxbp] C Standard set to C${LIBSXBP_C_STANDARD}")
set(CMAKE_C_STANDARD ${LIBSXBP_C_STANDARD})
set(CMAKE_C_STANDARD_REQUIRED ON)
set(
LIBSXBP_VERSION_STRING
"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}"
)
set(LIBSXBP_ESCAPED_VERSION_STRING "\"${LIBSXBP_VERSION_STRING}\"")
# end basic metadata
# must be running on an architecture of at least 32 bits
if(CMAKE_SIZEOF_VOID_P LESS 4)
message(
WARNING
"[sxbp] It looks like this system's architecture is not at least 32-bit.\n"
" libsxbp requires a system with an architecture of at least 32 bits!\n"
" We'll continue trying to compile anyway, but may fail.\n"
" Be sure to run the unit tests after!"
)
endif()
# pass in version of library as preprocessor definitions
add_definitions(-DLIBSXBP_VERSION_MAJOR=${PROJECT_VERSION_MAJOR})
add_definitions(-DLIBSXBP_VERSION_MINOR=${PROJECT_VERSION_MINOR})
add_definitions(-DLIBSXBP_VERSION_PATCH=${PROJECT_VERSION_PATCH})
add_definitions(-DLIBSXBP_VERSION_STRING=${LIBSXBP_ESCAPED_VERSION_STRING})
# used for enabling additional compiler options if supported
include(CheckCCompilerFlag)
function(enable_c_compiler_flag_if_supported flag)
string(FIND "${CMAKE_C_FLAGS}" "${flag}" flag_already_set)
if(flag_already_set EQUAL -1)
check_c_compiler_flag("${flag}" flag_supported)
if(flag_supported)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
endif()
endif()
endfunction()
# enable extra flags (warnings) if we're not in release mode
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "[sxbp] Warnings Enabled")
# enable all warnings about 'questionable constructs'
enable_c_compiler_flag_if_supported("-Wall")
# issue 'pedantic' warnings for strict ISO compliance
enable_c_compiler_flag_if_supported("-pedantic")
# enable 'extra' strict warnings
enable_c_compiler_flag_if_supported("-Wextra")
# enable warnings on missing prototypes of global functions
enable_c_compiler_flag_if_supported("-Wmissing-prototypes")
# convert all warnings into errors
enable_c_compiler_flag_if_supported("-Werror")
endif()
# begin dependencies
# add custom dependencies directory
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# libpng
# work out whether we have or have not requested PNG support, or don't care (default)
if(NOT DEFINED LIBSXBP_PNG_SUPPORT)
# try and find libpng v1.x, but don't fail if we can't
message(STATUS "[sxbp] PNG output support will be enabled if possible")
find_package(PNG 1 EXACT)
# set SXBP_PNG_SUPPORT based on value of PNG_FOUND
if(PNG_FOUND)
set(LIBSXBP_PNG_SUPPORT ON)
else()
set(LIBSXBP_PNG_SUPPORT OFF)
endif()
elseif(LIBSXBP_PNG_SUPPORT)
# find libpng v1.x and fail the build if we can't
message(STATUS "[sxbp] PNG output support explicitly enabled")
find_package(PNG 1 EXACT REQUIRED)
else()
# we've explicitly disabled PNG support, so don't include libpng
# issue a message saying so
message(STATUS "[sxbp] PNG output support explicitly disabled")
endif()
# include libpng directories and add feature test macro if support is enabled
if(LIBSXBP_PNG_SUPPORT)
include_directories(${PNG_INCLUDE_DIR})
# feature test macro
add_definitions(-DLIBSXBP_PNG_SUPPORT)
# issue message
message(STATUS "[sxbp] PNG output support enabled")
else()
# issue message
message(STATUS "[sxbp] PNG output support disabled")
endif()
# end dependencies
# C source files
file(
GLOB LIBSXBP_SOURCES
"sxbp/*.c" "sxbp/render_backends/*.c"
)
# Header files
file(GLOB LIBSXBP_HEADERS "sxbp/*.h")
# Header files for render_backends subdirectory
file(
GLOB LIBSXBP_RENDER_BACKENDS_HEADERS
"sxbp/render_backends/*.h"
)
add_library(sxbp ${LIBSXBP_SOURCES})
# set up version for library objects
set_target_properties(
sxbp PROPERTIES VERSION ${LIBSXBP_VERSION_STRING}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
# link libsxbp with C math library
target_link_libraries(sxbp m)
# Link libsxbp with libpng so we get libpng symbols (if support enabled)
if(LIBSXBP_PNG_SUPPORT)
target_link_libraries(sxbp ${PNG_LIBRARY})
endif()
add_executable(sxp_test tests.c)
target_link_libraries(sxp_test sxbp)
install(
TARGETS sxbp
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
# Generate rough (nearest major) version-dependent header installation folder
set(
LIBSXBP_ROUGH_HEADER_DESTINATION
"sxbp-${PROJECT_VERSION_MAJOR}"
)
# Generate precise (major and minor) version-dependent header installation folder
set(
LIBSXBP_PRECISE_HEADER_DESTINATION
"sxbp-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
)
# Install main library header files, both to rough and precise install locations
install(
FILES ${LIBSXBP_HEADERS}
DESTINATION "include/${LIBSXBP_ROUGH_HEADER_DESTINATION}"
)
# Install render_backends header files
install(
FILES ${LIBSXBP_RENDER_BACKENDS_HEADERS}
DESTINATION "include/${LIBSXBP_ROUGH_HEADER_DESTINATION}/render_backends"
)
install(
FILES ${LIBSXBP_HEADERS}
DESTINATION "include/${LIBSXBP_PRECISE_HEADER_DESTINATION}"
)
# Install render_backends header files
install(
FILES ${LIBSXBP_RENDER_BACKENDS_HEADERS}
DESTINATION "include/${LIBSXBP_PRECISE_HEADER_DESTINATION}/render_backends"
)
enable_testing()
add_test(unit_tests sxp_test)