Using components and editable packages

It is possible to define components in the layout() method, to support the case of editable packages. That is, if we want to put a package in editable mode, and that package defines components, it is necessary to define the components layout correctly in the layout() method. Let’s see it in a real example.

Please, first clone the sources to recreate this project. You can find them in the examples2 repository in GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/examples/conanfile/layout/editable_components

There we find a greetings subfolder and package, that contains 2 libraries, the hello library and the bye library. Each one is modeled as a component inside the package recipe:

greetings/conanfile.py

  1. class GreetingsConan(ConanFile):
  2. name = "greetings"
  3. version = "0.1"
  4. settings = "os", "compiler", "build_type", "arch"
  5. generators = "CMakeDeps", "CMakeToolchain"
  6. exports_sources = "src/*"
  7. def build(self):
  8. cmake = CMake(self)
  9. cmake.configure()
  10. cmake.build()
  11. def layout(self):
  12. cmake_layout(self, src_folder="src")
  13. # This "includedirs" starts in the source folder, which is "src"
  14. # So the components include dirs is the "src" folder (includes are
  15. # intended to be included as ``#include "hello/hello.h"``)
  16. self.cpp.source.components["hello"].includedirs = ["."]
  17. self.cpp.source.components["bye"].includedirs = ["."]
  18. # compiled libraries "libdirs" will be inside the "build" folder, depending
  19. # on the platform they will be in "build/Release" or directly in "build" folder
  20. bt = "." if self.settings.os != "Windows" else str(self.settings.build_type)
  21. self.cpp.build.components["hello"].libdirs = [bt]
  22. self.cpp.build.components["bye"].libdirs = [bt]
  23. def package(self):
  24. copy(self, "*.h", src=self.source_folder,
  25. dst=join(self.package_folder, "include"))
  26. copy(self, "*.lib", src=self.build_folder,
  27. dst=join(self.package_folder, "lib"), keep_path=False)
  28. copy(self, "*.a", src=self.build_folder,
  29. dst=join(self.package_folder, "lib"), keep_path=False)
  30. def package_info(self):
  31. self.cpp_info.components["hello"].libs = ["hello"]
  32. self.cpp_info.components["bye"].libs = ["bye"]
  33. self.cpp_info.set_property("cmake_file_name", "MYG")
  34. self.cpp_info.set_property("cmake_target_name", "MyGreetings::MyGreetings")
  35. self.cpp_info.components["hello"].set_property("cmake_target_name", "MyGreetings::MyHello")
  36. self.cpp_info.components["bye"].set_property("cmake_target_name", "MyGreetings::MyBye")

While the location of the hello and bye libraries in the final package is in the final lib folder, then nothing special is needed in the package_info() method, beyond the definition of the components. In this case, the customization of the CMake generated filenames and targets is also included, but it is not necessary for this example.

The important part is the layout() definition. Besides the common cmake_layout, it is necessary to define the location of the components headers (self.cpp.source as they are source code) and the location of the locally built libraries. As the location of the libraries depends on the platform, the final self.cpp.build.components["component"].libdirs depends on the platform.

With this recipe we can put the package in editable mode and locally build it with:

  1. $ conan editable add greetings
  2. $ conan build greetings
  3. # we might want to also build the debug config

In the app folder we have a package recipe to build 2 executables, that link with the greeting package components. The app/conanfile.py recipe there is simple, the build() method builds and runs both example and example2 executables that are built with CMakeLists.txt:

  1. # Note the MYG file name, not matching the package name,
  2. # because the recipe defined "cmake_file_name"
  3. find_package(MYG)
  4. add_executable(example example.cpp)
  5. # Note the MyGreetings::MyGreetings target name, not matching the package name,
  6. # because the recipe defined "cmake_target_name"
  7. # "example" is linking with the whole package, both "hello" and "bye" components
  8. target_link_libraries(example MyGreetings::MyGreetings)
  9. add_executable(example2 example2.cpp)
  10. # "example2" is only using and linking "hello" component, but not "bye"
  11. target_link_libraries(example2 MyGreetings::MyHello)
  1. $ conan build app
  2. hello: Release!
  3. bye: Release!

If you now go to the bye.cpp source file and modify the output message, then build greetings and app locally, the final output message for the “bye” component library should change:

  1. $ conan build greetings
  2. $ conan build app
  3. hello: Release!
  4. adios: Release!