question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Conan 2.0 Custom Generators

See original GitHub issue

I have a few custom generators that help support pre-conan setups, defining extra environment variables and such. I am attempting to upgrade these to conan 2.0, but it appears that custom generators are not supported in conan 2.0. Looking at conans/client/loader.py, it requires all custom generators to be a subclass of conans.model.conan_file.Generator but the 2.0 generators (like VirtualRunEnv) don’t use that base class.

Presently, if a user wants to use this custom generator, then they run a command such as

conan install package/version@ -g my_generator

I asked on the slack page and was told that custom generators are planned to go away in conan 2.0. Is there a way to produce the above behavior in conan 2.0. I know that I could include a generator in the conanfile, but it is only needed in certain circumstances and its use is orthogonal to the recipe.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
memshardedcommented, Jul 10, 2022

I have added a test in https://github.com/conan-io/conan/pull/11605 to illustrate how this can be done. Basically, use Python flexibility:

from conan import ConanFile

        class MyGenerator:
            common = None
            def __init__(self, conanfile):
                self.conanfile = conanfile
            def generate(self):
                self.conanfile.output.info("VALUE TOOL: {}!!!".format(MyGenerator.common.mycommon()))

        class Tool(ConanFile):
            name = "tool"
            version = "0.1"
            python_requires = "common/0.1"
            def init(self):
                MyGenerator.common = self.python_requires["common"].module

We can put the python_require in the way it belongs, in the Tool(ConanFile), but make it available to different parts of the classes and functions to be reused.

1reaction
memshardedcommented, Jul 9, 2022

There is no explicit examples, but there exist docs for python_requires in https://docs.conan.io/en/latest/extending/python_requires.html.

The approach to use python_requires to implement a generator is basically (code not tested, just the idea):

  • Create a generator like class inside a python_require, something like:
    from conan import ConanFile
    class MyGenerator:
           def __init__(self, conanfile):
                   self._conanfile = conanfile
           def generate(self):
                   for dep in self._conanfile.dependencies....
                          ...
                   # save files to be used in the build
    class MyTool(ConanFile):
           name = "tool"
           version = "0.1"
    
  • Reuse it like:
    from conan import ConanFile
    class Pkg(ConanFile):
           python_requires = "tool/0.1"
           def generate(self):
                 generator = self.python_requires["tool"].module.MyGenerator(self)
                 generator.generate()
    
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create and share a custom generator ... - Conan Docs
How to create and share a custom generator with generator packages¶. There are several built-in generators, like cmake , visual_studio , xcode …...
Read more >
Trying to create & use a conan package: lib dir not set
I created a conan package for a 3rd party tool. ... be defined in Conan 2.0) generators = "CMakeDeps", "CMakeToolchain", "VirtualBuildEnv", ...
Read more >
Conan 2.0: Recipe Hacking - Is your favorite project missing ...
Conan is really great. It is probably the best package manager for C++. Especially the new CMakeDeps Generator works incredibly well.
Read more >
Conan Package Manager for C++ in Practice - YouTube
By Jerry Wiltse, presented at Core C++ [online] meetup, March 2021. The slides can be found at http://bit.ly/ConanDemo, more links to Conan ......
Read more >
Tweets with replies by conan.io (@conan_io) / Twitter
2.0's new custom commands are going to be a game changer for #cpp doing #DevOps pipelines ... #openapi #generator is now available on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found