-
Notifications
You must be signed in to change notification settings - Fork 10
/
CMakeLists.txt
272 lines (230 loc) · 8.47 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
cmake_minimum_required(VERSION 3.1)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/toolchain.cmake"
CACHE FILEPATH "Toolchain to use for building this package")
project(opentracing-c VERSION 0.0.1)
include(CheckCCompilerFlag)
include(CheckIncludeFile)
include(CheckTypeSize)
include(CMakeDependentOption)
include(CTest)
include(GenerateExportHeader)
include(CheckAttributes)
include(GenerateDocumentation)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
function(add_c_flag flags_var new_flag)
string(MAKE_C_IDENTIFIER "have${new_flag}" new_flag_var)
check_c_compiler_flag(${new_flag} ${new_flag_var})
if(${new_flag_var})
set(${flags_var} ${${flags_var}} ${new_flag} PARENT_SCOPE)
endif()
endfunction()
add_c_flag(flags "-Wc++-compat")
add_c_flag(flags "-Werror")
add_c_flag(flags "-pedantic")
if("${CMAKE_C_COMPILER}" MATCHES "Clang")
add_c_flag(flags "-Weverything")
else()
add_c_flag(flags "-Wall")
add_c_flag(flags "-Wextra")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(debug_build ON)
endif()
cmake_dependent_option(OPENTRACINGC_COVERAGE "Enable code coverage" OFF
"BUILD_TESTING;debug_build" OFF)
if(OPENTRACINGC_COVERAGE)
include(CodeCoverage)
append_coverage_compiler_flags(coverage_flags)
string(REPLACE " " ";" coverage_flags "${coverage_flags}")
set(COVERAGE_EXCLUDES "test/*")
endif()
set(srcs
"src/opentracing-c/common.h"
"src/opentracing-c/destructible.h"
"src/opentracing-c/dynamic_load.c"
"src/opentracing-c/dynamic_load.h"
"src/opentracing-c/propagation.h"
"src/opentracing-c/span.h"
"src/opentracing-c/tracer.c"
"src/opentracing-c/tracer.h"
"src/opentracing-c/value.h")
add_library(opentracingc-static STATIC ${srcs})
add_library(opentracingc SHARED ${srcs})
generate_export_header(opentracingc
EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/src/opentracing-c/visibility.h")
set_target_properties(opentracingc-static PROPERTIES
OUTPUT_NAME opentracingc
COMPILE_DEFINITIONS OPENTRACINGC_STATIC_DEFINE)
set_target_properties(opentracingc PROPERTIES
C_VISIBILITY_INLINES hidden
C_VISIBILITY_PRESET hidden)
check_include_file("sys/time.h" HAVE_SYS_TIME_H)
check_type_size("struct timespec" OPENTRACINGC_USE_TIMESPEC)
check_attributes()
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/src/opentracing-c/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/src/opentracing-c/config.h" @ONLY)
foreach(lib opentracingc opentracingc-static)
target_include_directories(${lib} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>)
target_compile_options(${lib} PRIVATE ${flags})
if(OPENTRACINGC_HAVE_WEAK_SYMBOLS)
target_link_libraries(${lib} PUBLIC dl)
endif()
endforeach()
option(OPENTRACINGC_LINT "Run clang-tidy on source files" ON)
find_program(CLANG_TIDY_PATH "clang-tidy")
cmake_dependent_option(
OPENTRACINGC_LINT
"Enable running clang-tidy on sources during compilation"
ON
"CLANG_TIDY_PATH"
OFF)
if(OPENTRACINGC_LINT)
set_target_properties(opentracingc PROPERTIES
C_CLANG_TIDY "${CLANG_TIDY_PATH}")
endif()
if(BUILD_TESTING)
set(test_src "test/tracer_test.c")
if(BUILD_SHARED_LIBS AND OPENTRACINGC_HAVE_WEAK_SYMBOLS)
set(build_dynamic_load_test ON)
endif()
if(build_dynamic_load_test)
list(APPEND test_src "test/dynamic_load_test.c")
endif()
foreach(test_case_src ${test_src})
get_filename_component(test_component ${test_case_src} NAME_WE)
add_executable(${test_component} "${test_case_src}")
target_link_libraries(${test_component} PUBLIC opentracingc-static)
add_test(${test_component} ${test_component})
list(APPEND test_executables ${test_component})
endforeach()
if(build_dynamic_load_test)
add_library(mock_tracing_lib SHARED "test/mock_tracing_lib.c")
target_compile_definitions(mock_tracing_lib PRIVATE DEFINE_HOOK)
target_link_libraries(mock_tracing_lib PUBLIC opentracingc)
add_library(mock_tracing_lib_no_hook SHARED "test/mock_tracing_lib.c")
target_link_libraries(mock_tracing_lib_no_hook PUBLIC opentracingc)
add_library(mock_tracing_lib_bad_version SHARED "test/mock_tracing_lib.c")
target_compile_definitions(mock_tracing_lib_bad_version PRIVATE
BAD_VERSION DEFINE_HOOK)
target_link_libraries(mock_tracing_lib_bad_version PUBLIC opentracingc)
add_library(mock_tracing_lib_hook_return_null SHARED
"test/mock_tracing_lib.c")
target_compile_definitions(mock_tracing_lib_hook_return_null PRIVATE
RETURN_NULL_NO_ERROR DEFINE_HOOK)
target_link_libraries(mock_tracing_lib_hook_return_null PUBLIC opentracingc)
file(GENERATE
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/test/mock_tracing_lib_names.h"
INPUT "${CMAKE_CURRENT_SOURCE_DIR}/test/mock_tracing_lib_names.h.in")
target_include_directories(dynamic_load_test PUBLIC
"${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/test")
endif()
if(OPENTRACINGC_COVERAGE)
target_compile_options(opentracingc-static PRIVATE ${coverage_flags})
setup_target_for_coverage(NAME unit_test_coverage EXECUTABLE ${test_executables})
endif()
endif()
option(OPENTRACINGC_BUILD_SNIPPETS "Build opentracing-c snippets" ON)
if(OPENTRACINGC_BUILD_SNIPPETS)
add_executable(example
snippets/existing_span.c
snippets/extract.c
snippets/inject.c
snippets/main.c
snippets/new_span.c
snippets/text_map.c
snippets/text_map_iterator.c
snippets/tracing_impl.c)
target_include_directories(example PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/snippets>)
target_link_libraries(example opentracingc)
set(snippet_files
snippets/existing_span_snippet.c
snippets/extract_snippet.c
snippets/inject_snippet.c
snippets/main_snippet.c
snippets/new_span_snippet.c)
foreach(snippet_file ${snippet_files})
get_filename_component(snippet_name ${snippet_file} NAME_WE)
file(READ ${snippet_file} ${snippet_name})
endforeach()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/README.adoc.in
${CMAKE_CURRENT_SOURCE_DIR}/README.adoc)
endif()
generate_documentation()
# Installation (https://github.com/forexample/package-example)
# Layout. This works for all platforms:
# * <prefix>/lib/cmake/<PROJECT-NAME>
# * <prefix>/lib/
# * <prefix>/include/
set(config_install_dir "lib/cmake/${PROJECT_NAME}")
set(include_install_dir "include")
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
# Configuration
set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(namespace "${PROJECT_NAME}::")
# Include module with fuction 'write_basic_package_version_file'
include(CMakePackageConfigHelpers)
# Configure '<PROJECT-NAME>ConfigVersion.cmake'
# Use:
# * PROJECT_VERSION
write_basic_package_version_file(
"${version_config}" COMPATIBILITY SameMajorVersion
)
# Configure '<PROJECT-NAME>Config.cmake'
# Use variables:
# * TARGETS_EXPORT_NAME
# * PROJECT_NAME
configure_package_config_file(
"cmake/Config.cmake.in"
"${project_config}"
INSTALL_DESTINATION "${config_install_dir}"
)
# Targets:
# * <prefix>/lib/libopentracingc.so
# * <prefix>/lib/libopentracingc.a
# * header location after install: <prefix>/include/opentracing-c/tracer.h
# * headers can be included by C++ code `#include <opentracing-c/tracer.h>`
install(
TARGETS opentracingc opentracingc-static
EXPORT "${TARGETS_EXPORT_NAME}"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "${include_install_dir}"
)
# Headers:
# * src/opentracing-c/tracer.h -> <prefix>/include/src/tracer.h
install(
DIRECTORY "src/opentracing-c"
DESTINATION "${include_install_dir}"
FILES_MATCHING PATTERN "*.h"
)
# Generated headers:
# * ${CMAKE_CURRENT_BINARY_DIR}/.../opentracing-c/config.h ->
# <prefix>/include/opentracing-c/config.h
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/src/opentracing-c"
DESTINATION "${include_install_dir}"
FILES_MATCHING PATTERN "*.h"
)
# Config
# * <prefix>/lib/cmake/opentracing-c/opentracing-cConfig.cmake
# * <prefix>/lib/cmake/opentracing-c/opentracing-cConfigVersion.cmake
install(
FILES "${project_config}" "${version_config}"
DESTINATION "${config_install_dir}"
)
# Config
# * <prefix>/lib/cmake/opentracing-c/opentracing-cTargets.cmake
install(
EXPORT "${TARGETS_EXPORT_NAME}"
NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}"
)