From a8b09edaaf099896588ee43680b3f39a054357be Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Mon, 10 Jun 2024 17:20:22 +0200 Subject: [PATCH 01/10] Add optional arguments for FortUTF_Find_Tests --- cmake/fortutf.cmake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index 83f45f2..ed8844f 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -5,6 +5,9 @@ function(JOIN VALUES GLUE OUTPUT) endfunction() function(FortUTF_Find_Tests) + + cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "OPTIONS;LIBRARIES") + message(STATUS "[FortUTF]") message(STATUS "\tFinding tests in directory: ${FORTUTF_PROJECT_TEST_DIR}") if(NOT FORTUTF_PROJECT_TEST_DIR) @@ -143,10 +146,23 @@ function(FortUTF_Find_Tests) ) endif() + message(STATUS "\tLinking additional library: ${ARG_LIBRARIES}") + + target_link_libraries( + ${PROJECT_NAME}_Tests ${ARG_LIBRARIES} + ) + message(STATUS "\tCompiler Flags: ${CMAKE_Fortran_FLAGS}") target_link_libraries( ${PROJECT_NAME}_Tests ${FORTUTF} ) + target_compile_options( + ${PROJECT_NAME}_Tests + PRIVATE ${ARG_OPTIONS} + ) + + message(STATUS "\tAdditional compiler options: ${ARG_OPTIONS}") + endfunction() From 7d70a11112e80e9c432661dd622ac76b8f3f4c6c Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Wed, 12 Jun 2024 09:59:46 +0200 Subject: [PATCH 02/10] Add MODULE_DIRS option --- cmake/fortutf.cmake | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index ed8844f..36eb79e 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -6,7 +6,7 @@ endfunction() function(FortUTF_Find_Tests) - cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "OPTIONS;LIBRARIES") + cmake_parse_arguments(PARSE_ARGV 0 ARG "" "MODULE_DIRS" "OPTIONS;LIBRARIES") message(STATUS "[FortUTF]") message(STATUS "\tFinding tests in directory: ${FORTUTF_PROJECT_TEST_DIR}") @@ -129,6 +129,12 @@ function(FortUTF_Find_Tests) target_include_directories(${PROJECT_NAME}_Tests PUBLIC ${Fortran_MODULE_DIRECTORY}) + if(ARG_MODULE_DIRS) + message(STATUS "\tExternal module path: ${ARG_MODULE_DIRS}") + + target_include_directories(${PROJECT_NAME}_Tests PUBLIC ${Fortran_MODULE_DIRECTORY}) + endif() + if(FORTUTF_PROJECT_MOD_DIR) message(STATUS "\tIncluding library: ${FORTUTF_PROJECT_MOD_DIR}") TARGET_INCLUDE_DIRECTORIES( @@ -146,11 +152,13 @@ function(FortUTF_Find_Tests) ) endif() - message(STATUS "\tLinking additional library: ${ARG_LIBRARIES}") + if(ARG_LIBRARIES) + message(STATUS "\tLinking additional library: ${ARG_LIBRARIES}") - target_link_libraries( - ${PROJECT_NAME}_Tests ${ARG_LIBRARIES} - ) + target_link_libraries( + ${PROJECT_NAME}_Tests ${ARG_LIBRARIES} + ) + endif() message(STATUS "\tCompiler Flags: ${CMAKE_Fortran_FLAGS}") @@ -158,11 +166,13 @@ function(FortUTF_Find_Tests) ${PROJECT_NAME}_Tests ${FORTUTF} ) - target_compile_options( - ${PROJECT_NAME}_Tests - PRIVATE ${ARG_OPTIONS} - ) + if(ARG_OPTIONS) + target_compile_options( + ${PROJECT_NAME}_Tests + PRIVATE ${ARG_OPTIONS} + ) - message(STATUS "\tAdditional compiler options: ${ARG_OPTIONS}") + message(STATUS "\tAdditional compiler options: ${ARG_OPTIONS}") + endif() endfunction() From 3586cc9705e4bbbd97e77d6b7790de5595e66a54 Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Wed, 12 Jun 2024 10:00:28 +0200 Subject: [PATCH 03/10] Add options in example project --- examples/demo_project/CMakeLists.txt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/demo_project/CMakeLists.txt b/examples/demo_project/CMakeLists.txt index 2541bc5..4e20fd5 100644 --- a/examples/demo_project/CMakeLists.txt +++ b/examples/demo_project/CMakeLists.txt @@ -9,8 +9,20 @@ GET_FILENAME_COMPONENT(FORTUTF_ROOT ../../ ABSOLUTE) SET(FORTUTF_PROJECT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) SET(FORTUTF_PROJECT_TEST_FILES "") -FILE(GLOB FORTUTF_PROJECT_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.f90) +ADD_LIBRARY( + DEMO_FUNCTIONS + src/demo_functions.f90 +) +SET_TARGET_PROPERTIES( + DEMO_FUNCTIONS + PROPERTIES + Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mod +) INCLUDE(${FORTUTF_ROOT}/cmake/fortutf.cmake) -FortUTF_Find_Tests() +FortUTF_Find_Tests( + OPTIONS -Werror + LIBRARIES DEMO_FUNCTIONS + MODULE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/mod +) From 93805baa505f77bc665022af6e24c3d6e1168e9c Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Wed, 12 Jun 2024 10:11:28 +0200 Subject: [PATCH 04/10] Change module path to match previous style --- examples/demo_project/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/demo_project/CMakeLists.txt b/examples/demo_project/CMakeLists.txt index 4e20fd5..5628dfe 100644 --- a/examples/demo_project/CMakeLists.txt +++ b/examples/demo_project/CMakeLists.txt @@ -16,13 +16,13 @@ ADD_LIBRARY( SET_TARGET_PROPERTIES( DEMO_FUNCTIONS PROPERTIES - Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mod + Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/modules ) INCLUDE(${FORTUTF_ROOT}/cmake/fortutf.cmake) FortUTF_Find_Tests( OPTIONS -Werror LIBRARIES DEMO_FUNCTIONS - MODULE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/mod + MODULE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/modules ) From 83a90b7c4f0dcce15c18bf4e4193bedeb39b4223 Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Wed, 12 Jun 2024 10:22:24 +0200 Subject: [PATCH 05/10] Fix path error --- cmake/fortutf.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index 36eb79e..7a307fe 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -132,7 +132,7 @@ function(FortUTF_Find_Tests) if(ARG_MODULE_DIRS) message(STATUS "\tExternal module path: ${ARG_MODULE_DIRS}") - target_include_directories(${PROJECT_NAME}_Tests PUBLIC ${Fortran_MODULE_DIRECTORY}) + target_include_directories(${PROJECT_NAME}_Tests PUBLIC ${ARG_MODULE_DIRS}) endif() if(FORTUTF_PROJECT_MOD_DIR) From b81ee97bc105f44fd0fbfca1a26db8202b74c78e Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Thu, 13 Jun 2024 15:37:54 +0200 Subject: [PATCH 06/10] Add doc for recent updates --- docs/usage.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 2d50d82..4cf18fb 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,9 +1,9 @@ # Adding to your CMake project -To use the framework include the `cmake/forrtutf.cmake` file within the `CMakeLists.txt` of your project. +To use the FortUTF framework, you can either use `FetchContent` to fetch the whole repository or copy the `cmake/forrtutf.cmake` file directly to your project. Then, include the `cmake/forrtutf.cmake` file within the `CMakeLists.txt` of your project. In addition you must set either the `FORTUTF_PROJECT_SRC_FILES` variable to point to the location of the FORTRAN source files for your project, or the `FORTUTF_PROJECT_SRC_LIBRARY` variable to give the location of the library generated by building your project. -By default FortUTF will assume tests to lie within `${CMAKE_SOURCE_DIR}/tests`, you can override this by setting `FORTUTF_PROJECT_TEST_DIR` to your tests directory. +By default FortUTF will assume tests to lie within `${CMAKE_SOURCE_DIR}/tests`, you can override this by setting `FORTUTF_PROJECT_TEST_DIR` to your tests directory. FortUTF looks for add files prefixed with `test_` under the path `FORTUTF_PROJECT_TEST_DIR` to collect tests. Files that does not follow the naming convection can also be found by adding them to the `FORTUTF_PROJECT_TEST_FILES` variable. It is recommended that compiling and running of the tests be controlled by a CMake option: @@ -12,13 +12,19 @@ It is recommended that compiling and running of the tests be controlled by a CMa OPTION( BUILD_TESTS "Build Project Unit tests" OFF ) IF( BUILD_TESTS ) - # set required project files variable e.g. files in 'src' directory within the project directory - FILE( GLOB_RECURSE PROJECT_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.f90 ) + # Set path to test files (optional) + SET(FORTUTF_PROJECT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) + + # Set required project files variable e.g. files in 'src' directory within the project directory + FILE( GLOB FORTUTF_PROJECT_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.f90 ) # Include the FortUTF test finding script SET( FORTUTF_ROOT /path/to/FortUTF/folder ) INCLUDE( ${FORTUTF_ROOT}/cmake/fortutf.cmake ) ... + + # Find and compile with the CMake function + FortUTF_Find_Tests() ENDIF() ``` The script will automatically locate any tests within the project matching the pattern `test_*.f90`, and create a new script `run_tests.f90` within the build directory which will be compiled. @@ -28,6 +34,16 @@ To write unit tests create an F90 script prefixed with `test_` in a test directo The subroutine `TAG_TEST` can be called prior to the assertion to provide an identifier for recognising the test within the results should it fail, if a tag is not provided the test will be named `Test ` where `N` is the test number. +# Build unit tests with custom flags and external libraries +The CMake function `FortUTF_Find_Tests` contains optional arguments for adding flags and linking external libraries for the tests. The `FortUTF` library is linked automatically and does not need to be passed specifically. + +```cmake +FortUTF_Find_Tests(OPTIONS LIBRARIES MODULE_DIRS ) +``` +One can add compile options (flags) after the keyword `OPTIONS`, link libraries after the keyword `LIBRARIES` and include path to module files after the keyword `MODULE_DIRS`. +Both `` and `` can be multiple arguments, but `` is a single argument string to all include paths. +An usage example is provided with the `examples/demo_project`. + # Running the Tests The compiled test binary named in the form `_Tests` can be run either with arguments which will execute all tests, or by providing the names of the tests to run: From f03dbf4eab9835412bd65fa9b187a891fb96f32c Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Fri, 14 Jun 2024 10:16:44 +0200 Subject: [PATCH 07/10] Fix wrong description --- docs/usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage.md b/docs/usage.md index 4cf18fb..ea7401a 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,5 +1,5 @@ # Adding to your CMake project -To use the FortUTF framework, you can either use `FetchContent` to fetch the whole repository or copy the `cmake/forrtutf.cmake` file directly to your project. Then, include the `cmake/forrtutf.cmake` file within the `CMakeLists.txt` of your project. +To use the FortUTF framework, you can either use `FetchContent` to fetch the whole repository during configure time. Or you can clone the repo somewhere locally, then include the path to the `cmake/forrtutf.cmake` file within the `CMakeLists.txt` of your project. In addition you must set either the `FORTUTF_PROJECT_SRC_FILES` variable to point to the location of the FORTRAN source files for your project, or the `FORTUTF_PROJECT_SRC_LIBRARY` variable to give the location of the library generated by building your project. From 0f2ff3df8a2478749d5742879721c2c55841a4c0 Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Fri, 14 Jun 2024 10:24:38 +0200 Subject: [PATCH 08/10] Fix error when only passing library by argument --- cmake/fortutf.cmake | 6 ++++-- docs/usage.md | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index 7a307fe..6463a9e 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -19,8 +19,10 @@ function(FortUTF_Find_Tests) FILE(GLOB FORTUTF_SRCS ${FORTUTF_DIR}/src/*.f90) - if(NOT FORTUTF_PROJECT_SRC_FILES AND NOT FORTUTF_PROJECT_SRC_LIBRARY) - message(FATAL_ERROR "Variable SRC_FILES or SRC_LIBRARY must be set") + if(NOT ARG_LIBRARIES) + if(NOT FORTUTF_PROJECT_SRC_FILES AND NOT FORTUTF_PROJECT_SRC_LIBRARY) + message(FATAL_ERROR "Variable SRC_FILES or SRC_LIBRARY must be set") + endif() endif() FILE(GLOB_RECURSE TESTS ${FORTUTF_PROJECT_TEST_DIR}/test_*.f90) diff --git a/docs/usage.md b/docs/usage.md index ea7401a..a89be08 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -16,6 +16,7 @@ IF( BUILD_TESTS ) SET(FORTUTF_PROJECT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) # Set required project files variable e.g. files in 'src' directory within the project directory + # Or you can instead pass a built library into the FortUTF_Find_Tests() function. FILE( GLOB FORTUTF_PROJECT_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.f90 ) # Include the FortUTF test finding script From 67b4bede6184e191d747e0830ed1906da1d5fc03 Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Fri, 14 Jun 2024 10:34:36 +0200 Subject: [PATCH 09/10] Fix error when Fortran_MODULE_DIRECTORY is empty --- cmake/fortutf.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index 6463a9e..e2087a8 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -120,6 +120,7 @@ function(FortUTF_Find_Tests) endif() if(NOT TARGET ${FORTUTF}) add_library(${FORTUTF} ${FORTUTF_SRCS}) + set(Fortran_MODULE_DIRECTORY "${FORTUTF_DIR}/modules") set_target_properties( ${FORTUTF} PROPERTIES From 6c1fa3a8e04af7a7dbe7d27d63e8524b459a2378 Mon Sep 17 00:00:00 2001 From: Yen-Chen Chen Date: Fri, 14 Jun 2024 10:41:14 +0200 Subject: [PATCH 10/10] Fix error when Fortran_MODULE_DIRECTORY is empty again --- cmake/fortutf.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/fortutf.cmake b/cmake/fortutf.cmake index e2087a8..fcb4007 100644 --- a/cmake/fortutf.cmake +++ b/cmake/fortutf.cmake @@ -115,12 +115,12 @@ function(FortUTF_Find_Tests) write_file( ${FORTUTF_PROJECT_TEST_SCRIPT} " CALL TEST_SUMMARY" APPEND ) write_file( ${FORTUTF_PROJECT_TEST_SCRIPT} "END PROGRAM" APPEND ) + set(Fortran_MODULE_DIRECTORY "${FORTUTF_DIR}/modules") if(NOT DEFINED ${FORTUTF}) set(FORTUTF FortUTF) endif() if(NOT TARGET ${FORTUTF}) add_library(${FORTUTF} ${FORTUTF_SRCS}) - set(Fortran_MODULE_DIRECTORY "${FORTUTF_DIR}/modules") set_target_properties( ${FORTUTF} PROPERTIES