Skip to content

Commit

Permalink
Separate core and serial components
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi committed Mar 17, 2024
1 parent a7e779f commit 9f1c167
Show file tree
Hide file tree
Showing 46 changed files with 298 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
- name: Build Fortuno
run: |
cmake -B ${BUILD_DIR} -G Ninja -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}
cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -B ${BUILD_DIR} -G Ninja
cmake --build ${BUILD_DIR}
cmake --install ${BUILD_DIR}
rm -rf ${BUILD_DIR}
Expand Down
49 changes: 42 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,41 @@ project(
# Options #
]=================================================================================================]

include(CMakeDependentOption)

option(FORTUNO_BUILD_SHARED_LIBS "Fortuno: Build as shared library" ${PROJECT_IS_TOP_LEVEL})

option(FORTUNO_BUILD_TESTS "Fortuno: Build test suite" ${PROJECT_IS_TOP_LEVEL})
option(
FORTUNO_BUILD_SERIAL_INTERFACE
"Fortuno: whether serial interface should be built (or only core library)"
ON
)

option(FORTUNO_BUILD_EXAMPLES "Fortuno: Build example apps" ${PROJECT_IS_TOP_LEVEL})
cmake_dependent_option(
FORTUNO_BUILD_TESTS "Fortuno: Build test suite" ${PROJECT_IS_TOP_LEVEL}
"FORTUNO_BUILD_SERIAL_INTERFACE" OFF
)

cmake_dependent_option(
FORTUNO_BUILD_EXAMPLES "Fortuno: Build example apps" ${PROJECT_IS_TOP_LEVEL}
"FORTUNO_BUILD_SERIAL_INTERFACE" OFF
)

option(FORTUNO_INSTALL "Fortuno: Install project" ${PROJECT_IS_TOP_LEVEL})

set(
FORTUNO_INSTALL_MODULEDIR "modules" CACHE STRING
"Sub-directory to install the Fortran module files into (relative to CMAKE_INSTALL_LIBDIR)"
"Fortuno: Sub-directory to install the Fortran module files into (relative to CMAKE_INSTALL_LIBDIR)"
)

set(
FORTUNO_THREAD_SAFE_FLAGS "" CACHE STRING
"Fortuno: Flags for enforcing thread-safe build during compilation"
)

set(
FORTUNO_THREAD_SAFE_LINK_FLAGS "" CACHE STRING
"Fortuno: Flags for enforcing thread-safe build durink linkage"
)

#[=================================================================================================[
Expand All @@ -49,8 +73,9 @@ if (FORTUNO_INSTALL)
include(GNUInstallDirs)
endif ()

fortuno_setup_build_type("RelWithDebInfo")
set(BUILD_SHARED_LIBS ${FORTUNO_BUILD_SHARED_LIBS})
fortuno_setup_build_type("RelWithDebInfo")
fortuno_def_thread_safe_build_target()

#[=================================================================================================[
# Main definition #
Expand All @@ -75,11 +100,20 @@ if (FORTUNO_INSTALL)
COMPONENT Fortuno_development
)

if (FORTUNO_BUILD_SERIAL_INTERFACE)
configure_file(cmake/fortuno-serial.pc.in fortuno-serial.pc @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/fortuno-serial.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
COMPONENT Fortuno_development
)
endif ()

# cmake export files
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/FortunoConfigVersion.cmake
VERSION ${PROJECT_VERSION}
# COMPATIBILITY SameMajorVersion
# TODO: Switch to SameMajorVersion as soon as project version reaches 1.0.
COMPATIBILITY SameMinorVersion
)
configure_package_config_file(
Expand All @@ -88,8 +122,9 @@ if (FORTUNO_INSTALL)
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Fortuno
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/FortunoConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/FortunoConfig.cmake
FILES
${CMAKE_CURRENT_BINARY_DIR}/FortunoConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/FortunoConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Fortuno
COMPONENT Fortuno_development
)
Expand Down
26 changes: 13 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ The development can be followed and joined at the `Fortuno project
Quickstart
==========

The following instructions demonstrate how to add unit testing via Fortuno to an
existing project, which uses fpm, CMake or Meson as build system. If you are not
familiar with any of these build systems, visit the `Fortuno documentation
<https://fortuno.readthedocs.io>`_ for a step-by-step guide starting from
scratch.
The following instructions demonstrate how to add unit tests testing serial code
via Fortuno to an existing project, which uses fpm, CMake or Meson as build
system. If you are not familiar with any of these build systems, visit the
`Fortuno documentation <https://fortuno.readthedocs.io>`_ for a step-by-step
guide starting from scratch.

In the examples below, we will assume that your library has a module ``mylib``,
which provides a function ``factorial()`` for calculating the factorial of
Expand Down Expand Up @@ -81,8 +81,7 @@ project's build process. The actual steps depend on your build system:
Register Fortuno as a subproject by adding the following to your main
``meson.build`` file::

fortuno_subproject = subproject('fortuno')
fortuno_dep = fortuno_subproject.get_variable('fortuno_dep')
fortuno_serial_dep = dependency('fortuno-serial', fallback: ['fortuno', 'fortuno_serial_dep'])


Writing unit tests
Expand All @@ -102,8 +101,8 @@ could look as follows::
!> Test app driving Fortuno unit tests.
program testapp
use mylib, only : factorial
use fortuno, only : execute_serial_cmd_app, is_equal, test => serial_case_item,&
check => serial_check
use fortuno_serial, only : execute_serial_cmd_app, is_equal, test => serial_case_item,&
& check => serial_check
implicit none

!> Register tests by providing name and subroutine to run for each test.
Expand Down Expand Up @@ -145,13 +144,13 @@ build system:
fpm build

* **CMake:** Declare an executable ``testapp`` with ``testapp.f90`` as source
and target ``Fortuno::Fortuno`` as dependency in the ``CMakeLists.txt`` file.
Add also the target name of your library (e.g. ``mylib``) as dependency.
and target ``Fortuno::fortuno_serial`` as dependency in the ``CMakeLists.txt``
file. Add also the target name of your library (e.g. ``mylib``) as dependency.
Additionally, register the executable as a test, so that it can be executed
via ``ctest``::

add_executable(testapp testapp.f90)
target_link_libraries(testapp PRIVATE mylib Fortuno::Fortuno)
target_link_libraries(testapp PRIVATE mylib Fortuno::fortuno_serial)
add_test(NAME factorial COMMAND testapp)

Make also sure to call ``enable_testing()`` in your main ``CMakeLists.txt``
Expand All @@ -170,7 +169,7 @@ build system:
testapp_exe = executable(
'testapp',
sources: ['testapp.f90'],
dependencies: [mylib_dep, fortuno_dep],
dependencies: [mylib_dep, fortuno_serial_dep],
)
test('factorial', testapp_exe)

Expand Down Expand Up @@ -243,6 +242,7 @@ Fortuno. We recommend to use those compilers or any newer versions of them.
+------------------------+-----------------------------------------------------+
| GNU 13.2 | * serial: OK |
| | * mpi: OK |
| | * coarray: not tested yet |
+------------------------+-----------------------------------------------------+

If you are aware of any other compilers being able to build Fortuno, open a pull
Expand Down
22 changes: 22 additions & 0 deletions cmake/FortunoHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ function (fortuno_setup_build_type default_build_type)
endif()

endfunction()


# Defines the ThreadSafeBuild target for the thread-safe parts
function (fortuno_def_thread_safe_build_target)

if (FORTUNO_THREAD_SAFE_FLAGS)
set(_compiler_flags "${FORTUNO_THREAD_SAFE_FLAGS}")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG")
set(_compiler_flags "-thread_safe")
endif ()

if (FORTUNO_THREAD_SAFE_LINK_FLAGS)
set(_linker_flags "${FORTUNO_THREAD_SAFE_LINK_FLAGS}")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "NAG")
set(_linker_flags "-thread_safe")
endif ()

add_library(ThreadSafeBuild INTERFACE)
target_compile_options(ThreadSafeBuild INTERFACE ${_compiler_flags})
target_link_options(ThreadSafeBuild INTERFACE ${_linker_flags})

endfunction ()
7 changes: 7 additions & 0 deletions cmake/fortuno-serial.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@

Requires: fortuno
Cflags: -I@CMAKE_INSTALL_FULL_LIBDIR@/@FORTUNO_INSTALL_MODULEDIR@
Libs: -L@CMAKE_INSTALL_FULL_LIBDIR@ -lfortuno-serial
14 changes: 7 additions & 7 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

list(APPEND CMAKE_MESSAGE_CONTEXT Example)

add_library(Fortuno_example_mylib)
add_library(fortuno_example_mylib)
set_target_properties(
Fortuno_example_mylib PROPERTIES
fortuno_example_mylib PROPERTIES
OUTPUT_NAME mylib
)
target_sources(
Fortuno_example_mylib PRIVATE
fortuno_example_mylib PRIVATE
mylib.f90
)

add_executable(Fortuno_example_testapp)
add_executable(fortuno_example_testapp)
set_target_properties(
Fortuno_example_testapp PROPERTIES
fortuno_example_testapp PROPERTIES
OUTPUT_NAME testapp
)
target_sources(
Fortuno_example_testapp PRIVATE
fortuno_example_testapp PRIVATE
fixtured_tests.f90
simple_tests.f90
testapp.f90
)
target_link_libraries(Fortuno_example_testapp PRIVATE Fortuno_example_mylib Fortuno::Fortuno)
target_link_libraries(fortuno_example_testapp PRIVATE fortuno_example_mylib Fortuno::fortuno_serial)
5 changes: 2 additions & 3 deletions example/fixtured_tests.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

module fixtured_tests
use mylib, only : factorial
use fortuno, only : check => serial_check, test => serial_case_item,&
& suite => serial_suite_item, test_item, test_ptr_item,&
& serial_suite, serial_case_base
use fortuno_serial, only : check => serial_check, test => serial_case_item,&
& suite => serial_suite_item, serial_case_base, test_item
implicit none

private
Expand Down
2 changes: 1 addition & 1 deletion example/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ example_testapp_exe = executable(
'simple_tests.f90',
'testapp.f90',
],
dependencies: [example_mylib_dep, fortuno_dep],
dependencies: [example_mylib_dep, fortuno_serial_dep],
install: false,
)
2 changes: 1 addition & 1 deletion example/simple_tests.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module simple_tests
use mylib, only : factorial
use fortuno, only : is_equal, test => serial_case_item, check => serial_check,&
use fortuno_serial, only : is_equal, test => serial_case_item, check => serial_check,&
& suite => serial_suite_item, test_item
implicit none

Expand Down
4 changes: 2 additions & 2 deletions example/testapp.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

!> Test app, collecting and executing the tests
!> Test app with command line interface, collecting and executing the tests.
program testapp
use fortuno, only : execute_serial_cmd_app
use fortuno_serial, only : execute_serial_cmd_app
use simple_tests, only : get_simple_tests
use fixtured_tests, only : get_fixtured_tests
implicit none
Expand Down
33 changes: 24 additions & 9 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,41 @@ project(
version: '0.1.0',
)

### Building the library ###
build_serial_interface = get_option('build_serial_interface')
build_examples = get_option('build_examples')

fortuno_srcs = []
fortuno_sources = []
fortuno_serial_sources = []
subdir('src')

fortuno_lib = library(
meson.project_name(),
'fortuno',
version: meson.project_version(),
sources: fortuno_srcs,
sources: fortuno_sources,
)

### Exporting the library ###

fortuno_dep = declare_dependency(
link_with: fortuno_lib,
)

### Building optional components ###
if build_serial_interface

build_examples = get_option('build_examples')
if build_examples
fortuno_serial_deps = [fortuno_dep]

fortuno_serial_lib = library(
'fortuno_serial',
version: meson.project_version(),
sources: fortuno_serial_sources,
dependencies: fortuno_serial_deps,
)

fortuno_serial_dep = declare_dependency(
link_with: fortuno_serial_lib,
dependencies: fortuno_serial_deps,
)

endif

if build_serial_interface and build_examples
subdir('example')
endif
2 changes: 2 additions & 0 deletions meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# Licensed under the BSD-2-Clause Plus Patent license.
# SPDX-License-Identifier: BSD-2-Clause-Patent

option('build_serial_interface', type: 'boolean', value: true, description: 'Build serial interface')

option('build_examples', type: 'boolean', value: false, description: 'Build examples')
Loading

0 comments on commit 9f1c167

Please sign in to comment.