|
| 1 | +--- |
| 2 | +# Automatically generated from tests/protobuf/cmake.toml - DO NOT EDIT |
| 3 | +layout: default |
| 4 | +title: Demonstrates protobuf integration with cmkr custom commands |
| 5 | +permalink: /examples/protobuf |
| 6 | +parent: Examples |
| 7 | +nav_order: 14 |
| 8 | +--- |
| 9 | + |
| 10 | +# Demonstrates protobuf integration with cmkr custom commands |
| 11 | + |
| 12 | +This test demonstrates using protobuf with cmkr for code generation. |
| 13 | + |
| 14 | +```toml |
| 15 | +# |
| 16 | +# The example: |
| 17 | +# 1. Fetches protobuf from GitHub using FetchContent |
| 18 | +# 2. Uses a custom command to generate C++ sources from a .proto file |
| 19 | +# 3. Links an executable against the generated sources and protobuf library |
| 20 | +# 4. Serializes and deserializes a simple message |
| 21 | +# |
| 22 | +# Note: protobuf v21.x is used because newer versions (v22+) require abseil-cpp |
| 23 | +# as a dependency, which significantly complicates the build. |
| 24 | + |
| 25 | +[cmake] |
| 26 | +version = "3.18...3.31" |
| 27 | + |
| 28 | +[project] |
| 29 | +name = "protobuf-example" |
| 30 | +description = "Demonstrates protobuf integration with cmkr custom commands" |
| 31 | + |
| 32 | +# ----------------------------------------------------------------------------- |
| 33 | +# Fetch protobuf from GitHub |
| 34 | +# Using v21.12 (last version before abseil-cpp became required) |
| 35 | +# ----------------------------------------------------------------------------- |
| 36 | + |
| 37 | +[fetch-content.protobuf] |
| 38 | +git = "https://github.com/protocolbuffers/protobuf" |
| 39 | +tag = "v21.12" |
| 40 | +options = { |
| 41 | + protobuf_BUILD_TESTS = false, |
| 42 | + protobuf_BUILD_EXAMPLES = false, |
| 43 | + protobuf_BUILD_LIBPROTOC = false, |
| 44 | + protobuf_BUILD_SHARED_LIBS = false, |
| 45 | + protobuf_MSVC_STATIC_RUNTIME = false, |
| 46 | + SKIP_INSTALL_ALL = true, |
| 47 | + protobuf_WITH_ZLIB = false, |
| 48 | +} |
| 49 | + |
| 50 | +# ----------------------------------------------------------------------------- |
| 51 | +# Main executable using protobuf |
| 52 | +# ----------------------------------------------------------------------------- |
| 53 | + |
| 54 | +[target.protobuf_example] |
| 55 | +type = "executable" |
| 56 | +sources = [ |
| 57 | + "src/main.cpp", |
| 58 | +] |
| 59 | +include-directories = [ |
| 60 | + "${CMAKE_CURRENT_BINARY_DIR}/generated", |
| 61 | +] |
| 62 | +link-libraries = ["protobuf::libprotobuf"] |
| 63 | +compile-features = ["cxx_std_17"] |
| 64 | + |
| 65 | +# Create the generated directory before running protoc |
| 66 | +cmake-before = """ |
| 67 | +file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/generated") |
| 68 | +""" |
| 69 | + |
| 70 | +# Custom command to generate C++ sources from .proto file |
| 71 | +# This uses protoc from the fetched protobuf repository |
| 72 | +[[target.protobuf_example.custom-command]] |
| 73 | +outputs = [ |
| 74 | + "${CMAKE_CURRENT_BINARY_DIR}/generated/addressbook.pb.h", |
| 75 | + "${CMAKE_CURRENT_BINARY_DIR}/generated/addressbook.pb.cc", |
| 76 | +] |
| 77 | +depends = [ |
| 78 | + "${CMAKE_CURRENT_SOURCE_DIR}/proto/addressbook.proto", |
| 79 | + "protobuf::protoc", # Ensure protoc is built first |
| 80 | +] |
| 81 | +command = [ |
| 82 | + "$<TARGET_FILE:protobuf::protoc>", |
| 83 | + "--cpp_out=${CMAKE_CURRENT_BINARY_DIR}/generated", |
| 84 | + "-I${CMAKE_CURRENT_SOURCE_DIR}/proto", |
| 85 | + "${CMAKE_CURRENT_SOURCE_DIR}/proto/addressbook.proto", |
| 86 | +] |
| 87 | +comment = "Generate C++ sources from addressbook.proto" |
| 88 | +verbatim = true |
| 89 | + |
| 90 | +# Post-build: run the executable to verify it works |
| 91 | +[[target.protobuf_example.custom-command]] |
| 92 | +build-event = "post-build" |
| 93 | +command = ["$<TARGET_FILE:protobuf_example>"] |
| 94 | +comment = "Run protobuf_example to verify serialization/deserialization works" |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +<sup><sub>This page was automatically generated from [tests/protobuf/cmake.toml](https://github.com/build-cpp/cmkr/tree/main/tests/protobuf/cmake.toml).</sub></sup> |
0 commit comments