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

[package] pybind11/2.9.1: pybind11_add_module not found #9343

Closed
huweiATgithub opened this issue Feb 10, 2022 · 7 comments
Closed

[package] pybind11/2.9.1: pybind11_add_module not found #9343

huweiATgithub opened this issue Feb 10, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@huweiATgithub
Copy link

Package and Environment Details (include every applicable attribute)

  • Package Name/Version: pybind11/2.9.1
  • Operating System+version: Windows 10
  • Compiler+version: VS 16 2019
  • Conan version: conan 1.44.1
  • Python version: Python 3.7.7

Conan profile (output of conan profile show default or conan profile show <profile> if custom profile is in use)

[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=Visual Studio
compiler.version=16
build_type=Debug
[options]
[conf]
[build_requires]
[env]

Steps to reproduce (Include if Applicable)

conanfile.txt

[requires]
pybind11/2.9.1

[generators]
CMakeDeps
CMakeToolchain

CMakeLists.txt

find_package(pybind11 REQUIRED)
include_directories("/path/to/python/include")
pybind11_add_module(example pybind.cpp lib.cpp)

Logs (Include/Attach if Applicable)

Click to expand log
-- Conan: Component target declared 'pybind11::main'
-- Conan: Component target declared 'pybind11::headers'
-- Conan: Component target declared 'pybind11::embed'
-- Conan: Component target declared 'pybind11::module'
-- Conan: Component target declared 'pybind11::python_link_helper'
-- Conan: Component target declared 'pybind11::windows_extras'
-- Conan: Component target declared 'pybind11::lto'
-- Conan: Component target declared 'pybind11::thin_lto'
-- Conan: Component target declared 'pybind11::opt_size'
-- Conan: Component target declared 'pybind11::python2_no_register'
-- Conan: Target declared 'pybind11::pybind11'
CMake Error at src/pybind/CMakeLists.txt:4 (pybind11_add_module):
  Unknown CMake command "pybind11_add_module".
-- Configuring incomplete, errors occurred!

Workarounds

This can be fixed by manually include the pybind11Tools.cmake file which defines the function pybind11_add_module.

It seems that conan generated config.cmake file does not include pybind11's tools definition file. Is this the designed behaviour or my settings might be wrong?

@huweiATgithub huweiATgithub added the bug Something isn't working label Feb 10, 2022
@huweiATgithub
Copy link
Author

@phillip-keldenich
Copy link

phillip-keldenich commented Mar 16, 2022

Same issue here (MacOS with AppleClang, Python 3.7 from an Anaconda environment, and CMake 3.22.0).
Finding Python is done (before finding pybind11) as follows:

set(Python_FIND_VIRTUALENV FIRST)
find_package(Python REQUIRED COMPONENTS Interpreter Development)

It correctly identifies the python interpreter and include directories I want it to use (again, python3 in the active conda environment).

I can get it to compile & link by explicitly including the pybind11Tools.cmake and pybind11Common.cmake files as follows (finding them based on the include directory, which is just a disgusting workaround/hack that happens to work for conan).

find_package(pybind11 CONFIG REQUIRED)
if(NOT COMMAND pybind11_add_module)
foreach(INCDIR IN LISTS pybind11_INCLUDE_DIRS)
	if(EXISTS "${INCDIR}/../lib/cmake/pybind11/pybind11Tools.cmake")
		include("${INCDIR}/../lib/cmake/pybind11/pybind11Common.cmake")
		include("${INCDIR}/../lib/cmake/pybind11/pybind11Tools.cmake")
		break()
	elseif(EXISTS "${INCDIR}/../../lib/cmake/pybind11/pybind11Tools.cmake")
		include("${INCDIR}/../../lib/cmake/pybind11/pybind11Common.cmake")
		include("${INCDIR}/../../lib/cmake/pybind11/pybind11Tools.cmake")
		break()
	endif()
endforeach()
endif()

Afterwards, modules can be created as usual by using pybind11_add_module(... MODULE ...) and linking against pybind11::module.

Edit: If it helps, this occurs both for the Ninja Multi-Config generator and the GNU Makefiles single-config generator.

@CPickens42
Copy link

Hello, I have exactly the same problem. I used the patch provided by @phillip-keldenich and it works great.
It seems that this issue is also mentioned here: #4445

It would be great to know how to properly use this recipe or to fix it altogether. Sadly I'm not "fluent enough" with conan to understand what's going on and to do a PR for that.

BTW, @phillip-keldenich why do you need to have an elsif with an include of ../..? The first if is enough for me, but I fear that I forget something.

@phillip-keldenich
Copy link

For me, there were two suitable include directories in pybind11_INCLUDE_DIRS (basically, a subdirectory and its parent).
I wasn't sure whether there are any circumstances under which one of them might be missing, so I decided to basically try both of them (it shouldn't really hurt). This is why there are two alternatives, even though one of them should be enough.

@CPickens42
Copy link

CPickens42 commented Apr 1, 2022

It seems that the official pybind11 package from conan doesn't expose all cmake files from pybind11. For instance share/cmake/pybind11/pybind11Config.cmake was not present at all.

I ended up using a custom conan package that works nicely with CMakeDeps and CMakeToolchain generators (that are the recommended way for conan 2.0):

import os
from conans import ConanFile, tools, CMake


class PyBind11Conan(ConanFile):
    name = "pybind11"
    version = "2.9.2"
    settings = "os", "compiler", "arch", "build_type"
    description = "Seamless operability between C++11 and Python"
    homepage = "https://github.com/pybind/pybind11"
    license = "BSD Style: https://github.com/pybind/pybind11/blob/master/LICENSE"
    url = "https://github.com/conan-community/conan-pybind11"
    no_copy_sources = True

    def source(self):
        tools.get("%s/archive/v%s.tar.gz" % (self.homepage, self.version))

    def build(self):
        cmake = CMake(self)
        cmake.definitions["PYBIND11_TEST"] = False
        cmake.configure(source_folder="pybind11-%s" % self.version)
        cmake.build()
        cmake.install()

    def package(self):
        self.copy("*LICENSE", keep_path=False)

    def package_id(self):
        # Make all options and dependencies (direct and transitive) contribute
        # to the package id
        self.info.requires.full_package_mode()

    def package_info(self):
        self.cpp_info.builddirs.append(os.path.join("share", "cmake", "pybind11"))

It is an adaptation of this https://github.com/conan-community/conan-pybind11.
The code in package_info permits to have the CMakeToolchain generators to overload CMakeDeps when necessary.

My CMakeLists.txt looks like that:

include(${CMAKE_SOURCE_DIR}/.conanvirtualenv/conanbuildinfo.cmake)

# load pybind11
set(Python_FIND_VIRTUALENV FIRST)
find_package(Python REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG REQUIRED)

This was referenced Apr 1, 2022
@Tumb1eweed
Copy link

have this problem too

@perseoGI
Copy link
Contributor

Hi there,
I was reviewing old issues and I have come across this one.

I'm closing this issue as this recipe has suffer plenty of modifications since this issue was reported.
When this recipe was migrated to conan v2, CMakeToolchain generator was introduced and this problem was fixed.
See successful compilation traces below using latest version of this recipe and conan v2:

$ conan create recipes/pybind11/all --version 2.13.6 --build=missing
======== Exporting recipe to the cache ========
pybind11/2.13.6: Exporting package recipe: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/conanfile.py
pybind11/2.13.6: exports: File 'conandata.yml' found. Exporting it...
pybind11/2.13.6: Copied 1 '.py' file: conanfile.py
pybind11/2.13.6: Copied 1 '.yml' file: conandata.yml
pybind11/2.13.6: Exported to cache folder: /Users/perseo/.conan2/p/pybin52674e4dd74f6/e
pybind11/2.13.6: Exported: pybind11/2.13.6#7d301b76bc1a308a51b506dd2de145b0 (2024-09-18 15:54:16 UTC)

======== Input profiles ========
Profile host:
[settings]
arch=armv8
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=15
os=Macos

Profile build:
[settings]
arch=armv8
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu17
compiler.libcxx=libc++
compiler.version=15
os=Macos

======== Computing dependency graph ========
Graph root
    cli
Requirements
    pybind11/2.13.6#7d301b76bc1a308a51b506dd2de145b0 - Cache

======== Computing necessary packages ========
Requirements
    pybind11/2.13.6#7d301b76bc1a308a51b506dd2de145b0:da39a3ee5e6b4b0d3255bfef95601890afd80709#9d289186dc0b30c7d92df3b5a8f27a74 - Download (conancenter)

======== Installing packages ========

-------- Downloading 1 package --------
pybind11/2.13.6: Retrieving package da39a3ee5e6b4b0d3255bfef95601890afd80709 from remote 'conancenter'
pybind11/2.13.6: Package installed da39a3ee5e6b4b0d3255bfef95601890afd80709
pybind11/2.13.6: Downloaded package revision 9d289186dc0b30c7d92df3b5a8f27a74
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: pybind11/2.13.6
WARN: deprecated:     'cpp_info.build_modules' used in: pybind11/2.13.6

======== Launching test_package ========

======== Computing dependency graph ========
Graph root
    pybind11/2.13.6 (test package): /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/conanfile.py
Requirements
    pybind11/2.13.6#7d301b76bc1a308a51b506dd2de145b0 - Cache

======== Computing necessary packages ========
Requirements
    pybind11/2.13.6#7d301b76bc1a308a51b506dd2de145b0:da39a3ee5e6b4b0d3255bfef95601890afd80709#9d289186dc0b30c7d92df3b5a8f27a74 - Cache

======== Installing packages ========
pybind11/2.13.6: Already installed! (1 of 1)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: pybind11/2.13.6
WARN: deprecated:     'cpp_info.build_modules' used in: pybind11/2.13.6

======== Testing the package ========
Removing previously existing 'test_package' build folder: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release
pybind11/2.13.6 (test package): Test package build: build/apple-clang-15-armv8-gnu17-release
pybind11/2.13.6 (test package): Test package build folder: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release
pybind11/2.13.6 (test package): Calling generate()
pybind11/2.13.6 (test package): Generators folder: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release/generators
pybind11/2.13.6 (test package): CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(pybind11)
    target_link_libraries(... pybind11_all_do_not_use)
pybind11/2.13.6 (test package): CMakeToolchain generated: conan_toolchain.cmake
pybind11/2.13.6 (test package): CMakeToolchain generated: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release/generators/CMakePresets.json
pybind11/2.13.6 (test package): CMakeToolchain generated: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/CMakeUserPresets.json
pybind11/2.13.6 (test package): Generating aggregated env files
pybind11/2.13.6 (test package): Generated aggregated env files: ['conanrun.sh', 'conanbuild.sh']

======== Testing the package: Building ========
pybind11/2.13.6 (test package): Calling build()
pybind11/2.13.6 (test package): Running CMake.configure()
pybind11/2.13.6 (test package): RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package"
-- Using Conan toolchain: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release/generators/conan_toolchain.cmake
-- Conan toolchain: Defining libcxx as C++ flags: -stdlib=libc++
-- Conan toolchain: C++ Standard 17 with extensions ON
-- The CXX compiler identification is AppleClang 16.0.0.16000026
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Component target declared 'pybind11::headers'
-- Conan: Component target declared 'pybind11::pybind11'
-- Conan: Component target declared 'pybind11::embed'
-- Conan: Component target declared 'pybind11::module'
-- Conan: Component target declared 'pybind11::python_link_helper'
-- Conan: Component target declared 'pybind11::windows_extras'
-- Conan: Component target declared 'pybind11::lto'
-- Conan: Component target declared 'pybind11::thin_lto'
-- Conan: Component target declared 'pybind11::opt_size'
-- Conan: Component target declared 'pybind11::python2_no_register'
-- Conan: Target declared 'pybind11_all_do_not_use'
-- Conan: Including build module from '/Users/perseo/.conan2/p/pybin0711cb137f6d7/p/lib/cmake/pybind11/pybind11Common.cmake'
CMake Warning (dev) at /Users/perseo/.conan2/p/pybin0711cb137f6d7/p/lib/cmake/pybind11/FindPythonLibsNew.cmake:101 (message):
  Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
  are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning, or
  preferably upgrade to using FindPython, either by calling it explicitly
  before pybind11, or by setting PYBIND11_FINDPYTHON ON before pybind11.
Call Stack (most recent call first):
  /Users/perseo/.conan2/p/pybin0711cb137f6d7/p/lib/cmake/pybind11/pybind11Tools.cmake:50 (find_package)
  /Users/perseo/.conan2/p/pybin0711cb137f6d7/p/lib/cmake/pybind11/pybind11Common.cmake:228 (include)
  build/apple-clang-15-armv8-gnu17-release/generators/pybind11-config.cmake:38 (include)
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found PythonInterp: /Users/perseo/.pyenv/versions/3.12.3/envs/conan2/bin/python (found suitable version "3.12.3", minimum required is "3.7")
-- Found PythonLibs: /Users/perseo/.pyenv/versions/3.12.3/lib/libpython3.12.dylib
-- Performing Test HAS_FLTO_THIN
-- Performing Test HAS_FLTO_THIN - Success
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release

pybind11/2.13.6 (test package): Running CMake.build()
pybind11/2.13.6 (test package): RUN: cmake --build "/Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/build/apple-clang-15-armv8-gnu17-release" -- -j14
[ 50%] Building CXX object CMakeFiles/test_package.dir/test_package.cpp.o
[100%] Linking CXX shared module test_package.cpython-312-darwin.so
[100%] Built target test_package


======== Testing the package: Executing test ========
pybind11/2.13.6 (test package): Running test()
pybind11/2.13.6 (test package): RUN: /Users/perseo/.pyenv/versions/3.12.3/envs/conan2/bin/python "/Users/perseo/sources/conan-center-index/recipes/pybind11/all/test_package/test.py"
Adding 2 + 3 = 5
Message: 'Hello from the C++ world!'

Happy coding 🐸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants