Skip to content

Commit

Permalink
python installation
Browse files Browse the repository at this point in the history
  • Loading branch information
boeschf committed Aug 9, 2023
1 parent 6c1952d commit dbecb09
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ if(NOT CMAKE_BUILD_TYPE) # AND NOT CMAKE_CONFIGURATION_TYPES)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

# Set the output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Set the library and archive output directories
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# ---------------------------------------------------------------------
# Define main library
# ---------------------------------------------------------------------
Expand All @@ -41,7 +48,7 @@ set(GHEX_WITH_TESTING OFF CACHE BOOL "True if tests shall be built")
if (GHEX_WITH_TESTING)
enable_testing()
# Enable CDash dashboard testing integration
include(CTest)
#include(CTest)
endif()


Expand Down
62 changes: 43 additions & 19 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,74 @@ if (HAVE_MPI4PY)
endif()


# The Python library
# The Python module
# ==================
# Create the Python module in the build directory.
# The module contains the dynamic library, __init__.py and VERSION information.
set(python_mod_path "${CMAKE_CURRENT_BINARY_DIR}/ghex")
add_library(pyghex MODULE $<TARGET_OBJECTS:pyghex_obj>)
# With this, the full name of the library will be something like:
# _pyghex.cpython-36m-x86_64-linux-gnu.so
# _pyghex.cpython-36m-x86_64-linux-gnu.so
set_target_properties(pyghex PROPERTIES
OUTPUT_NAME _pyghex
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")
set_target_properties(pyghex PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${python_mod_path}")
# This dependency has to be spelt out again, despite being added to
# pyghex_obj because CMake.
target_link_libraries(pyghex PRIVATE pybind11::module)
target_link_libraries(pyghex PRIVATE ghex)
ghex_link_to_oomph(pyghex)


# The Python module
# ==================
# Create the Python module in the build directory.
# The module contains the dynamic library, __init__.py and VERSION information.
set(ghex_python_path "${CMAKE_CURRENT_BINARY_DIR}")
set(python_mod_path "${CMAKE_CURRENT_BINARY_DIR}/ghex")
set_target_properties(pyghex PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${python_mod_path}")

add_custom_target(pyghex_module)
copy_files(TARGET pyghex_module DESTINATION ${python_mod_path} FILES
# Copy the required python source files as well
add_custom_target(pyghex_files)
copy_files(TARGET pyghex_files DESTINATION ${python_mod_path} FILES
${CMAKE_CURRENT_SOURCE_DIR}/__init__.py
${PROJECT_BINARY_DIR}/version.txt
)
copy_files(TARGET pyghex_module DESTINATION ${python_mod_path}/util FILES
copy_files(TARGET pyghex_files DESTINATION ${python_mod_path}/util FILES
${CMAKE_CURRENT_SOURCE_DIR}/util/__init__.py
${CMAKE_CURRENT_SOURCE_DIR}/util/cpp_wrapper.py
${CMAKE_CURRENT_SOURCE_DIR}/util/architecture.py
)
copy_files(TARGET pyghex_module DESTINATION ${python_mod_path}/structured FILES
copy_files(TARGET pyghex_files DESTINATION ${python_mod_path}/structured FILES
${CMAKE_CURRENT_SOURCE_DIR}/structured/__init__.py
${CMAKE_CURRENT_SOURCE_DIR}/structured/index_space.py
${CMAKE_CURRENT_SOURCE_DIR}/structured/grid.py
)
copy_files(TARGET pyghex_module DESTINATION ${python_mod_path}/structured/regular FILES
copy_files(TARGET pyghex_files DESTINATION ${python_mod_path}/structured/regular FILES
${CMAKE_CURRENT_SOURCE_DIR}/structured/regular/__init__.py
)
copy_files(TARGET pyghex_module DESTINATION ${python_mod_path}/unstructured FILES
copy_files(TARGET pyghex_files DESTINATION ${python_mod_path}/unstructured FILES
${CMAKE_CURRENT_SOURCE_DIR}/unstructured/__init__.py
)
add_dependencies(pyghex pyghex_module)
add_dependencies(pyghex pyghex_files)

# Installation
# ============
# Ask Python where it keeps its system (platform) packages.
file(WRITE "${CMAKE_BINARY_DIR}/install-prefix" "${CMAKE_INSTALL_PREFIX}")
execute_process(
COMMAND ${PYTHON_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/where.py"
INPUT_FILE "${CMAKE_BINARY_DIR}/install-prefix"
OUTPUT_VARIABLE GHEX_PYTHON_LIB_PATH_DEFAULT_REL
OUTPUT_STRIP_TRAILING_WHITESPACE)
# convert to absolute path if needed (could be a relative path if ccmake was used)
get_filename_component(GHEX_PYTHON_LIB_PATH_DEFAULT "${GHEX_PYTHON_LIB_PATH_DEFAULT_REL}"
REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
# Default to installing in that path, override with user specified GHEX_PYTHON_LIB_PATH
set(GHEX_PYTHON_LIB_PATH ${GHEX_PYTHON_LIB_PATH_DEFAULT} CACHE PATH "path for installing Python bindings.")
message(STATUS "Python bindings installation path: ${GHEX_PYTHON_LIB_PATH}")

if(DEFINED SKBUILD_PROJECT_NAME)
# Building wheel through scikit-build-core
set(_python_module_install_path .)
else()
set(_python_module_install_path ${GHEX_PYTHON_LIB_PATH}/ghex)
endif()

install(DIRECTORY ${python_mod_path}
DESTINATION ${_python_module_install_path}/..
PATTERN "__pycache__" EXCLUDE
#FILES_MATCHING PATTERN "*.so" EXCLUDE
)
install(TARGETS pyghex DESTINATION ${_python_module_install_path})
9 changes: 6 additions & 3 deletions cmake/ghex_external_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ cmake_dependent_option(GHEX_USE_BUNDLED_GRIDTOOLS "Use bundled gridtools." OFF
"GHEX_USE_BUNDLED_LIBS" OFF)
if(GHEX_USE_BUNDLED_GRIDTOOLS)
check_git_submodule(GridTools ext/gridtools)
set(GT_BUILD_TESTING OFF CACHE BOOL "")
set(GT_INSTALL_EXAMPLES OFF CACHE BOOL "")
add_subdirectory(ext/gridtools)
set(BUILD_GMOCK OFF)
set(GT_BUILD_TESTING OFF)
set(GT_INSTALL_EXAMPLES OFF)
set(GT_TESTS_ENABLE_PYTHON_TESTS OFF)
set(GT_ENABLE_BINDINGS_GENERATION OFF)
add_subdirectory(ext/gridtools EXCLUDE_FROM_ALL)
else()
find_package(GridTools REQUIRED)
endif()
Expand Down
23 changes: 23 additions & 0 deletions scripts/where.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
import sysconfig

pfx = sys.stdin.read()
try:
# override scheme on debian/ubuntu py3.10, where 'posix_local' is set and malfunctioning.
if sysconfig.get_default_scheme() == "posix_local":
print(
sysconfig.get_path(
"platlib",
vars={} if pfx == "" else {"base": pfx, "platbase": pfx},
scheme="posix_prefix",
)
)
sys.exit()
except AttributeError:
# we're on Python <= 3.9, no scheme setting required and get_default_scheme does not exist.
pass
print(
sysconfig.get_path(
"platlib", vars={} if pfx == "" else {"base": pfx, "platbase": pfx}
)
)
2 changes: 1 addition & 1 deletion test/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ get_target_property(python_mod_path pyghex LIBRARY_OUTPUT_DIRECTORY)
get_filename_component(ghex_python_path ${python_mod_path}/.. ABSOLUTE)

add_custom_target(pyghex_tests)
add_dependencies(pyghex_module pyghex_tests)
add_dependencies(pyghex pyghex_tests)
copy_files(TARGET pyghex_tests DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/fixtures FILES
${CMAKE_CURRENT_SOURCE_DIR}/fixtures/context.py)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/fixtures/pyghex_path.py.in
Expand Down

0 comments on commit dbecb09

Please sign in to comment.