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

How to tell consumers to use specific generator? #17113

Open
1 task done
andrey-zherikov opened this issue Oct 4, 2024 · 9 comments · May be fixed by #17129
Open
1 task done

How to tell consumers to use specific generator? #17113

andrey-zherikov opened this issue Oct 4, 2024 · 9 comments · May be fixed by #17129
Assignees
Milestone

Comments

@andrey-zherikov
Copy link

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?

  • I've read the CONTRIBUTING guide
@memsharded memsharded self-assigned this Oct 4, 2024
@memsharded
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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:

  • 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

Please try that and let us know.

@andrey-zherikov
Copy link
Author

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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.

@memsharded memsharded added this to the 2.9.0 milestone Oct 8, 2024
@andrey-zherikov
Copy link
Author

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

@memsharded
Copy link
Member

It is this PR: #17129

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

Successfully merging a pull request may close this issue.

2 participants