conan-io / conan

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

[question] buildenv_info and generate method #14151

Open maitrey opened 1 year ago

maitrey commented 1 year ago

What is your question?

Dear Conan Folks,

I am using conan-2.0.6 and in the context of Windows. For one of my packages, that is a tool , I set the environment variable in the package_info method:

def package_info(self):
        self.buildenv_info.define_path("TOOLS_FORMATS", os.path.join(self.package_folder))

In the consumer package , I would like to access the above variable in the generate method as I have to copy it to a certain location. What is the way to fulfill this use case? I can see the variable set in the consumer package in the conanbuildenv.bat but how to get the contents in order to copy to a desired location in the generate method ?

Have you read the CONTRIBUTING guide?

memsharded commented 1 year ago

Hi @maitrey

The idea of using env-vars and buildenv_info is that such information is intended to be from Conan (recipes, profile) to other tools (via environment scripts). It is not a Conan -> Conan communication, and not intended to be read as a value directly from other Conan recipes. I am not saying it is not possible, but maybe that it is not optimal.

So some questions:

maitrey commented 1 year ago

Hi @memsharded , Thanks for your reply. We have some very old legacy tools based on python. These are about 20 in number. They have one common module that is shared across all the other 19 repositories. Since they are legacy they expect a certain folder structure before the actual build of the tool starts. So the idea was we put the common module in a conan package and using copy in generate method put it into the desired folder location. If I use it like this:

requires = "tools_formats/0.0.0-dev.0@autosar/featuretest"

def generate(self):
        formats = self.dependencies["tools_formats"]
        print ("dependencies are:", self.dependencies.values(), formats.package_folder)
        for deps in self.dependencies.values():
            print("deps is :", deps)
            copy(self, "*.pyc", formats.package_folder, os.path.join(self.build_folder, "formats"))

What do you think about the approach itself? Is there a better way of solving it ?

memsharded commented 1 year ago

Hi @maitrey

I think there is a problem with this approach. It seems you are modifying a dependency folder, the tools_formats package is being modified after it has been created. This is forbidden behavior, once a package is created it must remain immutable. Modifying its package_folder after it has been created can result in any unexpected error or undefined behavior.

You can copy things to your current self.generators_folder, and use things from there, but it is not possible to copy things to the package_folder of a depedency. With the self.generators_folder you can proactively re-package all tools in one for convenience of usage, maybe this is what you need?

maitrey commented 1 year ago

Hi @memsharded I think you got me wrong. I am copying something from package_folder and not the other way round. This is only due to legacy tools.

memsharded commented 1 year ago

Oh, you are right, I misread the copy() (this is why I tend to put the named args explicitly copy(self, "*.pyc", src=formats.package_folder, dst=os.path.join(self.build_folder, "formats"))

Then, that would be fine, it is copying the files from dependencies into its current build_folder/formats. Yes, this is a valid thing (you need to be careful later in the package() method to not re-package those copied binaries if that is not what you want, but otherwise it is good)

maitrey commented 1 year ago

Thanks James for replying. When I use the formats package in the consumer repo , I do the following steps:

conan install .
conan build . --user autosar --channel featurepoc
conan export-pkg .

When I run a conan upload after this step, it cannot find the consumer package even though it is exported in the cache. Could you please help?

memsharded commented 1 year ago

In general, please try to provide more detauls, like:

It this particular case, it seems that you are missing the conan export-pkg . --user=autosar --channel=featurepoc. If that is the package that you built locally, I guess that you want to export that one too?

Note, if you are using Conan 2.0, the first conan install is redundant. conan build is internally doing that already.