-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
429 lines (352 loc) · 14.2 KB
/
Copy pathCMakeLists.txt
File metadata and controls
429 lines (352 loc) · 14.2 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Copyright (c) (2018-2022,2024-2026) Apple Inc. All rights reserved.
#
# corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
# is contained in the License.txt file distributed with corecrypto) and only to
# people who accept that license. IMPORTANT: Any license rights granted to you by
# Apple Inc. (if any) are limited to internal use within your organization only on
# devices and computers you own or control, for the sole purpose of verifying the
# security characteristics and correct functioning of the Apple Software. You may
# not, directly or indirectly, redistribute the Apple Software or any portions thereof.
#
# CMake corecrypto build for Linux and Darwin.
#
cmake_minimum_required(VERSION 3.5.0)
set(CMAKE_OSX_SYSROOT "macosx.internal") # NOTE: This must be set before the
# call to project
option(CC_LINUX_ASM "Enable assembler support on Linux platform" OFF)
option(CC_SMALL_CODE "Enable small code" OFF)
option(BUILD_FOR_ARMV7 "Cross-compile for ARMv7" OFF)
option(BUILD_FOR_ARMV6 "Cross-compile for ARMv6" OFF)
if(BUILD_FOR_ARMV6)
set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
set(CMAKE_C_FLAGS "-mcpu=cortex-m0 -mfloat-abi=soft")
set(CMAKE_EXE_LINKER_FLAGS "--specs=rdimon.specs")
enable_language(ASM)
set(CMAKE_ASM_COMPILER "arm-none-eabi-gcc")
set(CMAKE_ASM_FLAGS "-x assembler-with-cpp ${CMAKE_C_FLAGS}")
add_compile_options("-DCC_USE_ASM=1")
endif()
if(BUILD_FOR_ARMV7)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(triple armv7-linux-gnueabihf)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_ASM_COMPILER_TARGET ${triple})
set(CMAKE_C_FLAGS "-I/usr/arm-linux-gnueabihf/include")
set(LINKER_FLAGS "-fuse-ld=/usr/arm-linux-gnueabihf/bin/ld")
set(CMAKE_EXE_LINKER_FLAGS "${LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${LINKER_FLAGS}")
set(CMAKE_LIBRARY_PATH "/usr/arm-linux-gnueabihf/lib/")
endif()
project(corecrypto C)
include(CoreCryptoSources.cmake)
#
# For code coverage, we use the code-coverage.cmake script from
# https://github.com/StableCoder/.
#
include(scripts/code-coverage.cmake)
#
# Build Macros and Targets
#
# get_include_dirs: extract include directories from list of headers
macro(get_include_dirs out in)
foreach(file ${in})
# Add directory including the header
get_filename_component(dir ${file} DIRECTORY)
list(APPEND ${out} ${dir})
# If the directory is corecrypto, we should also add its parent to the
# include dir.
get_filename_component(dirname ${dir} NAME)
if(${dirname} STREQUAL "corecrypto")
get_filename_component(parent ${dir} DIRECTORY)
list(APPEND ${out} ${parent})
endif()
endforeach()
endmacro()
# Project-level settings
# Build all objects with -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# CMake spelling of -std=gnu99
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS ON)
# Project-globals
set_property(
DIRECTORY
APPEND
PROPERTY COMPILE_DEFINITIONS
$<$<CONFIG:Debug>:DEBUG=1> $<$<CONFIG:Release>:NDEBUG>)
set(CC_C_OPTIONS -DBUILDKERNEL=0 -Wundef -Wcast-qual -D__STDC_WANT_LIB_EXT2__
-Wno-error=deprecated-declarations)
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${CC_C_OPTIONS}>")
if(CODE_COVERAGE)
add_code_coverage() # Adds instrumentation to all targets
endif()
# System dependencies
find_package(UnixCommands REQUIRED) # For ${BASH}
# Threads
find_package(Threads)
if(Threads_FOUND)
set(THREADS_PKG "Threads::Threads")
else()
message(WARNING "Threads not found. (This is fine for 'nostdlib' builds.)")
endif()
# libm
find_library(MATH_LIBRARY m DOC "libm")
if(NOT MATH_LIBRARY)
message(WARNING "libm not found. (This is fine for 'nostdlib' builds.)")
unset(MATH_LIBRARY CACHE)
endif()
# Platform-specific dependencies
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
find_library(SYSTEM_FRAMEWORK NAMES System)
mark_as_advanced(SYSTEM_FRAMEWORK)
find_path(SYSTEM_CPU_CAPABILITIES_PATH i386/cpu_capabilities.h
HINTS "${SYSTEM_FRAMEWORK}/PrivateHeaders")
mark_as_advanced(SYSTEM_CPU_CAPABILITIES_PATH)
if(NOT SYSTEM_FRAMEWORK OR NOT SYSTEM_CPU_CAPABILITIES_PATH)
unset(SYSTEM_FRAMEWORK CACHE)
message(
SEND_ERROR
"Could not find internal System.framework\n"
"HINT: Run cmake with xcrun to point it at the right SDK, or try:\n"
" ${CMAKE_COMMAND} -DCMAKE_OSX_SYSROOT=macosx.internal .")
else()
message("-- Found internal System.framework")
endif()
find_library(
KPERF_FRAMEWORK
NAMES kperf
PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
mark_as_advanced(KPERF_FRAMEWORK)
find_path(KPERF_KPC_PATH kpc.h HINTS "${KPERF_FRAMEWORK}/PrivateHeaders")
mark_as_advanced(KPERF_KPC_PATH)
if(NOT KPERF_FRAMEWORK OR NOT KPERF_KPC_PATH)
unset(KPERF_FRAMEWORK CACHE)
message(
SEND_ERROR
"Could not find internal kperf.framework\n"
"HINT: Run cmake with xcrun to point it at the right SDK, or try:\n"
" ${CMAKE_COMMAND} -DCMAKE_OSX_SYSROOT=macosx.internal .")
else()
message("-- Found internal kperf.framework")
endif()
# Compile assembler sources in OSX
enable_language(ASM)
# Enable FIPS POST trace in OSX
set_source_files_properties(
cc_fips/src/fipspost_trace.c cc_fips/crypto_test/crypto_test_cc_fips.c
PROPERTIES COMPILE_FLAGS -DCORECRYPTO_POST_TRACE=1)
# avoid `ranlib` (which `ar` runs automatically) warnings about
# objects with "no symbols" by modifying the archive commands
# Add the `S` argument to `ar` so it will not run `randlib`
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Sqc <TARGET> <LINK_FLAGS> <OBJECTS>")
# Run `ranlib` directly with the additional argument to avoid the
# warnings
set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Exclude sources that don't apply to Linux (or haven't yet been ported)
set(CORECRYPTO_EXCLUDE_SRCS
# exclude files that are OSX dependent
cc_fips/src/fipspost_get_hmac.c
cc_fips/src/fipspost_indicator.c
cc_fips/src/fipspost_post.c
cc_fips/src/fipspost_post_indicator.c
cc_fips/src/fipspost_post_integrity.c
cc_fips/src/fipspost_trace.c
cc_fips/src/module_id.c
cckprng/src/cckprng_diag.c
cckprng/src/cckprng_diaggens.c
cckprng/src/cckprng_generate.c
cckprng/src/cckprng_init.c
cckprng/src/cckprng_initgen.c
cckprng/src/cckprng_loadseed.c
cckprng/src/cckprng_printdiag.c
cckprng/src/cckprng_ratchetseed.c
cckprng/src/cckprng_refresh.c
cckprng/src/cckprng_rekeygen.c
cckprng/src/cckprng_rekeygens.c
cckprng/src/cckprng_reseed.c
cckprng/src/cckprng_storeseed.c
cckprng/src/prng.c)
set(CORECRYPTO_TEST_EXCLUDE_SRCS
# exclude files that are OSX dependent
cc_fips/src/fipspost_get_hmac.c
corecrypto_test/lib/cccycles.c
cckprng/crypto_test/crypto_test_kprng.c
# this test requires trace to be enabled
cc_fips/crypto_test/crypto_test_cc_fips.c)
set(CORECRYPTO_PERF_EXCLUDE_SRCS
# exclude files that are OSX dependent
corecrypto_perf/src/ccperf_kprng.c)
if(CC_LINUX_ASM)
enable_language(ASM)
# Add assembler specific clang flags
set(CC_ASM_OPTIONS -integrated-as # Always use clang internal assembler
-x assembler-with-cpp # Run preprocessor despite .s name
)
add_compile_options("$<$<COMPILE_LANGUAGE:ASM>:${CC_ASM_OPTIONS}>")
# Enable Linux assembler in corecrypto
add_compile_options("-DCC_LINUX_ASM=1")
endif()
endif()
include(GNUInstallDirs)
#
# corecrypto_static library target
#
# A few include dirs cannot be automatically generated by the above headers
# list. Manually fix it up.
set(CORECRYPTO_FIXED_INCLUDE_DIRS
ccaes/src/vng
cckprng
cckprng/corecrypto
corecrypto_test/include
acceleratecrypto/Include
acceleratecrypto/Header
ccec25519/src)
# Find include dirs for corecrypto_static headers.
set(cc_include_dir ${CORECRYPTO_FIXED_INCLUDE_DIRS})
get_include_dirs(cc_include_dir "${CORECRYPTO_PROJECT_HDRS}")
get_include_dirs(cc_include_dir "${CORECRYPTO_PUBLIC_HDRS}")
get_include_dirs(cc_include_dir "${CORECRYPTO_PRIVATE_HDRS}")
list(REMOVE_DUPLICATES cc_include_dir)
# Filter out excluded sources
if(CORECRYPTO_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_SRCS ${CORECRYPTO_EXCLUDE_SRCS})
endif()
# Create target for corecrypto_static
add_library(corecrypto_static STATIC ${CORECRYPTO_SRCS})
target_link_libraries(
corecrypto_static PRIVATE $<$<PLATFORM_ID:Darwin>:${SYSTEM_FRAMEWORK}>
${MATH_LIBRARY})
target_include_directories(corecrypto_static PRIVATE ${cc_include_dir})
set_property(TARGET corecrypto_static PROPERTY POSITION_INDEPENDENT_CODE ON)
# Generate pkgconfig for corecrypto_static
configure_file("corecrypto.pc.in" "corecrypto.pc" @ONLY)
# Add defines
target_compile_definitions(corecrypto_static PRIVATE ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS)
if(CC_SMALL_CODE)
target_compile_definitions(corecrypto_static PRIVATE CC_SMALL_CODE=1)
endif()
# Install corecrypto_static
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# install the archive like a regular file, `ranlib` has already been
# applied above.
install(FILES $<TARGET_FILE:corecrypto_static> DESTINATION "${CMAKE_INSTALL_LIBDIR}")
else()
install(TARGETS corecrypto_static ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()
install(FILES ${CORECRYPTO_PUBLIC_HDRS} ${CORECRYPTO_PRIVATE_HDRS}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/corecrypto")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/corecrypto.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
#
# corecrypto_test_dylib library target
#
if(CORECRYPTO_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_TEST_DYLIB_SRCS ${CORECRYPTO_EXCLUDE_SRCS})
endif()
add_library(corecrypto_test_dylib SHARED ${CORECRYPTO_TEST_DYLIB_SRCS})
target_link_libraries(
corecrypto_test_dylib PRIVATE $<$<PLATFORM_ID:Darwin>:${SYSTEM_FRAMEWORK}>
${MATH_LIBRARY})
target_include_directories(corecrypto_test_dylib PRIVATE ${cc_include_dir})
target_compile_definitions(corecrypto_test_dylib PRIVATE CORECRYPTO_BUILT_FOR_TESTING)
set_property(TARGET corecrypto_test_dylib PROPERTY POSITION_INDEPENDENT_CODE ON)
# Generate pkgconfig for corecrypto_test_dylib
configure_file("corecrypto.pc.in" "corecrypto.pc" @ONLY)
# Add defines
target_compile_definitions(corecrypto_test_dylib PRIVATE ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS)
if(CC_SMALL_CODE)
target_compile_definitions(corecrypto_test_dylib PRIVATE CC_SMALL_CODE=1)
endif()
#
# corecrypto_test target
#
# Remove the .inc and other non C files from the sources
foreach(file ${CORECRYPTO_TEST_SRCS})
string(REGEX MATCH ".+\\.c$" match ${file})
if(NOT match)
list(REMOVE_ITEM CORECRYPTO_TEST_SRCS ${file})
endif()
endforeach()
# A few include dirs cannot be automatically generated by the above headers
# list. Manually fix it up.
set(CORECRYPTO_TEST_FIXED_INCLUDE_DIRS
ccsha2/src ccrng/src ccec25519/src ccaes/src/ios_hardware corecrypto_test
cczp/src)
# Find include dirs for corecrypto_test headers.
set(cctest_include_dir ${CORECRYPTO_TEST_FIXED_INCLUDE_DIRS})
get_include_dirs(cctest_include_dir "${CORECRYPTO_TEST_HDRS}")
get_include_dirs(cctest_include_dir "${CORECRYPTO_TEST_SRCS}")
list(REMOVE_DUPLICATES cctest_include_dir)
# Create target for corecrypto_test
if(CORECRYPTO_TEST_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_TEST_SRCS ${CORECRYPTO_TEST_EXCLUDE_SRCS})
endif()
add_executable(corecrypto_test ${CORECRYPTO_TEST_SRCS})
target_compile_definitions(corecrypto_test PRIVATE CC_UNITTEST=1 CORECRYPTO_BUILT_FOR_TESTING)
target_include_directories(corecrypto_test PRIVATE ${cctest_include_dir}
${cc_include_dir})
target_link_libraries(
corecrypto_test
PRIVATE corecrypto_test_dylib ${THREADS_PKG} ${MATH_LIBRARY} ${CMAKE_DL_LIBS}
$<$<PLATFORM_ID:Darwin>:${KPERF_FRAMEWORK}>)
if(CODE_COVERAGE)
target_code_coverage(corecrypto_test EXCLUDE cc_fips/* fipscavs/*)
endif()
# Add defines
target_compile_definitions(corecrypto_test PRIVATE ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS)
if(CC_SMALL_CODE)
target_compile_definitions(corecrypto_test PRIVATE CC_SMALL_CODE=1)
endif()
#
# corecrypto_perf target
#
# ccperf.h lives in corecrypto_perf/corecrypto. Add it up
set(CORECRYPTO_PERF_FIXED_INCLUDE_DIRS corecrypto_perf/corecrypto)
set(ccperf_include_dir ${CORECRYPTO_PERF_FIXED_INCLUDE_DIRS})
# Create target for corecrypto_perf
if(CORECRYPTO_PERF_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_PERF_SRCS ${CORECRYPTO_PERF_EXCLUDE_SRCS})
endif()
add_executable(corecrypto_perf ${CORECRYPTO_PERF_SRCS})
target_include_directories(
corecrypto_perf PRIVATE ${ccperf_include_dir} ${cctest_include_dir}
${cc_include_dir})
target_link_libraries(corecrypto_perf PRIVATE corecrypto_static
${THREADS_PKG} ${MATH_LIBRARY})
# Add defines
target_compile_definitions(corecrypto_perf PRIVATE ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS)
if(CC_SMALL_CODE)
target_compile_definitions(corecrypto_perf PRIVATE CC_SMALL_CODE=1)
endif()
if(BUILD_FOR_ARMV6)
#
# corecrypto_nostdlib library target
#
# Filter out excluded sources
if(CORECRYPTO_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_SRCS ${CORECRYPTO_EXCLUDE_SRCS})
endif()
# Create target for corecrypto_nostdlib
add_library(corecrypto_nostdlib STATIC ${CORECRYPTO_NOSTDLIB_SRCS})
target_link_libraries(corecrypto_nostdlib PRIVATE)
target_include_directories(corecrypto_nostdlib PRIVATE ${cc_include_dir})
set_property(TARGET corecrypto_nostdlib PROPERTY POSITION_INDEPENDENT_CODE ON)
# Add defines
target_compile_definitions(corecrypto_nostdlib PRIVATE)
#
# corecrypto_test_nostdlib target
#
# Create target for corecrypto_test_nostdlib
if(CORECRYPTO_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_SRCS ${CORECRYPTO_EXCLUDE_SRCS})
endif()
add_executable(corecrypto_test_nostdlib ${CORECRYPTO_TEST_NOSTDLIB_SRCS})
target_include_directories(corecrypto_test_nostdlib PRIVATE ${cc_include_dir})
target_link_libraries(corecrypto_test_nostdlib PRIVATE corecrypto_nostdlib)
endif()