Skip to content

Commit

Permalink
CMake: Superbuild of pyAMReX
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed May 18, 2022
1 parent 64072ed commit 0be5551
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
20 changes: 16 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mark_as_advanced(ImpactX_MPI_THREAD_MULTIPLE)

option(ImpactX_ablastr_internal "Download & build ABLASTR" ON)
option(ImpactX_amrex_internal "Download & build AMReX" ON)
option(ImpactX_pyamrex_internal "Download & build pyAMReX" ON)

set(pyImpactX_VERSION_INFO "" CACHE STRING
"PEP-440 conformant version (set by distutils)")
Expand Down Expand Up @@ -158,6 +159,8 @@ endif()

# build Python module (this is always a shared library)
if(ImpactX_PYTHON)
include(${ImpactX_SOURCE_DIR}/cmake/dependencies/pyAMReX.cmake)

add_library(pyImpactX MODULE src/python/pyImpactX.cpp)
add_library(ImpactX::pyImpactX ALIAS pyImpactX)
target_link_libraries(pyImpactX PUBLIC lib)
Expand Down Expand Up @@ -358,8 +361,17 @@ if(ImpactX_PYTHON)
set(PYINSTALLOPTIONS "" CACHE STRING
"Additional parameters to pass to `pip install`")

# add a prefix to custom targets so we do not collide if used as a subproject
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(_ImpactX_CUSTOM_TARGET_PREFIX_DEFAULT "")
else()
set(_ImpactX_CUSTOM_TARGET_PREFIX_DEFAULT "impactx_")
endif()
set(ImpactX_CUSTOM_TARGET_PREFIX "${_ImpactX_CUSTOM_TARGET_PREFIX_DEFAULT}"
CACHE STRING "Prefix for custom targets")

# build the wheel by re-using the lib library we build
add_custom_target(pip_wheel
add_custom_target(${ImpactX_CUSTOM_TARGET_PREFIX}pip_wheel
${CMAKE_COMMAND} -E rm -f "impactx*.whl"
COMMAND
${CMAKE_COMMAND} -E env PYIMPACTX_LIBDIR=$<TARGET_FILE_DIR:pyImpactX>
Expand All @@ -371,7 +383,7 @@ if(ImpactX_PYTHON)
)

# this will also upgrade/downgrade dependencies, e.g., when the version of numpy changes
add_custom_target(pip_install_requirements
add_custom_target(${ImpactX_CUSTOM_TARGET_PREFIX}pip_install_requirements
${Python_EXECUTABLE} -m pip install ${PYINSTALLOPTIONS} -r "${ImpactX_SOURCE_DIR}/requirements.txt"
WORKING_DIRECTORY
${ImpactX_BINARY_DIR}
Expand All @@ -380,13 +392,13 @@ if(ImpactX_PYTHON)
# We force-install because in development, it is likely that the version of
# the package does not change, but the code did change. We need --no-deps,
# because otherwise pip would also force reinstall all dependencies.
add_custom_target(pip_install
add_custom_target(${ImpactX_CUSTOM_TARGET_PREFIX}pip_install
${CMAKE_COMMAND} -E env IMPACTX_MPI=${ImpactX_MPI}
${Python_EXECUTABLE} -m pip install --force-reinstall --no-deps ${PYINSTALLOPTIONS} "impactx*.whl"
WORKING_DIRECTORY
${ImpactX_BINARY_DIR}
DEPENDS
pyImpactX pip_wheel pip_install_requirements
pyImpactX ${ImpactX_CUSTOM_TARGET_PREFIX}pip_wheel ${ImpactX_CUSTOM_TARGET_PREFIX}pip_install_requirements pyamrex_pip_install
)
endif()

Expand Down
58 changes: 29 additions & 29 deletions cmake/dependencies/pyAMReX.cmake
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
function(find_pybind11)
if(pyAMReX_pybind11_src)
message(STATUS "Compiling local pybind11 ...")
message(STATUS "pybind11 source path: ${pyAMReX_pybind11_src}")
elseif(pyAMReX_pybind11_internal)
message(STATUS "Downloading pybind11 ...")
message(STATUS "pybind11 repository: ${pyAMReX_pybind11_repo} (${pyAMReX_pybind11_branch})")
function(find_pyamrex)
if(ImpactX_pyamrex_src)
message(STATUS "Compiling local pyAMReX ...")
message(STATUS "pyAMReX source path: ${ImpactX_pyamrex_src}")
elseif(ImpactX_pyamrex_internal)
message(STATUS "Downloading pyAMReX ...")
message(STATUS "pyAMReX repository: ${ImpactX_pyamrex_repo} (${ImpactX_pyamrex_branch})")
include(FetchContent)
endif()
if(pyAMReX_pybind11_internal OR pyAMReX_pybind11_src)
if(ImpactX_pyamrex_internal OR ImpactX_pyamrex_src)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

if(pyAMReX_pybind11_src)
add_subdirectory(${pyAMReX_pybind11_src} _deps/localpybind11-build/)
if(ImpactX_pyamrex_src)
add_subdirectory(${ImpactX_pyamrex_src} _deps/localpyamrex-build/)
else()
FetchContent_Declare(fetchedpybind11
GIT_REPOSITORY ${pyAMReX_pybind11_repo}
GIT_TAG ${pyAMReX_pybind11_branch}
FetchContent_Declare(fetchedpyamrex
GIT_REPOSITORY ${ImpactX_pyamrex_repo}
GIT_TAG ${ImpactX_pyamrex_branch}
BUILD_IN_SOURCE 0
)
FetchContent_GetProperties(fetchedpybind11)
FetchContent_GetProperties(fetchedpyamrex)

if(NOT fetchedpybind11_POPULATED)
FetchContent_Populate(fetchedpybind11)
add_subdirectory(${fetchedpybind11_SOURCE_DIR} ${fetchedpybind11_BINARY_DIR})
if(NOT fetchedpyamrex_POPULATED)
FetchContent_Populate(fetchedpyamrex)
add_subdirectory(${fetchedpyamrex_SOURCE_DIR} ${fetchedpyamrex_BINARY_DIR})
endif()

# advanced fetch options
mark_as_advanced(FETCHCONTENT_BASE_DIR)
mark_as_advanced(FETCHCONTENT_FULLY_DISCONNECTED)
mark_as_advanced(FETCHCONTENT_QUIET)
mark_as_advanced(FETCHCONTENT_SOURCE_DIR_FETCHEDpybind11)
mark_as_advanced(FETCHCONTENT_SOURCE_DIR_FETCHEDpyamrex)
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED)
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_FETCHEDpybind11)
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_FETCHEDpyamrex)
endif()
else()
find_package(pybind11 2.9.1 CONFIG REQUIRED)
message(STATUS "pybind11: Found version '${pybind11_VERSION}'")
find_package(pyAMReX 21.02 CONFIG REQUIRED)
message(STATUS "pyAMReX: Found version '${pyamrex_VERSION}'")
endif()
endfunction()

# local source-tree
set(pyAMReX_pybind11_src ""
set(ImpactX_pyamrex_src ""
CACHE PATH
"Local path to pybind11 source directory (preferred if set)")
"Local path to pyAMReX source directory (preferred if set)")

# Git fetcher
option(pyAMReX_pybind11_internal "Download & build pybind11" ON)
set(pyAMReX_pybind11_repo "https://github.com/pybind/pybind11.git"
option(ImpactX_pyamrex_internal "Download & build pyAMReX" ON)
set(ImpactX_pyamrex_repo "https://github.com/AMReX-Codes/pyamrex.git"
CACHE STRING
"Repository URI to pull and build pybind11 from if(pyAMReX_pybind11_internal)")
set(pyAMReX_pybind11_branch "v2.9.1"
"Repository URI to pull and build pyamrex from if(ImpactX_pyamrex_internal)")
set(ImpactX_pyamrex_branch "development"
CACHE STRING
"Repository branch for pyAMReX_pybind11_repo if(pyAMReX_pybind11_internal)")
"Repository branch for ImpactX_pyamrex_repo if(ImpactX_pyamrex_internal)")

find_pybind11()
find_pyamrex()

0 comments on commit 0be5551

Please sign in to comment.