Skip to content

Commit ef9b765

Browse files
committed
Fix build with cmake_find_package generator
The issue was that the CMake variables `CONAN_EMBEDDED_PYTHON_ROOT` and `CONAN_USER_EMBEDDED_PYTHON_pyversion` are only set when using the `cmake` generator but not with `cmake_find_package`. The solution is to avoid using these variables and instead rely on the fact that `Python_ROOT_DIR` is in the same directory as the `embedded_python.cmake` file. And for the version number, we do a string replacement in `conanfile.py`. It's fine to hardcode this since the version is always unique to the Conan package.
1 parent 04fab96 commit ef9b765

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v1.4.4 | In development
44

55
- Fixed build with Python >= 3.9.11 and >= 3.10.3 on Windows (the installer changed)
6+
- Fixed configuration with the Conan `cmake_find_package` generator
67

78
## v1.4.3 | 2022-01-28
89

conanfile.py

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def _gather_licenses(self, bootstrap):
124124
f" --with-license-file --no-license-path --output-file=package_licenses.txt")
125125

126126
def build(self):
127+
tools.replace_in_file("embedded_python.cmake", "${self.pyversion}", str(self.pyversion))
128+
127129
prefix = pathlib.Path(self.build_folder) / "embedded_python"
128130
if self.settings.os == "Windows":
129131
build_helper = WindowsBuildHelper(self, prefix)

embedded_python.cmake

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# A hint for `find_package(Python)`
2-
set(Python_ROOT_DIR "${CONAN_EMBEDDED_PYTHON_ROOT}/embedded_python")
2+
set(Python_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/embedded_python")
33

44
if(WIN32) # Extra hint to speed up the find (not needed for correctness)
55
set(Python_EXECUTABLE "${Python_ROOT_DIR}/python.exe")
66
endif()
77

8-
find_package(Python ${CONAN_USER_EMBEDDED_PYTHON_pyversion} EXACT REQUIRED
9-
COMPONENTS Interpreter Development)
8+
find_package(Python ${self.pyversion} EXACT REQUIRED COMPONENTS Interpreter Development)

test_package/CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
cmake_minimum_required(VERSION 3.18)
22
project(test_package)
33

4+
# `cmake` generator
45
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
56
conan_basic_setup(KEEP_RPATHS)
67

78
if(NOT Python_VERSION VERSION_EQUAL CONAN_USER_EMBEDDED_PYTHON_pyversion)
8-
message(FATAL_ERROR "Failed to find the correct Python version")
9+
message(FATAL_ERROR "`cmake` failed to find the correct Python version")
10+
endif()
11+
12+
# `cmake_find_package` generator
13+
unset(Python_ROOT_DIR)
14+
unset(Python_VERSION)
15+
find_package(embedded_python)
16+
17+
if(NOT Python_VERSION VERSION_EQUAL CONAN_USER_EMBEDDED_PYTHON_pyversion)
18+
message(FATAL_ERROR "`cmake_find_package` failed to find the correct Python version")
919
endif()

test_package/conanfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _read_env(name):
1414
class TestEmbeddedPython(ConanFile):
1515
name = "test_embedded_python"
1616
settings = "os"
17-
generators = "cmake"
17+
generators = "cmake", "cmake_find_package"
1818
options = {"env": "ANY"}
1919
default_options = {
2020
"env": None,

0 commit comments

Comments
 (0)