Skip to content

Commit 8509117

Browse files
CMakeLists.txt: locate pyscf with the build interpreter, from the build directory, or fail
Reworked along the review of 25 September: the pyscf lookup stays in one place (pyscf/lib/CMakeLists.txt) and runs with the interpreter the build runs under (find_package(Python); setup.py passes -DPython_EXECUTABLE=sys.executable), from the build directory instead of the checkout, and fails with a clear message when pyscf is not found or has no lib/config.h.in. PYSCF_SOURCE_DIR is a CACHE PATH instead of an option(). cmake_minimum_required 3.5 -> 3.12 for find_package(Python). Configure step tested on Linux (CMake 4.2): - /usr/bin/python3 (no pyscf) first on PATH, run from the checkout, no -DPython_EXECUTABLE: before, "Include pyscf source dir: <checkout>/pyscf" and config.h.in does not exist; now, "Could not locate an installed pyscf using /usr/bin/python3". - -DPython_EXECUTABLE=<env>/bin/python: "Include pyscf source dir: <env>/site-packages/pyscf", configuring done.
1 parent 1f1b65f commit 8509117

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎pyscf/lib/CMakeLists.txt‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 3.5)
1+
cmake_minimum_required (VERSION 3.12)
22
project (pyscf_forge)
33

44
# Build only the GAS FCI kernel library. This mode is intended for
@@ -93,11 +93,22 @@ if(ENABLE_OPENMP)
9393
endif()
9494
endif()
9595

96-
option(PYSCF_SOURCE_DIR "Pyscf source directory e.g. /usr/local/lib/python3.7/site-packages/pyscf")
96+
set(PYSCF_SOURCE_DIR "" CACHE PATH "Pyscf source directory e.g. /usr/local/lib/python3.7/site-packages/pyscf")
9797
if(NOT PYSCF_SOURCE_DIR)
98+
# Locate the installed pyscf with the interpreter the build runs under (setup.py passes -DPython_EXECUTABLE),
99+
# from the build directory: run from the checkout, an interpreter without pyscf would import the checkout's own
100+
# namespace-package portion pyscf/ and answer with the wrong directory, silently.
101+
find_package(Python COMPONENTS Interpreter REQUIRED)
98102
execute_process(
99-
COMMAND python3 -c "import pyscf; print(pyscf.__path__[0], end='')"
100-
OUTPUT_VARIABLE PYSCF_SOURCE_DIR)
103+
COMMAND ${Python_EXECUTABLE} -c "import pyscf; print(pyscf.__path__[0], end='')"
104+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
105+
OUTPUT_VARIABLE PYSCF_SOURCE_DIR
106+
RESULT_VARIABLE _pyscf_rc)
107+
if(NOT _pyscf_rc EQUAL 0 OR NOT EXISTS "${PYSCF_SOURCE_DIR}/lib/config.h.in")
108+
message(FATAL_ERROR
109+
"Could not locate an installed pyscf using ${Python_EXECUTABLE}. "
110+
"Pass -DPYSCF_SOURCE_DIR=<.../site-packages/pyscf> or -DPython_EXECUTABLE=<interpreter with pyscf>.")
111+
endif()
101112
endif()
102113
message(STATUS "Include pyscf source dir: ${PYSCF_SOURCE_DIR}")
103114
include_directories(${PYSCF_SOURCE_DIR}/lib ${PYSCF_SOURCE_DIR}/lib/deps/include)

‎setup.py‎

100755100644
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def run(self):
9898
self.announce('Configuring extensions', level=3)
9999
src_dir = os.path.abspath(os.path.join(__file__, '..', 'pyscf', 'lib'))
100100
cmd = ['cmake', f'-S{src_dir}', f'-B{self.build_temp}']
101+
# The interpreter this build runs under: CMakeLists.txt asks it where pyscf is installed, so that the lookup
102+
# does not depend on which python3 is first on PATH.
103+
cmd.append(f'-DPython_EXECUTABLE={sys.executable}')
101104

102105
# Allow user to disable OCCRI C extension build
103106
build_occri = os.getenv('BUILD_OCCRI', 'ON')

0 commit comments

Comments
 (0)