cmake_minimum_required(VERSION 3.8) project(TestFindMPI NONE) include(CTest) macro(source_code_mapper_helper LANG_NAME) if("${LANG_NAME}" STREQUAL "C") set(MPITEST_SOURCE_FILE "main.c") elseif("${LANG_NAME}" STREQUAL "CXX") configure_file("main.c" "main.cxx" COPYONLY) set(MPITEST_SOURCE_FILE "main.cxx") elseif("${LANG_NAME}" STREQUAL "Fortran") set(MPITEST_SOURCE_FILE "main.f90") endif() endmacro() foreach(c C CXX Fortran) if("${MPI_TEST_${c}}") message("Testing ${c}") enable_language(${c}) endif() endforeach() find_package(MPI REQUIRED) foreach(c C CXX Fortran) if(NOT "${MPI_TEST_${c}}") continue() endif() source_code_mapper_helper(${c}) add_executable(test_tgt_${c} ${MPITEST_SOURCE_FILE}) target_link_libraries(test_tgt_${c} MPI::MPI_${c}) add_test(NAME test_tgt_${c} COMMAND test_tgt_${c}) add_executable(test_var_${c} ${MPITEST_SOURCE_FILE}) target_include_directories(test_var_${c} PRIVATE "${MPI_${c}_INCLUDE_PATH}") target_link_libraries(test_var_${c} PRIVATE "${MPI_${c}_LINK_FLAGS}" "${MPI_${c}_LIBRARIES}") set(copied_MPI_${c}_OPTIONS "${MPI_${c}_COMPILE_FLAGS}") separate_arguments(copied_MPI_${c}_OPTIONS) target_compile_options(test_var_${c} PRIVATE "${copied_MPI_${c}_OPTIONS}") add_test(NAME test_var_${c} COMMAND test_var_${c}) endforeach()