Skip to content

Commit 4fd6115

Browse files
committed
Use find_package for OpenMC
Use find_package to populate OpenMC_DIR and then get relative paths to include and lib from there. Raises a warning if OpenMC is not present, as the source can still function but the OpenMC plugin won't be available.
1 parent 2eab819 commit 4fd6115

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ set(CMAKE_CXX_FLAGS "-Wall")
1313
set(CMAKE_CXX_FLAGS_DEBUG "-g")
1414
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
1515

16-
# Use output paths from OpenMC install - change if needed
17-
set(OPENMC_INC_DIR /usr/local/include/openmc)
18-
set(OPENMC_LIB_DIR /usr/local/lib)
19-
2016
# Ensure submodules are available and up to date
2117
find_package(Git QUIET)
2218
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
@@ -43,15 +39,29 @@ list(APPEND source_sampling_SOURCES
4339
${SRC_DIR}/plasma_source.cpp
4440
)
4541

46-
add_library(source_sampling SHARED ${source_sampling_SOURCES})
42+
# Use output paths from OpenMC install
43+
# If OpenMC isn't available then we won't be able to build the plugin
44+
# However, the package can still be run by using the sampling methods directly
45+
# So don't terminate the build
46+
find_package(OpenMC QUIET)
47+
48+
if(OpenMC_FOUND)
49+
# Build the source_sampling OpenMC plugin if OpenMC is available
50+
set(OPENMC_INC_DIR ${OpenMC_DIR}/../../../include/openmc)
51+
set(OPENMC_LIB_DIR ${OpenMC_DIR}/../../../lib)
4752

48-
find_library(OPENMC_LIB openmc HINTS ${OPENMC_LIB_DIR} OPTIONAL)
53+
add_library(source_sampling SHARED ${source_sampling_SOURCES})
4954

50-
if (OPENMC_LIB)
51-
set_target_properties(source_sampling PROPERTIES PREFIX "")
52-
set_target_properties(source_sampling PROPERTIES POSITION_INDEPENDENT_CODE ON)
53-
target_include_directories(source_sampling PUBLIC ${OPENMC_INC_DIR})
54-
target_link_libraries(source_sampling ${OPENMC_LIB} gfortran)
55+
find_library(OPENMC_LIB openmc HINTS ${OPENMC_LIB_DIR} OPTIONAL)
56+
57+
if (OPENMC_LIB)
58+
set_target_properties(source_sampling PROPERTIES PREFIX "")
59+
set_target_properties(source_sampling PROPERTIES POSITION_INDEPENDENT_CODE ON)
60+
target_include_directories(source_sampling PUBLIC ${OPENMC_INC_DIR})
61+
target_link_libraries(source_sampling ${OPENMC_LIB} gfortran)
62+
endif()
63+
else()
64+
message(WARNING "Unable to find OpenMC installation - the source_sampling plugin will not be built.")
5565
endif()
5666

5767
# Build plasma_source Python bindings

0 commit comments

Comments
 (0)