Skip to content

Commit

Permalink
Merge pull request #12 from stephenswat/feat/header_tests
Browse files Browse the repository at this point in the history
Add header completeness tests
  • Loading branch information
stephenswat authored Oct 11, 2023
2 parents 39b8a0d + 9af0c0d commit 6fd3a3c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
-DCOVFIE_BUILD_TESTS=On
-DCOVFIE_BUILD_EXAMPLES=On
-DCOVFIE_BUILD_BENCHMARKS=On
-DCOVFIE_TEST_HEADERS=On
-DCMAKE_CXX_STANDARD=${{ matrix.CXX_STANDARD }}
-DCMAKE_PREFIX_PATH="$GITHUB_WORKSPACE/.prefixes/gtest/"
-S $GITHUB_WORKSPACE
Expand Down Expand Up @@ -127,6 +128,7 @@ jobs:
-DCOVFIE_BUILD_BENCHMARKS=On
-DCOVFIE_PLATFORM_CUDA=On
-DCOVFIE_PLATFORM_CPU=On
-DCOVFIE_TEST_HEADERS=On
-DCMAKE_CXX_STANDARD=${{ matrix.CXX_STANDARD }}
-DCMAKE_PREFIX_PATH="$GITHUB_WORKSPACE/.prefixes/gtest/"
-S $GITHUB_WORKSPACE
Expand Down Expand Up @@ -181,6 +183,7 @@ jobs:
-DCOVFIE_BUILD_BENCHMARKS=On
-DCOVFIE_PLATFORM_CPU=On
-DCOVFIE_QUIET=On
-DCOVFIE_TEST_HEADERS=On
-DCMAKE_CXX_STANDARD=${{ matrix.CXX_STANDARD }}
-DCMAKE_PREFIX_PATH="${{ github.workspace }}\.prefix"
-S ${{ github.workspace }}
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ option(
"Build example executables."
)

# Declare option to enable header completeness tests.
option(
COVFIE_TEST_HEADERS
"Enable header completeness tests."
)

# Declare options for the different platforms that we wish to support.
option(
COVFIE_PLATFORM_CPU
Expand Down
41 changes: 41 additions & 0 deletions cmake/covfie-functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# Helper function testing the covfie public headers.
#
# It can be used to test that public headers would include everything
# that they need to work, and that the CMake library targets would take
# care of declaring all of their dependencies correctly for the public
# headers to work.
#
# Usage: covfie_test_public_headers( covfie_core
# include/header1.hpp ... )
#
function( covfie_test_public_headers library )
# All arguments are treated as header file names.
foreach( _headerName ${ARGN} )

# Make the header filename into a "string".
string( REPLACE "/" "_" _headerNormName "${_headerName}" )
string( REPLACE "." "_" _headerNormName "${_headerNormName}" )

# Write a small source file that would test that the public
# header can be used as-is.
set( _testFileName
"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test_${library}_${_headerNormName}.cpp" )
if( NOT EXISTS "${_testFileName}" )
file( WRITE "${_testFileName}"
"#include \"${_headerName}\"\n"
"int main() { return 0; }" )
endif()

# Set up an executable that would build it. But hide it, don't put it
# into ${CMAKE_BINARY_DIR}/bin.
add_executable( "test_${library}_${_headerNormName}" "${_testFileName}" )
target_link_libraries( "test_${library}_${_headerNormName}"
PRIVATE ${library} )
set_target_properties( "test_${library}_${_headerNormName}" PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}" )

endforeach()

endfunction( covfie_test_public_headers )

# Helper function for adding individual flags to "flag variables".
#
# Usage: covfie_add_flag( CMAKE_CXX_FLAGS "-Wall" )
Expand Down
18 changes: 18 additions & 0 deletions lib/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,21 @@ install(
# Hack for people using the disgusting mal-practice of pullling in external
# projects via "add_subdirectory"...
add_library(covfie::core ALIAS core)

# Test the public headers of covfie::core.
if(COVFIE_TEST_HEADERS)
include(covfie-functions)

file(
GLOB_RECURSE
public_headers
RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/covfie/*.hpp"
)

covfie_test_public_headers(
core
"${public_headers}"
)
endif()
18 changes: 18 additions & 0 deletions lib/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ target_link_libraries(

# Hack for compatibility
add_library(covfie::cpu ALIAS cpu)

# Test the public headers of covfie::cpu.
if(COVFIE_TEST_HEADERS)
include(covfie-functions)

file(
GLOB_RECURSE
public_headers
RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/covfie/*.hpp"
)

covfie_test_public_headers(
cpu
"${public_headers}"
)
endif()
18 changes: 18 additions & 0 deletions lib/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ install(

# Hack for compatibility
add_library(covfie::cuda ALIAS cuda)

# Test the public headers of covfie::cuda.
if(COVFIE_TEST_HEADERS)
include(covfie-functions)

file(
GLOB_RECURSE
public_headers
RELATIVE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/covfie/*.hpp"
)

covfie_test_public_headers(
cuda
"${public_headers}"
)
endif()

0 comments on commit 6fd3a3c

Please sign in to comment.