| 1 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# SPDX-License-Identifier: Apache-2.0. |
| 3 |
|
| 4 |
cmake_minimum_required(VERSION 3.9...3.31) |
| 5 |
|
| 6 |
project(aws-c-cal LANGUAGES C VERSION 0.1.0) |
| 7 |
|
| 8 |
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE) |
| 9 |
|
| 10 |
option(BYO_CRYPTO "Set this if you want to provide your own cryptography implementation. This will cause the defaults to not be compiled." OFF) |
| 11 |
option(USE_OPENSSL "Set this if you want to use your system's OpenSSL 1.0.2/1.1.1 compatible libcrypto" OFF) |
| 12 |
option(AWS_USE_CRYPTO_SHARED_LIBS "Force c-cal to use shared libs in Findcrypto" OFF) |
| 13 |
option(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE "Experimental feature to support ED25519 keygen on platforms that do not support it in os libs (i.e. win/mac)" OFF) |
| 14 |
|
| 15 |
if (NOT IN_SOURCE_BUILD) |
| 16 |
# this is required so we can use aws-c-common's CMake modules |
| 17 |
find_package(aws-c-common REQUIRED) |
| 18 |
endif() |
| 19 |
|
| 20 |
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules") |
| 21 |
|
| 22 |
include(AwsCFlags) |
| 23 |
include(AwsCheckHeaders) |
| 24 |
include(AwsSharedLibSetup) |
| 25 |
include(AwsSanitizers) |
| 26 |
include(AwsFindPackage) |
| 27 |
include(GNUInstallDirs) |
| 28 |
|
| 29 |
file(GLOB AWS_CAL_HEADERS |
| 30 |
"include/aws/cal/*.h" |
| 31 |
) |
| 32 |
|
| 33 |
file(GLOB AWS_CAL_SRC |
| 34 |
"source/*.c" |
| 35 |
) |
| 36 |
|
| 37 |
if (WIN32) |
| 38 |
|
| 39 |
if (NOT BYO_CRYPTO) |
| 40 |
file(GLOB AWS_CAL_OS_SRC |
| 41 |
"source/windows/*.c" |
| 42 |
) |
| 43 |
|
| 44 |
if (AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) |
| 45 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ed25519.c") |
| 46 |
list(APPEND AWS_CAL_OS_SRC "source/shared/lccrypto_common.c") |
| 47 |
else() |
| 48 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ed25519_noop.c") |
| 49 |
endif() |
| 50 |
|
| 51 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ref_hkdf.c") |
| 52 |
|
| 53 |
if (AWS_SUPPORT_WIN7) |
| 54 |
set(PLATFORM_LIBS bcrypt) |
| 55 |
else() |
| 56 |
set(PLATFORM_LIBS ncrypt) |
| 57 |
endif() |
| 58 |
endif() |
| 59 |
|
| 60 |
if (MSVC) |
| 61 |
source_group("Header Files\\aws\\cal" FILES ${AWS_CAL_HEADERS}) |
| 62 |
source_group("Source Files" FILES ${AWS_CAL_SRC}) |
| 63 |
source_group("Source Files\\windows" FILES ${AWS_CAL_OS_SRC}) |
| 64 |
endif () |
| 65 |
|
| 66 |
elseif (APPLE) |
| 67 |
if(DEFINED CMAKE_OSX_SYSROOT) |
| 68 |
message(STATUS "CMAKE_OSX_SYSROOT is defined and set to: ${CMAKE_OSX_SYSROOT} this value") |
| 69 |
else() |
| 70 |
message(STATUS "CMAKE_OSX_SYSROOT is not defined") |
| 71 |
endif() |
| 72 |
|
| 73 |
# As of CMake 4.0 isysroot is no longer set on mac and instead /usr/local is checked |
| 74 |
# On systems where custom headers are installed at that location (homebrew loves doing that) |
| 75 |
# we can end up building against random libcrypto headers, which are not guaranteed to be forward compatible. |
| 76 |
# This tries to set isysroot in a way that works on as many combinations of cmake and os as possible. |
| 77 |
if(NOT CMAKE_OSX_SYSROOT) |
| 78 |
execute_process( |
| 79 |
COMMAND xcrun --show-sdk-path |
| 80 |
RESULT_VARIABLE XCRUN_RESULT |
| 81 |
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT |
| 82 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 83 |
ERROR_QUIET |
| 84 |
) |
| 85 |
if(NOT XCRUN_RESULT EQUAL 0) |
| 86 |
message(WARNING "Failed to determine SDK path using xcrun") |
| 87 |
endif() |
| 88 |
endif() |
| 89 |
|
| 90 |
if (NOT BYO_CRYPTO) |
| 91 |
file(GLOB AWS_CAL_OS_SRC |
| 92 |
"source/darwin/*.c" |
| 93 |
) |
| 94 |
|
| 95 |
if (AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) |
| 96 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ed25519.c") |
| 97 |
list(APPEND AWS_CAL_OS_SRC "source/shared/lccrypto_common.c") |
| 98 |
else() |
| 99 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ed25519_noop.c") |
| 100 |
endif() |
| 101 |
|
| 102 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ref_hkdf.c") |
| 103 |
|
| 104 |
find_library(SECURITY_LIB Security) |
| 105 |
if (NOT SECURITY_LIB) |
| 106 |
message(FATAL_ERROR "Security Framework not found") |
| 107 |
endif () |
| 108 |
|
| 109 |
find_library(COREFOUNDATION_LIB CoreFoundation) |
| 110 |
if(NOT COREFOUNDATION_LIB) |
| 111 |
message(FATAL_ERROR "CoreFoundation Framework not found") |
| 112 |
endif() |
| 113 |
|
| 114 |
list(APPEND PLATFORM_LIBS "-framework Security -framework CoreFoundation") |
| 115 |
endif() |
| 116 |
else () |
| 117 |
if (NOT BYO_CRYPTO) |
| 118 |
file(GLOB AWS_CAL_OS_SRC |
| 119 |
"source/unix/*.c" |
| 120 |
"source/shared/ed25519.c" |
| 121 |
"source/shared/lccrypto_common.c" |
| 122 |
) |
| 123 |
endif() |
| 124 |
|
| 125 |
list(APPEND AWS_CAL_OS_SRC "source/shared/ref_hkdf.c") |
| 126 |
endif() |
| 127 |
|
| 128 |
if (NOT BYO_CRYPTO) |
| 129 |
if ((NOT WIN32 AND NOT APPLE) OR AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) |
| 130 |
if (USE_OPENSSL AND NOT ANDROID) |
| 131 |
find_package(OpenSSL REQUIRED) |
| 132 |
find_package(Threads REQUIRED) |
| 133 |
list(APPEND PLATFORM_LIBS OpenSSL::Crypto Threads::Threads) |
| 134 |
message(STATUS "Using libcrypto from system: ${OPENSSL_CRYPTO_LIBRARY}") |
| 135 |
elseif(NOT USE_OPENSSL AND IN_SOURCE_BUILD) |
| 136 |
if (TARGET crypto) |
| 137 |
message(STATUS "Using libcrypto from AWS-LC") |
| 138 |
list(APPEND PLATFORM_LIBS crypto) |
| 139 |
elseif(AWSLC_PREBUILT) |
| 140 |
message(STATUS "Using prebuilt libcrypto from AWS-LC") |
| 141 |
find_package(crypto REQUIRED) |
| 142 |
list(APPEND PLATFORM_LIBS AWS::crypto) |
| 143 |
else() |
| 144 |
message(FATAL_ERROR "Target crypto is not defined, failed to find libcrypto.") |
| 145 |
endif() |
| 146 |
else() |
| 147 |
# note aws_use_package() does this for you, except it appends to the public link targets |
| 148 |
# which we probably don't want for this case where we want the crypto dependency private |
| 149 |
if (IN_SOURCE_BUILD) |
| 150 |
list(APPEND PLATFORM_LIBS crypto) |
| 151 |
else() |
| 152 |
find_package(crypto REQUIRED) |
| 153 |
list(APPEND PLATFORM_LIBS AWS::crypto) |
| 154 |
endif() |
| 155 |
endif() |
| 156 |
endif() |
| 157 |
endif() |
| 158 |
|
| 159 |
file(GLOB CAL_HEADERS |
| 160 |
${AWS_CAL_HEADERS} |
| 161 |
) |
| 162 |
|
| 163 |
file(GLOB CAL_SRC |
| 164 |
${AWS_CAL_SRC} |
| 165 |
${AWS_CAL_OS_SRC} |
| 166 |
) |
| 167 |
|
| 168 |
add_library(${PROJECT_NAME} ${CAL_SRC}) |
| 169 |
aws_set_common_properties(${PROJECT_NAME} NO_WEXTRA) |
| 170 |
aws_prepare_symbol_visibility_args(${PROJECT_NAME} "AWS_CAL") |
| 171 |
aws_add_sanitizers(${PROJECT_NAME}) |
| 172 |
|
| 173 |
aws_use_package(aws-c-common) |
| 174 |
target_link_libraries(${PROJECT_NAME} PUBLIC ${DEP_AWS_LIBS} ${PLATFORM_LIBS}) |
| 175 |
|
| 176 |
if (BYO_CRYPTO) |
| 177 |
target_compile_definitions(${PROJECT_NAME} PRIVATE -DBYO_CRYPTO) |
| 178 |
endif() |
| 179 |
|
| 180 |
if (AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) |
| 181 |
target_compile_definitions(${PROJECT_NAME} PRIVATE -DAWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) |
| 182 |
endif() |
| 183 |
|
| 184 |
# Our ABI is not yet stable |
| 185 |
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.0.0) |
| 186 |
|
| 187 |
target_include_directories(${PROJECT_NAME} PUBLIC |
| 188 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 189 |
$<INSTALL_INTERFACE:include>) |
| 190 |
# When we install, the generated header will be at the INSTALL_INTERFACE:include location, |
| 191 |
# but at build time we need to explicitly include this here |
| 192 |
target_include_directories(${PROJECT_NAME} PUBLIC |
| 193 |
$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>) |
| 194 |
|
| 195 |
aws_prepare_shared_lib_exports(${PROJECT_NAME}) |
| 196 |
|
| 197 |
configure_file("cmake/${PROJECT_NAME}-config.cmake" |
| 198 |
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" |
| 199 |
@ONLY) |
| 200 |
|
| 201 |
aws_check_headers(${PROJECT_NAME} ${AWS_CAL_HEADERS}) |
| 202 |
install(FILES ${AWS_CAL_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/aws/cal" COMPONENT Development) |
| 203 |
|
| 204 |
if (BUILD_SHARED_LIBS) |
| 205 |
set (TARGET_DIR "shared") |
| 206 |
else() |
| 207 |
set (TARGET_DIR "static") |
| 208 |
endif() |
| 209 |
|
| 210 |
install(EXPORT "${PROJECT_NAME}-targets" |
| 211 |
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/${TARGET_DIR}/" |
| 212 |
NAMESPACE AWS:: |
| 213 |
COMPONENT Development) |
| 214 |
|
| 215 |
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" |
| 216 |
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
| 217 |
COMPONENT Development) |
| 218 |
|
| 219 |
list(APPEND EXPORT_MODULES |
| 220 |
"cmake/modules/Findcrypto.cmake" |
| 221 |
) |
| 222 |
|
| 223 |
install(FILES ${EXPORT_MODULES} |
| 224 |
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/modules" |
| 225 |
COMPONENT Development) |
| 226 |
|
| 227 |
if (NOT CMAKE_CROSSCOMPILING AND NOT BYO_CRYPTO) |
| 228 |
include(CTest) |
| 229 |
if (BUILD_TESTING) |
| 230 |
add_subdirectory(bin/sha256_profile) |
| 231 |
add_subdirectory(bin/produce_x_platform_fuzz_corpus) |
| 232 |
add_subdirectory(bin/run_x_platform_fuzz_corpus) |
| 233 |
add_subdirectory(tests) |
| 234 |
endif() |
| 235 |
endif() |