Skip to content

Commit

Permalink
Merge header checker with cpp header checker (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored May 25, 2023
1 parent cced6eb commit 2e01315
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
# Test downstream repos.
# This should not be required because we can run into a chicken and egg problem if there is a change that needs some fix in a downstream repo.
downstream:
runs-on: ubuntu-20.04 # latest
runs-on: ubuntu-22.04 # latest
steps:
# We can't use the `uses: docker://image` version yet, GitHub lacks authentication for actions -> packages
- name: Build ${{ env.PACKAGE_NAME }}
Expand Down
110 changes: 68 additions & 42 deletions cmake/AwsCheckHeaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,83 @@ endif()

# Call as: aws_check_headers(${target} HEADERS TO CHECK LIST)
function(aws_check_headers target)
if (PERFORM_HEADER_CHECK)
if (NOT PERFORM_HEADER_CHECK)
return()
endif()
# parse function arguments
set(options IS_CXX)
cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})

set(HEADER_CHECKER_ROOT "${CMAKE_CURRENT_BINARY_DIR}/header-checker")
aws_check_headers_internal(${target} 11 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS})
aws_check_headers_internal(${target} 14 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS})
aws_check_headers_internal(${target} 17 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS})
aws_check_headers_internal(${target} 20 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS})
aws_check_headers_internal(${target} 23 ${ARG_IS_CXX} ${ARG_UNPARSED_ARGUMENTS})
endfunction()

# Write stub main file
set(HEADER_CHECKER_MAIN "${HEADER_CHECKER_ROOT}/headerchecker_main.c")
file(WRITE ${HEADER_CHECKER_MAIN} "
int main(int argc, char **argv) {
(void)argc;
(void)argv;
function(aws_check_headers_internal target std is_cxx)
# Check that compiler supports this std
list (FIND CMAKE_CXX_COMPILE_FEATURES "cxx_std_${std}" feature_idx)
if (${feature_idx} LESS 0)
return()
endif()

return 0;
}\n")
set(HEADER_CHECKER_ROOT "${CMAKE_CURRENT_BINARY_DIR}/header-checker-cxx${std}")

set(HEADER_CHECKER_LIB ${target}-header-check)
add_executable(${HEADER_CHECKER_LIB} ${HEADER_CHECKER_MAIN})
target_link_libraries(${HEADER_CHECKER_LIB} ${target})
target_compile_definitions(${HEADER_CHECKER_LIB} PRIVATE AWS_UNSTABLE_TESTING_API=1 AWS_HEADER_CHECKER=1)
# Write stub main file
set(HEADER_CHECKER_MAIN "${HEADER_CHECKER_ROOT}/headerchecker_main.c")
set(HEADER_CHECKER_LIB ${target}-header-check-cxx${std})
file(WRITE ${HEADER_CHECKER_MAIN} "
int main(int argc, char **argv) {
(void)argc;
(void)argv;
# We want to be able to verify that the proper C++ header guards are in place, so
# build this target as a C++ application
set_target_properties(${HEADER_CHECKER_LIB} PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 11
CXX_STANDARD_REQUIRED 0
C_STANDARD 99
)
return 0;
}\n")

# Ensure our headers can be included by an application with its warnings set very high
if(MSVC)
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE /Wall /WX)

add_executable(${HEADER_CHECKER_LIB} ${HEADER_CHECKER_MAIN})
target_link_libraries(${HEADER_CHECKER_LIB} ${target})
target_compile_definitions(${HEADER_CHECKER_LIB} PRIVATE AWS_UNSTABLE_TESTING_API=1 AWS_HEADER_CHECKER=1)

# We want to be able to verify that the proper C++ header guards are in place, so
# build this target as a C++ application
set_target_properties(${HEADER_CHECKER_LIB} PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD ${std}
CXX_STANDARD_REQUIRED 0
C_STANDARD 99
)

# Ensure our headers can be included by an application with its warnings set very high
if(MSVC)
if (is_cxx)
# In MSVC, the C++ std library headers trigger /Wall warnings, so use /W4 instead.
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE /W4 /WX)
else()
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE -Wall -Wextra -Wpedantic -Werror)
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE /Wall /WX)
endif()
else()
target_compile_options(${HEADER_CHECKER_LIB} PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()

foreach(header IN LISTS ARGN)
if (NOT ${header} MATCHES "\\.inl$")
# create unique token for this file, e.g.:
# "${CMAKE_CURRENT_SOURCE_DIR}/include/aws/common/byte_buf.h" -> "aws_common_byte_buf_h"
file(RELATIVE_PATH include_path "${CMAKE_CURRENT_SOURCE_DIR}/include" ${header})
# replace non-alphanumeric characters with underscores
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" unique_token ${include_path})
set(c_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.c")
set(cpp_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.cpp")
# include header twice to check for include-guards
# define a unique int or compiler complains that there's nothing in the file
foreach(header IN LISTS ARGN)
if (NOT ${header} MATCHES "\\.inl$")
# create unique token for this file, e.g.:
# "${CMAKE_CURRENT_SOURCE_DIR}/include/aws/common/byte_buf.h" -> "aws_common_byte_buf_h"
file(RELATIVE_PATH include_path "${CMAKE_CURRENT_SOURCE_DIR}/include" ${header})
# replace non-alphanumeric characters with underscores
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" unique_token ${include_path})
set(c_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.c")
set(cpp_file "${HEADER_CHECKER_ROOT}/headerchecker_${unique_token}.cpp")
# include header twice to check for include-guards
# define a unique int or compiler complains that there's nothing in the file
file(WRITE "${cpp_file}" "#include <${include_path}>\n#include <${include_path}>\nint ${unique_token}_cpp;")
target_sources(${HEADER_CHECKER_LIB} PUBLIC "${cpp_file}")
if (NOT is_cxx)
file(WRITE "${c_file}" "#include <${include_path}>\n#include <${include_path}>\nint ${unique_token}_c;\n")
file(WRITE "${cpp_file}" "#include <${include_path}>\n#include <${include_path}>\nint ${unique_token}_cpp;")

target_sources(${HEADER_CHECKER_LIB} PUBLIC "${c_file}" "${cpp_file}")
target_sources(${HEADER_CHECKER_LIB} PUBLIC "${c_file}")
endif()
endforeach(header)
endif() # PERFORM_HEADER_CHECK
endif()
endforeach(header)
endfunction()

0 comments on commit 2e01315

Please sign in to comment.