Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pybind11 fix linkage #13283

Merged
merged 14 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions recipes/pybind11/all/CMakeLists.txt

This file was deleted.

109 changes: 43 additions & 66 deletions recipes/pybind11/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from conans import ConanFile, CMake
from conan.errors import ConanInvalidConfiguration
from conan.tools.files import get, copy, replace_in_file
from conan.tools.scm import Version
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain
from conan.tools.layout import basic_layout
from conan.tools.files import get, copy, replace_in_file, rm
import os
import functools


required_conan_version = ">=1.33.0"
required_conan_version = ">=1.52.0"


class PyBind11Conan(ConanFile):
Expand All @@ -16,83 +15,61 @@ class PyBind11Conan(ConanFile):
homepage = "https://github.com/pybind/pybind11"
license = "BSD-3-Clause"
url = "https://github.com/conan-io/conan-center-index"
exports_sources = "CMakeLists.txt"
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
no_copy_source = True

@property
def _source_subfolder(self):
return "source_subfolder"
def layout(self):
basic_layout(self, src_folder="src")

def source(self):
get(self, **self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], destination=self.source_folder, strip_root=True)

def validate(self):
if self.settings.compiler == "apple-clang" and Version(self.settings.compiler.version) >= "11.0":
raise ConanInvalidConfiguration("OSX support is bugged. Check https://github.com/pybind/pybind11/issues/3081")
def generate(self):
tc = CMakeToolchain(self)
tc.variables["PYBIND11_INSTALL"] = True
tc.variables["PYBIND11_TEST"] = False
tc.variables["PYBIND11_CMAKECONFIG_INSTALL_DIR"] = "lib/cmake/pybind11"
tc.generate()

@functools.lru_cache(1)
def _configure_cmake(self):
def build(self):
cmake = CMake(self)
cmake.definitions["PYBIND11_INSTALL"] = True
cmake.definitions["PYBIND11_TEST"] = False
cmake.definitions["PYBIND11_CMAKECONFIG_INSTALL_DIR"] = "lib/cmake/pybind11"
cmake.configure()
return cmake

def build(self):
cmake = self._configure_cmake()
cmake.build()

def package(self):
copy(self, "LICENSE", src=os.path.join(self.source_folder, self._source_subfolder), dst=os.path.join(self.package_folder, "licenses"))
cmake = self._configure_cmake()
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
for filename in ["pybind11Targets.cmake", "pybind11Config.cmake", "pybind11ConfigVersion.cmake"]:
try:
os.unlink(os.path.join(self.package_folder, "lib", "cmake", "pybind11", filename))
except:
pass
if Version(self.version) >= "2.6.0":
replace_in_file(self, os.path.join(self.package_folder, "lib", "cmake", "pybind11", "pybind11Common.cmake"),
"if(TARGET pybind11::lto)",
"if(FALSE)")
replace_in_file(self, os.path.join(self.package_folder, "lib", "cmake", "pybind11", "pybind11Common.cmake"),
"add_library(",
"# add_library(")
rm(self, filename, os.path.join(self.package_folder, "lib", "cmake", "pybind11"))
replace_in_file(self, os.path.join(self.package_folder, "lib", "cmake", "pybind11", "pybind11Common.cmake"),
"if(TARGET pybind11::lto)",
"if(FALSE)")
replace_in_file(self, os.path.join(self.package_folder, "lib", "cmake", "pybind11", "pybind11Common.cmake"),
"add_library(",
"# add_library(")

def package_id(self):
self.info.clear()

def package_info(self):
cmake_base_path = os.path.join("lib", "cmake", "pybind11")
if Version(self.version) >= "2.6.0":
self.cpp_info.components["main"].set_property("cmake_module_file_name", "pybind11")
self.cpp_info.components["main"].names["cmake_find_package"] = "pybind11"
self.cpp_info.components["main"].builddirs = [cmake_base_path]
cmake_file = os.path.join(cmake_base_path, "pybind11Common.cmake")
self.cpp_info.set_property("cmake_build_modules", [cmake_file])
for generator in ["cmake_find_package", "cmake_find_package_multi"]:
self.cpp_info.components["main"].build_modules[generator].append(cmake_file)
self.cpp_info.components["headers"].includedirs = [os.path.join("include", "pybind11")]
self.cpp_info.components["headers"].requires = ["main"]
self.cpp_info.components["embed"].requires = ["main"]
self.cpp_info.components["module"].requires = ["main"]
self.cpp_info.components["python_link_helper"].requires = ["main"]
self.cpp_info.components["windows_extras"].requires = ["main"]
self.cpp_info.components["lto"].requires = ["main"]
self.cpp_info.components["thin_lto"].requires = ["main"]
self.cpp_info.components["opt_size"].requires = ["main"]
self.cpp_info.components["python2_no_register"].requires = ["main"]
else:
self.cpp_info.includedirs.append(os.path.join(
self.package_folder, "include", "pybind11"))

self.cpp_info.builddirs = [cmake_base_path]

cmake_files = [os.path.join(cmake_base_path, "FindPythonLibsNew.cmake"),
os.path.join(cmake_base_path, "pybind11Tools.cmake")]
self.cpp_info.set_property("cmake_build_modules", cmake_files)
for generator in ["cmake", "cmake_multi", "cmake_find_package", "cmake_find_package_multi"]:
self.cpp_info.build_modules[generator] = cmake_files
self.cpp_info.set_property("cmake_target_name", "pybind11_all_do_not_use")
self.cpp_info.components["headers"].includedirs = ["include"]
self.cpp_info.components["pybind11_"].set_property("cmake_target_name", "pybind11::pybind11")
self.cpp_info.components["pybind11_"].set_property("cmake_module_file_name", "pybind11")
self.cpp_info.components["pybind11_"].names["cmake_find_package"] = "pybind11"
self.cpp_info.components["pybind11_"].builddirs = [cmake_base_path]
self.cpp_info.components["pybind11_"].requires = ["headers"]
cmake_file = os.path.join(cmake_base_path, "pybind11Common.cmake")
self.cpp_info.set_property("cmake_build_modules", [cmake_file])
for generator in ["cmake_find_package", "cmake_find_package_multi"]:
self.cpp_info.components["pybind11_"].build_modules[generator].append(cmake_file)
self.cpp_info.components["embed"].requires = ["pybind11_"]
self.cpp_info.components["module"].requires = ["pybind11_"]
self.cpp_info.components["python_link_helper"].requires = ["pybind11_"]
self.cpp_info.components["windows_extras"].requires = ["pybind11_"]
self.cpp_info.components["lto"].requires = ["pybind11_"]
self.cpp_info.components["thin_lto"].requires = ["pybind11_"]
self.cpp_info.components["opt_size"].requires = ["pybind11_"]
self.cpp_info.components["python2_no_register"].requires = ["pybind11_"]
11 changes: 0 additions & 11 deletions recipes/pybind11/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
cmake_minimum_required(VERSION 3.4)
project(test_package CXX)

include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_output_dirs_setup()
conan_set_rpath()
conan_set_std()
conan_set_fpic()
conan_check_compiler()
conan_set_libcxx()
conan_set_vs_runtime()

find_package(pybind11 REQUIRED CONFIG)

pybind11_add_module(test_package MODULE test_package.cpp)
set_property(TARGET test_package PROPERTY CXX_STANDARD 11)
39 changes: 31 additions & 8 deletions recipes/pybind11/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
from conan.tools.env import VirtualRunEnv
from conan.tools.build import can_run
from conan.tools.layout import cmake_layout
from conans.tools import environment_append

import os
from pathlib import PurePath
import sys
from platform import python_version


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def generate(self):
deps = CMakeDeps(self)
deps.generate()

toolchain = CMakeToolchain(self)
toolchain.variables["PYTHON_EXECUTABLE"] = PurePath(self._python_interpreter).as_posix()
toolchain.generate()

run = VirtualRunEnv(self)
run.generate()

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.definitions["PYTHON_EXECUTABLE"] = self._python_interpreter
cmake.configure()
cmake.build()

Expand All @@ -21,7 +43,8 @@ def _python_interpreter(self):
return sys.executable

def test(self):
if not tools.cross_building(self):
with tools.environment_append({"PYTHONPATH": "lib"}):
self.run("{} {}".format(self._python_interpreter, os.path.join(
self.source_folder, "test.py")), run_environment=True)
if can_run(self):
python_path = os.path.join(self.build_folder, self.cpp.build.libdirs[0])
with environment_append({"PYTHONPATH": python_path}):
module_path = os.path.join(self.source_folder, "test.py")
self.run(f"{self._python_interpreter} {module_path}", env="conanrun")
18 changes: 18 additions & 0 deletions recipes/pybind11/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.4)
project(test_package CXX)

include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_output_dirs_setup()
conan_set_rpath()
conan_set_std()
conan_set_fpic()
conan_check_compiler()
conan_set_libcxx()
conan_set_vs_runtime()

find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED CONFIG)

Python_add_library(test_package ../test_package/test_package.cpp)
target_link_libraries(test_package PRIVATE pybind11::headers)
set_property(TARGET test_package PROPERTY CXX_STANDARD 11)
27 changes: 27 additions & 0 deletions recipes/pybind11/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from conans import ConanFile, CMake, tools
import os
import sys
from platform import python_version


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.definitions["PYTHON_EXECUTABLE"] = self._python_interpreter
cmake.configure()
cmake.build()

@property
def _python_interpreter(self):
if getattr(sys, "frozen", False):
return "python"
return sys.executable

def test(self):
if not tools.cross_building(self):
with tools.environment_append({"PYTHONPATH": "lib"}):
python_module = os.path.join(self.source_folder, "..", "test_package", "test.py")
self.run(f"{self._python_interpreter} {python_module}", run_environment=True)