Skip to content

Commit

Permalink
Merge pull request CRPropa#466 from rafaelab/installPy
Browse files Browse the repository at this point in the history
Fix problems with Python, improve installation, and improve tests
  • Loading branch information
lukasmerten authored Feb 22, 2024
2 parents ca0ead9 + 1df6299 commit 3498d82
Show file tree
Hide file tree
Showing 22 changed files with 609 additions and 1,025 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/test_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ jobs:
fail-fast: false
matrix:
config:
- name: "ubuntu-20"
os: ubuntu-20.04
cxx: "g++-9"
cc: "gcc-9"
fc: "gfortran-9"
swig_builtin: "Off" #uses swig 4.0.1
py: "/usr/bin/python3" #python 3.8
- name: "ubuntu-22"
os: ubuntu-22.04
cxx: "g++-11"
cc: "gcc-11"
fc: "gfortran-11"
swig_builtin: "On" #uses swig 4.0.2
py: "/usr/bin/python3" #python 3.10
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Preinstall
run: |
sudo apt-get update
sudo apt-get install libmuparser-dev python3-dev python-dev python3-numpy python-numpy python3-setuptools python-setuptools libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
sudo apt-get install libmuparser-dev python3 python3-dev python3-numpy python3-setuptools python-setuptools libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
pip3 install -r doc/pages/example_notebooks/requirements.txt # load requrements for notebooks
pip3 install --upgrade Pygments
pip3 install --upgrade numpy
Expand All @@ -33,14 +33,14 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/.local -DENABLE_PYTHON=True -DPYTHON_EXECUTABLE=${{ matrix.config.py }} -DENABLE_TESTING=Off -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/.local -DENABLE_PYTHON=True -DPython_EXECUTABLE=${{ matrix.config.py }} -DENABLE_TESTING=Off -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
- name: Build CRPropa
run: |
cd build
make install -j
- name: convert notebooks to python
env:
PYTHONPATH: "/home/runner/.local/lib/python3.8/site-packages/"
PYTHONPATH: "/home/runner/.local"
runfolder: "/home/runner/notebook_run"
run: |
mkdir "$runfolder"
Expand All @@ -58,7 +58,7 @@ jobs:
done
- name: run all python scripts
env:
PYTHONPATH: "$/home/runner/.local/lib/python3.8/site-packages/"
PYTHONPATH: "/home/runner/.local"
runfolder: "/home/runner/notebook_run"
run: |
cp doc/pages/example_notebooks/galactic_lensing/crpropa_output.txt "$runfolder"/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ jobs:
fail-fast: false
matrix:
config:
- name: "macos-11"
os: macos-11
- name: "macos-14"
os: macos-14
cxx: "clang++"
cc: "clang"
fc: "gfortran-11"
swig_builtin: "On" #uses swig 4.0.2
py: "/usr/bin/python"
py: "/usr/bin/python3"
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Preinstall
run: |
brew install hdf5 fftw cfitsio muparser libomp
brew install hdf5 fftw cfitsio muparser libomp numpy swig
- name: Set up the build
env:
CXX: ${{ matrix.config.cxx }}
Expand All @@ -29,7 +29,7 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DENABLE_PYTHON=True -DPYTHON_EXECUTABLE=${{ matrix.config.py }} -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=avx
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS="none"
- name: Build CRPropa
run: |
cd build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: crpropa-testing
name: crpropa-testing_ubuntu20
on: [push, pull_request]

jobs:
Expand Down Expand Up @@ -30,7 +30,7 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DENABLE_PYTHON=True -DPYTHON_EXECUTABLE=${{ matrix.config.py }} -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
- name: Build CRPropa
run: |
cd build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing_ubuntu22.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DENABLE_PYTHON=True -DPYTHON_EXECUTABLE=${{ matrix.config.py }} -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DPython_EXECUTABLE=${{ matrix.config.py }} -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS=native
- name: Build CRPropa
run: |
cd build
Expand Down
127 changes: 67 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
if(APPLE)
# rpath specific patches
cmake_minimum_required(VERSION 2.8.12)
else(APPLE)
# require > 2.8.8 for FILE DOWNLOAD fixes
# allow < 2.8.12 for debian backports
cmake_minimum_required(VERSION 2.8.11)
endif(APPLE)
cmake_minimum_required(VERSION 3.14)

project(CRPropa Fortran C CXX)
set(CRPROPA_RELEASE_VERSION 3.2.1+) # Update for new release

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

set(CRPROPA_EXTRA_SOURCES)
Expand All @@ -18,17 +12,6 @@ set(CRPROPA_EXTRA_LIBRARIES)
set(CRPROPA_SWIG_DEFINES)
set(CRPROPA_SWIG_INPUTS)

macro(USE_CXX11)
if(CMAKE_VERSION VERSION_LESS "3.1")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
endif()
else()
set(CMAKE_CXX_STANDARD 11)
endif()
endmacro(USE_CXX11)
USE_CXX11()

if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--as-needed")
Expand Down Expand Up @@ -110,6 +93,15 @@ if(ENABLE_TESTING)
if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_USE_OWN_TR1_TUPLE=1")
endif(APPLE)

# temporary workaround for newer clang versions due to its handling of unwinding
# see: https://github.com/google/googletest/issues/3062
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15)
add_definitions(-DCRPROPA_TESTS_SKIP_EXCEPTIONS)
endif (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15)
endif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")

endif(ENABLE_TESTING)

#
Expand Down Expand Up @@ -164,12 +156,13 @@ add_subdirectory(libs/sophia)
list(APPEND CRPROPA_EXTRA_LIBRARIES sophia gfortran)
list(APPEND CRPROPA_EXTRA_INCLUDES libs/sophia)

# GlacticMagneticLenses
option(ENABLE_GALACTICMAGETICLENS "Galactic Magnetic Lens" ON)
# Galactic magnetic lenses
option(ENABLE_GALACTICMAGNETICLENS "Galactic Magnetic Lens" ON)
option(INSTALL_EIGEN "Install provided EIGEN headers" OFF)
SET(EIGEN_PATH "" CACHE STRING "Use EIGEN from this path instead of the version shipped with CRPropa")
SET(WITH_GALACTIC_LENSES FALSE)
if(ENABLE_GALACTICMAGETICLENS)

if(ENABLE_GALACTICMAGNETICLENS)
SET(WITH_GALACTIC_LENSES TRUE)

if(EIGEN_PATH)
Expand All @@ -196,7 +189,7 @@ if(ENABLE_GALACTICMAGETICLENS)
list(APPEND CRPROPA_EXTRA_SOURCES src/magneticLens/ModelMatrix.cpp)
list(APPEND CRPROPA_EXTRA_SOURCES src/magneticLens/Pixelization.cpp)
list(APPEND CRPROPA_EXTRA_SOURCES src/magneticLens/ParticleMapsContainer.cpp)
endif(ENABLE_GALACTICMAGETICLENS)
endif(ENABLE_GALACTICMAGNETICLENS)

# OpenMP (optional for shared memory multiprocessing)
option(ENABLE_OPENMP "OpenMP for multithreading" ON)
Expand Down Expand Up @@ -451,14 +444,45 @@ endif(BUILD_DOC)
# Python
# ----------------------------------------------------------------------------
option(ENABLE_PYTHON "Create python library via SWIG" ON)
find_package(PythonInterp)
find_package(PythonLibs)

if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
find_package(Python 3.0 REQUIRED COMPONENTS Interpreter Development NumPy)

if(ENABLE_PYTHON AND Python_FOUND)
find_package(SWIG 3.0 REQUIRED)
include(python/Python.cmake)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${Python_INCLUDE_DIRS})

# print Python info in detail
message(STATUS "Python: Found!")
message(STATUS " version ${Python_VERSION}")
message(STATUS " executable: ${Python_EXECUTABLE}")
message(STATUS " libraries: ${Python_LIBRARIES}")
message(STATUS " headers: ${Python_INCLUDE_DIRS}")
message(STATUS " site packages: ${Python_SITELIB}")
if(Python_Development_FOUND)
message(STATUS " development libraries: Found!")
elseif(Python_Development_FOUND)
message(STATUS " development libraries: NOT found!")
endif(Python_Development_FOUND)


# use Python_INSTALL_PACKAGE_DIR if provided; otherwise, install in Python_SITELIB
if(NOT DEFINED Python_INSTALL_PACKAGE_DIR)
set(Python_INSTALL_PACKAGE_DIR "${Python_SITELIB}")
endif(NOT DEFINED Python_INSTALL_PACKAGE_DIR)
message(STATUS " package install directory: ${Python_INSTALL_PACKAGE_DIR}")


# look for NumPy
if(Python_NumPy_FOUND)
set(CMAKE_SWIG_FLAGS -DWITHNUMPY ${CRP})
list(APPEND CRPROPA_SWIG_DEFINES -DWITHNUMPY)
include_directories(${Python_NumPy_INCLUDE_DIRS})
message(STATUS "NumPy: Found!")
message(STATUS " headers: ${Python_NumPy_INCLUDE_DIRS} (version ${Python_NumPy_VERSION})")
elseif(Python_NumPy_FOUND)
message(STATUS "NumPy: NOT found!")
message(STATUS " CRPropa might work just fine with Python, but features like Galactic lenses will not be available.")
endif(Python_NumPy_FOUND)

if(SWIG_VERSION VERSION_GREATER 4.0)
# Use swig 4 builtin doxygen instead of external program
Expand All @@ -476,39 +500,22 @@ if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)

if(ENABLE_SWIG_BUILTIN)
set(BUILTIN "-builtin")
set(PY3 "-py3")
if(SWIG_VERSION VERSION_LESS 4.0.2)
message(WARNING, "The SWIG builtin option should not be used with SWIG version below 4.0.2 due to https://github.com/swig/swig/issues/1595")
endif()
else(ENABLE_SWIG_BUILTIN)
set(BUILTIN "")
set(PY3 "")
endif(ENABLE_SWIG_BUILTIN)

if(PYTHON_VERSION_STRING VERSION_GREATER 3.0)
list(APPEND CRPROPA_SWIG_DEFINES -DSWIG_PYTHON3)
endif(PYTHON_VERSION_STRING VERSION_GREATER 3.0)

# tries to import numpy
execute_process(COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/python/checkNumpy.py" OUTPUT_VARIABLE numpyIncludePath)
if(numpyIncludePath)
MESSAGE(STATUS "Found numpy headers in " ${numpyIncludePath})
SET(CMAKE_SWIG_FLAGS -DWITHNUMPY ${CRP})
list(APPEND CRPROPA_SWIG_DEFINES -DWITHNUMPY)
include_directories(${numpyIncludePath})
else(numpyIncludePath)
MESSAGE(STATUS "Numpy not found.")
endif(numpyIncludePath)

set(SWIG_INCLUDES)
foreach(p in ${SWIG_INCLUDE_DIRECTORIES})
list(APPEND SWIG_INCLUDES -I${p})
endforeach()

file(GLOB_RECURSE CRPROPA_SWIG_INPUTS python/*.i)
set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/crpropa_wrap.cxx PROPERTIES GENERATED true )
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/crpropa_wrap.cxx PROPERTIES GENERATED true)
add_custom_target(crpropa-swig-wrapper
COMMAND swig ${BUILTIN} -c++ -python ${PY3} -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/libs/HepPID/include ${SWIG_INCLUDES} ${CRPROPA_SWIG_DEFINES} -dirprot -o ${CMAKE_CURRENT_BINARY_DIR}/crpropa_wrap.cxx -outdir ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/python/crpropa${BUILTIN}.i
COMMAND swig ${BUILTIN} -c++ -python -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/libs/HepPID/include ${SWIG_INCLUDES} ${CRPROPA_SWIG_DEFINES} -dirprot -o ${CMAKE_CURRENT_BINARY_DIR}/crpropa_wrap.cxx -outdir ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/python/crpropa${BUILTIN}.i
DEPENDS ${CRPROPA_SWIG_INPUTS} ${CRPROPA_INCLUDES} )

if(BUILD_DOC AND DOXYGEN_FOUND)
Expand All @@ -520,14 +527,15 @@ if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
# disable warnings on automatically generated interface code
set_target_properties(crpropa-swig PROPERTIES COMPILE_FLAGS "-w")
set_target_properties(crpropa-swig PROPERTIES OUTPUT_NAME "_crpropa")
target_link_libraries(crpropa-swig crpropa ${PYTHON_LIBRARIES})
target_link_libraries(crpropa-swig crpropa ${Python_LIBRARIES} ${Python_LIBRARY})
add_dependencies(crpropa-swig crpropa-swig-wrapper)

install(DIRECTORY "${CMAKE_SOURCE_DIR}/python/crpropa" DESTINATION "${PYTHON_SITE_PACKAGES}")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/crpropa.py" DESTINATION "${PYTHON_SITE_PACKAGES}/crpropa")
install(TARGETS crpropa-swig LIBRARY DESTINATION "${PYTHON_SITE_PACKAGES}/crpropa")
install(DIRECTORY "${CMAKE_SOURCE_DIR}/python/crpropa" DESTINATION "${Python_INSTALL_PACKAGE_DIR}")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/crpropa.py" DESTINATION "${Python_INSTALL_PACKAGE_DIR}/crpropa")
install(TARGETS crpropa-swig LIBRARY DESTINATION "${Python_INSTALL_PACKAGE_DIR}/crpropa")
install(FILES ${CRPROPA_SWIG_INPUTS} DESTINATION share/crpropa/swig_interface)
endif(ENABLE_PYTHON AND PYTHONLIBS_FOUND)

endif(ENABLE_PYTHON AND Python_FOUND)


# ----------------------------------------------------------------------------
Expand All @@ -538,7 +546,6 @@ install(TARGETS crpropa DESTINATION lib)
install(DIRECTORY include/ DESTINATION include FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${CMAKE_BINARY_DIR}/include/ DESTINATION include FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${CMAKE_BINARY_DIR}/data/ DESTINATION share/crpropa/ PATTERN ".git" EXCLUDE)

install(DIRECTORY libs/kiss/include/ DESTINATION include)

# ------------------------------------------------------------------
Expand Down Expand Up @@ -643,23 +650,23 @@ if(ENABLE_TESTING)
endif(WITH_GALACTIC_LENSES)

# python tests
if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
if(ENABLE_PYTHON AND Python_FOUND)
CONFIGURE_FILE(test/testMagneticLensPythonInterface.py testMagneticLensPythonInterface.py COPYONLY)
if(numpyIncludePath AND WITH_GALACTIC_LENSES)
add_test(testMagneticLensPythonInterface ${PYTHON_EXECUTABLE} testMagneticLensPythonInterface.py)
add_test(testMagneticLensPythonInterface ${Python_EXECUTABLE} testMagneticLensPythonInterface.py)
endif(numpyIncludePath AND WITH_GALACTIC_LENSES)

CONFIGURE_FILE(test/testSimulationExecution.py testSimulationExecution.py COPYONLY)
add_test(testSimulationExecution ${PYTHON_EXECUTABLE} testSimulationExecution.py)
add_test(testSimulationExecution ${Python_EXECUTABLE} testSimulationExecution.py)

CONFIGURE_FILE(test/testDiffusionSDE.py testDiffusionSDE.py COPYONLY)
add_test(testDiffusionSDE ${PYTHON_EXECUTABLE} testDiffusionSDE.py)
add_test(testDiffusionSDE ${Python_EXECUTABLE} testDiffusionSDE.py)

CONFIGURE_FILE(test/testMomentumDiffusion.py testMomentumDiffusion.py COPYONLY)
add_test(testMomentumDiffusion ${PYTHON_EXECUTABLE} testMomentumDiffusion.py)
add_test(testMomentumDiffusion ${Python_EXECUTABLE} testMomentumDiffusion.py)

CONFIGURE_FILE(test/testPythonExtension.py testPythonExtension.py COPYONLY)
add_test(testPythonExtension ${PYTHON_EXECUTABLE} testPythonExtension.py)
endif(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
add_test(testPythonExtension ${Python_EXECUTABLE} testPythonExtension.py)
endif(ENABLE_PYTHON AND Python_FOUND)

endif(ENABLE_TESTING)
19 changes: 0 additions & 19 deletions cmake/FindSAGA.cmake

This file was deleted.

20 changes: 0 additions & 20 deletions cmake/FindSQLite3.cmake

This file was deleted.

Loading

0 comments on commit 3498d82

Please sign in to comment.