Skip to content

Commit

Permalink
Merge pull request #23 from yanjen/cmake_option
Browse files Browse the repository at this point in the history
Add option arguments for FortUTF_Find_Tests
  • Loading branch information
artemis-beta authored Jul 2, 2024
2 parents 91980e0 + 6c1fa3a commit ab4cead
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
33 changes: 31 additions & 2 deletions cmake/fortutf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ function(JOIN VALUES GLUE OUTPUT)
endfunction()

function(FortUTF_Find_Tests)

cmake_parse_arguments(PARSE_ARGV 0 ARG "" "MODULE_DIRS" "OPTIONS;LIBRARIES")

message(STATUS "[FortUTF]")
message(STATUS "\tFinding tests in directory: ${FORTUTF_PROJECT_TEST_DIR}")
if(NOT FORTUTF_PROJECT_TEST_DIR)
Expand All @@ -16,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)
Expand Down Expand Up @@ -110,6 +115,7 @@ 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()
Expand All @@ -126,6 +132,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 ${ARG_MODULE_DIRS})
endif()

if(FORTUTF_PROJECT_MOD_DIR)
message(STATUS "\tIncluding library: ${FORTUTF_PROJECT_MOD_DIR}")
TARGET_INCLUDE_DIRECTORIES(
Expand All @@ -143,10 +155,27 @@ function(FortUTF_Find_Tests)
)
endif()

if(ARG_LIBRARIES)
message(STATUS "\tLinking additional library: ${ARG_LIBRARIES}")

target_link_libraries(
${PROJECT_NAME}_Tests ${ARG_LIBRARIES}
)
endif()

message(STATUS "\tCompiler Flags: ${CMAKE_Fortran_FLAGS}")

target_link_libraries(
${PROJECT_NAME}_Tests ${FORTUTF}
)

if(ARG_OPTIONS)
target_compile_options(
${PROJECT_NAME}_Tests
PRIVATE ${ARG_OPTIONS}
)

message(STATUS "\tAdditional compiler options: ${ARG_OPTIONS}")
endif()

endfunction()
25 changes: 21 additions & 4 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -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 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.

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:

Expand All @@ -12,13 +12,20 @@ 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
# 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
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.
Expand All @@ -28,6 +35,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 <N>` 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 <compile_options> LIBRARIES <external_libraries> MODULE_DIRS <path_to_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 `<compile_options>` and `<external_libraries>` can be multiple arguments, but `<path_to_dirs>` 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 `<PROJECT_NAME>_Tests` can be run either with arguments which will execute all tests, or by providing the names of the tests to run:
Expand Down
16 changes: 14 additions & 2 deletions examples/demo_project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}/modules
)

INCLUDE(${FORTUTF_ROOT}/cmake/fortutf.cmake)
FortUTF_Find_Tests()
FortUTF_Find_Tests(
OPTIONS -Werror
LIBRARIES DEMO_FUNCTIONS
MODULE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/modules
)

0 comments on commit ab4cead

Please sign in to comment.