Skip to content

Commit

Permalink
update lib paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnaf-tahmid-chowdhury committed Oct 17, 2024
1 parent d0740ce commit fbda530
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
21 changes: 7 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,36 +337,29 @@ if(SKBUILD)
# Scikit-build installs OpenMC to ${SKBUILD_PLATLIB_DIR}/openmc
# So, set bin directory to root environment (install prefix/bin)
set(CMAKE_INSTALL_BINDIR ${SKBUILD_SCRIPTS_DIR})
# Unfortunately, OpenMC has lib directory in the openmc/lib subdirectory
set(CMAKE_INSTALL_LIBDIR lib)
set(CMAKE_INSTALL_INCLUDEDIR ${SKBUILD_HEADERS_DIR})
set(CMAKE_INSTALL_INCLUDEDIR ${SKBUILD_SUBDIR}/${CMAKE_INSTALL_INCLUDEDIR})
set(CMAKE_INSTALL_DATADIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_DATADIR})
set(CMAKE_INSTALL_DOCDIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_DOCDIR})
set(CMAKE_INSTALL_INFODIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_INFODIR})
set(CMAKE_INSTALL_MANDIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_MANDIR})
set(CMAKE_INSTALL_LOCALEDIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_LOCALEDIR})
set(CMAKE_INSTALL_LOCALSTATEDIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_LOCALSTATEDIR})
set(CMAKE_INSTALL_RUNSTATEDIR ${SKBUILD_DATA_DIR}/${CMAKE_INSTALL_RUNSTATEDIR})
set(SKBUILD_LIB_DIR ${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/openmc/${CMAKE_INSTALL_LIBDIR})
set(SKBUILD_SUBDIR core/)
set(SKBUILD_LIB_DIR ${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/openmc/${SKBUILD_SUBDIR}${CMAKE_INSTALL_LIBDIR})

# Auditwheel and Delocate need this when repairing the wheel
# Since OpenMC libs are installed in the openmc subdirectory
# Auditwheel and Delocate need this to find the OpenMC libs for openmc.data
# It's a bit of a hack, but it works
set(SKBUILD_REPAIR_WHEEL_PATCH openmc/${CMAKE_INSTALL_LIBDIR})
set(SKBUILD_REPAIR_WHEEL_PATCH openmc/${SKBUILD_SUBDIR}${CMAKE_INSTALL_LIBDIR})
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
set(OPENMC_LIBRARY_RPATH "@loader_path")
set(OPENMC_PYTHON_MODULE_RPATH "@loader_path/${CMAKE_INSTALL_LIBDIR}")
set(OPENMC_BINARY_RPATH "@loader_path/../${SKBUILD_LIB_DIR};@loader_path/../../${SKBUILD_REPAIR_WHEEL_PATCH}")
elseif(UNIX)
set(OPENMC_LIBRARY_RPATH "$ORIGIN")
set(OPENMC_PYTHON_MODULE_RPATH "$ORIGIN/${CMAKE_INSTALL_LIBDIR}")
set(OPENMC_BINARY_RPATH "$ORIGIN/../${SKBUILD_LIB_DIR};$ORIGIN/../${SKBUILD_REPAIR_WHEEL_PATCH};$ORIGIN/../../${SKBUILD_REPAIR_WHEEL_PATCH}")
else()
set(OPENMC_LIBRARY_RPATH OFF)
set(OPENMC_PYTHON_MODULE_RPATH OFF)
set(OPENMC_BINARY_RPATH OFF)
endif()
else()
# use, i.e. don't skip the full RPATH for the build tree
Expand Down Expand Up @@ -672,7 +665,7 @@ endif()
install(TARGETS openmc libopenmc
EXPORT openmc-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${SKBUILD_SUBDIR}${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

Expand All @@ -696,5 +689,5 @@ install(FILES
DESTINATION ${INSTALL_CONFIGDIR})
install(FILES man/man1/openmc.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
install(FILES LICENSE DESTINATION "${CMAKE_INSTALL_DOCDIR}" RENAME copyright)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES "${CMAKE_BINARY_DIR}/include/openmc/version.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openmc)
install(DIRECTORY include/ DESTINATION ${SKBUILD_SUBDIR}${CMAKE_INSTALL_INCLUDEDIR})
install(FILES "${CMAKE_BINARY_DIR}/include/openmc/version.h" DESTINATION ${SKBUILD_SUBDIR}${CMAKE_INSTALL_INCLUDEDIR}/openmc)
2 changes: 1 addition & 1 deletion cmake/OpenMCConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if(@SKBUILD@)

# Extract MOAB include paths, library paths, and extra libraries
run_python_command(OpenMC_INCLUDE_DIRS "import openmc; print(openmc.include_path)")
run_python_command(OpenMC_LIBRARY_DIRS "import openmc; print(openmc.lib_path)")
run_python_command(OpenMC_LIBRARY_DIRS "import openmc; print(' '.join(openmc.lib_path))")
run_python_command(OpenMC_EXTRA_LIBRARIES "import openmc; print(' '.join(openmc.extra_lib))")

# Check if the wheel was repaired using auditwheel or delocate
Expand Down
30 changes: 30 additions & 0 deletions src/openmc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import glob
import importlib.metadata
from openmc.arithmetic import *
from openmc.bounding_box import *
Expand Down Expand Up @@ -43,3 +45,31 @@


__version__ = importlib.metadata.version("openmc")

def get_path(subdir, pattern=""):
"""Helper function to return paths that match a given pattern within a subdirectory."""
path = os.path.join(__path__[0], "core", subdir)
return glob.glob(os.path.join(path, pattern)) if os.path.exists(path) else []

def get_include_path():
"""Return the include directory path for OpenMC headers."""
return get_path("include")

def get_core_libraries():
"""Return library paths and library directory paths."""
lib_paths = [libs for lib in ["lib", "lib64"] for libs in get_path(lib)]
lib = [libs for lib in ["lib", "lib64"] for libs in get_path(lib, pattern="*openmc*")]
return lib, lib_paths

def get_extra_libraries():
"""List all the extra libraries of OpenMC."""
libs_path = os.path.join(__path__[0], ".dylibs") if sys.platform == "darwin" else os.path.join(__path__[0], "..", "pyne.libs")
return (glob.glob(os.path.join(libs_path, "*")), libs_path) if os.path.exists(libs_path) else ([], [])

# Setup variables
include_path = get_include_path()
lib, lib_path = get_core_libraries()
extra_lib, extra_lib_path = get_extra_libraries()

# Export variables for easy access
__all__ = ["include_path", "lib", "lib_path", "extra_lib", "extra_lib_path"]
12 changes: 2 additions & 10 deletions src/openmc/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@
"""

from ctypes import CDLL, c_bool, c_int
import importlib.resources
import os
import sys


# Determine shared-library suffix
if sys.platform == 'darwin':
_suffix = 'dylib'
else:
_suffix = 'so'

if os.environ.get('READTHEDOCS', None) != 'True':
# Open shared library
_filename = importlib.resources.files(__name__) / f'libopenmc.{_suffix}'
import openmc
_filename = openmc.lib[0]
_dll = CDLL(str(_filename)) # TODO: Remove str() when Python 3.12+
else:
# For documentation builds, we don't actually have the shared library
Expand Down

0 comments on commit fbda530

Please sign in to comment.