diff --git a/pyscf/lib/CMakeLists.txt b/pyscf/lib/CMakeLists.txt index 67bc57acd..77b4cd83a 100644 --- a/pyscf/lib/CMakeLists.txt +++ b/pyscf/lib/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.5) +cmake_minimum_required (VERSION 3.15) project (pyscf_forge) # Build only the GAS FCI kernel library. This mode is intended for @@ -93,11 +93,22 @@ if(ENABLE_OPENMP) endif() endif() -option(PYSCF_SOURCE_DIR "Pyscf source directory e.g. /usr/local/lib/python3.7/site-packages/pyscf") +set(PYSCF_SOURCE_DIR "" CACHE PATH "Pyscf source directory e.g. /usr/local/lib/python3.7/site-packages/pyscf") if(NOT PYSCF_SOURCE_DIR) + # Locate the installed pyscf with the interpreter the build runs under (setup.py passes -DPython_EXECUTABLE), + # from the build directory: run from the checkout, an interpreter without pyscf would import the checkout's own + # namespace-package portion pyscf/ and answer with the wrong directory, silently. + find_package(Python COMPONENTS Interpreter REQUIRED) execute_process( - COMMAND python3 -c "import pyscf; print(pyscf.__path__[0], end='')" - OUTPUT_VARIABLE PYSCF_SOURCE_DIR) + COMMAND ${Python_EXECUTABLE} -c "import pyscf; print(pyscf.__path__[0], end='')" + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + OUTPUT_VARIABLE PYSCF_SOURCE_DIR + RESULT_VARIABLE _pyscf_rc) + if(NOT _pyscf_rc EQUAL 0 OR NOT EXISTS "${PYSCF_SOURCE_DIR}/lib/config.h.in") + message(FATAL_ERROR + "Could not locate an installed pyscf using ${Python_EXECUTABLE}. " + "Pass -DPYSCF_SOURCE_DIR=<.../site-packages/pyscf> or -DPython_EXECUTABLE=.") + endif() endif() message(STATUS "Include pyscf source dir: ${PYSCF_SOURCE_DIR}") include_directories(${PYSCF_SOURCE_DIR}/lib ${PYSCF_SOURCE_DIR}/lib/deps/include) diff --git a/setup.py b/setup.py index 7cd3a4a19..c4ca84a3f 100755 --- a/setup.py +++ b/setup.py @@ -98,6 +98,9 @@ def run(self): self.announce('Configuring extensions', level=3) src_dir = os.path.abspath(os.path.join(__file__, '..', 'pyscf', 'lib')) cmd = ['cmake', f'-S{src_dir}', f'-B{self.build_temp}'] + # The interpreter this build runs under: CMakeLists.txt asks it where pyscf is installed, so that the lookup + # does not depend on which python3 is first on PATH. + cmd.append(f'-DPython_EXECUTABLE={sys.executable}') # Allow user to disable OCCRI C extension build build_occri = os.getenv('BUILD_OCCRI', 'ON')