conan icon indicating copy to clipboard operation
conan copied to clipboard

[bug] Unable to use the editable mode with cmake component target

Open MamaTierra opened this issue 6 months ago • 1 comments

Describe the bug

I want to use the editable mode with cmake component targets (ie: say1 and say2) instead of global target (say::say). Currently I'm not able to do this. Either I'm missing something or there is a bug.

My Setup: ❯ conan version version: 2.6.0 conan_path: /home/pierre/.local/bin/conan python version: 3.8.19 sys_version: 3.8.19 (default, Mar 25 2024, 21:14:39) [GCC 13.2.1 20230801] sys_executable: /usr/bin/python3.8 is_frozen: False architecture: x86_64 system version: #1 SMP PREEMPT_DYNAMIC Wed Aug 14 17:17:23 UTC 2024 platform: Linux-6.6.46-1-MANJARO-x86_64-with-glibc2.34 system: Linux release: 6.6.46-1-MANJARO cpu:

How to reproduce it

For the example I followed the procedure described in https://docs.conan.io/2/tutorial/developing_packages/editable_packages.html#building-editable-dependencies and modified a bit to use component target.

What is working for me:

1: The example for docs.conan.io

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/tutorial/developing_packages/editable_packages
$ conan editable add say
$ conan build hello --build=editable

2: The example from docs.conan.io + add a lib

Same commands + following modification also works

  • src/say2.cpp
#include <iostream>
#include "say2.h"

void say2(){
    #ifdef NDEBUG
    std::cout << "@@@@ say/1.0: Hello World Release!\n";
    #else
    std::cout << "say/1.0: Hello World Debug!\n";
    #endif
}
  • include/say2.h
#pragma once

#ifdef WIN32
  #define say_EXPORT __declspec(dllexport)
#else
  #define say_EXPORT
#endif

say_EXPORT void say2();
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(say CXX)

add_library(say1 src/say.cpp)
target_include_directories(say1 PUBLIC include)
install(TARGETS say1)

add_library(say2 src/say2.cpp)
target_include_directories(say2 PUBLIC include)
install(TARGETS say2)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include)
  • conanfile.py
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout


class SayConan(ConanFile):
    name = "say"
    version = "1.0"

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True}

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*", "include/*"

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

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

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.libs = ["say1", "say2"]

3: (what is working#2) + test_package + disable editable mode

Following the consumer example of hello lib from the example, I am able to create a package of say with a test package

  • conanfile.py
...
    def package_info(self):
        self.cpp_info.components["say1"].set_property("cmake_target_name", "say1")
        self.cpp_info.components["say1"].libs = ["say1"]
        self.cpp_info.components["say2"].set_property("cmake_target_name", "say2")
        self.cpp_info.components["say2"].libs = ["say2"]
  • test_package/CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)

find_package(say CONFIG REQUIRED)

add_executable(example src/example.cpp)
target_link_libraries(example say1 say2)
  • test_package/example.cpp
#include "say.h"
#include "say2.h"

int main() {
    say();
    say2();
}

Run this command

$ conan editable remove say
$ conan create say

give me this

======== Testing the package: Executing test ========
say/1.0 (test package): Running test()
say/1.0 (test package): RUN: ./example
say/1.0: Hello World Release!
@@@@ say/1.0: Hello World Release!

What is not working for me:

1: (what is working#3) + enable editable mode

Run these commands

$ conan editable add say
$ conan create say

give me this

======== Testing the package: Building ========
say/1.0 (test package): Calling build()
say/1.0 (test package): Running CMake.configure()
say/1.0 (test package): RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/tmp/examples2/tutorial/developing_packages/editable_packages/say/test_package" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/tmp/examples2/tutorial/developing_packages/editable_packages/say/test_package"
-- Using Conan toolchain: /tmp/examples2/tutorial/developing_packages/editable_packages/say/test_package/build/gcc-14-x86_64-gnu17-release/generators/conan_toolchain.cmake
-- Conan toolchain: Defining architecture flag: -m64
-- Conan toolchain: C++ Standard 17 with extensions ON
-- The CXX compiler identification is GNU 14.2.1
-- 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
-- Conan: Component target declared 'say1'
-- Conan: Component target declared 'say2'
-- Conan: Target declared 'say::say'
CMake Error at build/gcc-14-x86_64-gnu17-release/generators/cmakedeps_macros.cmake:67 (message):
  Library 'say2' not found in package.  If 'say2' is a system library,
  declare it with 'cpp_info.system_libs' property
Call Stack (most recent call first):
  build/gcc-14-x86_64-gnu17-release/generators/say-Target-release.cmake:23 (conan_package_library_targets)
  build/gcc-14-x86_64-gnu17-release/generators/sayTargets.cmake:24 (include)
  build/gcc-14-x86_64-gnu17-release/generators/say-config.cmake:16 (include)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!

ERROR: say/1.0 (test package): Error in build() method, line 17
        cmake.configure()
        ConanException: Error 1 while executing

2: (what is not working#1) + build hello

In a similar way, this command failed with the same message

conan build hello --build=editable

give

======== Calling build() ========
conanfile.py (hello/1.0): Calling build()
conanfile.py (hello/1.0): Running CMake.configure()
conanfile.py (hello/1.0): RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/tmp/examples2/tutorial/developing_packages/editable_packages/hello" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/tmp/examples2/tutorial/developing_packages/editable_packages/hello"
-- Using Conan toolchain: /tmp/examples2/tutorial/developing_packages/editable_packages/hello/build/Release/generators/conan_toolchain.cmake
-- Conan toolchain: Defining architecture flag: -m64
-- Conan toolchain: C++ Standard 17 with extensions ON
-- Conan: Component target declared 'say1'
-- Conan: Component target declared 'say2'
-- Conan: Target declared 'say::say'
CMake Error at build/Release/generators/cmakedeps_macros.cmake:67 (message):
  Library 'say2' not found in package.  If 'say2' is a system library,
  declare it with 'cpp_info.system_libs' property
Call Stack (most recent call first):
  build/Release/generators/say-Target-release.cmake:23 (conan_package_library_targets)
  build/Release/generators/sayTargets.cmake:24 (include)
  build/Release/generators/say-config.cmake:16 (include)
  CMakeLists.txt:5 (find_package)

MamaTierra avatar Aug 23 '24 16:08 MamaTierra