]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - CMakeLists.txt
Make CMakeLists.txt work better with static OpenSSL libraries
[openssl-gost/engine.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2 project(gost-engine LANGUAGES C)
3
4 include(GNUInstallDirs)
5 include(CheckLibraryExists)
6 include(CheckFunctionExists)
7 include(CheckCSourceRuns)
8
9 enable_testing()
10
11 find_package(OpenSSL 3.0 REQUIRED)
12
13 if (NOT DEFINED OPENSSL_ROOT_DIR)
14   get_filename_component(OPENSSL_ROOT_DIR ${OPENSSL_INCLUDE_DIR} DIRECTORY)
15   message(STATUS "Setting OpenSSL root: ${OPENSSL_ROOT_DIR}")
16 endif()
17 find_program(OPENSSL_PROGRAM openssl
18   PATHS ${OPENSSL_ROOT_DIR} PATH_SUFFIXES apps bin NO_DEFAULT_PATH)
19 message(STATUS "Found OpenSSL application: ${OPENSSL_PROGRAM}")
20 include_directories(${OPENSSL_INCLUDE_DIR})
21
22 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
23   set(CMAKE_C_FLAGS_RELEASE -O2)
24   set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
25   set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -ggdb")
26   add_compile_options(-Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -Qunused-arguments -Wno-deprecated-declarations)
27 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
28   set(CMAKE_C_FLAGS_RELEASE -O2)
29   set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
30   set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -ggdb")
31   add_compile_options(-Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -Wno-error=unknown-pragmas -Wno-error=pragmas -Wno-deprecated-declarations)
32 elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
33   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
34   add_definitions(-D_CRT_DEPRECATED_NO_WARNINGS)
35   add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
36   add_compile_options(/MP /WX /W4 /wd4100 /wd4267 /wd4206 /wd4706 /wd4244 /wd4115)
37 endif()
38
39 if (ASAN)
40   message(STATUS "address sanitizer enabled")
41   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g3 -fno-omit-frame-pointer")
42   set(SKIP_PERL_TESTS 1)
43 endif()
44
45 # DEPRECATEDIN_3_0 CMAC
46 set_source_files_properties(gost_omac.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
47 # DEPRECATEDIN_3_0 HMAC
48 set_source_files_properties(gost_keyexpimp.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
49
50 set(CMAKE_C_STANDARD 90)
51 CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME_C)
52 CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
53 if(HAVE_CLOCK_GETTIME_RT AND NOT HAVE_CLOCK_GETTIME_C)
54   set(CLOCK_GETTIME_LIB rt)
55 endif()
56
57 include (TestBigEndian)
58 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
59 if(IS_BIG_ENDIAN)
60  message(STATUS "BIG_ENDIAN")
61 else()
62  message(STATUS "LITTLE_ENDIAN")
63  add_definitions(-DL_ENDIAN)
64 endif()
65
66 check_c_source_runs("
67   #ifdef _MSC_VER
68   # include <intrin.h>
69   #else
70   # include <x86intrin.h>
71   #endif
72   int main(void) {
73     unsigned long long x = -1, y = 1, r;
74     unsigned char cf;
75     cf = _addcarry_u64(1, x, y, &r);
76     return !(cf == 1 && r == 1);
77   }
78   " ADDCARRY_U64)
79 if (ADDCARRY_U64)
80   add_definitions(-DHAVE_ADDCARRY_U64)
81 endif()
82
83 check_c_source_runs("
84   int main(void) {
85     char buf[16] = { 0, 1, 2 };
86     int *p = (int *)(buf + 1);
87     int *q = (int *)(buf + 2);
88     return (*p == *q);
89   }
90   " RELAXED_ALIGNMENT)
91 if (NOT RELAXED_ALIGNMENT)
92   add_definitions(-DSTRICT_ALIGNMENT)
93 endif()
94
95 set(BIN_DIRECTORY bin)
96
97 set(OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BIN_DIRECTORY})
98
99 #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
100 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
101 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
102
103 # Remove when https://gitlab.kitware.com/cmake/cmake/issues/18525 is addressed
104 set(OPENSSL_ENGINES_DIR "" CACHE PATH "OpenSSL Engines Directory")
105 if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
106         include(FindPkgConfig)
107         pkg_get_variable(OPENSSL_ENGINES_DIR libcrypto enginesdir)
108         if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
109                 message( FATAL_ERROR "Unable to discover the OpenSSL engines directory. Provide the path using -DOPENSSL_ENGINES_DIR" )
110         endif()
111 endif()
112
113 set(GOST_89_SOURCE_FILES
114         gost89.c
115         gost89.h
116         )
117
118 set(GOST_HASH_SOURCE_FILES
119         gosthash.c
120         gosthash.h
121         )
122
123 set(GOST_HASH_2012_SOURCE_FILES
124         gosthash2012.c
125         gosthash2012.h
126         gosthash2012_const.h
127         gosthash2012_precalc.h
128         gosthash2012_ref.h
129         gosthash2012_sse2.h
130         )
131
132 set(GOST_GRASSHOPPER_SOURCE_FILES
133         gost_grasshopper.h
134         gost_grasshopper_core.h
135         gost_grasshopper_core.c
136         gost_grasshopper_defines.h
137         gost_grasshopper_defines.c
138         gost_grasshopper_math.h
139         gost_grasshopper_galois_precompiled.c
140         gost_grasshopper_precompiled.c
141         gost_grasshopper_cipher.h
142         gost_grasshopper_cipher.c
143         )
144
145 set(GOST_CORE_SOURCE_FILES
146         e_gost_err.c
147         e_gost_err.h
148         gost_ameth.c
149         gost_pmeth.c
150         gost_ctl.c
151         gost_asn1.c
152         gost_crypt.c
153         gost_keywrap.c
154         gost_keywrap.h
155         gost_md.c
156         gost_md2012.c
157         gost_omac.c
158         gost_omac_acpkm.c
159         gost_gost2015.c
160         gost_lcl.h
161         gost_params.c
162         gost_keyexpimp.c
163         )
164
165 set(GOST_EC_SOURCE_FILES
166         gost_ec_keyx.c
167         gost_ec_sign.c
168         ecp_id_GostR3410_2001_CryptoPro_A_ParamSet.c
169         ecp_id_GostR3410_2001_CryptoPro_B_ParamSet.c
170         ecp_id_GostR3410_2001_CryptoPro_C_ParamSet.c
171         ecp_id_GostR3410_2001_TestParamSet.c
172         ecp_id_tc26_gost_3410_2012_256_paramSetA.c
173         ecp_id_tc26_gost_3410_2012_512_paramSetA.c
174         ecp_id_tc26_gost_3410_2012_512_paramSetB.c
175         ecp_id_tc26_gost_3410_2012_512_paramSetC.c
176         )
177
178 set (GOST_OMAC_SOURCE_FILES
179         gost_omac.c
180         gost_omac_acpkm.c
181         )
182
183 set(GOST_LIB_SOURCE_FILES
184         ${GOST_CORE_SOURCE_FILES}
185         ${GOST_89_SOURCE_FILES}
186         ${GOST_HASH_SOURCE_FILES}
187         ${GOST_HASH_2012_SOURCE_FILES}
188         ${GOST_GRASSHOPPER_SOURCE_FILES}
189         ${GOST_EC_SOURCE_FILES}
190         ${GOST_OMAC_SOURCE_FILES}
191         )
192
193 set(GOST_ENGINE_SOURCE_FILES
194         gost_eng.c
195         )
196
197 set(TEST_ENVIRONMENT
198         CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
199         PERL5LIB=${CMAKE_CURRENT_SOURCE_DIR}/test
200         OPENSSL_ENGINES=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
201         OPENSSL_PROGRAM=${OPENSSL_PROGRAM}
202         OPENSSL_CRYPTO_LIBRARY=${OPENSSL_CRYPTO_LIBRARY}
203         OPENSSL_CONF=${CMAKE_CURRENT_SOURCE_DIR}/test/engine.cnf
204         )
205 add_executable(test_digest test_digest.c)
206 target_link_libraries(test_digest ${OPENSSL_CRYPTO_LIBRARIES})
207 add_test(NAME digest COMMAND test_digest)
208 set_tests_properties(digest PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
209
210 add_executable(test_ciphers test_ciphers.c)
211 target_link_libraries(test_ciphers ${OPENSSL_CRYPTO_LIBRARIES})
212 add_test(NAME ciphers COMMAND test_ciphers)
213 set_tests_properties(ciphers PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
214
215 # test_curves is an internals testing program, it doesn't need a test env
216 add_executable(test_curves test_curves.c)
217 target_link_libraries(test_curves gost_core ${OPENSSL_CRYPTO_LIBRARIES})
218 add_test(NAME curves COMMAND test_curves)
219
220 add_executable(test_params test_params.c)
221 target_link_libraries(test_params ${OPENSSL_CRYPTO_LIBRARIES})
222 add_test(NAME parameters COMMAND test_params)
223 set_tests_properties(parameters PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
224
225 add_executable(test_derive test_derive.c)
226 target_link_libraries(test_derive ${OPENSSL_CRYPTO_LIBRARIES})
227 add_test(NAME derive COMMAND test_derive)
228 set_tests_properties(derive PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
229
230 add_executable(test_sign test_sign.c)
231 target_link_libraries(test_sign ${OPENSSL_CRYPTO_LIBRARIES})
232 add_test(NAME sign/verify COMMAND test_sign)
233 set_tests_properties(sign/verify PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
234
235 add_executable(test_tls test_tls.c)
236 target_link_libraries(test_tls ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES})
237 add_test(NAME TLS COMMAND test_tls)
238 set_tests_properties(TLS PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
239
240 add_executable(test_context test_context.c)
241 target_link_libraries(test_context ${OPENSSL_CRYPTO_LIBRARIES})
242 add_test(NAME context COMMAND test_context)
243 set_tests_properties(context PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
244
245 # test_keyexpimp is an internals testing program, it doesn't need a test env
246 add_executable(test_keyexpimp test_keyexpimp.c)
247 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
248 target_link_libraries(test_keyexpimp gost_core ${OPENSSL_CRYPTO_LIBRARIES})
249 add_test(NAME keyexpimp COMMAND test_keyexpimp)
250
251 # test_gost89 is an internals testing program, it doesn't need a test env
252 add_executable(test_gost89 test_gost89.c)
253 target_link_libraries(test_gost89 gost_core ${OPENSSL_CRYPTO_LIBRARIES})
254 add_test(NAME gost89 COMMAND test_gost89)
255
256 if(NOT SKIP_PERL_TESTS)
257     execute_process(COMMAND perl -MTest2::V0 -e ""
258         ERROR_QUIET RESULT_VARIABLE HAVE_TEST2_V0)
259     if(NOT HAVE_TEST2_V0)
260         add_test(NAME engine
261             COMMAND prove --merge -PWrapOpenSSL ${CMAKE_CURRENT_SOURCE_DIR}/test)
262         set_tests_properties(engine PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
263     else()
264       message(STATUS "No Test2::V0 perl module (engine tests skipped)")
265     endif()
266 endif()
267
268 add_executable(sign benchmark/sign.c)
269 target_link_libraries(sign gost_core ${OPENSSL_CRYPTO_LIBRARIES} ${CLOCK_GETTIME_LIB})
270
271 # All that may need to load just built engine will have path to it defined.
272 set(BINARY_TESTS_TARGETS
273         test_digest
274         test_ciphers
275         test_curves
276         test_params
277         test_derive
278         test_sign
279         test_context
280         test_keyexpimp
281         test_gost89
282         test_tls
283         )
284 set_property(TARGET ${BINARY_TESTS_TARGETS} APPEND PROPERTY COMPILE_DEFINITIONS ENGINE_DIR="${OUTPUT_DIRECTORY}")
285
286 add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES})
287 set_target_properties(gost_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
288
289 # The GOST engine in module form
290 add_library(gost_engine MODULE ${GOST_ENGINE_SOURCE_FILES})
291 # Set the suffix explicitly to adapt to OpenSSL's idea of what a
292 # module suffix should be
293 set_target_properties(gost_engine PROPERTIES
294   PREFIX "" OUTPUT_NAME "gost" SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
295 target_link_libraries(gost_engine PRIVATE gost_core ${OPENSSL_CRYPTO_LIBRARIES})
296
297 # The GOST engine in library form
298 add_library(lib_gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
299 set_target_properties(lib_gost_engine PROPERTIES
300   COMPILE_DEFINITIONS "BUILDING_ENGINE_AS_LIBRARY"
301   PUBLIC_HEADER gost-engine.h
302   OUTPUT_NAME "gost")
303 target_link_libraries(lib_gost_engine PRIVATE gost_core ${OPENSSL_CRYPTO_LIBRARIES})
304
305
306 set(GOST_SUM_SOURCE_FILES
307         gostsum.c
308         )
309
310 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
311 target_link_libraries(gostsum gost_core ${OPENSSL_CRYPTO_LIBRARIES})
312
313 set(GOST_12_SUM_SOURCE_FILES
314         gost12sum.c
315         )
316
317 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
318 target_link_libraries(gost12sum gost_core)
319
320 set_source_files_properties(tags PROPERTIES GENERATED true)
321 add_custom_target(tags
322     COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
323     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
324
325 add_custom_target(tcl_tests
326     COMMAND OPENSSL_LIBCRYPTO=${OPENSSL_CRYPTO_LIBRARY}
327             OPENSSL_APP=${OPENSSL_PROGRAM}
328             TESTSRC=${CMAKE_SOURCE_DIR}/tcl_tests
329             TESTDIR=${CMAKE_BINARY_DIR}/tcl_tests
330             ENGINE_DIR=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
331             sh ./runtest.sh
332     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
333
334 add_executable(test_tlstree test_tlstree.c)
335 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARIES})
336
337 # install programs and manuals
338 install(TARGETS gostsum gost12sum RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
339 install(FILES gostsum.1 gost12sum.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
340
341 # install engine in library and module form
342 install(TARGETS lib_gost_engine EXPORT GostEngineConfig LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
343 install(TARGETS gost_engine EXPORT GostEngineConfig
344         LIBRARY  DESTINATION ${OPENSSL_ENGINES_DIR}
345         RUNTIME  DESTINATION ${OPENSSL_ENGINES_DIR})
346 if (MSVC)
347   install(FILES $<TARGET_PDB_FILE:lib_gost_engine>
348     EXPORT GostEngineConfig DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
349   install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum>
350     EXPORT GostEngineConfig DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
351   install(FILES $<TARGET_PDB_FILE:gost_engine>
352     EXPORT GostEngineConfig DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
353 endif()
354 install(EXPORT GostEngineConfig DESTINATION GostEngine/share/cmake/GostEngine)