diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 60032ff..a9deaa6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,11 +1,42 @@ -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# Define a list set(ALL_EXAMPLES - #identity_as_default_projection -identity_direct_usage) + identity_direct_usage 17 + identity_as_default_projection 20) -foreach(example ${ALL_EXAMPLES}) +file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/tmp) + +# Iterate over the list in steps of 2 +list(LENGTH ALL_EXAMPLES ALL_EXAMPLES_LENGTH) +math(EXPR LAST_INDEX "${ALL_EXAMPLES_LENGTH} - 1") +foreach(i RANGE 0 ${LAST_INDEX} 2) + list(GET ALL_EXAMPLES "${i}" example) # Get the example name + math(EXPR cpp_standard_index "${i} + 1") + list(GET ALL_EXAMPLES ${cpp_standard_index} cpp_standard) # Get the C++ standard for that example + message(STATUS "Try compile example: ${example} cpp_standard: ${cpp_standard}") + + # Try to compile the example. Continue to the next example if compilation fails. + try_compile( + COMPILE_SUCCESS + ${CMAKE_BINARY_DIR}/tmp + ${CMAKE_CURRENT_SOURCE_DIR}/${example}.cpp + CMAKE_FLAGS + -DCMAKE_CXX_STANDARD=${cpp_standard} + -DCMAKE_CXX_STANDARD_REQUIRED=YES + -DCMAKE_CXX_EXTENSIONS=NO + -DCMAKE_CXX_FLAGS="-I${CMAKE_CURRENT_SOURCE_DIR}/../include" # does not work + OUTPUT_VARIABLE COMPILE_OUTPUT # Capture output + ) + message(STATUS "Compile output: ${COMPILE_OUTPUT}") # debug + if (NOT COMPILE_SUCCESS) + message(WARNING "Skipping ${example}: Requires C++${cpp_standard} or later") + continue() + endif() + + # Add executable and link to the target. add_executable(beman.exemplar.examples.${example}) target_sources(beman.exemplar.examples.${example} PRIVATE ${example}.cpp) target_link_libraries(beman.exemplar.examples.${example} beman::exemplar) + target_compile_features(beman.exemplar.examples.${example} PRIVATE cxx_std_${cpp_standard}) endforeach()