Skip to content

Commit

Permalink
Merge branch 'master' into cornu/modlunit_own_cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed May 31, 2024
2 parents 860a0fe + 71bf299 commit f4b8250
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 84 deletions.
9 changes: 4 additions & 5 deletions cmake/ExecuteFindPython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
# This is called from PythonHelper.cmake in a subprocess, to allow multiple Python versions to be
# searched for in the project without CACHE variable hackery.
project(ExecuteFindPython LANGUAGES C)
find_package(Python3 COMPONENTS ${Python3_COMPONENTS})
message(STATUS "Python3_INCLUDE_DIRS=${Python3_INCLUDE_DIRS}")
message(STATUS "Python3_LIBRARIES=${Python3_LIBRARIES}")
message(STATUS "Python3_VERSION_MAJOR=${Python3_VERSION_MAJOR}")
message(STATUS "Python3_VERSION_MINOR=${Python3_VERSION_MINOR}")
find_package(Python COMPONENTS ${Python_COMPONENTS})
message(STATUS "Python_INCLUDE_DIRS=${Python_INCLUDE_DIRS}")
message(STATUS "Python_LIBRARIES=${Python_LIBRARIES}")
message(STATUS "Python_VERSION=${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
16 changes: 0 additions & 16 deletions cmake/NeuronFileLists.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,6 @@ set(SCOPMATH_FILES_LIST

set(NRNMPI_FILES_LIST nrnmpi.cpp memory_usage.cpp bbsmpipack.cpp mpispike.cpp)

# nrnpython sources (only if ${NRN_ENABLE_PYTHON_DYNAMIC} is OFF}
set(NRNPYTHON_FILES_LIST
nrnpython.cpp
nrnpy_hoc.cpp
nrnpy_nrn.cpp
nrnpy_p2h.cpp
grids.cpp
rxd.cpp
rxd_extracellular.cpp
rxd_intracellular.cpp
rxd_vol.cpp
rxd_marching_cubes.cpp
rxd_llgramarea.cpp)

set(IVOS_FILES_LIST observe.cpp resource.cpp)

set(MPI_DYNAMIC_INCLUDE nrnmpi_dynam.h nrnmpi_dynam_cinc nrnmpi_dynam_wrappers.inc)
Expand Down Expand Up @@ -416,8 +402,6 @@ nrn_create_file_list(NRN_SPARSE_SRC_FILES ${PROJECT_SOURCE_DIR}/src/sparse ${SPA
nrn_create_file_list(NRN_SCOPMATH_SRC_FILES ${PROJECT_SOURCE_DIR}/src/scopmath
${SCOPMATH_FILES_LIST})
nrn_create_file_list(NRN_NRNMPI_SRC_FILES ${PROJECT_SOURCE_DIR}/src/nrnmpi ${NRNMPI_FILES_LIST})
nrn_create_file_list(NRN_NRNPYTHON_SRC_FILES ${PROJECT_SOURCE_DIR}/src/nrnpython
${NRNPYTHON_FILES_LIST})
nrn_create_file_list(NRN_BIN_SRC_FILES ${PROJECT_SOURCE_DIR}/src/ivoc/ nrnmain.cpp)
nrn_create_file_list(NRN_BIN_SRC_FILES ${PROJECT_SOURCE_DIR}/src/oc/ ockludge.cpp modlreg.cpp)
nrn_create_file_list(NRNMPI_DYNAMIC_INCLUDE_FILE ${PROJECT_SOURCE_DIR}/src/nrnmpi
Expand Down
32 changes: 13 additions & 19 deletions cmake/PythonHelper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ endif()
# * nrnpy_EXECUTABLE
# * nrnpy_INCLUDES
# * nrnpy_LIBRARIES
# * nrnpy_VERSION_MAJOR
# * nrnpy_VERSION_MINOR
# * nrnpy_VERSION
#
# If NRN_ENABLE_PYTHON is *not* set then only nrnpy_EXECUTABLE will be set. There is some special
# handling on macOS when sanitizers are enabled:
Expand All @@ -73,7 +72,7 @@ function(nrn_find_python)
message(FATAL_ERROR "${opt_KEYWORDS_MISSING_VALUES} values are required")
endif()
if(NOT IS_ABSOLUTE "${opt_NAME}")
# Find the full path to ${opt_NAME} as Python3_EXECUTABLE does not accept relative paths.
# Find the full path to ${opt_NAME} as Python_EXECUTABLE does not accept relative paths.
find_program(
"${opt_NAME}_full" "${opt_NAME}"
PATHS ENV PATH
Expand All @@ -88,14 +87,14 @@ function(nrn_find_python)
endif()
# Only bother finding version/include/library information if NRN_ENABLE_PYTHON is set.
if(NRN_ENABLE_PYTHON)
# Run find_package(Python3 ...) in a subprocess, so there is no pollution of CMakeCache.txt and
# Run find_package(Python ...) in a subprocess, so there is no pollution of CMakeCache.txt and
# so on. Our desire to include multiple Python versions in one build means we have to handle
# lists of versions/libraries/... manually. Unfortunately one cannot safely use find_package in
# CMake script mode, so we configure an extra project.
string(SHA1 pyexe_hash "${opt_NAME}")
string(SUBSTRING "${pyexe_hash}" 0 6 pyexe_hash)
# Which attributes we're trying to learn about this Python
set(python_vars Python3_INCLUDE_DIRS Python3_VERSION_MAJOR Python3_VERSION_MINOR)
set(python_vars Python_INCLUDE_DIRS Python_VERSION)
if(NRN_ENABLE_PYTHON_DYNAMIC AND NOT NRN_LINK_AGAINST_PYTHON)
# Do not link against Python, so we don't need the library -- just as well, it's not available
# in manylinux
Expand All @@ -106,15 +105,15 @@ function(nrn_find_python)
)
endif()
set(dev_component "Development.Module")
set(Python3_LIBRARIES "do-not-link-against-libpython-in-dynamic-python-builds")
set(Python_LIBRARIES "do-not-link-against-libpython-in-dynamic-python-builds")
else()
set(dev_component "Development")
list(APPEND python_vars Python3_LIBRARIES)
list(APPEND python_vars Python_LIBRARIES)
endif()
execute_process(
COMMAND
${CMAKE_COMMAND} "-DPython3_EXECUTABLE:STRING=${opt_NAME}"
"-DPython3_COMPONENTS=${dev_component};Interpreter" -S
${CMAKE_COMMAND} "-DPython_EXECUTABLE:STRING=${opt_NAME}"
"-DPython_COMPONENTS=${dev_component};Interpreter" -S
${CMAKE_SOURCE_DIR}/cmake/ExecuteFindPython -B
${CMAKE_BINARY_DIR}/ExecuteFindPython_${pyexe_hash}
RESULT_VARIABLE result
Expand All @@ -133,16 +132,13 @@ function(nrn_find_python)
set(${var} "${CMAKE_MATCH_1}")
endforeach()
set("${opt_PREFIX}_INCLUDES"
"${Python3_INCLUDE_DIRS}"
"${Python_INCLUDE_DIRS}"
PARENT_SCOPE)
set("${opt_PREFIX}_LIBRARIES"
"${Python3_LIBRARIES}"
"${Python_LIBRARIES}"
PARENT_SCOPE)
set("${opt_PREFIX}_VERSION_MAJOR"
"${Python3_VERSION_MAJOR}"
PARENT_SCOPE)
set("${opt_PREFIX}_VERSION_MINOR"
"${Python3_VERSION_MINOR}"
set("${opt_PREFIX}_VERSION"
"${Python_VERSION}"
PARENT_SCOPE)
endif()
# Finally do our special treatment for macOS + sanitizers
Expand Down Expand Up @@ -195,7 +191,6 @@ foreach(pyexe ${python_executables})
nrn_find_python(NAME "${pyexe}" PREFIX nrnpy)
if(NRN_ENABLE_PYTHON)
# If NRN_ENABLE_PYTHON=OFF then we're only using Python to run build scripts etc.
set(nrnpy_VERSION "${nrnpy_VERSION_MAJOR}.${nrnpy_VERSION_MINOR}")
if(${nrnpy_VERSION} VERSION_LESS NRN_MINIMUM_PYTHON_VERSION)
message(FATAL_ERROR "${pyexe} too old (${nrnpy_VERSION} < ${NRN_MINIMUM_PYTHON_VERSION})")
endif()
Expand All @@ -207,7 +202,7 @@ foreach(pyexe ${python_executables})
message(FATAL_ERROR "Cannot handle multiple Python include dirs: ${nrnpy_INCLUDES}")
endif()
if(NOT num_lib_dirs EQUAL 1)
message(FATAL_ERROR "Cannot handle multiple Python libraries: ${Python3_LIBRARIES}")
message(FATAL_ERROR "Cannot handle multiple Python libraries: ${Python_LIBRARIES}")
endif()
if(nrnpy_VERSION IN_LIST NRN_PYTHON_VERSIONS)
# We cannot build against multiple copies of the same pythonX.Y version.
Expand Down Expand Up @@ -236,7 +231,6 @@ if(NRN_ENABLE_TESTS AND NRN_ENABLE_PYTHON)
set(NRN_PYTHON_EXTRA_FOR_TESTS_VERSIONS)
foreach(pyexe ${NRN_PYTHON_EXTRA_FOR_TESTS})
nrn_find_python(NAME "${pyexe}" PREFIX nrnpy)
set(nrnpy_VERSION "${nrnpy_VERSION_MAJOR}.${nrnpy_VERSION_MINOR}")
if(nrnpy_VERSION IN_LIST NRN_PYTHON_VERSIONS)
string(JOIN ", " versions ${NRN_PYTHON_VERSIONS})
message(FATAL_ERROR "NRN_PYTHON_EXTRA_FOR_TESTS=${NRN_PYTHON_EXTRA_FOR_TESTS} cannot contain"
Expand Down
14 changes: 2 additions & 12 deletions src/nrniv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ if(MINGW)
list(APPEND NRN_NRNIV_LIB_SRC_FILES ${PROJECT_SOURCE_DIR}/src/mswin/dlfcn.c)
endif()

# NRN_ENABLE_MODULE_INSTALL will create a separate nrnpython lib
if(NRN_ENABLE_PYTHON AND NOT NRN_ENABLE_PYTHON_DYNAMIC)
# Include nrnpython in nrniv - useful for single lib neuron and wheels
list(APPEND NRN_NRNIV_LIB_SRC_FILES ${NRN_NRNPYTHON_SRC_FILES})
endif()

if(NOT NRN_ENABLE_INTERVIEWS)
list(APPEND NRN_NRNIV_LIB_SRC_FILES ${NRN_IVOS_SRC_FILES})
endif()
Expand Down Expand Up @@ -305,7 +299,7 @@ cpp_cc_configure_sanitizers(TARGET nrniv_lib)
# Source-directory .cpp needs to find generated .hpp.
target_include_directories(nrniv_lib PUBLIC "${NRN_OC_GEN}")
if(NRN_ENABLE_PYTHON AND NOT NRN_ENABLE_PYTHON_DYNAMIC)
target_include_directories(nrniv_lib PUBLIC "${NRN_DEFAULT_PYTHON_INCLUDES}")
target_link_libraries(nrniv_lib nrnpython)
endif()
if(NRN_ENABLE_THREADS)
target_link_libraries(nrniv_lib Threads::Threads)
Expand Down Expand Up @@ -346,10 +340,6 @@ endif()
# =============================================================================
# Link with all libraries
# =============================================================================
if(NRN_ENABLE_PYTHON AND NOT NRN_ENABLE_PYTHON_DYNAMIC)
target_link_libraries(nrniv_lib ${NRN_DEFAULT_PYTHON_LIBRARIES})
endif()

if(NRN_ENABLE_MPI)
if(NRN_ENABLE_MPI_DYNAMIC)
list(LENGTH NRN_MPI_LIBNAME_LIST _num_mpi)
Expand Down Expand Up @@ -392,7 +382,7 @@ if(NRN_ENABLE_INTERVIEWS)
include_directories(${IV_INCLUDE_DIR})
target_link_libraries(nrniv_lib interviews)
else()
target_include_directories(nrniv_lib PUBLIC ${NRN_IVOS_SRC_DIR} ${PROJECT_BINARY_DIR}/src/ivos)
target_include_directories(nrniv_lib PUBLIC ${NRN_IVOS_SRC_DIR})
endif()

if(IV_ENABLE_X11_DYNAMIC)
Expand Down
56 changes: 38 additions & 18 deletions src/nrnpython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,41 @@ install(TARGETS rxdmath DESTINATION ${NRN_INSTALL_SHARE_LIB_DIR})
# =============================================================================
set(nrnpython_lib_list)

set(NRNPYTHON_FILES_LIST
nrnpython.cpp
nrnpy_hoc.cpp
nrnpy_nrn.cpp
nrnpy_p2h.cpp
grids.cpp
rxd.cpp
rxd_extracellular.cpp
rxd_intracellular.cpp
rxd_vol.cpp
rxd_marching_cubes.cpp
rxd_llgramarea.cpp)

set(INCLUDE_DIRS
..
../oc
../nrnoc
../ivoc
../nrniv
../nrnmpi
${CMAKE_CURRENT_BINARY_DIR}
${PROJECT_BINARY_DIR}/src/oc)
if(NRN_ENABLE_INTERVIEWS)
list(APPEND INCLUDE_DIRS ${IV_INCLUDE_DIR})
else()
list(APPEND INCLUDE_DIRS ../ivos)
endif()

# user has selected dynamic python support (could be multiple versions)
if(NRN_ENABLE_PYTHON_DYNAMIC)
set(INCLUDE_DIRS
.
..
../oc
../nrnoc
../ivoc
../nrniv
../gnu
../nrnmpi
${PROJECT_BINARY_DIR}/src/nrnpython
${PROJECT_BINARY_DIR}/src/ivos
${PROJECT_BINARY_DIR}/src/oc)
if(NRN_ENABLE_INTERVIEWS)
list(APPEND INCLUDE_DIRS ${IV_INCLUDE_DIR})
else()
list(APPEND INCLUDE_DIRS ../ivos)
endif()
foreach(val RANGE ${NRN_PYTHON_ITERATION_LIMIT})
list(GET NRN_PYTHON_VERSIONS ${val} pyver)
list(GET NRN_PYTHON_INCLUDES ${val} pyinc)
list(GET NRN_PYTHON_LIBRARIES ${val} pylib)
add_library(nrnpython${pyver} SHARED ${NRN_NRNPYTHON_SRC_FILES})
add_library(nrnpython${pyver} SHARED ${NRNPYTHON_FILES_LIST})
target_include_directories(nrnpython${pyver} BEFORE PUBLIC ${pyinc} ${INCLUDE_DIRS})
target_link_libraries(nrnpython${pyver} nrniv_lib ${Readline_LIBRARY})
if(NRN_LINK_AGAINST_PYTHON)
Expand All @@ -48,6 +59,15 @@ if(NRN_ENABLE_PYTHON_DYNAMIC)
list(APPEND nrnpython_lib_list nrnpython${pyver})
install(TARGETS nrnpython${pyver} DESTINATION ${NRN_INSTALL_SHARE_LIB_DIR})
endforeach()
else()
add_library(nrnpython OBJECT ${NRNPYTHON_FILES_LIST})
add_dependencies(nrnpython generated_source_files)
set_property(TARGET nrnpython PROPERTY POSITION_INDEPENDENT_CODE ON)
target_include_directories(nrnpython PUBLIC ${INCLUDE_DIRS})
target_include_directories(nrnpython PUBLIC "${NRN_DEFAULT_PYTHON_INCLUDES}")
target_link_libraries(nrnpython ${NRN_DEFAULT_PYTHON_LIBRARIES})
target_include_directories(nrnpython PUBLIC ${PROJECT_SOURCE_DIR}/${NRN_3RDPARTY_DIR}/eigen)
target_include_directories(nrnpython PUBLIC ${PROJECT_BINARY_DIR}/src/nrniv/oc_generated)
endif()

configure_file(_config_params.py.in "${PROJECT_BINARY_DIR}/lib/python/neuron/_config_params.py"
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static void add2topdict(PyObject*);
static const char* hocobj_docstring = "class neuron.hoc.HocObject - Hoc Object wrapper";

#if 1
#include <hoccontext.h>
#include "hoccontext.h"
#else
extern Object* hoc_thisobject;
#define HocTopContextSet \
Expand Down
4 changes: 2 additions & 2 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <InterViews/resource.h>
#include <nrnoc2iv.h>
#include <classreg.h>
#include <nrnpython.h>
#include <hoccontext.h>
#include "nrnpython.h"
#include "hoccontext.h"
#include "nrnpy.h"
#include "nrnpy_utils.h"
#include "oc_ansi.h"
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpy_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <nrnwrap_Python.h>
#include "nrnwrap_Python.h"
#include <cassert>

inline bool is_python_string(PyObject* python_string) {
Expand Down
6 changes: 3 additions & 3 deletions src/nrnpython/nrnpython.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <nrnpython.h>
#include <nrnpy_utils.h>
#include "nrnpython.h"
#include "nrnpy_utils.h"
#include "oc_ansi.h"
#include <stdio.h>
#include <InterViews/resource.h>
#if HAVE_IV
#include <InterViews/session.h>
#endif
#include <nrnoc2iv.h>
#include <hoccontext.h>
#include "hoccontext.h"
#include <ocfile.h> // bool isDirExist(const std::string& path);

#include <hocstr.h>
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpython.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#if defined(USE_PYTHON)
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <nrnwrap_Python.h>
#include "nrnwrap_Python.h"

#endif /*USE_PYTHON*/

Expand Down
4 changes: 2 additions & 2 deletions src/nrnpython/rxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <../nrnoc/section.h>
#include <../nrnoc/nrn_ansi.h>
#include <../nrnoc/multicore.h>
#include <nrnwrap_Python.h>
#include <nrnpython.h>
#include "nrnwrap_Python.h"
#include "nrnpython.h"

#include <thread>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/rxd_extracellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>
#include "grids.h"
#include "rxd.h"
#include <nrnwrap_Python.h>
#include "nrnwrap_Python.h"
#include <cmath>
#include <ocmatrix.h>
#include <cfloat>
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/rxd_intracellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>
#include "grids.h"
#include "rxd.h"
#include <nrnwrap_Python.h>
#include "nrnwrap_Python.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/rxd_vol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <assert.h>
#include "grids.h"
#include "rxd.h"
#include <nrnwrap_Python.h>
#include "nrnwrap_Python.h"

/*Tortuous diffusion coefficients*/
#define DcX(x, y, z) (g->dc_x * PERM(x, y, z))
Expand Down
1 change: 0 additions & 1 deletion test/pyinit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ foreach(val RANGE ${NRN_PYTHON_COUNT})
# Not an error if a user's system doesn't have all the names in ${search_names}
continue()
endif()
set(nrnpy_VERSION "${nrnpy_VERSION_MAJOR}.${nrnpy_VERSION_MINOR}")
if(NOT nrnpy_VERSION IN_LIST NRN_PYTHON_VERSIONS)
# e.g. python or python3 points to a version we didn't build against
continue()
Expand Down

0 comments on commit f4b8250

Please sign in to comment.