Skip to content

Commit

Permalink
[Build] Use CMakeDeps Conan generator
Browse files Browse the repository at this point in the history
Whilst attempting to write a test executable with an embedded Python
interpreter for OpenAssetIO#739, it turned out that the old problem reported in
OpenAssetIO#528 was still present.

To recap, ultimately (via circuitous logic) every CMake target created
by the Conan pybind11 package links to `libpython` (the
`pybind11::embed` target is the only one that should). This means that
Python extension modules (`pybind11::module`s) link to `libpython`,
causing linker errors at runtime or even segfaults due to ODR
violations.

As of conan-io/conan-center-index#13283 this bug
was fixed upstream. However, we still see it because the bug was not
fixed for the deprecated `cmake_find_package` generator, which we use.

It has not affected us so far because we haven't added in the
`Development.Embed` component to our `find_package(Python` call. As soon
as we add that component (e.g. for adding the aforementioned Python unit
test) we see this problem.

So modernise our `conanfile.py` to use the non-deprecated `CMakeDeps`
generator.

`CMakeDeps` insists on having the `build_type` setting available, so add
that.

Unfortunately `CMakeDeps` is designed to install dependencies with the
same configuration (Release, Debug, etc) as the parent project, which is
usually undesirable - we want Release versions of dependencies whilst
we develop the project in Debug mode. This is logged upstream in
conan-io/conan#11607, where a workaround is
posted, which is used here (see code comments).

Signed-off-by: David Feltell <david.feltell@foundry.com>
  • Loading branch information
feltech committed Nov 23, 2022
1 parent 739a3eb commit 66e47f5
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions resources/build/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
from conans import ConanFile, CMake, tools
from conans import ConanFile, tools
from conan.tools.cmake import CMakeDeps, CMakeToolchain


class OpenAssetIOConan(ConanFile):
generators = (
# Generate a CMake toolchain preamble file `conan_paths.cmake`,
# which augments package search paths with conan package
# directories.
"cmake_paths",
# Generate `Find<PackageName>.cmake` finders, required for
# various public ConanCenter packages (e.g. pybind11) since they
# disallow bundling of `<PackageName>Config.cmake`-like files in
# the package.
"cmake_find_package",
)
settings = "os"
settings = "os", "build_type"
# Generate a CMake toolchain preamble file `conan_paths.cmake`,
# which augments package search paths with conan package
# directories.
# TODO(DF): This is deprecated and should be swapped for the
# CMakeToolchain generator, but that is not yet compatible with the
# cpython recipe (it does not specify `builddirs` so is not added to
# the CMAKE_PREFIX_PATH).
generators = "cmake_paths"

def generate(self):
"""
This function is called at the end of a `conan install` and is
responsible for generating build toolchain helpers.
"""
# Generate `<PackageName>Config.cmake` files for each
# dependency, which CMake's `find_package` will locate and
# parse.
# Note: we override the configuration and re-run multiple times
# to allow us to use a different configuration for dependencies
# than we use for the main project. See
# https://github.com/conan-io/conan/issues/11607#issuecomment-1188500937
deps = CMakeDeps(self)
deps.configuration = "Release"
deps.generate()
deps.configuration = "Debug"
deps.generate()
deps.configuration = "RelWithDebInfo"
deps.generate()

def requirements(self):
# CY2022
Expand Down

0 comments on commit 66e47f5

Please sign in to comment.