conan icon indicating copy to clipboard operation
conan copied to clipboard

[question] Is there a way to define information in a recipe that can be used in CMake (or other build systems potentially)

Open rconde01 opened this issue 1 year ago • 2 comments

What is your question?

I have a package which contains a python script that I want to call from my CMakeLists.txt. It lives in the bin directory within the package. In my CMakeLists.txt I would like to do something like:

Python3::Interpreter ${PATH_TO_MY_PYTHON_SCRIPT} ${some_args}

Is there any way to do this? The best I can find is to do something like mypackage_INCLUDE_DIR/../bin/myscript.py but that's pretty yuck.

Have you read the CONTRIBUTING guide?

  • [X] I've read the CONTRIBUTING guide

rconde01 avatar May 17 '24 20:05 rconde01

Hi @rconde01

If I understood correctly, you have a Python script in one of your dependencies, and you want to run that script from the CMakeLists.txt of a consumer of that dependency, that has a requires() or tool_requires to it?

Would the explicit way be good for your use case? Something like:

requires = "mydep/0.1"

def  generate(self):
    tc = CMakeToolchain(self)
    tc.variables["PATH_TO_MY_PYTHON_SCRIPT"] = os.path.join(self.dependencies["mydep"].cpp_info.bindir, "myscript.py")
    tc.generate()

memsharded avatar May 18 '24 10:05 memsharded

I did a slight variation which was:

class my_tool(ConanFile):
   def package_info(self):
      self.cpp_info.set_property(my_tool_py", join(self.package_folder, "bin", "my_tool.py"))
def generate(self):
   tc= CMakeToolchain(self)
   tc.variables["MY_TOOL_PATH"] = self.dependencies["my_tool"].cpp_info.get_property("my_tool_py")
   tc.generate()

which works well - except on windows the path ends up in the variable with unescaped backslashes. I can manually fix that up but maybe CMakeToolchain should be doing that?

rconde01 avatar May 18 '24 17:05 rconde01

Sorry I didn't follow up on this.

I was re-reading, isn't it enough that the dependency sets the PYTHONPATH in its self.buildenv, so the consumer doesn't need to inject anything, as it will be already in the PYTHONPATH?

memsharded avatar Jul 29 '24 15:07 memsharded