Skip to content

Commit 06824e8

Browse files
authored
[CMake] FIX pybind11 finding python (2) (#156)
* [CMake] FIX pybind11 finding python (2) * [CMake] find_package(Python) first * [CMake] FIX badly named PYTHON_USER_SITE variable * add debug logs * add debug logs (2) * [CMake] Fallback values for PYTHON_LIBRARY and PYTHON_INCLUDE_DIR * [CMake] Reset PYTHON_* vars as late as possible * [CMake] Propagate fix to Config.cmake.in * remove debug logs * Merge branch 'ci_improve_workflows' into fix_fileformat_error * debug python env on MacOS * debug python env on MacOS (2) * debug python env on MacOS (3) * debug python env on MacOS (4) * debug python env on MacOS (5) * [GitHub] CLEAN worflows
1 parent 935f1f0 commit 06824e8

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
lines changed

.github/workflows/macos.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ jobs:
4343
python-version: '3.7'
4444

4545
- name: Install requirements
46+
shell: bash
4647
run: |
4748
brew install eigen boost
4849
brew install ccache ninja
50+
brew list | grep python | while read package; do
51+
brew unlink $package
52+
done
4953
python3 -m pip install numpy scipy
5054
5155
- name: pybind11 cache files
@@ -93,6 +97,9 @@ jobs:
9397
CCACHE_COMPRESSLEVEL: 6
9498
CCACHE_MAXSIZE: "500M"
9599
run: |
100+
echo "RUNNER_TOOL_CACHE = $RUNNER_TOOL_CACHE"
101+
python_root="$(find $RUNNER_TOOL_CACHE -maxdepth 3 -type d -path '**/Python/3.7*/x64')"
102+
echo "python_root = $python_root"
96103
export CCACHE_BASEDIR=$GITHUB_WORKSPACE
97104
export CCACHE_DIR=$GITHUB_WORKSPACE/.ccache
98105
ccache -z
@@ -102,7 +109,8 @@ jobs:
102109
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
103110
-DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake \
104111
-DCMAKE_BUILD_TYPE=Release \
105-
-DPYTHON_EXECUTABLE=$(which python3.7) \
112+
-DPython_EXECUTABLE=$(which python3.7) \
113+
-DPython_ROOT=$python_root \
106114
.
107115
ninja install
108116
echo ${CCACHE_BASEDIR}
@@ -152,8 +160,12 @@ jobs:
152160
python-version: '3.7'
153161

154162
- name: Install requirements
163+
shell: bash
155164
run: |
156165
brew install boost
166+
brew list | grep python | while read package; do
167+
brew unlink $package
168+
done
157169
python3 -m pip install numpy scipy
158170
159171
- name: Download and install the latest SOFA ${{ matrix.sofa_branch }} binaries

.github/workflows/ubuntu.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ jobs:
9393
CCACHE_COMPRESSLEVEL: 6
9494
CCACHE_MAXSIZE: "500M"
9595
run: |
96+
echo "RUNNER_TOOL_CACHE = $RUNNER_TOOL_CACHE"
97+
python_root="$(find $RUNNER_TOOL_CACHE -maxdepth 3 -type d -path '**/Python/3.7*/x64')"
98+
echo "python_root = $python_root"
9699
export CCACHE_BASEDIR=$GITHUB_WORKSPACE
97100
export CCACHE_DIR=$GITHUB_WORKSPACE/.ccache
98101
ccache -z
@@ -102,7 +105,8 @@ jobs:
102105
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
103106
-DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake \
104107
-DCMAKE_BUILD_TYPE=Release \
105-
-DPYTHON_EXECUTABLE=$(which python3.7) \
108+
-DPython_EXECUTABLE=$(which python3.7) \
109+
-DPython_ROOT=$python_root \
106110
.
107111
ninja install
108112
echo ${CCACHE_BASEDIR}

CMakeLists.txt

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,36 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
8585
# Set the minimum python version to 3.7
8686
set(PYBIND11_PYTHON_VERSION 3.7)
8787

88-
# Find Python3 executable and set PYTHON_EXECUTABLE before finding pybind11
89-
# to be sure that pybind11 relies on the right Python version
90-
set(python_version "${PYBIND11_PYTHON_VERSION}")
91-
set(python_version_flag "")
92-
if(PYTHON_EXECUTABLE)
93-
execute_process(
94-
COMMAND "${PYTHON_EXECUTABLE}" "--version"
95-
OUTPUT_VARIABLE cmd_output
96-
)
97-
string(REGEX MATCH "[0-9]+\.[0-9]+" python_exec_version "${cmd_output}")
98-
if(python_exec_version VERSION_GREATER_EQUAL PYBIND11_PYTHON_VERSION)
99-
set(python_version "${python_exec_version}")
100-
set(python_version_flag "EXACT")
101-
endif()
102-
endif()
103-
find_package(Python ${python_version} ${python_version_flag} COMPONENTS Interpreter Development REQUIRED)
88+
# Find Python3
89+
find_package(Python ${PYBIND11_PYTHON_VERSION} COMPONENTS Interpreter Development REQUIRED)
90+
91+
# Save PYTHON_* vars
92+
set(PYTHON_VERSION_RESET "${PYTHON_VERSION}")
10493
set(PYTHON_EXECUTABLE_RESET "${PYTHON_EXECUTABLE}")
94+
set(PYTHON_LIBRARIES_RESET "${PYTHON_LIBRARIES}")
95+
set(PYTHON_INCLUDE_DIRS_RESET "${PYTHON_INCLUDE_DIRS}")
96+
set(PYTHON_LIBRARY_RESET "${PYTHON_LIBRARY}")
97+
set(PYTHON_INCLUDE_DIR_RESET "${PYTHON_INCLUDE_DIR}")
98+
99+
# Change PYTHON_* vars before pybind11 find_package
100+
# to be sure that pybind11 relies on the right Python version
101+
set(PYTHON_VERSION "${Python_VERSION}" CACHE STRING "" FORCE)
105102
set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}" CACHE FILEPATH "" FORCE)
103+
set(PYTHON_LIBRARIES "${Python_LIBRARIES}" CACHE STRING "" FORCE)
104+
set(PYTHON_INCLUDE_DIRS "${Python_INCLUDE_DIRS}" CACHE STRING "" FORCE)
105+
if(EXISTS "${Python_LIBRARY}")
106+
set(PYTHON_LIBRARY "${Python_LIBRARY}" CACHE INTERNAL "" FORCE)
107+
elseif(EXISTS "${Python_LIBRARIES}")
108+
set(PYTHON_LIBRARY "${Python_LIBRARIES}" CACHE INTERNAL "" FORCE)
109+
endif()
110+
if(EXISTS "${Python_INCLUDE_DIR}")
111+
set(PYTHON_INCLUDE_DIR "${Python_INCLUDE_DIR}" CACHE INTERNAL "" FORCE)
112+
elseif(EXISTS "${Python_INCLUDE_DIRS}")
113+
set(PYTHON_INCLUDE_DIR "${Python_INCLUDE_DIRS}" CACHE INTERNAL "" FORCE)
114+
endif()
106115

107116
# Set the minimum pybind11 version to 2.3 (before that the pybind11::embed target did not exist)
108117
find_package(pybind11 2.3 CONFIG QUIET REQUIRED)
109-
set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_RESET}" CACHE FILEPATH "" FORCE)
110118

111119
set(SP3_PYTHON_PACKAGES_DIRECTORY
112120
"python3/site-packages"
@@ -138,7 +146,7 @@ message(STATUS "Python:
138146
Executable: ${Python_EXECUTABLE}
139147
Headers: ${Python_INCLUDE_DIRS}
140148
Libraries: ${Python_LIBRARIES}
141-
User site: ${Python_USER_SITE}"
149+
User site: ${PYTHON_USER_SITE}"
142150
)
143151
message(STATUS "pybind11:
144152
Version: ${pybind11_VERSION}
@@ -224,3 +232,11 @@ if (SP3_LINK_TO_USER_SITE AND SP3_PYTHON_PACKAGES_LINK_DIRECTORY)
224232
endif()
225233
endforeach()
226234
endif()
235+
236+
# Reset PYTHON_* vars
237+
set(PYTHON_VERSION "${PYTHON_VERSION_RESET}" CACHE STRING "" FORCE)
238+
set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_RESET}" CACHE FILEPATH "" FORCE)
239+
set(PYTHON_LIBRARIES "${PYTHON_LIBRARIES_RESET}" CACHE STRING "" FORCE)
240+
set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS_RESET}" CACHE STRING "" FORCE)
241+
set(PYTHON_LIBRARY "${PYTHON_LIBRARY_RESET}" CACHE INTERNAL "" FORCE)
242+
set(PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR_RESET}" CACHE INTERNAL "" FORCE)

SofaPython3Config.cmake.in

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,45 @@ set(SP3_PYTHON_PACKAGES_DIRECTORY @SP3_PYTHON_PACKAGES_DIRECTORY@)
99
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
1010
include(SofaPython3Tools)
1111

12-
# Find Python3 executable and set PYTHON_EXECUTABLE before finding pybind11
13-
# to be sure that pybind11 relies on the right Python version
14-
set(python_version @PYTHON_VERSION@)
15-
set(python_version_flag @python_version_flag@)
12+
# Find Python3
13+
find_package(Python @PYBIND11_PYTHON_VERSION@ COMPONENTS Interpreter Development REQUIRED)
1614

17-
find_package(Python ${python_version} ${python_version_flag} COMPONENTS Interpreter Development REQUIRED)
15+
# Save PYTHON_* vars
16+
set(PYTHON_VERSION_RESET "${PYTHON_VERSION}")
17+
set(PYTHON_EXECUTABLE_RESET "${PYTHON_EXECUTABLE}")
18+
set(PYTHON_LIBRARIES_RESET "${PYTHON_LIBRARIES}")
19+
set(PYTHON_INCLUDE_DIRS_RESET "${PYTHON_INCLUDE_DIRS}")
20+
set(PYTHON_LIBRARY_RESET "${PYTHON_LIBRARY}")
21+
set(PYTHON_INCLUDE_DIR_RESET "${PYTHON_INCLUDE_DIR}")
1822

19-
set(PYTHON_VERSION ${Python_VERSION})
20-
set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
21-
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
22-
set(PYTHON_INCLUDE_DIRS ${Python_INCLUDE_DIRS})
23-
set(PYTHON_LIBRARY ${Python_LIBRARY})
24-
set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIR})
23+
# Change PYTHON_* vars before pybind11 find_package
24+
# to be sure that pybind11 relies on the right Python version
25+
set(PYTHON_VERSION "${Python_VERSION}" CACHE STRING "" FORCE)
26+
set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}" CACHE FILEPATH "" FORCE)
27+
set(PYTHON_LIBRARIES "${Python_LIBRARIES}" CACHE STRING "" FORCE)
28+
set(PYTHON_INCLUDE_DIRS "${Python_INCLUDE_DIRS}" CACHE STRING "" FORCE)
29+
if(EXISTS "${Python_LIBRARY}")
30+
set(PYTHON_LIBRARY "${Python_LIBRARY}" CACHE INTERNAL "" FORCE)
31+
elseif(EXISTS "${Python_LIBRARIES}")
32+
set(PYTHON_LIBRARY "${Python_LIBRARIES}" CACHE INTERNAL "" FORCE)
33+
endif()
34+
if(EXISTS "${Python_INCLUDE_DIR}")
35+
set(PYTHON_INCLUDE_DIR "${Python_INCLUDE_DIR}" CACHE INTERNAL "" FORCE)
36+
elseif(EXISTS "${Python_INCLUDE_DIRS}")
37+
set(PYTHON_INCLUDE_DIR "${Python_INCLUDE_DIRS}" CACHE INTERNAL "" FORCE)
38+
endif()
2539

2640
# Set the minimum pybind11 version to 2.3 (before that the pybind11::embed target did not exist)
2741
find_package(pybind11 2.3 CONFIG QUIET REQUIRED)
2842

43+
# Reset PYTHON_* vars
44+
set(PYTHON_VERSION "${PYTHON_VERSION_RESET}" CACHE STRING "" FORCE)
45+
set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_RESET}" CACHE FILEPATH "" FORCE)
46+
set(PYTHON_LIBRARIES "${PYTHON_LIBRARIES_RESET}" CACHE STRING "" FORCE)
47+
set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS_RESET}" CACHE STRING "" FORCE)
48+
set(PYTHON_LIBRARY "${PYTHON_LIBRARY_RESET}" CACHE INTERNAL "" FORCE)
49+
set(PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR_RESET}" CACHE INTERNAL "" FORCE)
50+
2951
if(NOT SofaPython3_FIND_COMPONENTS)
3052
set(SofaPython3_FIND_COMPONENTS Plugin Bindings)
3153
endif()

0 commit comments

Comments
 (0)