cmake-conan icon indicating copy to clipboard operation
cmake-conan copied to clipboard

Dependency environment variables not available in CMake

Open kramermt opened this issue 2 years ago • 1 comments

My project has dependencies which set env_info variables in their conanfile.py, which my application requires during generation in its CMakeLists.txt.

If using conan install from the command-line, I'd use a virtualenv and activate that before running cmake.

But using cmake-conan to install, I see no way to obtain/set these environment variables and make them available during CMake generation.

DepA conanfile.py:

    def package_info(self):
        self.env_info.DEPA_FOO = "bar"

App's installed conanbuildinfo.txt:

[ENV_depa]
DEPA_FOO=bar

App's CMakeLists.txt:

   conan_cmake_install(PATH_OR_REFERENCE ${CMAKE_SOURCE_DIR}/conanfile.py
                      INSTALL_FOLDER ${CONAN_INSTALL_FOLDER}
                      REMOTE internal-conan)
  include(${CONAN_INSTALL_FOLDER}/conan_paths.cmake)

  message(STATUS "ENV{DEPA_FOO}: $ENV{DEPA_FOO}") # DEPA_FOO not set!

Is there a known solution for this w/ cmake-conan? Or will our build need to rely on conan install & source activate.sh to satisfy this use case?

kramermt avatar Aug 04 '21 21:08 kramermt

Hi @kramermt, If you have to do this maybe you could try to use build modules. You could include a build module in the package that defines the variables you want for the consumers in CMake. You can have a look at the docs: https://docs.conan.io/en/latest/reference/conanfile/attributes.html#cpp-info also check the new set_property interface to define these attributes. Please tell me if this helps.

czoido avatar Aug 09 '21 08:08 czoido

I came accross the same issue as @kramermt.

My workaround was to create a CMake helper function that reads the generated conanbuildinfo.txt and exports the environment variables to the current process:

  • Read the conanbuildinfo.txt
  • Find all key=value lines in all the ENV_ sections
  • Use CMake's set(ENV{${KEY}} ${VALUE}) (or extend existing environment variables)

Does this make sense, or will it cause other issues down the line? Would it be useful to have this functionality as a function in cmake-conan? I can create a PR.

vobject avatar Sep 25 '23 13:09 vobject

Hi @vobject

I think this issue is a bit too legacy, and Conan got new mechanisms to do this better, mostly in the generate() method, and using the Environment new tools.

I am going to close this ticket as outdated, if you would like to discuss your case and the possible approaches, would you please mind opening a new ticket elaborating a bit more the problem at hand, the constraints and the approach? Many thanks!

memsharded avatar Sep 25 '23 14:09 memsharded