Skip to content

Commit bd28bc4

Browse files
authored
Option to check headers at compile time. (aws#175)
This is adapted from the header-checker in aws-c-common, but with separate passes for c++ 11,14,17,20. Also, sync submodules back to latest release
1 parent 8b05d02 commit bd28bc4

File tree

11 files changed

+84
-12
lines changed

11 files changed

+84
-12
lines changed

CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.1)
22
project(aws-crt-cpp CXX C)
33
option(BUILD_DEPS "Builds aws common runtime dependencies as part of build, only do this if you don't want to control your dependency chain." OFF)
44

5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
6+
include(AwsCheckHeadersCxx)
57
include(CTest)
68

79
if (DEFINED CMAKE_PREFIX_PATH)
@@ -119,14 +121,20 @@ file(GLOB AWS_CRT_EXTERNAL_HEADERS
119121
"include/aws/crt/external/*.h"
120122
)
121123

122-
file(GLOB AWS_CRT_CPP_HEADERS
124+
file(GLOB AWS_CRT_PUBLIC_HEADERS
123125
${AWS_CRT_HEADERS}
124126
${AWS_CRT_AUTH_HEADERS}
125127
${AWS_CRT_CRYPTO_HEADERS}
126128
${AWS_CRT_IO_HEADERS}
127129
${AWS_CRT_IOT_HEADERS}
128130
${AWS_CRT_MQTT_HEADERS}
129131
${AWS_CRT_HTTP_HEADERS}
132+
)
133+
134+
aws_check_headers_cxx(${PROJECT_NAME} ${AWS_CRT_PUBLIC_HEADERS})
135+
136+
file(GLOB AWS_CRT_CPP_HEADERS
137+
${AWS_CRT_PUBLIC_HEADERS}
130138
${AWS_CRT_EXTERNAL_HEADERS}
131139
)
132140

@@ -195,7 +203,7 @@ if (WIN32)
195203
endif ()
196204
endif()
197205

198-
add_library(${PROJECT_NAME} ${AWS_CRT_CPP_SRC})
206+
add_library(${PROJECT_NAME} ${AWS_CRT_CPP_HEADERS} ${AWS_CRT_CPP_SRC})
199207

200208
target_compile_definitions(${PROJECT_NAME} PRIVATE -DCJSON_HIDE_SYMBOLS)
201209

@@ -247,7 +255,6 @@ install(FILES ${AWS_CRT_IO_HEADERS} DESTINATION "include/aws/crt/io" COMPONENT D
247255
install(FILES ${AWS_CRT_IOT_HEADERS} DESTINATION "include/aws/iot" COMPONENT Development)
248256
install(FILES ${AWS_CRT_MQTT_HEADERS} DESTINATION "include/aws/crt/mqtt" COMPONENT Development)
249257
install(FILES ${AWS_CRT_HTTP_HEADERS} DESTINATION "include/aws/crt/http" COMPONENT Development)
250-
install(FILES ${AWS_CRT_EXTERNAL_HEADERS} DESTINATION "include/aws/crt/external" COMPONENT Development)
251258

252259
install(
253260
TARGETS ${PROJECT_NAME}

aws-common-runtime/aws-c-cal

aws-common-runtime/aws-c-http

aws-common-runtime/aws-c-mqtt

aws-common-runtime/s2n

Submodule s2n updated from 9311cab to 97799be

builder.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aws-crt-cpp",
33
"!cmake_args": [
44
"-DBUILD_DEPS=ON",
5-
"-DPERFORM_HEADER_CHECK=OFF",
5+
"-DPERFORM_HEADER_CHECK_CXX=ON",
66
"-DS2N_NO_PQ_ASM=ON"
77
],
88
"targets": {

cmake/AwsCheckHeadersCxx.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 builds with each supported c++ standard.
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_CXX "Performs compile-time checks that each header can be included independently.")
11+
12+
# Call as: aws_check_headers_cxx(${target} HEADERS TO CHECK LIST)
13+
function(aws_check_headers_cxx target)
14+
# Check headers against each supported CXX_STANDARD
15+
if (PERFORM_HEADER_CHECK_CXX)
16+
aws_check_headers_cxx_internal(${target} 11 ${ARGN})
17+
aws_check_headers_cxx_internal(${target} 14 ${ARGN})
18+
19+
if (NOT CMAKE_VERSION VERSION_LESS "3.8")
20+
aws_check_headers_cxx_internal(${target} 17 ${ARGN})
21+
22+
if (NOT CMAKE_VERSION VERSION_LESS "3.12")
23+
aws_check_headers_cxx_internal(${target} 20 ${ARGN})
24+
endif ()
25+
endif ()
26+
endif ()
27+
endfunction()
28+
29+
function(aws_check_headers_cxx_internal target std)
30+
set(HEADER_CHECKER_ROOT "${CMAKE_CURRENT_BINARY_DIR}/header-checker-cxx-${std}")
31+
32+
# Write stub main file
33+
set(HEADER_CHECKER_MAIN "${HEADER_CHECKER_ROOT}/stub.cpp")
34+
file(WRITE ${HEADER_CHECKER_MAIN} "
35+
int main(int argc, char **argv) {
36+
(void)argc;
37+
(void)argv;
38+
39+
return 0;
40+
}")
41+
42+
set(HEADER_CHECKER_LIB ${target}-header-check-cxx-${std})
43+
add_executable(${HEADER_CHECKER_LIB} ${HEADER_CHECKER_MAIN})
44+
target_link_libraries(${HEADER_CHECKER_LIB} ${target})
45+
target_compile_definitions(${HEADER_CHECKER_LIB} PRIVATE AWS_UNSTABLE_TESTING_API=1 AWS_HEADER_CHECKER=1)
46+
47+
set_target_properties(${HEADER_CHECKER_LIB} PROPERTIES
48+
LINKER_LANGUAGE CXX
49+
CXX_STANDARD ${std}
50+
CXX_STANDARD_REQUIRED OFF
51+
)
52+
53+
foreach(header IN LISTS ARGN)
54+
if (NOT ${header} MATCHES "\\.inl$")
55+
file(RELATIVE_PATH rel_header ${CMAKE_HOME_DIRECTORY} ${header})
56+
file(RELATIVE_PATH include_path "${CMAKE_HOME_DIRECTORY}/include" ${header})
57+
set(stub_dir "${HEADER_CHECKER_ROOT}/${rel_header}")
58+
file(MAKE_DIRECTORY "${stub_dir}")
59+
# include file twice to ensure header guards are present
60+
file(WRITE "${stub_dir}/check.cpp" "#include <${include_path}>\n#include <${include_path}>\n")
61+
62+
target_sources(${HEADER_CHECKER_LIB} PUBLIC "${stub_dir}/check.cpp")
63+
endif()
64+
endforeach(header)
65+
endfunction()

0 commit comments

Comments
 (0)