conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.29k stars 985 forks source link

[question] How to generate protobuf source-only library #7963

Open RonaldOlsthoorn opened 4 years ago

RonaldOlsthoorn commented 4 years ago

I'm trying to achieve the following: Creation of a proto repository, separated from it's clients. I use several languages, so the only true source code files of this repo are .proto definitions itself. Then use protoc to generate source files in C++, java, python etc. The generated files need to be packaged in whatever the language uses (jar/maven for java, wheels/pip for python and conan/cmake for c++). Generating the .pb.cc and .pb.h files is easy, both in python and cmake it can be done.

But then packaging it up such that it can be used in other conan based projects is harder. I either have to compile libraries for each used OS/architecture and upload it to our artifactory server. Or, I can regard the generated .pb.cc and .pb.h files as the output artifact and compile in the client application. This has my preference. When I search "source only conan" it brings me to the "header only" section. It's not the same, I think? How would I package a source-only and import it correctly using cmake find_package correctly?

malachib commented 4 years ago

I too am interested in this. I develop a multi-platform library which runs on more compilers than are usually installed. CMake-oriented.

As such, I'd prefer the consumer to compile the library rather than prebuilt.

jgallucci32 commented 3 years ago

I did exactly this for Go using the JFrog CLI and uploading the packages directory Artifactory. It works great and we can tag/version the generated code. I am trying to find an equivalent of this for Conan but as the OP pointed out Conan focuses heavily on building/compiling the packages. In the protobuf world you simply want the generated code as headers to be built with the code at compile time.

jgallucci32 commented 3 years ago

Ok looks like this method works the way we want (well for those using protobufs anyways). I followed the documentation here: https://docs.conan.io/en/latest/howtos/header_only.html

Basically what you need to do is create conanfile.py as instructed but add the *.cc files in addition to the header files. This assumes the *.pb.cc and *.pb.h files are at the root of the directory.

from conans import ConanFile

class GeneratedProtobufConan(ConanFile):
    name = "generatedprotobuf"
    version = "0.0.1"
    exports_sources = "*"
    no_copy_source = True

    def package(self):
        self.copy("*.h")
        self.copy("*.cc")

Once you have this file created you can create the new package by running

conan create .

Once the package is created you can upload to a package repository such as Artifactory

conan upload generatedprotobuf/0.0.1 --remote=artifactory