In my last post, I showed how I got my feet wet while migrating the dependencies of my existing code-base to conan. The first major hurdle I saw coming when I started was adding something with a “special” build step, e.g. something like source-preprocessing. In my case, this was protobuf, where a special build-step converts .proto files to sources and headers.
In my previous solution, my devenv build scripts would install the protobuf converter binary to my devenv’s bin/ folder, which I then used to run my preprocessing. At first, it was not obvious how to do this with conan. It turns out that the lovely people and bincrafters made this pretty comfortable. conan_basic_setup() will add all required package paths to your CMAKE_MODULE_PATH, which you can use to include() some bundled CMake scripts that will either let you execute the protobuf-compiler via a target or run protobuf_generate to automagically handle the preprocessing. It’s probably worth noting, that this really depends on how the package is made. Conan does not really have an official way on how to handle this.
Let’s start with some sample code – Person.proto, like the sample from the protobuf website:
message Person { required string name = 1; required int32 id = 2; optional string email = 3; }
And some sample code that uses it:
#include "Person.pb.h" int main(int argn, char** argv) { Person message; message.set_name("Hello Protobuf"); std::cout << message.name() << std::endl; }
Again, we’re using the bincrafters repository for our dependencies in a conanfile.txt
:
[requires] protobuf/3.6.1@bincrafters/stable protoc_installer/3.6.1@bincrafters/stable [options] [generators] cmake
Now we just need to wire it all up in the CMakeLists.txt
cmake_minimum_required(VERSION 3.0) project(ProtobufTest) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup(TARGETS KEEP_RPATHS) # This loads the cmake/protoc-config.cmake file # from the protoc_installer dependency include(cmake/protoc-config) set(TARGET_NAME ProtobufSample) # Just add the .proto files to the target add_executable(${TARGET_NAME} Person.proto ProtobufTest.cpp ) # Let this function to the magic protobuf_generate(TARGET ${TARGET_NAME}) # Need to use protobuf, of course target_link_libraries(${TARGET_NAME} PUBLIC CONAN_PKG::protobuf ) # Make sure we can find the generated headers target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} )
There you have it! Pretty neat, and all without a brittle find_package
call.
Hi I’m a conan newbie. I don’t really get how i am supposed to include the cmake/protoc-config directory since, following your tutorial step by step, it doesnt appear into my project afetr conan install