Skip to content

Commit

Permalink
Add WORKING_DIRECTORY option to benchmarks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chapman39 committed Sep 18, 2024
1 parent 17189f6 commit e17ff4b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
35 changes: 23 additions & 12 deletions cmake/BLTMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,19 @@ endmacro(blt_add_executable)


##------------------------------------------------------------------------------
## blt_add_test( NAME [name]
## COMMAND [command]
## NUM_MPI_TASKS [n]
## NUM_OMP_THREADS [n]
## CONFIGURATIONS [config1 [config2...]])
## blt_add_test( NAME [name]
## COMMAND [command]
## NUM_MPI_TASKS [n]
## NUM_OMP_THREADS [n]
## CONFIGURATIONS [config1 [config2...]]
## WORKING_DIRECTORY [dir])
##
## Adds a test to the project.
##------------------------------------------------------------------------------
macro(blt_add_test)

set(options )
set(singleValueArgs NAME NUM_MPI_TASKS NUM_OMP_THREADS)
set(singleValueArgs NAME NUM_MPI_TASKS NUM_OMP_THREADS WORKING_DIRECTORY)
set(multiValueArgs COMMAND CONFIGURATIONS)

# Parse the arguments to the macro
Expand All @@ -400,6 +401,13 @@ macro(blt_add_test)
message(FATAL_ERROR "COMMAND is a required parameter to blt_add_test")
endif()

# Set default working directory if not set
if ( NOT DEFINED arg_WORKING_DIRECTORY )
set(_working_directory ${CMAKE_CURRENT_BINARY_DIR})
else()
set(_working_directory ${arg_WORKING_DIRECTORY})
endif()

# Extract test directory and executable from arg_NAME and arg_COMMAND
set(_test_directory)
if(NOT TARGET ${arg_NAME})
Expand All @@ -415,14 +423,14 @@ macro(blt_add_test)
endif()

# Append the test_directory to the test argument, accounting for multi-config generators
if(NOT CMAKE_CONFIGURATION_TYPES)
if( NOT CMAKE_CONFIGURATION_TYPES )
if(NOT "${_test_directory}" STREQUAL "")
set(_test_command ${_test_directory}/${arg_COMMAND} )
else()
set(_test_command ${arg_COMMAND})
endif()
else()
if(TARGET ${_test_executable})
if( TARGET ${_test_executable} )
list(INSERT arg_COMMAND 0 "$<TARGET_FILE:${_test_executable}>")
list(REMOVE_AT arg_COMMAND 1)
endif()
Expand Down Expand Up @@ -450,7 +458,8 @@ macro(blt_add_test)

add_test(NAME ${arg_NAME}
COMMAND ${_test_command}
CONFIGURATIONS ${arg_CONFIGURATIONS})
CONFIGURATIONS ${arg_CONFIGURATIONS}
WORKING_DIRECTORY ${_working_directory})

# Handle OpenMP
if( arg_NUM_OMP_THREADS )
Expand All @@ -466,14 +475,15 @@ endmacro(blt_add_test)
## COMMAND [command]
## NUM_MPI_TASKS [n]
## NUM_OMP_THREADS [n]
## CONFIGURATIONS [config1 [config2...]])
## CONFIGURATIONS [config1 [config2...]]
## WORKING_DIRECTORY [dir])
##
## Adds a benchmark to the project.
##------------------------------------------------------------------------------
macro(blt_add_benchmark)

set(options)
set(singleValueArgs NAME NUM_MPI_TASKS NUM_OMP_THREADS)
set(singleValueArgs NAME NUM_MPI_TASKS NUM_OMP_THREADS WORKING_DIRECTORY)
set(multiValueArgs COMMAND CONFIGURATIONS)

## parse the arguments to the macro
Expand All @@ -486,7 +496,8 @@ macro(blt_add_benchmark)
COMMAND ${arg_COMMAND}
NUM_MPI_TASKS ${arg_NUM_MPI_TASKS}
NUM_OMP_THREADS ${arg_NUM_OMP_THREADS}
CONFIGURATIONS Benchmark ${arg_CONFIGURATIONS})
CONFIGURATIONS Benchmark ${arg_CONFIGURATIONS}
WORKING_DIRECTORY ${arg_WORKING_DIRECTORY})

# The 'LABELS Benchmark` prevents regular tests from
# running when running benchmarks custom target
Expand Down
32 changes: 22 additions & 10 deletions docs/api/target.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ blt_add_benchmark

.. code-block:: cmake
blt_add_benchmark( NAME [name]
COMMAND [command]
NUM_MPI_TASKS [n]
NUM_OMP_THREADS [n]
CONFIGURATIONS [config1 [config2...]])
blt_add_benchmark( NAME [name]
COMMAND [command]
NUM_MPI_TASKS [n]
NUM_OMP_THREADS [n]
CONFIGURATIONS [config1 [config2...]]
WORKING_DIRECTORY [dir])
Adds a benchmark to the project.

Expand All @@ -38,6 +39,11 @@ CONFIGURATIONS
Optionally set additional CTest configuration(s) for this test. Benchmark tests
will always be added to the ``Benchmark`` CTest configuration.

WORKING_DIRECTORY
Set the test property ``WORKING_DIRECTORY`` in which to execute the test. If
not specified, the test will be run in ``CMAKE_CURRENT_BINARY_DIR``. The working
directory may be specified using generator expressions.

This macro adds a benchmark test to the ``Benchmark`` CTest configuration
which can be run by the ``run_benchmarks`` build target. These tests are
not run when you use the regular ``test`` build target. The ``run_benchmarks``
Expand Down Expand Up @@ -261,11 +267,12 @@ blt_add_test

.. code-block:: cmake
blt_add_test( NAME [name]
COMMAND [command]
NUM_MPI_TASKS [n]
NUM_OMP_THREADS [n]
CONFIGURATIONS [config1 [config2...]])
blt_add_test( NAME [name]
COMMAND [command]
NUM_MPI_TASKS [n]
NUM_OMP_THREADS [n]
CONFIGURATIONS [config1 [config2...]]
WORKING_DIRECTORY [dir])
Adds a test to the project.

Expand All @@ -286,6 +293,11 @@ CONFIGURATIONS
Set the CTest configuration for this test. When not specified, the test
will be added to the default CTest configuration.

WORKING_DIRECTORY
Set the test property ``WORKING_DIRECTORY`` in which to execute the test. If
not specified, the test will be run in ``CMAKE_CURRENT_BINARY_DIR``. The working
directory may be specified using generator expressions.

This macro adds the named test to CTest, which is run by the build target ``test``. This macro
does not build the executable and requires a prior call to :ref:`blt_add_executable`.

Expand Down

0 comments on commit e17ff4b

Please sign in to comment.