diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 909d843..3d29d66 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -20,6 +20,10 @@ jobs: cmake -S . -B build # cmake -S -B cmake --build build + - name: Run CTest + if: ${{ runner.os == 'Linux' }} + run: ctest --test-dir build --output-on-failure + - name: Run compiled programs on Linux if: ${{ runner.os == 'Linux' }} run: | diff --git a/.gitignore b/.gitignore index 1a60365..13304df 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ build/ .codex +# Generated by CMake at configure time - do not commit +libdedx/dedx_config.h + # Created by https://www.toptal.com/developers/gitignore/api/c,visualstudiocode,jetbrains+all # Edit at https://www.toptal.com/developers/gitignore?templates=c,visualstudiocode,jetbrains+all diff --git a/CMakeLists.txt b/CMakeLists.txt index e25c63d..9220e01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,43 +1,59 @@ -#[[ As minimum CMake we choose version which supports Ubuntu 18.04 LTS -I followed advice from https://hsf-training.github.io/hsf-training-cmake-webpage/aio/index.html -CMake 3.10 was released 20.11.2017 (https://www.kitware.com/cmake-3-10-0-available-for-download/) - -consider using newer version of CMake, i.e. CMake 3.15 (released 17.07.2019) supports `-j N threads` and `-t target` -(following https://indico.jlab.org/event/420/contributions/7961/attachments/6507/8734/CMakeSandCroundtable.slides.pdf)]] - cmake_minimum_required(VERSION 3.10) project(dedx LANGUAGES C) -# List of variables to be inserted into libdedx/dedx_config.h by `configure_file` command -# The DEDX version number. -set (DEDX_VERSION_MAJOR 1) -set (DEDX_VERSION_MINOR 2) -set (DEDX_VERSION_PATCH 1) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# ---- Version from git tag ---- +find_package(Git QUIET) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --always --dirty + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) +endif() +if(NOT GIT_VERSION) + set(GIT_VERSION "0.0.0-unknown") +endif() +message(STATUS "libdedx version: ${GIT_VERSION}") -set (DEDX_DATA_PATH_LOCAL "data/") -if (WIN32) - set (DEDX_DATA_PATH "data/") +# Parse semver components from tag (e.g. v1.2.1 or v1.2.1-3-gabcdef) +string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ "${GIT_VERSION}") +if(CMAKE_MATCH_1) + set(DEDX_VERSION_MAJOR ${CMAKE_MATCH_1}) + set(DEDX_VERSION_MINOR ${CMAKE_MATCH_2}) + set(DEDX_VERSION_PATCH ${CMAKE_MATCH_3}) else() - set (DEDX_DATA_PATH "${CMAKE_INSTALL_PREFIX}/share/libdedx/") + set(DEDX_VERSION_MAJOR 0) + set(DEDX_VERSION_MINOR 0) + set(DEDX_VERSION_PATCH 0) endif() -# Copy a file to another location and modify its contents -# Copies an file to an file and -# substitutes variable values referenced as @VAR@ or ${VAR} in the input file content. -# Each variable reference will be replaced with the current value of the variable, -# or the empty string if the variable is not defined -# https://cmake.org/cmake/help/v3.12/command/configure_file.html -configure_file ( - "${PROJECT_SOURCE_DIR}/libdedx/dedx_config.h.in" - "${PROJECT_SOURCE_DIR}/libdedx/dedx_config.h" - ) - -include_directories ("${PROJECT_SOURCE_DIR}/libdedx") -link_directories ("${PROJECT_SOURCE_DIR}/libdedx") +# ---- Data paths (baked into dedx_config.h at configure time) ---- +set(DEDX_DATA_PATH_LOCAL "${PROJECT_SOURCE_DIR}/libdedx/data/") +if(WIN32) + set(DEDX_DATA_PATH "data/") +else() + set(DEDX_DATA_PATH "${CMAKE_INSTALL_PREFIX}/share/libdedx/") +endif() + +# Generate dedx_config.h into the build directory (not source tree) +configure_file( + "${PROJECT_SOURCE_DIR}/libdedx/dedx_config.h.in" + "${PROJECT_BINARY_DIR}/dedx_config.h" +) + +include_directories("${PROJECT_SOURCE_DIR}/libdedx") +include_directories("${PROJECT_BINARY_DIR}") +link_directories("${PROJECT_SOURCE_DIR}/libdedx") add_subdirectory(buildbins) add_subdirectory(examples) add_subdirectory(libdedx) + +enable_testing() add_subdirectory(test) # add uninstall target according to CMAKE FAQ: @@ -48,8 +64,3 @@ configure_file( IMMEDIATE @ONLY) add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) -# Do we want to build the tests? -#option(ENABLE_TESTS "Enable unit tests" ON) -#if(ENABLE_TESTS) -# add_subdirectory(test) -#endif() \ No newline at end of file diff --git a/buildbins/CMakeLists.txt b/buildbins/CMakeLists.txt index c2dfe77..8a75228 100644 --- a/buildbins/CMakeLists.txt +++ b/buildbins/CMakeLists.txt @@ -7,25 +7,17 @@ target_link_libraries (dedx_build_bin dedx) # Add a target with no output so it will always be built. # The target has no output file and is always considered out of date even if the commands try to create a file with the name of the target # ALL Indicate that this target should be added to the default build target so that it will be run every time -add_custom_target( binary_table ALL ) +add_custom_target(binary_table ALL) -# once `dedx_build_bin` we call it to generate *.bin files in `libdedx` directory +# Ensure dedx and dedx_build_bin are built before binary_table runs +add_dependencies(binary_table dedx_build_bin) -# Add a custom build rule to the generated build system. -# The second signature adds a custom command to a target such as a library or executable. -# This is useful for performing an operation before or after building the target. -# The command becomes part of the target and will only execute when the target itself is built. -# If the target is already built, the command will not execute -# https://cmake.org/cmake/help/v3.12/command/add_custom_command.html add_custom_command( TARGET binary_table - POST_BUILD # POST_BUILD - Run after all other rules within the target have been executed. - # `${CMAKE_COMMAND}` -E executes CMake commands (like echo or rm) in a portable way, this should work on Linux and Windows + POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo "Removing old *.bin files..." - # remove previously generated files, `-f` force option used to have success even if files-to-be-removed do not exists COMMAND ${CMAKE_COMMAND} -E remove -f "${PROJECT_SOURCE_DIR}/libdedx/data/*.bin" - COMMAND dedx_build_bin + COMMAND $ COMMENT "Execute command to build platform dependent binary data tables..." WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/libdedx - DEPENDS dedx dedx_build_bin ) \ No newline at end of file diff --git a/libdedx/CMakeLists.txt b/libdedx/CMakeLists.txt index df78a1d..12e0227 100644 --- a/libdedx/CMakeLists.txt +++ b/libdedx/CMakeLists.txt @@ -3,11 +3,18 @@ set(dedx_data data/ASTAR.bin data/astarEng.dat data/mstarEng.dat data/pstarEng.b set(dedx_data_bin data/ASTAR.bin data/astarEng.bin data/ICRU73.bin data/icru73Eng.bin data/ICRU73_NEW.bin data/icru73_newEng.bin data/MSTAR.bin data/mstarEng.bin data/PSTAR.bin data/ICRU_PSTAR.bin data/ICRU_ASTAR.bin data/pstarEng.bin data/betheEng.bin data/icru_astarEng.bin data/icru_pstarEng.bin data/compos.txt data/effective_charge.dat data/short_names data/composition data/gas_states.dat data/atima_compos) -add_library (dedx STATIC dedx_tools.c dedx_bethe.c dedx.c dedx_wrappers.c dedx_file_access.c dedx_file.c dedx_mpaul.c dedx_mstar.c dedx_spline.c dedx_split.c dedx_periodic_table.c tools/dedx_math.c dedx_validate.c) +add_library(dedx STATIC dedx_tools.c dedx_bethe.c dedx.c dedx_wrappers.c dedx_file_access.c dedx_file.c dedx_mpaul.c dedx_mstar.c dedx_spline.c dedx_split.c dedx_periodic_table.c tools/dedx_math.c dedx_validate.c) # removed math_dedx.h # removed dedx_atima.c -target_link_libraries (dedx) +# Version passed as compile definitions, derived from git tag at configure time +target_compile_definitions(dedx PRIVATE + DEDX_VERSION_MAJOR=${DEDX_VERSION_MAJOR} + DEDX_VERSION_MINOR=${DEDX_VERSION_MINOR} + DEDX_VERSION_PATCH=${DEDX_VERSION_PATCH} +) + +target_link_libraries(dedx) # Visual Studio does not need or want you to explicitly request linking the math library. # You must avoid adding it as a link library when building for Windows. diff --git a/libdedx/dedx_config.h b/libdedx/dedx_config.h deleted file mode 100644 index 89edf1c..0000000 --- a/libdedx/dedx_config.h +++ /dev/null @@ -1,6 +0,0 @@ -// the configured options and settings for DEDX -#define DEDX_VERSION_MAJOR 1 -#define DEDX_VERSION_MINOR 2 -#define DEDX_VERSION_PATCH 1 -#define DEDX_DATA_PATH "/usr/local/share/libdedx/" -#define DEDX_DATA_PATH_LOCAL "data/" diff --git a/libdedx/dedx_config.h.in b/libdedx/dedx_config.h.in index e70d9c1..0f9ce0f 100644 --- a/libdedx/dedx_config.h.in +++ b/libdedx/dedx_config.h.in @@ -1,6 +1,3 @@ -// the configured options and settings for DEDX -#define DEDX_VERSION_MAJOR @DEDX_VERSION_MAJOR@ -#define DEDX_VERSION_MINOR @DEDX_VERSION_MINOR@ -#define DEDX_VERSION_PATCH @DEDX_VERSION_PATCH@ +// Data paths configured by CMake at build time #define DEDX_DATA_PATH "@DEDX_DATA_PATH@" #define DEDX_DATA_PATH_LOCAL "@DEDX_DATA_PATH_LOCAL@" diff --git a/libdedx/dedx_file_access.c b/libdedx/dedx_file_access.c index 50402a3..99431c9 100644 --- a/libdedx/dedx_file_access.c +++ b/libdedx/dedx_file_access.c @@ -19,7 +19,7 @@ // char folder[] = "data/"; // DEDX_DATA_PATH is set by dedx_config.h which is generated by cmake. -char folder[] = DEDX_DATA_PATH; // default is installation path +char folder[512] = DEDX_DATA_PATH; // default is installation path int attemps = 0; void _dedx_set_folder(void) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 92458ff..c7dfc5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,28 +1,27 @@ include_directories ("${PROJECT_SOURCE_DIR}") add_executable(loadTest loadTest.c) -add_executable(test test.c) +add_executable(dedx_test test.c) target_link_libraries (loadTest dedx) -target_link_libraries (test dedx) +target_link_libraries (dedx_test dedx) # Visual Studio does not need or want you to explicitly request linking the math library. # You must avoid adding it as a link library when building for Windows. # see https://stackoverflow.com/questions/54935559/linking-math-library-in-cmake-file-on-windows-and-linux -IF (NOT WIN32) - target_link_libraries (loadTest m) - target_link_libraries (test m) -ENDIF() +if(NOT WIN32) + target_link_libraries(loadTest m) + target_link_libraries(dedx_test m) +endif() -# TODO: add testing suite -ENABLE_TESTING() -ADD_TEST(test_initialiaze loadTest 1) -ADD_TEST(test_load_ASTAR loadTest 2) -ADD_TEST(test_load_PSTAR loadTest 3) -ADD_TEST(test_load_MSTAR loadTest 4) -ADD_TEST(test_load_ICRU49_proton loadTest 5) -ADD_TEST(test_load_ICRU49_helium loadTest 6) -ADD_TEST(test_load_Bethe loadTest 7) -ADD_TEST(test_load_ICRU73 loadTest 8) -ADD_TEST(test_load_ICRU73_NEW loadTest 9) +# enable_testing() is called in the root CMakeLists.txt +add_test(NAME test_initialiaze COMMAND loadTest 1) +add_test(NAME test_load_ASTAR COMMAND loadTest 2) +add_test(NAME test_load_PSTAR COMMAND loadTest 3) +add_test(NAME test_load_MSTAR COMMAND loadTest 4) +add_test(NAME test_load_ICRU49_proton COMMAND loadTest 5) +add_test(NAME test_load_ICRU49_helium COMMAND loadTest 6) +add_test(NAME test_load_Bethe COMMAND loadTest 7) +add_test(NAME test_load_ICRU73 COMMAND loadTest 8) +add_test(NAME test_load_ICRU73_NEW COMMAND loadTest 9)