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 6 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.

42 changes: 18 additions & 24 deletions recipes/pybind11/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from conans import ConanFile, CMake
from conan.errors import ConanInvalidConfiguration
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
from conan.tools.scm import Version
import os
import functools


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


class PyBind11Conan(ConanFile):
Expand All @@ -16,38 +16,32 @@ 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):
toolchain = CMakeToolchain(self)
toolchain.cache_variables.update({
"PYBIND11_INSTALL": True,
"PYBIND11_TEST": False,
"PYBIND11_CMAKECONFIG_INSTALL_DIR": "lib/cmake/pybind11"
})
toolchain.generate()
planetmarshall marked this conversation as resolved.
Show resolved Hide resolved

@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:
Expand Down
15 changes: 4 additions & 11 deletions recipes/pybind11/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
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)

pybind11_add_module(test_package MODULE test_package.cpp)
#pybind11_add_module(test_package MODULE test_package.cpp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lingering comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR still in draft - there are still a few things I want to test.

Python_add_library(test_package test_package.cpp)
target_link_libraries(test_package PRIVATE pybind11::headers)
set_property(TARGET test_package PROPERTY CXX_STANDARD 11)
38 changes: 30 additions & 8 deletions recipes/pybind11/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
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
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.cache_variables["PYTHON_EXECUTABLE"] = self._python_interpreter
planetmarshall marked this conversation as resolved.
Show resolved Hide resolved
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 +42,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)