Skip to content

Instantly share code, notes, and snippets.

@plevold
Last active May 18, 2017 06:28
Show Gist options
  • Save plevold/330d490d994395e18729ea1ed5a6f5b5 to your computer and use it in GitHub Desktop.
Save plevold/330d490d994395e18729ea1ed5a6f5b5 to your computer and use it in GitHub Desktop.
Using pFUnit with CMake
# Note that this section is a bit hack-ish, as ExternalProject_Add does
# not by default build the project at configure time. However, for pFUnit,
# this is an advantage.
include (ProcessorCount)
ProcessorCount(NPROC)
if (${NPROC} EQUAL 0)
set (${NPROC} 1)
message (WARNING "Processor count not found, setting to 1")
endif (${NPROC} EQUAL 0)
## pFUnit config and build settings
if (WIN32)
set (PFUNIT_CONFIG_FLAGS "-DROBUST=OFF")
endif (WIN32)
if (CMAKE_GENERATOR MATCHES ".*Makefiles.*")
set (PFUNIT_BUILD_FLAGS "-j${NPROC}")
endif (CMAKE_GENERATOR MATCHES ".*Makefiles.*")
# Store Fortran mod files and generated interfaces in separate folder
set (CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/include/)
# Download and unpack pfunit at configure time
configure_file (${CMAKE_CURRENT_LIST_DIR}/pfunit-CMakeLists.txt.in ${PFUNIT_DOWNLOAD_DIR}/CMakeLists.txt)
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY ${PFUNIT_DOWNLOAD_DIR})
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
WORKING_DIRECTORY ${PFUNIT_DOWNLOAD_DIR})
# Configure pFUnit
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${PFUNIT_SRC_DIR} -DCMAKE_INSTALL_PREFIX=${PFUNIT_INSTALL_DIR} ${PFUNIT_CONFIG_FLAGS}
WORKING_DIRECTORY ${PFUNIT_BUILD_DIR})
## Build pFUnit
execute_process(
COMMAND ${CMAKE_COMMAND} --build . --target install -- ${PFUNIT_BUILD_FLAGS}
WORKING_DIRECTORY ${PFUNIT_BUILD_DIR})
cmake_minimum_required (VERSION 3.2 FATAL_ERROR)
# Enable CTest
enable_testing()
# This assumes that you've already cloned the pFUnit source code, built and installed it (make install)
# to a suitable location on your computer. Adding search paths using the PATHS argument is often necessary.
# See https://cmake.org/cmake/help/v3.0/command/find_package.html for more information.
# Note that the pFUnit package will be OS, compiler and compiler version dependent as it distributes Fortran mod-files.
find_package("pFUnit" REQUIRED)
# Alternatively pFUnit could be built during configuration of the CMake build folder by commenting out the line above
# and uncommenting the following lines:
# set (PFUNIT_SRC_DIR ${CMAKE_BINARY_DIR}/pfunit/src)
# set (PFUNIT_DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/pfunit/download)
# set (PFUNIT_BUILD_DIR ${CMAKE_BINARY_DIR}/pfunit/build)
# set (PFUNIT_INSTALL_DIR ${CMAKE_BINARY_DIR}/pfunit/install)
# set (pFUnit_DIR ${PFUNIT_INSTALL_DIR})
# message ("Building pFUnit")
# include (build_pfuint_from_source.cmake)
# message ("Looking for pFUnit here: ${PFUNIT_INSTALL_DIR}")
# find_package ("pFUnit"
# CONFIG
# PATHS ${PFUNIT_INSTALL_DIR})
# Now that we have found the pFUnit package we can define a test executbale.
# Here, it's assumed that we'd like to test a library defined in the LIB_NAME variable,
# e.g. set (LIB_NAME some_lib)
# pFUnit test source files
set (TEST_SOURCES
test_something.pf
test_another_thing.pf
)
# Extra Fortran and C/C++ test sources (often not needed)
set (EXTRA_TEST_SOURCES_Fortran
)
set (EXTRA_TEST_SOURCES_C
)
# Add pFUnit test if any TEST_SOURCES is defined
if (NOT "${TEST_SOURCES}" STREQUAL "")
add_pfunit_test(test_${LIB_NAME}
"${TEST_SOURCES}"
"${EXTRA_TEST_SOURCES_Fortran}"
"${EXTRA_TEST_SOURCES_C}")
target_link_libraries (test_${LIB_NAME}
${LIB_NAME})
endif (NOT "${TEST_SOURCES}" STREQUAL "")
cmake_minimum_required(VERSION 3.2)
project(pfunit-download LANGUAGES NONE
VERSION 0.0.0.0)
include(ExternalProject)
ExternalProject_Add(pfunit
GIT_REPOSITORY https://git.code.sf.net/p/pfunit/code
GIT_TAG master
SOURCE_DIR "${PFUNIT_SRC_DIR}"
BINARY_DIR "${PFUNIT_BUILD_DIR}"
INSTALL_DIR "${PFUNIT_INSTALL_DIR}"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment