-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
294 lines (255 loc) · 11.9 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
# PiCode Library
# https://github.com/latchdevel/PiCode
# Copyright (c) 2021-2022 Jorge Rivera. All right reserved.
# License GNU Lesser General Public License v3.0.
cmake_minimum_required(VERSION 3.18)
project(picode)
# Set version number for shared libraries and executables
set(CU_VERSION 1.4) # current version
set(SO_VERSION 1.4) # compatibility version
# Set C/C++ Standard
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
if(NOT BUILD_COMPILER)
# Set complier identification
SET(BUILD_COMPILER "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}" )
MESSAGE( STATUS "Compiler: " ${BUILD_COMPILER} )
endif()
# Check for git repository information
IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
FIND_PACKAGE(Git)
IF(GIT_FOUND)
EXECUTE_PROCESS(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE "BUILD_VERSION"
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
SET(BUILD_VERSION "${BUILD_VERSION}" )
MESSAGE( STATUS "Git commit: ${BUILD_VERSION}")
ENDIF(GIT_FOUND)
ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
# Force re-run CMake to set BUILD_VERSION on any git commit
set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS
.git/index
)
# Check if has parent directory
get_directory_property(hasParent PARENT_DIRECTORY)
# Check if set CMAKE_BUILD_TYPE var
if(NOT CMAKE_BUILD_TYPE)
# Set default build type to "release" set -O3 -DNDEBUG
set(DEFAULT_BUILD_TYPE "release")
SET(CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE})
MESSAGE( STATUS "Build type set to default: " ${CMAKE_BUILD_TYPE} )
else()
# Check if set and valid CMAKE_BUILD_TYPE var
STRING( TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE )
if((CMAKE_BUILD_TYPE STREQUAL "debug") OR (CMAKE_BUILD_TYPE STREQUAL "release"))
# If no has parent directory show message
if(NOT hasParent)
MESSAGE( STATUS "Build type set to: " ${CMAKE_BUILD_TYPE} )
endif()
else()
MESSAGE( FATAL_ERROR "If set CMAKE_BUILD_TYPE it must be 'release' or 'debug'")
endif()
endif()
# Setting build type to "debug" add only -g
if(CMAKE_BUILD_TYPE STREQUAL "debug")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
endif()
# Set C++ flags
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -fcommon")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wconversion -Woverloaded-virtual -Wsign-conversion")
elseif(MSVC)
set(MSVC_DISABLED_WARNINGS_LIST
"C4996" # warning C4996: 'may be unsafe/disable deprecation'
# To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
"C4244" # warning C4244: '=': conversion from '__int64' to 'int', possible loss of data
"C4267" # warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
# "C4305" # warning C4305: '=': truncation from 'int' to 'uint16_t'
"C4018" # warning C4018: '>': signed/unsigned mismatch
"C5105" # warning C5105: macro expansion producing 'defined' has undefined behavior
"C4201" # warning C4201: nonstandard extension used: nameless struct/union
"C4473" # warning C4473: 'printf' : not enough arguments passed for format string
"C4100" # warning C4100: unreferenced formal parameter
"C4706" # warning C4706: assignment within conditional expression
)
string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR ${MSVC_DISABLED_WARNINGS_LIST})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -W4 ${MSVC_DISABLED_WARNINGS_STR}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W4 ${MSVC_DISABLED_WARNINGS_STR}")
endif()
# If macOS builds a Mach-O universal binary with 2 architectures: x86_64 and arm64 for Apple M processors
if (APPLE)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
endif()
# Add sources for C++ library
FILE (GLOB ${PROJECT_NAME}_SRC_CPP src/*.cpp )
# Pure C PiCode Library sources
FILE (GLOB ${PROJECT_NAME}_SRC_C src/*.c )
# Add library common sources
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/core/ CORE )
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/protocols/ PROTOCOL )
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/protocols/433.92/ PROTOCOLS )
# Compile common library objects
add_library(
${PROJECT_NAME}-common OBJECT
${CORE}
${PROTOCOL}
${PROTOCOLS}
)
# Compile C++ library as object
add_library(
${PROJECT_NAME}-obj OBJECT
${${PROJECT_NAME}_SRC_CPP}
)
# Pure C PiCode Library compile as object
add_library(
c${PROJECT_NAME}-obj OBJECT
${${PROJECT_NAME}_SRC_C}
)
# If git info available adds to pure C PiCode Library
if(DEFINED BUILD_VERSION)
target_compile_definitions( c${PROJECT_NAME}-obj PRIVATE BUILD_VERSION=${BUILD_VERSION} )
endif()
# If version number for shared libraries and executables available adds to pure C PiCode Library
if(DEFINED CU_VERSION)
target_compile_definitions( c${PROJECT_NAME}-obj PRIVATE CU_VERSION=${CU_VERSION} )
endif()
# Shared libraries need flag -fPIC
set_property(TARGET ${PROJECT_NAME}-common PROPERTY POSITION_INDEPENDENT_CODE 1)
set_property(TARGET ${PROJECT_NAME}-obj PROPERTY POSITION_INDEPENDENT_CODE 1)
# Pure C PiCode Library
set_property(TARGET c${PROJECT_NAME}-obj PROPERTY POSITION_INDEPENDENT_CODE 1)
# Shared library built from the same object files
add_library(
${PROJECT_NAME}-dynamic SHARED
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
)
# C++ PiCode Library v1.2 require the pure C library (dynamic or static) as "c${PROJECT_NAME}-dynamic"
target_link_libraries(${PROJECT_NAME}-dynamic PUBLIC c${PROJECT_NAME})
# File extension OS depends, like: libpicode.so or libpicode.dylib or libpicode.dll
set_target_properties( ${PROJECT_NAME}-dynamic PROPERTIES OUTPUT_NAME ${PROJECT_NAME} )
# Set version numbers for the versioned shared libraries target.
# For shared libraries and executables on Windows and Mach-O systems
# the SOVERSION property corresponds to the compatibility version
# and VERSION corresponds to the current version
#
# Note that SOVERSION will still be used to form the install_name and
# both SOVERSION and VERSION may also affect the file and symlink names.
# Use the NAMELINK_SKIP option of the install command to prevent the
# generation of the versionless library name symbolic link to the
# versioned library file.
set_target_properties( ${PROJECT_NAME}-dynamic PROPERTIES
SOVERSION ${SO_VERSION}
VERSION ${CU_VERSION}
)
# Add static library
# File extension OS depends, like: libpicode.a or libpicode.lib
add_library(
${PROJECT_NAME} STATIC
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
)
# C++ PiCode Library v1.2 require the pure C library static as "c${PROJECT_NAME}"
target_link_libraries(${PROJECT_NAME} PUBLIC c${PROJECT_NAME})
# Pure C PiCode Library dynamic
# ---------------------------------------------------------------------------------
# Shared library built from the same object files
add_library(
c${PROJECT_NAME}-dynamic SHARED
$<TARGET_OBJECTS:c${PROJECT_NAME}-obj>
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
)
# File extension OS depends, like: libcpicode.so or libcpicode.dylib or libcpicode.dll
set_target_properties( c${PROJECT_NAME}-dynamic PROPERTIES OUTPUT_NAME c${PROJECT_NAME} )
# Set version numbers for the versioned shared libraries target.
# For shared libraries and executables on Windows and Mach-O systems
# the SOVERSION property corresponds to the compatibility version
# and VERSION corresponds to the current version
#
# Note that SOVERSION will still be used to form the install_name and
# both SOVERSION and VERSION may also affect the file and symlink names.
# Use the NAMELINK_SKIP option of the install command to prevent the
# generation of the versionless library name symbolic link to the
# versioned library file.
set_target_properties( c${PROJECT_NAME}-dynamic PROPERTIES
SOVERSION ${SO_VERSION}
VERSION ${CU_VERSION}
)
# Pure C PiCode Library static
# File extension OS depends, like: libcpicode.a or libcpicode.lib
add_library(
c${PROJECT_NAME} STATIC
$<TARGET_OBJECTS:c${PROJECT_NAME}-obj>
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
)
# Add install targets
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(TARGETS ${PROJECT_NAME}-dynamic DESTINATION lib)
# Pure C PiCode Library install targets
# ---------------------------------------------------------------------------------
install(TARGETS c${PROJECT_NAME} DESTINATION lib)
install(TARGETS c${PROJECT_NAME}-dynamic DESTINATION lib)
# If no has parent directory, add uninstall targets
if(NOT hasParent)
MESSAGE(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
add_custom_target( uninstall
"${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/uninstall.cmake"
)
endif()
# Examples
# ---------------------------------------------------------------------------------
# Checking for math library 'libm' used when including <math.h> in pilight sources
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.lib;.tbd;.so;.dylib;.dll.a")
find_library(MATH_LIBRARY NAMES m )
if(MATH_LIBRARY)
MESSAGE( STATUS "Math library: " ${MATH_LIBRARY} )
else()
if(NOT MSVC)
message(FATAL_ERROR "Cannot find math library 'libm' ")
else()
set(MATH_LIBRARY "")
endif()
endif()
# Add picode_example source file, link static, no build as default
add_executable( picode_example picode_example.cpp )
target_link_libraries( picode_example PRIVATE ${PROJECT_NAME} ${MATH_LIBRARY} )
set_target_properties( picode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
# Add executable export symbols for loadable modules to prevent policy CMP0065 warning
# Basic check for BSD based systems, like as FreeBSD, NetBSD, OpenBSD, etc.
string( TOUPPER ${CMAKE_SYSTEM_NAME} CMAKE_SYSTEM_NAME_UPPER )
if(NOT CMAKE_SYSTEM_NAME_UPPER MATCHES "BSD")
# If not a BSD system, add executable export symbols for loadable modules. Add linker flag "-Wl,--export-dynamic"
set_target_properties( picode_example PROPERTIES ENABLE_EXPORTS TRUE )
endif()
# If git info available adds to picode_example executable as environment var
if(DEFINED BUILD_VERSION)
target_compile_definitions( picode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
endif(DEFINED BUILD_VERSION)
# Add complier identification to picode_example executable as environment var
target_compile_definitions( picode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )
# Pure C PiCode Library example
# ---------------------------------------------------------------------------------
# Add cpicode_example source file, link static, no build as default
add_executable( cpicode_example cpicode_example.c )
target_link_libraries( cpicode_example PRIVATE c${PROJECT_NAME} ${MATH_LIBRARY} )
set_target_properties( cpicode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
# Add executable export symbols for loadable modules to prevent policy CMP0065 warning
# Basic check for BSD based systems, like as FreeBSD, NetBSD, OpenBSD, etc.
string( TOUPPER ${CMAKE_SYSTEM_NAME} CMAKE_SYSTEM_NAME_UPPER )
if(NOT CMAKE_SYSTEM_NAME_UPPER MATCHES "BSD")
# If not a BSD system, add executable export symbols for loadable modules. Add linker flag "-Wl,--export-dynamic"
set_target_properties( cpicode_example PROPERTIES ENABLE_EXPORTS TRUE )
endif()
# If git info available adds to cpicode_example executable as environment var
if(DEFINED BUILD_VERSION)
target_compile_definitions( cpicode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
endif(DEFINED BUILD_VERSION)
# Add complier identification to cpicode_example executable as environment var
target_compile_definitions( cpicode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )