| 1 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# SPDX-License-Identifier: Apache-2.0. |
| 3 |
|
| 4 |
# This cmake logic verifies that each of our headers is complete, in that it |
| 5 |
# #includes any necessary dependencies, and that it builds under C++ as well. |
| 6 |
# |
| 7 |
# To do so, we generate a single-line C or C++ source file that includes each |
| 8 |
# header, and link all of these stub source files into a test executable. |
| 9 |
|
| 10 |
option(PERFORM_HEADER_CHECK "Performs compile-time checks that each header can be included independently. Requires a C++ compiler.") |
| 11 |
|
| 12 |
if (PERFORM_HEADER_CHECK) |
| 13 |
enable_language(CXX) |
| 14 |
endif() |
| 15 |
|
| 16 |
# Call as: aws_check_headers(${target} HEADERS TO CHECK LIST) |
| 17 |
function(aws_check_headers target) |
| 18 |
if (NOT PERFORM_HEADER_CHECK) |
| 19 |
return() |
| 20 |
endif() |
| 21 |
# parse function arguments |
| 22 |
set(options IS_CXX) |
| 23 |
cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) |
| 24 |
|
| 25 |
aws_check_headers_internal(${target} 11 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS}) |
| 26 |
aws_check_headers_internal(${target} 14 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS}) |
| 27 |
aws_check_headers_internal(${target} 17 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS}) |
| 28 |
aws_check_headers_internal(${target} 20 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS}) |
| 29 |
aws_check_headers_internal(${target} 23 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS}) |
| 30 |
endfunction() |
| 31 |
|
| 32 |
function(aws_check_headers_internal target std is_cxx) |
| 33 |
# Check that compiler supports this std |
| 34 |
list (FIND CMAKE_CXX_COMPILE_FEATURES "cxx_std_${std}" feature_idx) |
| 35 |
if (${feature_idx} LESS 0) |
| 36 |
return() |
| 37 |
endif() |
| 38 |
|
| 39 |
set(HEADER_CHECKER_ROOT "${CMAKE_CURRENT_BINARY_DIR}/header-checker-cxx${std}") |
| 40 |
|
| 41 |
# Write stub main file |
| 42 |
set(HEADER_CHECKER_MAIN "${HEADER_CHECKER_ROOT}/headerchecker_main.c") |
| 43 |
set(HEADER_CHECKER_LIB ${target}-header-check-cxx${std}) |
| 44 |
file(WRITE ${HEADER_CHECKER_MAIN} " |
| 45 |
int main(int argc, char **argv) { |
| 46 |
(void)argc; |
| 47 |
(void)argv; |
| 48 |
|
| 49 |
return 0; |
| 50 |
}\n") |
| 51 |
|
| 52 |
|
| 53 |
add_executable(${HEADER_CHECKER_LIB} ${HEADER_CHECKER_MAIN}) |
| 54 |
target_link_libraries(${HEADER_CHECKER_LIB} ${target}) |
| 55 |
target_compile_definitions(${HEADER_CHECKER_LIB} PRIVATE AWS_UNSTABLE_TESTING_API=1 AWS_HEADER_CHECKER=1) |
| 56 |
|
| 57 |
# We want to be able to verify that the proper C++ header guards are in place, so |
| 58 |
# build this target as a C++ application |
| 59 |
set_target_properties(${HEADER_CHECKER_LIB} PROPERTIES |
| 60 |
LINKER_LANGUAGE CXX |
| 61 |
CXX_STANDARD ${std} |
| 62 |
CXX_STANDARD_REQUIRED 0 |
| 63 |
C_STANDARD 99 |
| 64 |
# Forbid public headers from relying on compiler extensions |
| 65 |
CXX_EXTENSIONS OFF |
| 66 |
C_EXTENSIONS OFF |
| 67 |
) |
| 68 |
|
| 69 |
# Ensure our headers can be included by an application with its warnings set very high. |
| 70 |
# Most compiler options are universal, but some are C++ only. |
| 71 |
set(compiler_options_all "") |
| 72 |
set(compiler_options_cxx_only "") |
| 73 |
if(MSVC) |
| 74 |
# MSVC complains about windows' own header files. Use /W4 instead of /Wall |
| 75 |
list(APPEND compiler_options_all /W4 /WX) |
| 76 |
else() |
| 77 |
list(APPEND compiler_options_all -Wall -Wextra -Wpedantic -Werror) |
| 78 |
|
| 79 |
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 80 |
# -Wuseless-cast requested by https://github.com/awslabs/aws-c-common/issues/973 |
| 81 |
list(APPEND compiler_options_cxx_only -Wuseless-cast) |
| 82 |
endif() |
| 83 |
endif() |
| 84 |
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE ${compiler_options_all}) |
| 85 |
|
| 86 |
foreach(header IN LISTS ARGN) |
| 87 |
if (NOT ${header} MATCHES "\\.inl$") |
| 88 |
# create unique token for this file, e.g.: |
| 89 |
# "${CMAKE_CURRENT_SOURCE_DIR}/include/aws/common/byte_buf.h" -> "aws_common_byte_buf_h" |
| 90 |
file(RELATIVE_PATH include_path "${CMAKE_CURRENT_SOURCE_DIR}/include" ${header}) |
| 91 |
# replace non-alphanumeric characters with underscores |
| 92 |
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" unique_token ${include_path}) |
| 93 |
# test compiling header from a .cpp and .c file (or just .cpp if this header IS_CXX) |
| 94 |
set(c_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.c") |
| 95 |
set(cpp_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.cpp") |
| 96 |
# include header twice to check for include-guards |
| 97 |
# define a unique int or compiler complains that there's nothing in the file |
| 98 |
file(WRITE "${cpp_file}" "#include <${include_path}>\n#include <${include_path}>\nint ${unique_token}_cpp;") |
| 99 |
target_sources(${HEADER_CHECKER_LIB} PUBLIC "${cpp_file}") |
| 100 |
if (NOT is_cxx) |
| 101 |
file(WRITE "${c_file}" "#include <${include_path}>\n#include <${include_path}>\nint ${unique_token}_c;\n") |
| 102 |
target_sources(${HEADER_CHECKER_LIB} PUBLIC "${c_file}") |
| 103 |
endif() |
| 104 |
|
| 105 |
# for .cpp file, apply C++ only compiler options |
| 106 |
if(compiler_options_cxx_only) |
| 107 |
set_source_files_properties(${cpp_file} PROPERTIES COMPILE_OPTIONS ${compiler_options_cxx_only}) |
| 108 |
endif() |
| 109 |
endif() |
| 110 |
endforeach(header) |
| 111 |
endfunction() |