Using the same requirement as a requires and as a tool_requires

There are libraries which could behave as a library and as a tool requirement, e.g., protobuf Those libraries normally contains headers/sources of the library itself, and, perhaps, some extra tools (compilers, shell scripts, etc.). Both parts are used in different contexts, let’s think of this scenario using protobuf for instance:

  • I want to create a library which includes a compiled protobuf message. The protobuf compiler (build context) needs to be invoked at build time, and the library with the compiled .pb.cc file needs to be linked against the protobuf library (host context).

Given that, we should be able to use protobuf in build/host context in the same Conan recipe. Basically, your package recipe should look like:

  1. def requirements(self):
  2. self.requires("protobuf/3.18.1")
  3. def build_requirements(self):
  4. self.tool_requires("protobuf/<host_version>")

Note

The protobuf/<host_version> expression ensures that the same version of the library is used in both contexts. You can read more about it here.

This is the way to proceed with any other library used in both contexts. Nonetheless, let’s see a detailed example to see how the example looks like.

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

  1. git clone https://github.com/conan-io/examples2.git
  2. cd examples2/examples/graph/tool_requires/using_protobuf/myaddresser

The structure of the project is the following:

  1. ./
  2. ├── conanfile.py
  3. ├── CMakeLists.txt
  4. ├── addressbook.proto
  5. ├── apple-arch-armv8
  6. ├── apple-arch-x86_64
  7. └── src
  8. └── myaddresser.cpp
  9. └── include
  10. └── myaddresser.h
  11. └── test_package
  12. ├── conanfile.py
  13. ├── CMakeLists.txt
  14. └── src
  15. └── example.cpp

The conanfile.py looks like:

./conanfile.py

  1. from conan import ConanFile
  2. from conan.tools.cmake import CMake, cmake_layout
  3. class myaddresserRecipe(ConanFile):
  4. name = "myaddresser"
  5. version = "1.0"
  6. package_type = "library"
  7. settings = "os", "compiler", "build_type", "arch"
  8. options = {"shared": [True, False], "fPIC": [True, False]}
  9. default_options = {"shared": False, "fPIC": True}
  10. generators = "CMakeDeps", "CMakeToolchain"
  11. # Sources are located in the same place as this recipe, copy them to the recipe
  12. exports_sources = "CMakeLists.txt", "src/*", "include/*", "addressbook.proto"
  13. def config_options(self):
  14. if self.settings.os == "Windows":
  15. self.options.rm_safe("fPIC")
  16. def configure(self):
  17. if self.options.shared:
  18. self.options.rm_safe("fPIC")
  19. def requirements(self):
  20. self.requires("protobuf/3.18.1")
  21. def build_requirements(self):
  22. self.tool_requires("protobuf/<host_version>")
  23. def layout(self):
  24. cmake_layout(self)
  25. def build(self):
  26. cmake = CMake(self)
  27. cmake.configure()
  28. cmake.build()
  29. def package(self):
  30. cmake = CMake(self)
  31. cmake.install()
  32. def package_info(self):
  33. self.cpp_info.libs = ["myaddresser"]
  34. self.cpp_info.requires = ["protobuf::libprotobuf"]

As you can see, we’re using protobuf at the same time but in different contexts.

The CMakeLists.txt shows how this example uses protobuf compiler and library:

./CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.15)
  2. project(myaddresser LANGUAGES CXX)
  3. find_package(protobuf CONFIG REQUIRED)
  4. protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS addressbook.proto)
  5. add_library(myaddresser src/myaddresser.cpp ${PROTO_SRCS})
  6. target_include_directories(myaddresser PUBLIC include)
  7. target_include_directories(myaddresser PUBLIC
  8. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  9. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
  10. $<INSTALL_INTERFACE:include>
  11. )
  12. target_link_libraries(myaddresser PUBLIC protobuf::libprotobuf)
  13. set_target_properties(myaddresser PROPERTIES PUBLIC_HEADER "include/myaddresser.h;${PROTO_HDRS}")
  14. install(TARGETS myaddresser)

Where the library itself defines a simple myaddresser.cpp which uses the generated addressbook.pb.h header:

./src/myaddresser.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "addressbook.pb.h"
  5. #include "myaddresser.h"
  6. void myaddresser(){
  7. // Testing header generated by protobuf
  8. GOOGLE_PROTOBUF_VERIFY_VERSION;
  9. tutorial::AddressBook address_book;
  10. auto * person = address_book.add_people();
  11. person->set_id(1337);
  12. std::cout << "myaddresser(): created a person with id 1337\n";
  13. // Optional: Delete all global objects allocated by libprotobuf.
  14. google::protobuf::ShutdownProtobufLibrary();
  15. }

Finally, the test_package example simply calls the myaddresser() function to check that everything works correctly:

./test_package/src/example.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "myaddresser.h"
  5. int main(int argc, char* argv[]) {
  6. myaddresser();
  7. return 0;
  8. }

So, let’s see if it works fine:

  1. $ conan create . --build missing
  2. ...
  3. Requirements
  4. myaddresser/1.0#71305099cc4dc0b08bb532d4f9196ac1:c4e35584cc696eb5dd8370a2a6d920fb2a156438 - Build
  5. protobuf/3.18.1#ac69396cd9fbb796b5b1fc16473ca354:e60fa1e7fc3000cc7be2a50a507800815e3f45e0#0af7d905b0df3225a3a56243841e041b - Cache
  6. zlib/1.2.13#13c96f538b52e1600c40b88994de240f:d0599452a426a161e02a297c6e0c5070f99b4909#69b9ece1cce8bc302b69159b4d437acd - Cache
  7. Build requirements
  8. protobuf/3.18.1#ac69396cd9fbb796b5b1fc16473ca354:e60fa1e7fc3000cc7be2a50a507800815e3f45e0#0af7d905b0df3225a3a56243841e041b - Cache
  9. ...
  10. -- Install configuration: "Release"
  11. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/lib/libmyaddresser.a
  12. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/include/myaddresser.h
  13. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/include/addressbook.pb.h
  14. myaddresser/1.0: package(): Packaged 2 '.h' files: myaddresser.h, addressbook.pb.h
  15. myaddresser/1.0: package(): Packaged 1 '.a' file: libmyaddresser.a
  16. ....
  17. ======== Testing the package: Executing test ========
  18. myaddresser/1.0 (test package): Running test()
  19. myaddresser/1.0 (test package): RUN: ./example
  20. myaddresser(): created a person with id 1337

After seeing it’s running OK, let’s try to use cross-building. Notice that this part is based on MacOS Intel systems, and cross-compiling for MacOS ARM ones, but you could use your own profiles depending on your needs for sure.

Warning

MacOS system is required to run this part of the example.

  1. $ conan create . --build missing -pr:b apple-arch-x86_64 -pr:h apple-arch-armv8
  2. ...
  3. -- Install configuration: "Release"
  4. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/lib/libmyaddresser.a
  5. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/include/myaddresser.h
  6. -- Installing: /Users/myuser/.conan2/p/b/myser03f790a5a5533/p/include/addressbook.pb.h
  7. myaddresser/1.0: package(): Packaged 2 '.h' files: myaddresser.h, addressbook.pb.h
  8. myaddresser/1.0: package(): Packaged 1 '.a' file: libmyaddresser.a
  9. ....
  10. ======== Testing the package: Executing test ========
  11. myaddresser/1.0 (test package): Running test()

Now, we cannot see the example running because of the host architecture. If we want to check that the example executable is built for the correct one:

  1. $ file test_package/build/apple-clang-13.0-armv8-gnu17-release/example
  2. test_package/build/apple-clang-13.0-armv8-gnu17-release/example: Mach-O 64-bit executable arm64

Everything works as expected, and the executable was built for 64-bit executable arm64 architectures.