protobuf_generate_cpp and fetchedconttent

Is it possible to FetchContent_Declare and FetchContent_MakeAvailable a Google protocol buffers version, then have find_package(Protobuf) use the resulting built protoc and libraries? If I force find_package to use the path to the built protoc, it says I need an alias. But, the lib and protoc are present and available.

It sounds like protobuf isn’t set up to expose the right API for FetchContent (not all projects are prepared to be add_subdirectory into another project). I’m not sure what else would be needed on their side exactly.

Cc: @craig.scott

find_package() usually requires things to have been built, but that hasn’t happened yet during the first configure run. If you use FetchContent to pull in protobuf, then you can’t use find_package() on it like that. Also, as you’ve discovered, you’ll probably end up with conflicting target names, since find_package() assumes it is adding the libraries rather than using the ones that will come in as a result of FetchContent pulling in protobuf to be built with the project.

Note that there is a proposal which would probably make things work like how you hope, but I haven’t had the time to push it forward yet.

Actually, can you just skip the call to find_package()? If FetchContent is already pulling in the project and it is providing the targets you need, the call to find_package() should be unnecessary (unless only some of the required targets are provided - I’m not familiar with the details of this particular dependency).

Thank you Craig and Ben. Craig, that is a great suggestion, and where I ended up. I skip find_package, and reach down into the protobuf_BINARY_DIR for the protoc and libprotobuf.a it built, and down into protobuf_SOURCE_DIR for the necessary includes. Then I construct an add_custom_command to invoke protoc for my proto compile. It’s just that find_package(Protobuf) / protobuf_generate_cpp could do the same thing and be much less verbose than the mess I now have :frowning:

I think upstream might have protobuf_generate(LANGS) now if you can find where they stuff the API in the repo.