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

POC for tool_requires that make direct dependees inherit generators #17129

Merged
merged 9 commits into from
Oct 23, 2024
15 changes: 13 additions & 2 deletions conan/internal/api/install/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def load_cache_generators(path):
def write_generators(conanfile, app, envs_generation=None):
new_gen_folder = conanfile.generators_folder
_receive_conf(conanfile)
_receive_generators(conanfile)

hook_manager = app.hook_manager
# TODO: Optimize this, so the global generators are not loaded every call to write_generators
Expand All @@ -91,8 +92,11 @@ def write_generators(conanfile, app, envs_generation=None):
conanfile.generators = []
try:
for generator_name in old_generators:
global_generator = global_generators.get(generator_name)
generator_class = global_generator or _get_generator_class(generator_name)
if isinstance(generator_name, str):
global_generator = global_generators.get(generator_name)
generator_class = global_generator or _get_generator_class(generator_name)
else:
generator_class = generator_name
if generator_class:
try:
generator = generator_class(conanfile)
Expand Down Expand Up @@ -151,6 +155,13 @@ def _receive_conf(conanfile):
conanfile.conf.compose_conf(build_require.conf_info)


def _receive_generators(conanfile):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
""" Collect generators_info from the immediate build_requires"""
for build_require in conanfile.dependencies.direct_build.values():
if build_require.generator_info:
memsharded marked this conversation as resolved.
Show resolved Hide resolved
conanfile.generators = build_require.generator_info + conanfile.generators


def _generate_aggregated_env(conanfile):

def deactivates(filenames):
Expand Down
1 change: 1 addition & 0 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ConanFile:
buildenv_info = None
runenv_info = None
conf_info = None
generator_info = None

def __init__(self, display_name=""):
self.display_name = display_name
Expand Down
4 changes: 4 additions & 0 deletions conans/model/conanfile_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def context(self):
def conf_info(self):
return self._conanfile.conf_info

@property
def generator_info(self):
return self._conanfile.generator_info

@property
def dependencies(self):
return self._conanfile.dependencies
Expand Down
31 changes: 31 additions & 0 deletions test/integration/generators/test_generators_from_br.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import textwrap

from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.tools import TestClient


def test_inject_generators_conf():
tc = TestClient(light=True)
tc.save({"tool/conanfile.py": textwrap.dedent("""
from conan import ConanFile

class MyGenerator:
def __init__(self, conanfile):
self.conanfile = conanfile

def generate(self):
self.conanfile.output.info("MyGenerator generated")

class ToolConan(ConanFile):
name = "tool"
version = "0.1"

def package_info(self):
self.generator_info = ["CMakeToolchain", MyGenerator]
"""),
"conanfile.py": GenConanfile("app", "1.0").with_tool_requires("tool/0.1")})

tc.run("create tool")
tc.run("create .")
assert "app/1.0: CMakeToolchain generated: conan_toolchain.cmake" in tc.out
assert "app/1.0: MyGenerator generated" in tc.out