# ParmaCosmicray: the cosmic-ray samples of PHITS with the PARMA source.
# Built as part of parma-g4 when Geant4 is found, or standalone against an
# installed PARMA:  cmake -S . -B build -DParma_DIR=<install>/lib/cmake/Parma
cmake_minimum_required(VERSION 3.16)
project(ParmaCosmicray CXX)

if(NOT TARGET Parma::parma_g4)
  find_package(Parma REQUIRED)
endif()
find_package(Geant4 REQUIRED)

add_executable(ParmaCosmicray
  ParmaCosmicray.cc
  src/DetectorConstruction.cc
  src/ActionInitialization.cc
  src/RunAction.cc
  src/SteppingAction.cc
  src/WallReflection.cc)
target_include_directories(ParmaCosmicray PRIVATE include)
target_link_libraries(ParmaCosmicray PRIVATE Parma::parma_g4 ${Geant4_LIBRARIES})

# macros in the same folders as in PHITS (sample/source/Cosmicray)
set(PARMA_COSMICRAY_MACROS
  sphere/GCR-atmosphere/GCR-atmosphere.mac
  sphere/GCR-ground_level/GCR-ground_level.mac
  plane/GCR-ground/GCR-ground.mac
  check/void-sphere.mac
  check/void-plane.mac)
foreach(mac ${PARMA_COSMICRAY_MACROS})
  configure_file(${mac} ${CMAKE_CURRENT_BINARY_DIR}/${mac} COPYONLY)
endforeach()

# checks with vacuum when built inside parma-g4
if(PARMA_BUILD_TESTS)
  foreach(name void-sphere void-plane)
    add_test(NAME cosmicray_${name} COMMAND ParmaCosmicray ${name}.mac
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/check)
    set_tests_properties(cosmicray_${name} PROPERTIES
      FAIL_REGULAR_EXPRESSION "check FAILED|Parma00[23]|Parma010|command not found|\\*\\*\\* Fatal"
      PASS_REGULAR_EXPRESSION "check: free-field flux")
    if(WIN32)
      get_filename_component(G4_BIN_DIR "${Geant4_DIR}/../../../bin" ABSOLUTE)
      set_tests_properties(cosmicray_${name} PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${G4_BIN_DIR}")
    endif()
  endforeach()
endif()
