conan: [question] CMakeDeps and layout() creates but wrong test-package

This is a followup to #9937:

I have the following conanfile.py

import os
import sys
from conans import ConanFile, tools
from conan.tools.cmake import CMakeDeps, CMakeToolchain, CMake
from conans.tools import Version
from conan.tools.layout import cmake_layout

import cg_conan as cg  # noqa: E402

class BaseLibrary(ConanFile):
  name = "base"

  description = """This is a test project with a library base::io
    and base::math and an executable cli."""

  license = "MIT"
  url = "..."
  homepage = "..."
  default_options = {"fmt:shared": True}
  build_policy = "missing"  # if this package is build by default if missing.
  settings = "os", "compiler", "build_type", "arch"

  scm = {
      "type":
          "git",
      "subfolder":
          "repo",
      "url":
          "https://bitbucket.dentsplysirona.com/scm/devops/cmake-general.git",
      "revision":
          "auto",
      "shallow":
          True
  }

  apply_env = True

  def set_version(self):
    self.version = cg.version.get_version_from_git_tag(self)

  def requirements(self):
    if Version(self.version) >= "0.0.0":
      self.requires("fmt/8.0.1")

  def build_requirements(self):
    if Version(self.version) >= "0.0.0":
      self.build_requires("catch2/2.13.7")
      self.build_requires("gtest/1.11.0")

  def configure(self):
    cg.assert_compiler_and_stdlib_match(self)

  def layout(self):
    # https://docs.conan.io/en/latest/reference/conanfile/methods.html#layout
    cmake_layout(self)
    self.folders.source = "repo/tests/project"

  def generate(self):
    tc = CMakeToolchain(self)
    tc.variables["GENERAL_BUILD_LIBS"] = True
    tc.variables["GENERAL_BUILD_TOOLS"] = True
    tc.variables["GENERAL_BUILD_TESTS"] = False
   tc.generate()

    deps = CMakeDeps(self)
    # When you have a build-require, by default,
    # the config files (xxx-config.cmake) files are not generated.
    # https://docs.conan.io/en/latest/reference/conanfile/tools/cmake/cmakedeps.html#build-context-activated
    deps.build_context_activated = ["catch2", "gtest"]
    deps.generate()

  def build(self):
    cmake = CMake(self)
    cmake.configure()
    cmake.build()
    cmake.install()

  def package(self):
    cmake = CMake(self)
    cmake.install()
    tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
    tools.rmdir(os.path.join(self.package_folder, "share", "base", "cmake"))

  def package_info(self):
    self.cpp_info.name = self.name

    debugSuffix = ""
    if self.settings.build_type == "Debug":
      debugSuffix = "-dbg"

    # libs = tools.collect_libs(self)
    # print(libs)

    compMath = "math"
    dllMath = f"{self.name}-{compMath}{debugSuffix}"
    self.cpp_info.components[compMath].libs = [dllMath]
    self.cpp_info.components[compMath].requires = ["fmt::fmt"]

    compIo = f"io"
    dllIO = f"{self.name}-{compIo}{debugSuffix}"
    self.cpp_info.components[compIo].libs = [dllIO]
    self.cpp_info.components[compIo].requires = ["fmt::fmt", compMath]

When I create the package for this by

 conan create \
    -pr:b "clang-default" \
    -pr:h "clang-default" \
    -s build_type=Debug \
    --test-folder tests/package \
    --build=missing \
    . demo/testing

It successfully creates the package and testing it with test/package also works. The tests/package/build folder contains a base-debug-x86_64-data.cmake with e.g. an “io” component:

########### COMPONENT math VARIABLES #############################################
set(base_math_INCLUDE_DIRS_DEBUG "${base_PACKAGE_FOLDER_DEBUG}/include")
set(base_math_LIB_DIRS_DEBUG "${base_PACKAGE_FOLDER_DEBUG}/lib")
set(base_math_RES_DIRS_DEBUG "${base_PACKAGE_FOLDER_DEBUG}/res")
set(base_math_DEFINITIONS_DEBUG )
set(base_math_OBJECTS_DEBUG )
set(base_math_COMPILE_DEFINITIONS_DEBUG )
set(base_math_COMPILE_OPTIONS_C_DEBUG "")
set(base_math_COMPILE_OPTIONS_CXX_DEBUG "")
set(base_math_LIBS_DEBUG base-math-dbg)
set(base_math_SYSTEM_LIBS_DEBUG )
set(base_math_FRAMEWORK_DIRS_DEBUG "${base_PACKAGE_FOLDER_DEBUG}/Frameworks")
set(base_math_FRAMEWORKS_DEBUG )
set(base_math_DEPENDENCIES_DEBUG fmt::fmt)
set(base_math_LINKER_FLAGS_DEBUG
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>
)

Which is correct!

However as I introduce the layout() override to specify where my conan files should go (e.g. build/conan instead of .) :

  • uncomment the layout() function and set sources_folder="."

Then testing the created package fails becaue the base-debug-x86_64-data.cmake contains now:

########### COMPONENT math VARIABLES #############################################
set(base_math_INCLUDE_DIRS_DEBUG )
set(base_math_LIB_DIRS_DEBUG )
set(base_math_RES_DIRS_DEBUG )
set(base_math_DEFINITIONS_DEBUG )
set(base_math_OBJECTS_DEBUG )
set(base_math_COMPILE_DEFINITIONS_DEBUG )
set(base_math_COMPILE_OPTIONS_C_DEBUG "")
set(base_math_COMPILE_OPTIONS_CXX_DEBUG "")
set(base_math_LIBS_DEBUG base-math-dbg)
set(base_math_SYSTEM_LIBS_DEBUG )
set(base_math_FRAMEWORK_DIRS_DEBUG )
set(base_math_FRAMEWORKS_DEBUG )
set(base_math_DEPENDENCIES_DEBUG fmt::fmt)
set(base_math_LINKER_FLAGS_DEBUG
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>
        $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>
)

which is somewhat wrong which reports as :

base/v1.1.1-9-g237fdd66df-dirty@demo/testing: Already installed!
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Generator txt created conanbuildinfo.txt
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Generator 'CMakeDeps' calling 'generate()'
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Generator 'CMakeToolchain' calling 'generate()'
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Aggregating env generators
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Generated conaninfo.txt
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Generated graphinfo
Using lockfile: '/workspaces/cmake-general/tests/project/tests/package/build/8a0f14e3d7eea6ad8888100be528c014879dab53/conan.lock'
Using cached profile from lockfile
base/v1.1.1-9-g237fdd66df-dirty@demo/testing (test package): Calling build()
-- Cmake-General: /workspaces/cmake-general
-- The C compiler identification is Clang 12.0.1
-- The CXX compiler identification is Clang 12.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Cmake-General: Compiler cache '/usr/bin/ccache' found and enabled.
-- Cmake-General: Downloading 'conan.cmake' from 'https://raw.githubusercontent.com/gabyx/cmake-conan/patch-1/conan.cmake'.
-- Conan: Component target declared 'base::math'
-- Conan: Component target declared 'base::io'
-- Conan: Target declared 'base::base'
CMake Error at build/8a0f14e3d7eea6ad8888100be528c014879dab53/cmakedeps_macros.cmake:4 (message):
  Library 'base-io-dbg' not found in package.  If 'base-io-dbg' is a system
  library, declare it with 'cpp_info.system_libs' property
Call Stack (most recent call first):
  build/8a0f14e3d7eea6ad8888100be528c014879dab53/cmakedeps_macros.cmake:48 (conan_message)
  build/8a0f14e3d7eea6ad8888100be528c014879dab53/base-Target-debug.cmake:21 (conan_package_library_targets)
  build/8a0f14e3d7eea6ad8888100be528c014879dab53/baseTargets.cmake:26 (include)
  build/8a0f14e3d7eea6ad8888100be528c014879dab53/base-config.cmake:11 (include)
  CMakeLists.txt:25 (find_package)

*Also the generated files in the tests/package test-folder are not inside test/8a0f14e3d7eea6ad8888100be528c014879dab53/conan (I guess thats how testing a package works, why is it so different?`:

image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Also,

  • you are doing tc.variables["GENERAL_BUILD_LIBS"] = True after the generation. It should be defined before tc.generate()
  • self.build_requires("catch2/2.13.7") => self.build_requires("catch2/2.13.7", force_host_context=True). This is important if you are going to use the 2 profiles (which is recommended)