forked from nanomsg/nng
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NNGHelpers.cmake
167 lines (147 loc) · 6 KB
/
NNGHelpers.cmake
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
#
# Copyright 2020 Staysail Systems, Inc. <[email protected]>
#
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
# file was obtained (LICENSE.txt). A copy of the license may also be
# found online at https://opensource.org/licenses/MIT.
#
# Some NNG helper functions.
include(CheckFunctionExists)
include(CheckSymbolExists)
include(CheckStructHasMember)
include(CheckLibraryExists)
include(CheckCSourceCompiles)
# nng_sources adds library sources using files in the current directory.
function(nng_sources)
foreach (f ${ARGN})
target_sources(nng PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${f})
target_sources(nng_testing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${f})
endforeach ()
endfunction()
# nng_headers adds library sources as public headers taken rooted at the include/ directory.
function(nng_headers)
foreach (f ${ARGN})
target_sources(nng PRIVATE ${PROJECT_SOURCE_DIR}/include/${f})
target_sources(nng_testing PRIVATE ${PROJECT_SOURCE_DIR}/include/${f})
endforeach ()
endfunction()
# nng_defines adds defines unconditionally.
# The public library keeps these defines private, but the test library exposes these definitions
# as some of our test cases would like to know details about how the library was compiled
# as that may modify the tests themselves.
function(nng_defines)
target_compile_definitions(nng PRIVATE ${ARGN})
target_compile_definitions(nng_testing PUBLIC ${ARGN})
target_compile_definitions(nng_private INTERFACE ${ARGN})
endfunction()
# nng_find_package looks up required package and adds dependency to the cmake config.
macro(nng_find_package PACKAGE_NAME)
find_package(${PACKAGE_NAME} REQUIRED)
list(APPEND NNG_PKGS ${PACKAGE_NAME})
list(REMOVE_DUPLICATES NNG_PKGS)
set(NNG_PKGS ${NNG_PKGS} CACHE INTERNAL "nng package dependencies" FORCE)
endmacro()
# nng_link_libraries adds link dependencies to the libraries.
function(nng_link_libraries)
target_link_libraries(nng PRIVATE ${ARGN})
target_link_libraries(nng_testing PRIVATE ${ARGN})
endfunction()
# nng_include_directories adds include directories.
function(nng_include_directories)
target_include_directories(nng PRIVATE ${ARGN})
target_include_directories(nng_testing PRIVATE ${ARGN})
endfunction()
# nng_sources_if adds the sources unconditionally to the test library,
# but conditionally to the production library. This allows us to get
# full test coverage while allowing a minimized delivery.
function(nng_sources_if COND)
foreach (f ${ARGN})
if (${COND})
target_sources(nng PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${f})
endif ()
target_sources(nng_testing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${f})
endforeach ()
endfunction()
function(nng_headers_if COND)
foreach (f ${ARGN})
if (COND)
target_sources(nng PRIVATE ${PROJECT_SOURCE_DIR}/include/${f})
endif ()
target_sources(nng_testing PRIVATE ${PROJECT_SOURCE_DIR}/include/${f})
endforeach ()
endfunction()
function(nng_defines_if COND)
if (${COND})
target_compile_definitions(nng PRIVATE ${ARGN})
target_compile_definitions(nng_private INTERFACE ${ARGN})
endif ()
target_compile_definitions(nng_testing PUBLIC ${ARGN})
endfunction()
function(nng_link_libraries_if COND)
if (${COND})
target_link_libraries(nng PRIVATE ${ARGN})
endif ()
target_link_libraries(nng_testing PRIVATE ${ARGN})
endfunction()
function(nng_test NAME)
if (NNG_TESTS)
add_executable(${NAME} ${NAME}.c ${ARGN})
target_link_libraries(${NAME} nng_testing)
target_include_directories(${NAME} PRIVATE
${PROJECT_SOURCE_DIR}/tests
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/include)
add_test(NAME ${NNG_TEST_PREFIX}.${NAME} COMMAND ${NAME} -t -v)
set_tests_properties(${NNG_TEST_PREFIX}.${NAME} PROPERTIES TIMEOUT 180)
endif ()
endfunction()
function(nng_test_if COND NAME)
if (${COND} AND NNG_TESTS)
add_executable(${NAME} ${NAME}.c ${ARGN})
target_link_libraries(${NAME} nng_testing)
target_include_directories(${NAME} PRIVATE
${PROJECT_SOURCE_DIR}/tests
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/include)
add_test(NAME ${NNG_TEST_PREFIX}.${NAME} COMMAND ${NAME} -t -v)
set_tests_properties(${NNG_TEST_PREFIX}.${NAME} PROPERTIES TIMEOUT 180)
endif ()
endfunction()
function(nng_check_func SYM DEF)
check_function_exists(${SYM} ${DEF})
if (${DEF})
target_compile_definitions(nng PRIVATE ${DEF}=1)
target_compile_definitions(nng_testing PUBLIC ${DEF}=1)
target_compile_definitions(nng_private INTERFACE ${DEF}=1)
endif ()
endfunction(nng_check_func)
function(nng_check_sym SYM HDR DEF)
check_symbol_exists(${SYM} ${HDR} ${DEF})
if (${DEF})
target_compile_definitions(nng PRIVATE ${DEF}=1)
target_compile_definitions(nng_testing PUBLIC ${DEF}=1)
target_compile_definitions(nng_private INTERFACE ${DEF}=1)
endif ()
endfunction(nng_check_sym)
function(nng_check_lib LIB SYM DEF)
check_library_exists(${LIB} ${SYM} "" ${DEF})
if (${DEF})
target_compile_definitions(nng PRIVATE ${DEF}=1)
target_compile_definitions(nng_testing PUBLIC ${DEF}=1)
target_compile_definitions(nng_private INTERFACE ${DEF}=1)
target_link_libraries(nng PRIVATE ${LIB})
target_link_libraries(nng_testing PRIVATE ${LIB})
endif ()
endfunction(nng_check_lib)
function(nng_check_struct_member STR MEM HDR DEF)
check_struct_has_member("struct ${STR}" ${MEM} ${HDR} ${DEF})
if (${DEF})
target_compile_definitions(nng PRIVATE ${DEF}=1)
target_compile_definitions(nng_testing PUBLIC ${DEF}=1)
target_compile_definitions(nng_private INTERFACE ${DEF}=1)
endif ()
endfunction(nng_check_struct_member)
macro(nng_directory DIR)
set(NNG_TEST_PREFIX ${NNG_TEST_PREFIX}.${DIR})
endmacro(nng_directory)