conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.19k stars 977 forks source link

How to tell consumers to use specific generator? #17113

Open andrey-zherikov opened 1 week ago

andrey-zherikov commented 1 week ago

What is your question?

I have a package that requires certain generator to be used by consumers to work properly. How can I force consumers to use specific generator in addition to what they use in recipes?

Have you read the CONTRIBUTING guide?

memsharded commented 1 week ago

Hi @andrey-zherikov

This is not possible, packages cannot mandate to the consumers what build system should they use. The information needs to be abstracted in the package_info() method, and consumers decide how to use it.

Forcing a generator in consumers is also pointless if their build system won't use it, what problem are you trying to solve? What would be your generator doing?

andrey-zherikov commented 1 week ago

My package provides VS build tools. To use it, consumer should use VCVars generator because it calls vcvars.bat from my package which properly sets build environment.

Do you have any ideas how I can do this without explicitly listing VCVars generator in all consumers?

memsharded commented 1 week ago

My package provides VS build tools. To use it, consumer should use VCVars generator because it calls vcvars.bat from my package which properly sets build environment.

Quick question: The VCVars generator calls vswhere unless the tools.microsoft.msbuild:installation_path is defined, it is this conf being defined in your package conf_info?

I am afraid there is no direct way to force a consumer to call a generator. But a pragmatic and relatively simple possibility is to use a hook:

Please try that and let us know.

andrey-zherikov commented 1 week ago

My package provides VS build tools. To use it, consumer should use VCVars generator because it calls vcvars.bat from my package which properly sets build environment.

Quick question: The VCVars generator calls vswhere unless the tools.microsoft.msbuild:installation_path is defined, it is this conf being defined in your package conf_info?

Yes, it does this plus some environment tuning through self.buildenv_info:

def package_info(self):
    self.conf_info.define(
        "tools.microsoft.msbuild:installation_path", self.package_folder)
    )

But a pragmatic and relatively simple possibility is to use a hook

I'll give it a try and circle back to you.

Meantime, the issue IMHO is pretty straightforward. I'd like to provide a custom command that sets build environment (that basically what VCVars is doing - it adds a call to vcvars.bat). Right now the only way to set build env for consumers is through buildenv_info in a form of specific operations (set/unset/append/prepend) on a specific variables but there is no way to tell "call this .bat/.sh to setup build environment"

andrey-zherikov commented 1 week ago

But a pragmatic and relatively simple possibility is to use a hook:

  • A pre_generate() hook
  • The hook checks if your package is a tool_require (checking for example conanifile.dependencies.build
  • If it is a dependency, force the addition conanfile.generators.append("VCVars") or something like that

I tried this and was able to add VCVars generator but it doesn't work:

pkg/1.0: Generator 'VCVars' calling 'generate()'
ERROR: Error in generator 'VCVars': VS non-existing installation: Visual Studio 17. If using a non-default toolset from a VS IDE version consider specifying it with the 'tools.microsoft.msbuild:vs_version' conf

Although I have the following in profile (which is printed by conan create command:

tools.microsoft.msbuild:installation_path="C:/conan-work/.conan2/p/vs-bu61a1308f29b1c/p"
tools.microsoft.msbuild:vs_version=17
memsharded commented 1 week ago

I see there is a check that fails if the path doesn't exist. I think it is the extra quotes in the "tools.microsoft.msbuild:installation_path definition, you should remove them.

andrey-zherikov commented 4 days ago

It seems I messed up with CONAN_HOME dir so I re-tested from the scratch and can confirm that hook works. Here is basically the hook I used:

def pre_generate(conanfile):
    if "vs-buildtools" in conanfile.dependencies.build:
        conanfile.output.info("Adding VCVars generator")
        conanfile.generators.append("VCVars")

@memsharded Thank you for your help. Feel free to close this.

andrey-zherikov commented 1 day ago

@memsharded I see something is planned for 2.9 release here. Could you please share what's that?

memsharded commented 1 day ago

It is this PR: https://github.com/conan-io/conan/pull/17129