Skip to content

Commit

Permalink
Use NMODLHOME for NMODL_WRAPLIB (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed May 3, 2023
1 parent 631793d commit f1c32db
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
4 changes: 0 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ for example:
````sh
export NMODL_PYLIB=/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/Python
````
* 'NMODL_WRAPLIB': This variable should point to the `libpywrapper.so` built as part of NMODL, for example:
```sh
export NMODL_WRAPLIB=/opt/nmodl/lib/libpywrapper.so
```

**Note**: In order for all unit tests to function correctly when building without linking against libpython we must
set `NMODL_PYLIB` before running cmake!
Expand Down
7 changes: 0 additions & 7 deletions pywheel/shim/_binwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ def _config_exe(exe_name):
NMODL_PREFIX = os.path.join(working_set.by_key[package_name].location, "nmodl")
NMODL_HOME = os.path.join(NMODL_PREFIX, ".data")
NMODL_BIN = os.path.join(NMODL_HOME, "bin")
NMODL_LIB = os.path.join(NMODL_HOME, "lib")

# add pywrapper path to environment
if sys.platform == "darwin":
os.environ["NMODL_WRAPLIB"] = os.path.join(NMODL_LIB, "libpywrapper.dylib")
else:
os.environ["NMODL_WRAPLIB"] = os.path.join(NMODL_LIB, "libpywrapper.so")

# add libpython*.so path to environment
os.environ["NMODL_PYLIB"] = find_libpython()
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const std::string nmodl::Version::GIT_REVISION = "@NMODL_GIT_REVISION@";
/// NMODL version
const std::string nmodl::Version::NMODL_VERSION = "@PROJECT_VERSION@";

const std::string nmodl::CMakeInfo::SHARED_LIBRARY_SUFFIX = "@CMAKE_SHARED_LIBRARY_SUFFIX@";

/**
* \brief Path of nrnutils.lib file
*
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ struct NrnUnitsLib {
}
};

struct CMakeInfo {
static const std::string SHARED_LIBRARY_SUFFIX;
};

} // namespace nmodl
3 changes: 2 additions & 1 deletion src/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ target_link_libraries(pyembed PRIVATE util)

if(NOT LINK_AGAINST_PYTHON)
add_library(pywrapper SHARED ${CMAKE_CURRENT_SOURCE_DIR}/wrapper.cpp)
set_target_properties(pywrapper PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set_target_properties(pywrapper PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${NMODL_PROJECT_BINARY_DIR}/lib)
else()
add_library(pywrapper ${CMAKE_CURRENT_SOURCE_DIR}/wrapper.cpp)
set_property(TARGET pywrapper PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down
18 changes: 12 additions & 6 deletions src/pybind/pyembed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

#include <cstdlib>
#include <dlfcn.h>
#include <filesystem>

#include "config/config.h"
#include "pybind/pyembed.hpp"
#include "utils/logger.hpp"

namespace fs = std::filesystem;

namespace nmodl {

namespace pybind_wrappers {
Expand Down Expand Up @@ -41,16 +45,18 @@ void EmbeddedPythonLoader::load_libraries() {
logger->critical(errstr);
throw std::runtime_error("Failed to dlopen");
}
const auto pybind_wraplib_env = std::getenv("NMODL_WRAPLIB");
if (!pybind_wraplib_env) {
auto pybind_wraplib_env = fs::path(std::getenv("NMODLHOME")) / "lib" / "libpywrapper";
pybind_wraplib_env.concat(CMakeInfo::SHARED_LIBRARY_SUFFIX);
if (!fs::exists(pybind_wraplib_env)) {
logger->critical(
"NMODL_WRAPLIB environment variable must be set to load the pybind wrapper library");
throw std::runtime_error("NMODL_WRAPLIB not set");
"NMODLHOME environment variable must be set to load the pybind "
"wrapper library");
throw std::runtime_error("NMODLHOME not set");
}
pybind_wrapper_handle = dlopen(pybind_wraplib_env, dlopen_opts);
pybind_wrapper_handle = dlopen(pybind_wraplib_env.c_str(), dlopen_opts);
if (!pybind_wrapper_handle) {
const auto errstr = dlerror();
logger->critical("Tried but failed to load {}", pybind_wraplib_env);
logger->critical("Tried but failed to load {}", pybind_wraplib_env.string());
logger->critical(errstr);
throw std::runtime_error("Failed to dlopen");
}
Expand Down
2 changes: 0 additions & 2 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ set(test_env ${NMODL_SANITIZER_ENABLE_ENVIRONMENT})
set(testvisitor_env "PYTHONPATH=${PROJECT_BINARY_DIR}/lib:$ENV{PYTHONPATH}")
if(NOT LINK_AGAINST_PYTHON)
list(APPEND testvisitor_env "NMODL_PYLIB=$ENV{NMODL_PYLIB}")
list(APPEND testvisitor_env
"NMODL_WRAPLIB=${PROJECT_BINARY_DIR}/lib/nmodl/libpywrapper${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()

foreach(
Expand Down

0 comments on commit f1c32db

Please sign in to comment.