diff --git a/CMakeLists.txt b/CMakeLists.txt index 91133466..bc7948e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,15 @@ # Require a certain version of cmake cmake_minimum_required(VERSION 3.9) +# Set the name of the project +project(ThermoFun VERSION 0.4.3 LANGUAGES CXX) + # Set the cmake module path of the project set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") # Use ccache to speed up repeated compilations include(CCache) -# Set the name of the project -project(ThermoFun VERSION 0.4.2 LANGUAGES CXX) - # Define variables with the GNU standard installation directories include(GNUInstallDirs) @@ -43,6 +43,13 @@ else() set(CMAKE_POSITION_INDEPENDENT_CODE ON) endif() +# Used into conda only +if(DEFINED ENV{CONDA_PREFIX}) + option(USE_SPDLOG_PRECOMPILED "Use spdlog in compiled version" ON) +else() + option(USE_SPDLOG_PRECOMPILED "Use spdlog in compiled version" OFF) +endif() + # Set the default build type to Release if(NOT CMAKE_BUILD_TYPE) # The build type selection for the project diff --git a/ThermoFun/CMakeLists.txt b/ThermoFun/CMakeLists.txt index 3458ce63..130695ef 100644 --- a/ThermoFun/CMakeLists.txt +++ b/ThermoFun/CMakeLists.txt @@ -1,3 +1,4 @@ + # Recursively collect all header files from the current directory file(GLOB_RECURSE HEADER_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.hpp *.h) @@ -24,8 +25,6 @@ add_library(ThermoFun::ThermoFun ALIAS ThermoFun) # Link ThermoFun against all required dependencies target_link_libraries(ThermoFun PUBLIC ChemicalFun::ChemicalFun - # PUBLIC spdlog::spdlog - PRIVATE nlohmann_json::nlohmann_json ) # Specify the include directories of the library target diff --git a/ThermoFun/Common/Exception.cpp b/ThermoFun/Common/Exception.cpp index 1f30c202..450820da 100644 --- a/ThermoFun/Common/Exception.cpp +++ b/ThermoFun/Common/Exception.cpp @@ -19,6 +19,8 @@ void update_loggers( bool use_cout, const std::string& logfile_name, size_t log_ { auto thermofun_logger = spdlog::get("thermofun"); auto chemicalfun_logger = spdlog::get("chemicalfun"); + if(!thermofun_logger || !chemicalfun_logger) + return; // change level spdlog::level::level_enum log_lev = spdlog::level::info; @@ -49,6 +51,8 @@ void clear_loggers( const std::string& logfile_name) { auto thermofun_logger = spdlog::get("thermofun"); auto chemicalfun_logger = spdlog::get("chemicalfun"); + if(!thermofun_logger || !chemicalfun_logger) + return; thermofun_logger->sinks().clear(); chemicalfun_logger->sinks().clear(); diff --git a/ThermoFun/ThermoEngine.cpp b/ThermoFun/ThermoEngine.cpp index 3c0d4d26..d18d5932 100644 --- a/ThermoFun/ThermoEngine.cpp +++ b/ThermoFun/ThermoEngine.cpp @@ -87,11 +87,18 @@ struct ThermoEngine::Impl Impl() { + set_fn() ; } Impl(const Database &database) : database(database) { + set_fn(); + } + + void set_fn() + { + thermo_properties_substance_fn = [=](double T, double P_, double &P, std::string symbol) { auto x = P_; return thermoPropertiesSubstance(T, P, symbol); @@ -748,8 +755,10 @@ struct ThermoEngine::Impl }; ThermoEngine::ThermoEngine(const std::string filename) - : pimpl(new Impl(*(new const Database(filename)))) + : pimpl() { + Database db(filename); + pimpl.reset(new Impl(db)); } ThermoEngine::ThermoEngine(const Database &database) diff --git a/cmake/modules/ThermoFunFindDeps.cmake b/cmake/modules/ThermoFunFindDeps.cmake index a371c197..3a893dcb 100644 --- a/cmake/modules/ThermoFunFindDeps.cmake +++ b/cmake/modules/ThermoFunFindDeps.cmake @@ -9,22 +9,22 @@ if(TFUN_BUILD_PYTHON) endif() endif() -find_package(nlohmann_json REQUIRED) -if(NOT nlohmann_json_FOUND) - message(FATAL_ERROR "nlohmann_json library not found") -endif() +#find_package(nlohmann_json REQUIRED) +#if(NOT nlohmann_json_FOUND) +# message(FATAL_ERROR "nlohmann_json library not found") +#endif() find_package(ChemicalFun REQUIRED) if(NOT ChemicalFun_FOUND) message(FATAL_ERROR "ChemicalFun library not found") endif() -find_package(spdlog REQUIRED) -if(NOT spdlog_FOUND) - message(FATAL_ERROR "spdlog not found") +if(USE_SPDLOG_PRECOMPILED) + if(NOT TARGET spdlog::spdlog) + find_package(spdlog CONFIG REQUIRED) + if(NOT spdlog_FOUND) + message(FATAL_ERROR "spdlog not found") + endif() + endif() endif() -find_package(fmt REQUIRED) -if(NOT fmt_FOUND) - message(FATAL_ERROR "fmt not found") -endif() diff --git a/environment.devenv.yml b/environment.devenv.yml index bb5bae61..7af9202f 100644 --- a/environment.devenv.yml +++ b/environment.devenv.yml @@ -6,7 +6,6 @@ channels: - conda-forge dependencies: - - setuptools=59.7.0 - pip - gxx_linux-64 # [linux] - cmake @@ -16,9 +15,7 @@ dependencies: - clangxx_osx-64 # [osx] - pybind11 - nlohmann_json - # - spdlog=1.11.0 - # - fmt=9.1.0 - - chemicalfun=0.1.6 + - chemicalfun - python={{ python_version }} - pytest - vs2019_win-64 # [win] diff --git a/install-dependencies.sh b/install-dependencies.sh index 2fe39b8b..883f158e 100755 --- a/install-dependencies.sh +++ b/install-dependencies.sh @@ -1,31 +1,30 @@ #!/bin/bash -# Installing dependencies needed to build ThermoFun on (k)ubuntu linux 16.04 or 18.04 +# Installing dependencies needed to build ThermoFun on (k)ubuntu linux 20.04 EXTN=so -#sudo rm -f /usr/local/include/nlohmann/json.hpp -#sudo rm -rf /usr/local/include/eigen3/Eigen +#sudo rm -rf /usr/local/include/nlohmann +#sudo rm -rf /usr/local/include/eigen3/Eigen/Eigen #sudo rm -rf /usr/local/include/pybind11 #sudo rm -rf /usr/local/include/spdlog -#sudo rm -rf /usr/local/include/fmt #sudo rm -f /usr/local/lib/libChemicalFun.$EXTN workfolder=${PWD} BRANCH_TFUN=master BUILD_TYPE=Release -# nlohmann/json +# nlohmann/json library v. 3.6.1 or up +# if not installed in /usr/local/include/nlohmann) test -f /usr/local/include/nlohmann/json.hpp || { - # Building json library + # Getting nlohmann/json library mkdir -p ~/code && \ - cd ~/code && \ + cd ~/code && \ git clone https://github.com/nlohmann/json.git && \ cd json && \ mkdir -p build && \ cd build && \ - cmake .. -DJSON_BuildTests=OFF && \ - make && \ + cmake .. -DCMAKE_BUILD_TYPE=Release -DJSON_BuildTests=OFF -DJSON_MultipleHeaders=ON && \ sudo make install # Removing generated build files @@ -33,18 +32,18 @@ test -f /usr/local/include/nlohmann/json.hpp || { rm -rf ~/code } -# spdlog -# if no spdlog installed in /usr/local/lib/ (/usr/local/include/spdlog) -test -d /usr/local/include/spdlog || { +# Eigen3 math library (added for building and installing xGEMS) +# if not installed in /usr/local/include/eigen3) +test -d /usr/local/include/eigen3/Eigen || { - # Building thermofun library + # Building eigen library mkdir -p ~/code && \ cd ~/code && \ - git clone https://github.com/gabime/spdlog -b v1.11.0 && \ - cd spdlog && \ + git clone https://gitlab.com/libeigen/eigen.git -b 3.4.0 && \ + cd eigen && \ mkdir -p build && \ cd build && \ - cmake .. -DCMAKE_BUILD_TYPE=Release -DSPDLOG_BUILD_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=lib -DSPDLOG_BUILD_SHARED=ON -DSPDLOG_FMT_EXTERNAL=ON \ + cmake .. \ make && \ sudo make install @@ -53,20 +52,16 @@ test -d /usr/local/include/spdlog || { rm -rf ~/code } -# fmt -# if no spdlog installed in /usr/local/lib/ (/usr/local/include/fmt) -test -d /usr/local/include/fmt || { +# spdlog +# if no spdlog installed in /usr/local/include/spdlog (copy only headers) +test -d /usr/local/include/spdlog || { - # Building thermofun library + # Building spdlog library mkdir -p ~/code && \ cd ~/code && \ - git clone hhttps://github.com/fmtlib/fmt.git -b v9.1.0 && \ - cd fmt && \ - mkdir -p build && \ - cd build && \ - cmake .. \ - make && \ - sudo make install + git clone https://github.com/gabime/spdlog -b v1.11.0 && \ + cd spdlog/include && \ + sudo cp -r spdlog /usr/local/include # Removing generated build files cd ~ && \ @@ -77,40 +72,19 @@ test -d /usr/local/include/fmt || { test -d /usr/local/include/pybind11 || { # Building pybind11 library - mkdir -p ~/code && \ - cd ~/code && \ - git clone https://github.com/pybind/pybind11.git && \ - cd pybind11 && \ - mkdir -p build && \ - cd build && \ - cmake .. -DPYBIND11_TEST=OFF && \ - make && \ - sudo make install - - # Removing generated build files - cd ~ && \ - rm -rf ~/code -} - -# Eigen -# Eigen3 math library (added for building and installing xGEMS) -# if not installed in /usr/local/include/eigen3) -test -d /usr/local/include/eigen3/Eigen || { - - # Building eigen library - mkdir -p ~/code && \ - cd ~/code && \ - git clone https://gitlab.com/libeigen/eigen.git -b 3.4.0 && \ - cd eigen && \ - mkdir -p build && \ - cd build && \ - cmake .. \ - make && \ - sudo make install + mkdir -p ~/code && \ + cd ~/code && \ + git clone https://github.com/pybind/pybind11.git && \ + cd pybind11 && \ + mkdir -p build && \ + cd build && \ + cmake .. -DPYBIND11_TEST=OFF && \ + make && \ + sudo make install - # Removing generated build files - cd ~ && \ - rm -rf ~/code + # Removing generated build files + cd ~ && \ + rm -rf ~/code } @@ -126,7 +100,7 @@ test -f /usr/local/lib/libChemicalFun.$EXTN || { cd chemicalfun && \ mkdir -p build && \ cd build && \ - cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DTFUN_BUILD_PYTHON=OFF && \ + cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=$BUILD_TYPE && \ make -j $threads && \ sudo make install diff --git a/python/pyThermoFun/CMakeLists.txt b/python/pyThermoFun/CMakeLists.txt index b2df0fce..390e6d98 100644 --- a/python/pyThermoFun/CMakeLists.txt +++ b/python/pyThermoFun/CMakeLists.txt @@ -9,4 +9,4 @@ include_directories(${PROJECT_SOURCE_DIR}/python) pybind11_add_module(PyThermoFun ${SOURCE_FILES} NO_EXTRAS) # NO_EXTRAS used to decrease linking time, but causes larger module file size # Link PyThermoFun against ThermoFun library -target_link_libraries(PyThermoFun LINK_PRIVATE ThermoFun::ThermoFun) \ No newline at end of file +target_link_libraries(PyThermoFun PUBLIC ThermoFun::ThermoFun) \ No newline at end of file diff --git a/python/thermofun/CMakeLists.txt b/python/thermofun/CMakeLists.txt index 78dc637b..cb4460fe 100644 --- a/python/thermofun/CMakeLists.txt +++ b/python/thermofun/CMakeLists.txt @@ -11,11 +11,22 @@ set(SETUP_PY ${CMAKE_CURRENT_BINARY_DIR}/setup.py) # Configure the setup.py script with the provided CMake variables configure_file(${SETUP_PY_IN} ${SETUP_PY}) +# https://www.scivision.dev/cmake-install-python-package/ +# https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs +# detect virtualenv and set Pip args accordingly +if(DEFINED ENV{VIRTUAL_ENV} OR DEFINED ENV{CONDA_PREFIX}) + set(_PIP_ARG) +else() + set(_PIP_ARG "--user") +endif() +#message(STATUS "_PIP_ARG: ${_PIP_ARG}") + # Create a custom target to build thermofun python package during build stage add_custom_target(thermofun ALL COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_CURRENT_BINARY_DIR}/$ - COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} --quiet install --prefix=${CMAKE_BINARY_DIR} + #COMMAND ${PYTHON_EXECUTABLE} -m pip install ${_PIP_ARG} -e ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${PYTHON_EXECUTABLE} -m pip install --prefix ${CMAKE_BINARY_DIR} --ignore-installed . WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) # Set dependencies of thermofun target @@ -43,7 +54,7 @@ install(CODE endif() execute_process( - COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install --prefix=\${THERMOFUN_PYTHON_INSTALL_PREFIX_NATIVE} + COMMAND ${PYTHON_EXECUTABLE} -m pip install --prefix \${THERMOFUN_PYTHON_INSTALL_PREFIX_NATIVE} ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) " )