Skip to content

[BUG]: CMake invalid include guard in pybind11Common.cmake #5393

Description

@petersteneteg

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.13.6

Problem description

pybind11Common.cmake defines a set of imported targets pybind11::pybind11 etc, They are guarded behind a global include guard. But the defined targets only have directory scope. Hence if one runs find_package(pybind11) from different subdirectories, the first one will run and pybind11Common.cmake and define the targets, the second one will bail at the global include guard, but the targets are not available since they where defined in a different directory and are not global.

This will need to an hard error, since the needed targets are now missing in the second subdirectory.

Changing the guard to "DIRECTORY" solves the issues. But one might still run into issues if one subdirectory would promote one or more targets to global scope. Generally I think each targets should be guarded by a if(NOT TARGET ..)That seems safer to me. Generally I would prefer of handle it more in line with how the autogenerated pybind11Targets.cmake handle it.
Basically it has a list of all the expected targets, and checks if all are defined, then it can return early, or if none is defined, then it can add them. Everything else is a fatal error.

The relevant line,

include_guard(GLOBAL)

which was introduced in a recent change. 28dbce4

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

2.13.5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions