Skip to content

Commit

Permalink
Migrate Python api to sub-project
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
  • Loading branch information
LecrisUT committed Jan 19, 2024
1 parent deb1520 commit 1664ec6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ set(spglib_dependencies "")
if (SPGLIB_USE_OMP)
find_package(OpenMP REQUIRED COMPONENTS C)
endif ()
if (SPGLIB_WITH_Python)
find_package(Python3 COMPONENTS REQUIRED Interpreter Development.Module NumPy)
endif ()

#[=============================================================================[
# Main definition #
Expand Down
103 changes: 87 additions & 16 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,105 @@
Python3_add_library(Spglib_python MODULE WITH_SOABI _spglib.c)
cmake_minimum_required(VERSION 3.15)
# CMake version compatibility
# TODO: Remove when cmake 3.25 is commonly distributed
if (POLICY CMP0140)
cmake_policy(SET CMP0140 NEW)
endif ()

#[=============================================================================[
# Basic project definition #
]=============================================================================]

list(APPEND CMAKE_MESSAGE_CONTEXT Python)
project(Spglib_Python
LANGUAGES C
)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()

#[=============================================================================[
# Options #
]=============================================================================]

option(SPGLIB_INSTALL "Spglib: Install project" ${PROJECT_IS_TOP_LEVEL})
option(SPGLIB_SHARED_LIBS "Spglib: Build as a shared library" ${PROJECT_IS_TOP_LEVEL})

#[=============================================================================[
# Project configuration #
]=============================================================================]

set(BUILD_SHARED_LIBS ${SPGLIB_SHARED_LIBS})

#[=============================================================================[
# External packages #
]=============================================================================]

set(external_libs)
include(FetchContent)

# Get Spglib if it's run as stand-alone project
if (NOT Spglib_IS_TOP_LEVEL)
FetchContent_Declare(Spglib
GIT_REPOSITORY https://github.com/spglib/spglib
GIT_TAG develop
FIND_PACKAGE_ARGS CONFIG
)
list(APPEND external_libs Spglib)
endif ()
find_package(Python 3.8 COMPONENTS REQUIRED Interpreter Development.Module NumPy)
FetchContent_MakeAvailable(${external_libs})

#[=============================================================================[
# Main definition #
]=============================================================================]

Python_add_library(Spglib_python MODULE WITH_SOABI _spglib.c)
set_target_properties(Spglib_python PROPERTIES
OUTPUT_NAME _spglib)
OUTPUT_NAME _spglib
)
target_link_libraries(Spglib_python PRIVATE
Spglib_symspg Python3::NumPy)
Spglib::symspg Python::NumPy
)

#[=============================================================================[
# Install or Export #
]=============================================================================]

if (NOT Python_INSTALL_DIR)
if (SKBUILD)
# If built with scikit-build-core, let it handle the installation
set(Python_INSTALL_DIR ".")
else ()
# Otherwise try to install in current python executable's setup
set(Python_INSTALL_DIR ${Python3_SITEARCH}/spglib)
set(Python_INSTALL_DIR ${Python_SITEARCH}/spglib)
endif ()
endif ()
if (SPGLIB_INSTALL)
if (WIN32)
# Windows needs RUNTIME as well to install the .dll file to
install(TARGETS Spglib_symspg
LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
RUNTIME DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)
else ()
# TODO: Cmake forces to install PUBLIC_HEADER when defined
# https://gitlab.kitware.com/cmake/cmake/-/issues/24326
install(TARGETS Spglib_symspg
LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)
if (TARGET Spglib_symspg)
# If Spglib is not already installed on the system, install it in ${Python_INSTALL_DIR}
if (WIN32)
# Windows needs RUNTIME as well to install the .dll file to
install(TARGETS Spglib_symspg
LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
RUNTIME DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)
else ()
# TODO: Cmake forces to install PUBLIC_HEADER when defined
# https://gitlab.kitware.com/cmake/cmake/-/issues/24326
install(TARGETS Spglib_symspg
LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime
PUBLIC_HEADER DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)
endif ()
endif ()
install(TARGETS Spglib_python
LIBRARY DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)

# Install spglib module to local python path
if (NOT SKBUILD)
install(DIRECTORY spglib/ DESTINATION ${Python_INSTALL_DIR} COMPONENT Spglib_Runtime)
endif ()
Expand Down

0 comments on commit 1664ec6

Please sign in to comment.