Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Python & NumPy versions from configure/build time to runtime #558

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmake/dynamic_sourced_library.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Name : dynamic_sourced_library
## Params: LIB_NAME, WORK_DIR
## Add/create a new library with the given name, passing a source files all the '*.cpp' files found in the given working directory for the library
## Add/create a new library with the given name, passing as source files all the '*.cpp' files found in the given working directory for the library
function(dynamic_sourced_cxx_library LIB_NAME WORK_DIR)
execute_process(COMMAND ls
COMMAND grep .cpp
Expand All @@ -12,4 +12,4 @@ function(dynamic_sourced_cxx_library LIB_NAME WORK_DIR)
set(lib_CPP_FILES_LIST ${lib_CPP_FILES})

add_library(${LIB_NAME} STATIC ${lib_CPP_FILES_LIST})
endfunction()
endfunction()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file's change was something I noticed as I was implementing the main goal of this PR

29 changes: 29 additions & 0 deletions include/utilities/python/InterpreterUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ namespace utils {
addToPath(std::string(py::str(opt.attr("parent"))));
addToPath(std::string(py::str(opt)));
}

py::object python_version_info = importedTopLevelModules["sys"].attr("version_info");
py::str runtime_python_version = importedTopLevelModules["sys"].attr("version");
int major = py::int_(python_version_info.attr("major"));
int minor = py::int_(python_version_info.attr("minor"));
int patch = py::int_(python_version_info.attr("micro"));
if (major != python_major
|| minor != python_minor
|| patch != python_patch) {
throw std::runtime_error("Python version mismatch between configure/build ("
+ std::string(python_version)
+ ") and runtime (" + std::string(runtime_python_version) + ")");
}

importTopLevelModule("numpy");
py::str runtime_numpy_version = importedTopLevelModules["numpy"].attr("version").attr("version");
if(std::string(runtime_numpy_version) != numpy_version) {
throw std::runtime_error("NumPy version mismatch between configure/build ("
+ std::string(numpy_version)
+ ") and runtime (" + std::string(runtime_numpy_version) + ")");
}
}

~InterpreterUtil() = default;
Expand Down Expand Up @@ -332,6 +353,14 @@ namespace utils {

py::object Path;

static const int python_major;
static const int python_minor;
static const int python_patch;
static const char* python_version;

// NumPy version is not broken down by CMake
static const char* numpy_version;

/**
* Import a specified top level module.
*
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ if (${NGEN_ACTIVATE_PYTHON})
${PROJECT_SOURCE_DIR}/include/utilities
${PROJECT_SOURCE_DIR}/extern/pybind11/include
)
configure_file(${PROJECT_SOURCE_DIR}/src/core/NGen_Python_Build_Versions.in NGen_Python_Build_Versions.cpp)
target_sources(core PRIVATE NGen_Python_Build_Versions.cpp)
target_link_libraries(core PUBLIC
Boost::boost # Headers-only Boost
pybind11::embed
Expand Down
17 changes: 17 additions & 0 deletions src/core/NGen_Python_Build_Versions.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifdef ACTIVATE_PYTHON

#include "python/InterpreterUtil.hpp"

namespace utils {
namespace ngenPy {

const int InterpreterUtil::python_major = ${Python_VERSION_MAJOR};
const int InterpreterUtil::python_minor = ${Python_VERSION_MINOR};
const int InterpreterUtil::python_patch = ${Python_VERSION_PATCH};
const char* InterpreterUtil::python_version = "${Python_VERSION}";

const char* InterpreterUtil::numpy_version = "${Python_NumPy_VERSION}";
}
}

#endif
1 change: 0 additions & 1 deletion src/realizations/catchment/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ target_link_libraries(realizations_catchment PUBLIC
)

if(NGEN_ACTIVATE_PYTHON)
target_include_directories(realizations_catchment PUBLIC ${PROJECT_SOURCE_DIR}/extern/pybind11/include)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was something I noticed as I was implementing the main goal of this PR

target_link_libraries(realizations_catchment PUBLIC pybind11::embed)
endif()

Expand Down