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 cmakedeps #9348

Closed
wants to merge 15 commits into from
12 changes: 6 additions & 6 deletions recipes/pybind11/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from conans import ConanFile, tools, CMake
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.43.0"


class PyBind11Conan(ConanFile):
Expand Down Expand Up @@ -66,6 +66,7 @@ def package_info(self):
self.cpp_info.components["main"].builddirs = [cmake_base_path]
for generator in ["cmake_find_package", "cmake_find_package_multi"]:
self.cpp_info.components["main"].build_modules[generator].append(os.path.join(cmake_base_path, "pybind11Common.cmake"))
self.cpp_info.set_property("cmake_build_modules", [os.path.join(cmake_base_path, "pybind11Common.cmake")])
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"]
Expand All @@ -77,11 +78,10 @@ def package_info(self):
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.includedirs.append(os.path.join("include", "pybind11"))
self.cpp_info.builddirs = [cmake_base_path]

build_modules = [os.path.join(cmake_base_path, "FindPythonLibsNew.cmake"), os.path.join(cmake_base_path, "pybind11Tools.cmake")]
for generator in ["cmake", "cmake_multi", "cmake_find_package", "cmake_find_package_multi"]:
self.cpp_info.build_modules[generator] = [os.path.join(cmake_base_path, "FindPythonLibsNew.cmake"),
os.path.join(cmake_base_path, "pybind11Tools.cmake")]
self.cpp_info.build_modules[generator] = build_modules
self.cpp_info.set_property("cmake_build_modules", build_modules)
14 changes: 14 additions & 0 deletions recipes/pybind11/all/test_cmakedeps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.4)
project(test_package CXX)


find_package(pybind11 REQUIRED)

pybind11_add_module(test_package MODULE test_package.cpp)
target_link_libraries(test_package PRIVATE pybind11::pybind11)
set_property(TARGET test_package PROPERTY CXX_STANDARD 11)

enable_testing()

add_test(run_example
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test.py ${CMAKE_CURRENT_BINARY_DIR}/lib)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the same project files (CMakeLists.txt) from test_package, even if you need some if to skip the conanbuildinfo.cmake. It is important to check that both generators use the same find_package call, same target names,...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that is a good practice:

  1. Tricking the layout to include a ../test_package/CMakeLists.txt is very dirty and not something you will do except for this situation.
  2. Introducing an IF in the original CMakelists.txt to skip the conanbuildinfo.cmake would be very very weird, so you edit that file and you are supposed to know that is shared between several test_packages.
  3. When Conan 2.0 is introduced here, we want to remove/rename the test_package folder and it will require extra effort.
  4. I get that using the same target name is important, and that can be checked in the review of the PR.

Overall I think that code reuse, far from helping, will cause more trouble and dirty code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank God it is more or less easy with the layout, just:

    def layout(self):
        cmake_layout(self)
        self.folders.source = os.path.join("..", "test_package")

I find it important, but let's wait for other reviewers, they are the ones that will maintain this recipe and possible inconsistencies between the two files.

39 changes: 39 additions & 0 deletions recipes/pybind11/all/test_cmakedeps/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from conans import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain
from conan.tools.layout import cmake_layout
from conan.tools.env import Environment
from conan.tools.cross_building import cross_building
import os
import sys


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain", "CMakeDeps"

def generate(self):
toolchain = CMakeToolchain(self)
toolchain.variables["PYTHON_EXECUTABLE"] = self._python_interpreter.replace("\\", "/")
toolchain.generate()

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

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

def test(self):
if not cross_building(self):
env = Environment()
env.define("PYTHONPATH", self.cpp.build.libdirs[0])
env.vars(self).save_script("launcher")
test_path = os.path.join(self.source_folder, "test.py")
self.run("{} {}".format(self._python_interpreter, test_path), env="launcher")
5 changes: 5 additions & 0 deletions recipes/pybind11/all/test_cmakedeps/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test_package

print("Adding 2 + 3 = {}".format(test_package.add(2, 3)))

print("Message: '{}'".format(test_package.msg()))
16 changes: 16 additions & 0 deletions recipes/pybind11/all/test_cmakedeps/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <pybind11/pybind11.h>

static int add(int i, int j) {
return i + j;
}

static const char *hello() {
return "Hello from the C++ world!";
}

PYBIND11_MODULE(test_package, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring

m.def("add", &add, "A function which adds two numbers");
m.def("msg", &hello, "A function returning a message");
}
3 changes: 1 addition & 2 deletions recipes/pybind11/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from conans import ConanFile, CMake, tools
import os
import sys
from platform import python_version


class TestPackageConan(ConanFile):
Expand All @@ -10,7 +9,7 @@ class TestPackageConan(ConanFile):

def build(self):
cmake = CMake(self)
cmake.definitions["PYTHON_EXECUTABLE"] = self._python_interpreter
cmake.definitions["PYTHON_EXECUTABLE"] = self._python_interpreter.replace("\\", "/")
lasote marked this conversation as resolved.
Show resolved Hide resolved
cmake.configure()
cmake.build()

Expand Down