conan-io / conan

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

[question] How to generate no configuration suffix when using CMakeDeps? #12222

Open uilianries opened 2 years ago

uilianries commented 2 years ago

When consuming targets and CMake variables generated by CMakeDeps, it generates all variables with a suffix based on its configuration:

https://github.com/conan-io/conan/blob/47353ab2e38b7cf5638dfce26d1fd2640eb2775e/conan/tools/cmake/cmakedeps/templates/__init__.py#L78

@property
def suffix(self):
    if not self.conanfile.is_build_context:
        return ""
    return self.cmakedeps.build_context_suffix.get(self.conanfile.ref.name, "")

As result, it regenerates variables like FOO_LIBS_RELEASE and foo_COMPILE_DEFINITIONS_RELEASE.

The question is, how to generate only FOO_LIBS (without build type suffix)?

We could try a workaround, forcing the CMakeDeps configuration, but fails:

def generate(self):
    deps = CMakeDeps(self)
    deps.configuration = ""
    deps.generate()
conan create . foo/0.1@
ERROR: Traceback (most recent call last):
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conans/errors.py", line 34, in conanfile_exception_formatter
    yield
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conans/client/generators/__init__.py", line 253, in write_toolchain
    conanfile.generate()
  File "/Users/uilian/.conan/data/foo/0.1/_/_/export/conanfile.py", line 16, in generate
    tc.generate()
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conan/tools/cmake/cmakedeps/cmakedeps.py", line 46, in generate
    generator_files = self.content
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conan/tools/cmake/cmakedeps/cmakedeps.py", line 88, in content
    self._generate_files(require, dep, ret, find_module_mode=False)
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conan/tools/cmake/cmakedeps/cmakedeps.py", line 101, in _generate_files
    ret[data_target.filename] = data_target.render()
  File "/Users/uilian/.pyenv/versions/3.10.4/lib/python3.10/site-packages/conan/tools/cmake/cmakedeps/templates/target_data.py", line 20, in filename
    data_fname += "{}-{}".format(self.file_name, self.configuration.lower())
AttributeError: 'NoneType' object has no attribute 'lower'

Some projects use LIBS instead of LIBRARIES and have been working so far, and forcing to patch only because Conan uses target multi feature sounds a bit aggressive.

/cc @ericLemanissier

ericLemanissier commented 2 years ago

Thanks for the report. cf https://github.com/conan-io/conan-center-index/pull/13228 for an example of actual failure caused by this issue

jcar87 commented 2 years ago

Hi there! Thanks for reporting this.

I am trying to replicate this on my end, but I see the "unsuffixed" variables being generated.

With the following conanfile.txt:

[requires]
zlib/1.2.12
opencv/4.5.5

[generators]
CMakeDeps
CMakeToolchain

After running conan install on 1.53.0, I get:

ZLIBConfig.cmake:

set(ZLIB_VERSION_STRING "1.2.12")
set(ZLIB_INCLUDE_DIRS ${zlib_INCLUDE_DIRS_RELEASE} )
set(ZLIB_INCLUDE_DIR ${zlib_INCLUDE_DIRS_RELEASE} )
set(ZLIB_LIBRARIES ${zlib_LIBRARIES_RELEASE} )
set(ZLIB_DEFINITIONS ${zlib_DEFINITIONS_RELEASE} )

OpenCVConfig.cmake:

set(OpenCV_VERSION_STRING "4.5.5")
set(OpenCV_INCLUDE_DIRS ${opencv_INCLUDE_DIRS_RELEASE} )
set(OpenCV_INCLUDE_DIR ${opencv_INCLUDE_DIRS_RELEASE} )
set(OpenCV_LIBRARIES ${opencv_LIBRARIES_RELEASE} )
set(OpenCV_DEFINITIONS ${opencv_DEFINITIONS_RELEASE} )

`OpenSSLConfig.cmake:

set(OpenSSL_VERSION_STRING "1.1.1q")
set(OpenSSL_INCLUDE_DIRS ${openssl_INCLUDE_DIRS_RELEASE} )
set(OpenSSL_INCLUDE_DIR ${openssl_INCLUDE_DIRS_RELEASE} )
set(OpenSSL_LIBRARIES ${openssl_LIBRARIES_RELEASE} )
set(OpenSSL_DEFINITIONS ${openssl_DEFINITIONS_RELEASE} )

I can see these variables pretty much in all generated xxConfig.cmake files. Is there anything I'm missing?

ericLemanissier commented 2 years ago

The variables related each component do not have a suffix-less alternative, eg OpenSSL_Crypto_LIBS, OpenSSL_Crypto_DEPENDENCIES, OpenSSL_Crypto_FRAMEWORKS and OpenSSL_Crypto_SYSTEM_LIBS

jcar87 commented 2 years ago

The variables related each component do not have a suffix-less alternative

Specifically, which variables would need to be defined that currently aren't?

ericLemanissier commented 2 years ago

Sorry, I edited my message at the same time you sent your question. we got a race condition here 😉 missing variables are OpenSSL_Crypto_LIBS, OpenSSL_Crypto_DEPENDENCIES, OpenSSL_Crypto_FRAMEWORKS and OpenSSL_Crypto_SYSTEM_LIBS

jcar87 commented 2 years ago

Sorry, I edited my message at the same time you sent your question. we got a race condition here 😉 missing variables are OpenSSL_Crypto_LIBS, OpenSSL_Crypto_DEPENDENCIES, OpenSSL_Crypto_FRAMEWORKS and OpenSSL_Crypto_SYSTEM_LIBS

Hehe, it happens.

Perfect, I think we have enough information to look closer into this one. thanks!! :)

ericLemanissier commented 2 years ago

friendly ping @jcar87 What is the status of this issue ?

ericLemanissier commented 2 years ago

re-ping @jcar87 do you need more information ?