| 1 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# SPDX-License-Identifier: Apache-2.0. |
| 3 |
|
| 4 |
include(CheckCCompilerFlag) |
| 5 |
include(CheckIncludeFile) |
| 6 |
include(CheckSymbolExists) |
| 7 |
|
| 8 |
option(AWS_ENABLE_LTO "Enables LTO on libraries. Ensure this is set on all consumed targets, or linking will fail" OFF) |
| 9 |
option(LEGACY_COMPILER_SUPPORT "This enables builds with compiler versions such as gcc 4.1.2. This is not a 'supported' feature; it's just a best effort." OFF) |
| 10 |
option(AWS_SUPPORT_WIN7 "Restricts WINAPI calls to Win7 and older (This will have implications in downstream libraries that use TLS especially)" OFF) |
| 11 |
option(AWS_WARNINGS_ARE_ERRORS "Compiler warning is treated as an error. Try turning this off when observing errors on a new or uncommon compiler" OFF) |
| 12 |
option(AWS_ENABLE_TRACING "Enable tracing macros" OFF) |
| 13 |
option(AWS_STATIC_MSVC_RUNTIME_LIBRARY "Windows-only. Turn ON to use the statically-linked MSVC runtime lib, instead of the DLL" OFF) |
| 14 |
option(STATIC_CRT "Deprecated. Use AWS_STATIC_MSVC_RUNTIME_LIBRARY instead" OFF) |
| 15 |
|
| 16 |
# Check for Posix Large Files Support (LFS). |
| 17 |
# On most 64bit systems, LFS is enabled by default. |
| 18 |
# On some 32bit systems, LFS must be enabled by via defines before headers are included. |
| 19 |
# For more info, see docs: |
| 20 |
# https://www.gnu.org/software/libc/manual/html_node/File-Position-Primitive.html |
| 21 |
# https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html |
| 22 |
function(aws_check_posix_lfs extra_flags variable) |
| 23 |
list(APPEND CMAKE_REQUIRED_FLAGS ${extra_flags}) |
| 24 |
check_c_source_compiles(" |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
/* fails to compile if off_t smaller than 64bits */ |
| 28 |
typedef char array[sizeof(off_t) >= 8 ? 1 : -1]; |
| 29 |
|
| 30 |
int main() { |
| 31 |
return 0; |
| 32 |
}" |
| 33 |
HAS_64BIT_FILE_OFFSET_${variable}) |
| 34 |
|
| 35 |
if (HAS_64BIT_FILE_OFFSET_${variable}) |
| 36 |
# sometimes off_t is 64bit, but fseeko() is missing (ex: Android API < 24) |
| 37 |
check_symbol_exists(fseeko "stdio.h" HAS_FSEEKO_${variable}) |
| 38 |
|
| 39 |
if (HAS_FSEEKO_${variable}) |
| 40 |
set(${variable} 1 PARENT_SCOPE) |
| 41 |
endif() |
| 42 |
endif() |
| 43 |
endfunction() |
| 44 |
|
| 45 |
# This function will set all common flags on a target |
| 46 |
# Options: |
| 47 |
# NO_WGNU: Disable -Wgnu |
| 48 |
# NO_WEXTRA: Disable -Wextra |
| 49 |
# NO_PEDANTIC: Disable -pedantic |
| 50 |
function(aws_set_common_properties target) |
| 51 |
set(options NO_WGNU NO_WEXTRA NO_PEDANTIC NO_LTO) |
| 52 |
cmake_parse_arguments(SET_PROPERTIES "${options}" "" "" ${ARGN}) |
| 53 |
|
| 54 |
if(MSVC) |
| 55 |
# Remove other /W flags |
| 56 |
if(CMAKE_C_FLAGS MATCHES "/W[0-4]") |
| 57 |
string(REGEX REPLACE "/W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
| 58 |
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" PARENT_SCOPE) |
| 59 |
endif() |
| 60 |
|
| 61 |
list(APPEND AWS_C_FLAGS /W4 /MP) |
| 62 |
|
| 63 |
if(AWS_WARNINGS_ARE_ERRORS) |
| 64 |
list(APPEND AWS_C_FLAGS /WX) |
| 65 |
endif() |
| 66 |
|
| 67 |
# /volatile:iso relaxes some implicit memory barriers that MSVC normally applies for volatile accesses |
| 68 |
# Since we want to be compatible with user builds using /volatile:iso, use it for the tests. |
| 69 |
list(APPEND AWS_C_FLAGS /volatile:iso) |
| 70 |
|
| 71 |
# disable non-constant initializer warning, it's not non-standard, just for Microsoft |
| 72 |
list(APPEND AWS_C_FLAGS /wd4204) |
| 73 |
# disable passing the address of a local warning. Again, not non-standard, just for Microsoft |
| 74 |
list(APPEND AWS_C_FLAGS /wd4221) |
| 75 |
|
| 76 |
if (AWS_SUPPORT_WIN7) |
| 77 |
# Use only APIs available in Win7 and later |
| 78 |
message(STATUS "Windows 7 support requested, forcing WINVER and _WIN32_WINNT to 0x0601") |
| 79 |
list(APPEND AWS_C_FLAGS /DWINVER=0x0601) |
| 80 |
list(APPEND AWS_C_FLAGS /D_WIN32_WINNT=0x0601) |
| 81 |
list(APPEND AWS_C_FLAGS /DAWS_SUPPORT_WIN7=1) |
| 82 |
endif() |
| 83 |
|
| 84 |
# Set MSVC runtime libary. |
| 85 |
# Note: there are other ways of doing this if we bump our CMake minimum to 3.14+ |
| 86 |
# See: https://cmake.org/cmake/help/latest/policy/CMP0091.html |
| 87 |
if (AWS_STATIC_MSVC_RUNTIME_LIBRARY OR STATIC_CRT) |
| 88 |
list(APPEND AWS_C_FLAGS "/MT$<$<CONFIG:Debug>:d>") |
| 89 |
else() |
| 90 |
list(APPEND AWS_C_FLAGS "/MD$<$<CONFIG:Debug>:d>") |
| 91 |
endif() |
| 92 |
|
| 93 |
else() |
| 94 |
list(APPEND AWS_C_FLAGS -Wall -Wstrict-prototypes) |
| 95 |
|
| 96 |
list(APPEND AWS_C_FLAGS $<$<NOT:$<CONFIG:Release>>:-fno-omit-frame-pointer>) |
| 97 |
|
| 98 |
if(AWS_WARNINGS_ARE_ERRORS) |
| 99 |
list(APPEND AWS_C_FLAGS -Werror) |
| 100 |
endif() |
| 101 |
|
| 102 |
if(NOT SET_PROPERTIES_NO_WEXTRA) |
| 103 |
list(APPEND AWS_C_FLAGS -Wextra) |
| 104 |
endif() |
| 105 |
|
| 106 |
if(NOT SET_PROPERTIES_NO_PEDANTIC) |
| 107 |
list(APPEND AWS_C_FLAGS -pedantic) |
| 108 |
endif() |
| 109 |
|
| 110 |
# Warning disables always go last to avoid future flags re-enabling them |
| 111 |
list(APPEND AWS_C_FLAGS -Wno-long-long) |
| 112 |
|
| 113 |
# Always enable position independent code, since this code will always end up in a shared lib |
| 114 |
check_c_compiler_flag("-fPIC -Werror" HAS_FPIC_FLAG) |
| 115 |
if (HAS_FPIC_FLAG) |
| 116 |
list(APPEND AWS_C_FLAGS -fPIC) |
| 117 |
endif() |
| 118 |
|
| 119 |
if (LEGACY_COMPILER_SUPPORT) |
| 120 |
list(APPEND AWS_C_FLAGS -Wno-strict-aliasing) |
| 121 |
endif() |
| 122 |
|
| 123 |
# -moutline-atomics generates code for both older load/store exclusive atomics and also |
| 124 |
# Arm's Large System Extensions (LSE) which scale substantially better on large core count systems. |
| 125 |
# |
| 126 |
# Test by compiling a program that actually uses atomics. |
| 127 |
# Previously we'd simply used check_c_compiler_flag() but that wasn't detecting |
| 128 |
# some real-world problems (see https://github.com/awslabs/aws-c-common/issues/902). |
| 129 |
if (AWS_ARCH_ARM64) |
| 130 |
set(old_flags "${CMAKE_REQUIRED_FLAGS}") |
| 131 |
set(CMAKE_REQUIRED_FLAGS "-moutline-atomics -Werror") |
| 132 |
check_c_source_compiles(" |
| 133 |
int main() { |
| 134 |
int x = 1; |
| 135 |
__atomic_fetch_add(&x, -1, __ATOMIC_SEQ_CST); |
| 136 |
return x; |
| 137 |
}" HAS_MOUTLINE_ATOMICS) |
| 138 |
set(CMAKE_REQUIRED_FLAGS "${old_flags}") |
| 139 |
|
| 140 |
if (HAS_MOUTLINE_ATOMICS) |
| 141 |
list(APPEND AWS_C_FLAGS -moutline-atomics) |
| 142 |
endif() |
| 143 |
endif() |
| 144 |
|
| 145 |
# Check for Posix Large File Support (LFS). |
| 146 |
# Doing this check here, instead of AwsFeatureTests.cmake, |
| 147 |
# because we might need to modify AWS_C_FLAGS to enable it. |
| 148 |
set(HAS_LFS FALSE) |
| 149 |
aws_check_posix_lfs("" BY_DEFAULT) |
| 150 |
if (BY_DEFAULT) |
| 151 |
set(HAS_LFS TRUE) |
| 152 |
else() |
| 153 |
aws_check_posix_lfs("-D_FILE_OFFSET_BITS=64" VIA_DEFINES) |
| 154 |
if (VIA_DEFINES) |
| 155 |
list(APPEND AWS_C_FLAGS "-D_FILE_OFFSET_BITS=64") |
| 156 |
set(HAS_LFS TRUE) |
| 157 |
endif() |
| 158 |
endif() |
| 159 |
# This becomes a define in config.h |
| 160 |
set(AWS_HAVE_POSIX_LARGE_FILE_SUPPORT ${HAS_LFS} CACHE BOOL "Posix Large File Support") |
| 161 |
|
| 162 |
# Hide symbols from libcrypto.a |
| 163 |
# This avoids problems when an application ends up using both libcrypto.a and libcrypto.so. |
| 164 |
# |
| 165 |
# An example of this happening is the aws-c-io tests. |
| 166 |
# All the C libs are compiled statically, but then a PKCS#11 library is |
| 167 |
# loaded at runtime which happens to use libcrypto.so from OpenSSL. |
| 168 |
# If the symbols from libcrypto.a aren't hidden, then SOME function calls use the libcrypto.a implementation |
| 169 |
# and SOME function calls use the libcrypto.so implementation, and this mismatch leads to weird crashes. |
| 170 |
if (UNIX AND NOT APPLE) |
| 171 |
# If we used target_link_options() (CMake 3.13+) we could make these flags PUBLIC |
| 172 |
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--exclude-libs,libcrypto.a") |
| 173 |
endif() |
| 174 |
|
| 175 |
|
| 176 |
endif() |
| 177 |
|
| 178 |
check_include_file(stdint.h HAS_STDINT) |
| 179 |
check_include_file(stdbool.h HAS_STDBOOL) |
| 180 |
|
| 181 |
if (NOT HAS_STDINT) |
| 182 |
list(APPEND AWS_C_FLAGS -DNO_STDINT) |
| 183 |
endif() |
| 184 |
|
| 185 |
if (NOT HAS_STDBOOL) |
| 186 |
list(APPEND AWS_C_FLAGS -DNO_STDBOOL) |
| 187 |
endif() |
| 188 |
|
| 189 |
if(NOT SET_PROPERTIES_NO_WGNU) |
| 190 |
check_c_compiler_flag("-Wgnu -Werror" HAS_WGNU) |
| 191 |
if(HAS_WGNU) |
| 192 |
# -Wgnu-zero-variadic-macro-arguments results in a lot of false positives |
| 193 |
list(APPEND AWS_C_FLAGS -Wgnu -Wno-gnu-zero-variadic-macro-arguments) |
| 194 |
|
| 195 |
# some platforms implement htonl family of functions via GNU statement expressions (https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html) |
| 196 |
# which generates -Wgnu-statement-expression warning. |
| 197 |
set(old_flags "${CMAKE_REQUIRED_FLAGS}") |
| 198 |
set(CMAKE_REQUIRED_FLAGS "-Wgnu -Werror") |
| 199 |
check_c_source_compiles(" |
| 200 |
#include <netinet/in.h> |
| 201 |
|
| 202 |
int main() { |
| 203 |
uint32_t x = 0; |
| 204 |
x = htonl(x); |
| 205 |
return (int)x; |
| 206 |
}" NO_GNU_EXPR) |
| 207 |
set(CMAKE_REQUIRED_FLAGS "${old_flags}") |
| 208 |
|
| 209 |
if (NOT NO_GNU_EXPR) |
| 210 |
list(APPEND AWS_C_FLAGS -Wno-gnu-statement-expression) |
| 211 |
endif() |
| 212 |
endif() |
| 213 |
endif() |
| 214 |
|
| 215 |
# some platforms (especially when cross-compiling) do not have the sysconf API in their toolchain files. |
| 216 |
check_c_source_compiles(" |
| 217 |
#include <unistd.h> |
| 218 |
int main() { |
| 219 |
sysconf(_SC_NPROCESSORS_ONLN); |
| 220 |
}" HAVE_SYSCONF) |
| 221 |
|
| 222 |
if (HAVE_SYSCONF) |
| 223 |
list(APPEND AWS_C_DEFINES_PRIVATE -DHAVE_SYSCONF) |
| 224 |
endif() |
| 225 |
|
| 226 |
list(APPEND AWS_C_DEFINES_PRIVATE $<$<CONFIG:Debug>:DEBUG_BUILD>) |
| 227 |
|
| 228 |
if ((NOT SET_PROPERTIES_NO_LTO) AND AWS_ENABLE_LTO) |
| 229 |
# enable except in Debug builds |
| 230 |
set(_ENABLE_LTO_EXPR $<NOT:$<CONFIG:Debug>>) |
| 231 |
|
| 232 |
# try to check whether compiler supports LTO/IPO |
| 233 |
include(CheckIPOSupported) |
| 234 |
check_ipo_supported(RESULT ipo_supported) |
| 235 |
if (ipo_supported) |
| 236 |
message(STATUS "Enabling IPO/LTO for Release builds") |
| 237 |
else() |
| 238 |
message(STATUS "AWS_ENABLE_LTO is enabled, but cmake/compiler does not support it, disabling") |
| 239 |
set(_ENABLE_LTO_EXPR OFF) |
| 240 |
endif() |
| 241 |
else() |
| 242 |
set(_ENABLE_LTO_EXPR OFF) |
| 243 |
endif() |
| 244 |
|
| 245 |
if(AWS_ENABLE_TRACING) |
| 246 |
target_link_libraries(${target} PRIVATE ittnotify) |
| 247 |
else() |
| 248 |
# Disable intel notify api if tracing is not enabled |
| 249 |
list(APPEND AWS_C_DEFINES_PRIVATE -DINTEL_NO_ITTNOTIFY_API) |
| 250 |
endif() |
| 251 |
target_compile_options(${target} PRIVATE ${AWS_C_FLAGS}) |
| 252 |
target_compile_definitions(${target} PRIVATE ${AWS_C_DEFINES_PRIVATE} PUBLIC ${AWS_C_DEFINES_PUBLIC}) |
| 253 |
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C C_STANDARD 99 C_STANDARD_REQUIRED ON) |
| 254 |
set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${_ENABLE_LTO_EXPR}>) |
| 255 |
|
| 256 |
# Don't hide symbols in Debug builds. |
| 257 |
# We do this so that backtraces are more likely to show function names. |
| 258 |
# We mostly use backtraces to diagnose memory leaks. |
| 259 |
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 260 |
# And dont hide symbols on anything pre GCC 5.0 (Visibility support was not great on older compilers and some libraries didnt annotate visibility - |
| 261 |
# looking at you jni, which does not annotate on gcc less than 4.2. Mixing no annotation and hidden symbols leads to unexpected failures.). |
| 262 |
if (NOT (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "5.0")) |
| 263 |
set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON) |
| 264 |
endif () |
| 265 |
endif () |
| 266 |
endfunction() |