Debian Codegraph @

build/common.cmake

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # ===========================================================================
2 #
3 # PUBLIC DOMAIN NOTICE
4 # National Center for Biotechnology Information
5 #
6 # This software/database is a "United States Government Work" under the
7 # terms of the United States Copyright Act. It was written as part of
8 # the author's official duties as a United States Government employee and
9 # thus cannot be copyrighted. This software/database is freely available
10 # to the public for use. The National Library of Medicine and the U.S.
11 # Government have not placed any restriction on its use or reproduction.
12 #
13 # Although all reasonable efforts have been taken to ensure the accuracy
14 # and reliability of the software and data, the NLM and the U.S.
15 # Government do not and cannot warrant the performance or results that
16 # may be obtained by using this software or data. The NLM and the U.S.
17 # Government disclaim all warranties, express or implied, including
18 # warranties of performance, merchantability or fitness for any particular
19 # purpose.
20 #
21 # Please cite the author in any work or product based on this material.
22 #
23 # ===========================================================================
24
25 #
26 # for a static library target, redirect its output to the public library
27 # location (.../lib) and create versioned names and symlinks
28 #
29
30 function( MSVS_StaticRuntime name )
31 if( WIN32 )
32 set_property(TARGET ${name} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
33 endif()
34 endfunction()
35
36 function( MSVS_DLLRuntime name )
37 if( WIN32 )
38 set_property(TARGET ${name} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
39 endif()
40 endfunction()
41
42 function( GenerateStaticLibsWithDefs target_name sources compile_defs )
43 add_library( ${target_name} STATIC ${sources} )
44 if( NOT "" STREQUAL "${compile_defs}" )
45 target_compile_definitions( ${target_name} PRIVATE ${compile_defs} )
46 endif()
47 if( WIN32 AND NOT _NCBIVDB_CFG_PACKAGING)
48 MSVS_StaticRuntime( ${target_name} )
49 add_library( ${target_name}-md STATIC ${sources} )
50 if(NOT "" STREQUAL "${compile_defs}" )
51 target_compile_definitions( ${target_name}-md PRIVATE ${compile_defs} )
52 endif()
53 MSVS_DLLRuntime( ${target_name}-md )
54 endif()
55
56 if( RUN_SANITIZER_TESTS )
57 set( asan_defs "-fsanitize=address" "-fsanitize=undefined" )
58 add_library( "${target_name}-asan" STATIC ${sources} )
59 if( NOT "" STREQUAL "${compile_defs}" )
60 target_compile_definitions( "${target_name}-asan" PRIVATE ${compile_defs} )
61 endif()
62 target_compile_options( "${target_name}-asan" PUBLIC ${asan_defs} )
63 target_link_options( "${target_name}-asan" PUBLIC ${asan_defs} )
64
65 set( tsan_defs "-fsanitize=thread" "-fsanitize=undefined" )
66 add_library( "${target_name}-tsan" STATIC ${sources} )
67 if( NOT "" STREQUAL "${compile_defs}" )
68 target_compile_definitions( "${target_name}-tsan" PRIVATE ${compile_defs} )
69 endif()
70 target_compile_options( "${target_name}-tsan" PUBLIC ${tsan_defs} )
71 target_link_options( "${target_name}-tsan" PUBLIC ${tsan_defs} )
72 endif()
73 endfunction()
74
75 function( GenerateStaticLibs target_name sources )
76 GenerateStaticLibsWithDefs( ${target_name} "${sources}" "" )
77 endfunction()
78
79
80 function( ExportStatic name install )
81 if(_NCBIVDB_CFG_PACKAGING)
82 return()
83 endif()
84 # the output goes to .../lib
85 if( SINGLE_CONFIG )
86 # make the output name versioned, create all symlinks
87 set_target_properties( ${name} PROPERTIES
88 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
89 add_custom_command(TARGET ${name}
90 POST_BUILD
91 COMMAND rm -f lib${name}.a.${VERSION}
92 COMMAND mv lib${name}.a lib${name}.a.${VERSION}
93 COMMAND ln -f -s lib${name}.a.${VERSION} lib${name}.a.${MAJVERS}
94 COMMAND ln -f -s lib${name}.a.${MAJVERS} lib${name}.a
95 COMMAND ln -f -s lib${name}.a lib${name}-static.a
96 WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
97 )
98
99 set_property(
100 TARGET ${name}
101 APPEND
102 PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a.${VERSION};${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a.${MAJVERS};${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a;${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}-static.a"
103 )
104
105 if ( ${install} )
106 install( FILES ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a.${VERSION}
107 ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a.${MAJVERS}
108 ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.a
109 ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}-static.a
110 DESTINATION ${INST_LIBDIR}
111 )
112 endif()
113 else()
114 set_target_properties( ${name} PROPERTIES
115 ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG})
116 set_target_properties( ${name} PROPERTIES
117 ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE})
118 # if ( ${install} )
119 # install( TARGETS ${name} DESTINATION ${INST_LIBDIR} )
120 # endif()
121 endif()
122 endfunction()
123
124 #
125 # create versioned names and symlinks for a shared library
126 #
127 function(MakeLinksShared target name install)
128 if(_NCBIVDB_CFG_PACKAGING)
129 return()
130 endif()
131 set_target_properties( ${target} PROPERTIES OUTPUT_NAME ${name} )
132 if( SINGLE_CONFIG )
133 if( ${OS} STREQUAL "mac" )
134 set( LIBSUFFIX ".${VERSION}.${SHLX}" )
135 set( MAJLIBSUFFIX ".${MAJVERS}.${SHLX}" )
136 else()
137 set( LIBSUFFIX ".${SHLX}.${VERSION}" )
138 set( MAJLIBSUFFIX ".${SHLX}.${MAJVERS}" )
139 endif()
140 add_custom_command(TARGET ${target}
141 POST_BUILD
142 COMMAND rm -f lib${name}${LIBSUFFIX}
143 COMMAND mv lib${name}.${SHLX} lib${name}${LIBSUFFIX}
144 COMMAND ln -f -s lib${name}${LIBSUFFIX} lib${name}${MAJLIBSUFFIX}
145 COMMAND ln -f -s lib${name}${MAJLIBSUFFIX} lib${name}.${SHLX}
146 WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
147 )
148
149 set_property(
150 TARGET ${target}
151 APPEND
152 PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}${LIBSUFFIX};${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}${MAJLIBSUFFIX};${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.${SHLX}"
153 )
154
155 if ( ${install} )
156 install( PROGRAMS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}${LIBSUFFIX}
157 ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}${MAJLIBSUFFIX}
158 ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${name}.${SHLX}
159 DESTINATION ${INST_LIBDIR}
160 )
161 endif()
162 else()
163 set_target_properties( ${target} PROPERTIES
164 ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
165 set_target_properties( ${target} PROPERTIES
166 ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE})
167 # if ( ${install} )
168 # install( TARGETS ${target}
169 # ARCHIVE DESTINATION ${INST_BINDIR}
170 # RUNTIME DESTINATION ${INST_BINDIR}
171 # )
172 # endif()
173 endif()
174 endfunction()
175
176 #
177 # for a static library target, create a public shared target with the same base name and contents
178 #
179 function(ExportShared lib install extra_libs)
180 if(_NCBIVDB_CFG_PACKAGING)
181 return()
182 endif()
183 get_target_property( src ${lib} SOURCES )
184 add_library( ${lib}-shared SHARED ${src} )
185 target_link_libraries( ${lib}-shared ${extra_libs} )
186 MakeLinksShared( ${lib}-shared ${lib} ${install} )
187
188 if( WIN32 )
189 MSVS_StaticRuntime( ${lib}-shared )
190
191 # on Windows, add a DLL built with /MD
192 add_library( ${lib}-shared-md SHARED ${src} )
193 target_link_libraries( ${lib}-shared-md ${extra_libs})
194 MSVS_DLLRuntime( ${lib}-shared-md )
195 MakeLinksShared( ${lib}-shared-md ${lib}-md ${install} )
196 endif()
197 endfunction()
198
199 function( BuildExecutableForTest exe_name sources libraries )
200 add_executable( ${exe_name} ${sources} )
201 if(NOT _NCBIVDB_CFG_PACKAGING)
202 MSVS_StaticRuntime( ${exe_name} )
203 endif()
204 target_link_libraries( ${exe_name} ${libraries} )
205 endfunction()
206
207 function( AddExecutableTest test_name sources libraries )
208 BuildExecutableForTest( "${test_name}" "${sources}" "${libraries}" )
209 add_test( NAME ${test_name} COMMAND ${test_name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
210
211 if( RUN_SANITIZER_TESTS )
212 set( asan_defs "-fsanitize=address" "-fsanitize=undefined" )
213 BuildExecutableForTest( "${test_name}_asan" "${sources}" "${libraries}" )
214 target_compile_options( "${test_name}_asan" PRIVATE ${asan_defs} )
215 target_link_options( "${test_name}_asan" PRIVATE ${asan_defs} )
216 add_test( NAME "${test_name}_asan" COMMAND "${test_name}_asan" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
217
218 set( tsan_defs "-fsanitize=thread" "-fsanitize=undefined" )
219 BuildExecutableForTest( "${test_name}_tsan" "${sources}" "${libraries}" )
220 target_compile_options( "${test_name}_tsan" PRIVATE ${tsan_defs} )
221 target_link_options( "${test_name}_tsan" PRIVATE ${tsan_defs} )
222 add_test( NAME "${test_name}_tsan" COMMAND "${test_name}_tsan" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
223 endif()
224 endfunction()
225
226 include(CheckIncludeFileCXX)
227 #check_include_file_cxx(mbedtls/md.h HAVE_MBEDTLS_H)
228 set(HAVE_MBEDTLS_H 0) # TODO: disabling system mbedtls since it may be outdated
229 set( MBEDTLS_LIBS mbedx509 mbedtls mbedcrypto ) # need to link against mbedtls in any case: system or local mbedtls
230 set(CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIBS})
231 if ( HAVE_MBEDTLS_H )
232 include(CheckCXXSourceRuns)
233 check_cxx_source_runs("
234 #include <stdio.h>
235 #include \"mbedtls/md.h\"
236 #include \"mbedtls/sha256.h\"
237 int main(int argc, char *argv[]) {
238 mbedtls_md_context_t ctx;
239 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
240 mbedtls_md_init(&ctx);
241 printf(\"test p: %p\", ctx.md_ctx);
242 }
243 " HAVE_MBEDTLS_F)
244 else()
245 set(MBEDTLS_LIBS "")
246 endif()