-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathCMakeLists.txt
173 lines (154 loc) · 5.53 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
# /****************************************************************************
# **
# ** Copyright (C) 2015 Intel Corporation
# **
# ** Permission is hereby granted, free of charge, to any person obtaining a copy
# ** of this software and associated documentation files (the "Software"), to deal
# ** in the Software without restriction, including without limitation the rights
# ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# ** copies of the Software, and to permit persons to whom the Software is
# ** furnished to do so, subject to the following conditions:
# **
# ** The above copyright notice and this permission notice shall be included in
# ** all copies or substantial portions of the Software.
# **
# ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# ** THE SOFTWARE.
# **
# ****************************************************************************/
cmake_minimum_required(VERSION 3.10)
project(tinycbor LANGUAGES C CXX VERSION 7.0)
# Set path to additional cmake scripts
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
set(TARGETS_EXPORT_NAME "TinyCBOR-targets")
option(WITH_CBOR2JSON "Compile code to convert from CBOR to JSON" ON)
option(WITH_FREESTANDING "Compile TinyCBOR in C freestanding mode" OFF)
if(WITH_FLOATING_POINT AND NOT WITH_FREESTANDING)
option(WITH_FLOATING_POINT "Use floating point code in TinyCBOR" ON)
option(WITH_TOOLS "Compile the TinyCBOR tools" ON)
endif()
# Include additional modules that are used unconditionally
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CheckLinkerFlag)
include(CheckSymbolExists)
add_library(tinycbor
src/cborencoder.c
src/cborencoder_close_container_checked.c
src/cborerrorstrings.c
src/cborparser.c
src/cborpretty.c
src/cborvalidation.c
src/cbor.h
)
if(WITH_FREESTANDING)
target_compile_options(tinycbor PUBLIC
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-ffreestanding>
)
else()
target_sources(tinycbor PRIVATE
src/cborparser_dup_string.c
src/cborpretty_stdio.c
)
if(WITH_CBOR2JSON)
target_sources(tinycbor PRIVATE
src/cbortojson.c
)
endif()
endif()
if(WITH_FLOATING_POINT)
target_sources(tinycbor PRIVATE
src/cborencoder_float.c
src/cborparser_float.c
)
if(NOT WIN32)
target_link_libraries(tinycbor m)
endif()
else()
target_compile_definitions(tinycbor PUBLIC CBOR_NO_FLOATING_POINT)
endif()
set_target_properties(tinycbor PROPERTIES
# Force this library to link as C and compile as C99, to ensure we
# don't use something of a newer language level.
LINKER_LANGUAGE C
C_EXTENSIONS OFF
C_STANDARD 99
# Set version and output name
VERSION "0.${PROJECT_VERSION}"
SOVERSION "0"
)
if(BUILD_SHARED_LIBS)
set_target_properties(tinycbor PROPERTIES C_VISIBILITY_PRESET hidden)
# Check if the linker supports "-z defs" (a.k.a "--no-undefined")
check_linker_flag(C "-Wl,-z,defs" HAVE_NO_UNDEFINED)
if(HAVE_NO_UNDEFINED)
target_link_options(tinycbor PRIVATE "-Wl,-z,defs")
endif()
else()
target_compile_definitions(tinycbor PUBLIC CBOR_STATIC_DEFINE)
endif()
# Enable warnings
target_compile_options(tinycbor PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:-W3>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:
-Wall -Wextra
-Werror=format-security
-Werror=incompatible-pointer-types
-Werror=implicit-function-declaration
-Werror=int-conversion
>
)
# Generate export macros
generate_export_header(tinycbor
BASE_NAME "cbor"
EXPORT_MACRO_NAME "CBOR_API"
EXPORT_FILE_NAME "tinycbor-export.h"
)
# Generate version header
configure_file(src/tinycbor-version.h.in tinycbor-version.h)
# Check for open_memstream and store the result in HAVE_OPEN_MEMSTREAM
check_symbol_exists(open_memstream stdio.h HAVE_OPEN_MEMSTREAM)
check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN)
check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE)
if(NOT HAVE_OPEN_MEMSTREAM)
if (HAVE_OPEN_FUNOPEN AND HAVE_OPEN_FOPENCOOKIE)
message(STATUS "using open_memstream implementation")
target_sources(tinycbor PRIVATE src/open_memstream.c)
else()
target_compile_definitions(tinycbor PRIVATE WITHOUT_OPEN_MEMSTREAM)
message(WARNING "funopen and fopencookie unavailable, open_memstream can not be implemented and conversion to JSON will not work properly!")
endif()
endif()
target_include_directories(tinycbor
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
PUBLIC "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>"
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
install(FILES
src/cbor.h
${CMAKE_BINARY_DIR}/tinycbor-version.h
${CMAKE_BINARY_DIR}/tinycbor-export.h
TYPE INCLUDE
)
install(
TARGETS tinycbor
EXPORT "${TARGETS_EXPORT_NAME}"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # import library
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so files are libraries
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # .dll files are binaries
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # this does not actually install anything (but used by downstream projects)
)
set(PROJECT_LIBRARIES TinyCBOR)
include(PackageConfig)
if(BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
endif()
if(WITH_TOOLS)
add_subdirectory(tools)
endif()