Skip to content

cmake: support narrowly constrained lite-only runtime builds#27407

Closed
dudantas wants to merge 2 commits into
protocolbuffers:mainfrom
dudantas:dudantas/cmake-lite-only-runtime-pr
Closed

cmake: support narrowly constrained lite-only runtime builds#27407
dudantas wants to merge 2 commits into
protocolbuffers:mainfrom
dudantas:dudantas/cmake-lite-only-runtime-pr

Conversation

@dudantas

@dudantas dudantas commented May 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements the narrowly constrained lite-only runtime build discussed in #27231.

This adds one new CMake option:

option(protobuf_BUILD_LIBPROTOBUF "Build libprotobuf" ON)

The default behavior remains unchanged. With defaults, Protobuf still builds and installs both libprotobuf-lite and libprotobuf.

When protobuf_BUILD_LIBPROTOBUF=OFF, the build is only accepted in a strict target-side lite-only configuration:

  • protobuf_BUILD_PROTOC_BINARIES=OFF
  • protobuf_BUILD_LIBPROTOC=OFF
  • protobuf_BUILD_LIBUPB=OFF
  • protobuf_BUILD_TESTS=OFF
  • protobuf_BUILD_CONFORMANCE=OFF
  • protobuf_BUILD_EXAMPLES=OFF

In that mode, CMake still builds, installs, and exports protobuf::libprotobuf-lite, but does not build/install/export protobuf::libprotobuf, libprotoc, protoc, or libupb.

Motivation

Some downstream/package-manager workflows use host protoc for code generation while the target package only needs the lite C++ runtime.

For example, generated C++ sources may use:

option optimize_for = LITE_RUNTIME;

and the target consumer only links:

target_link_libraries(app PRIVATE protobuf::libprotobuf-lite)

This PR does not try to support every possible CMake combination. It only adds a strict lite-only target-runtime mode where the full runtime and compiler-side artifacts are explicitly disabled.

Scope

This PR intentionally does not:

  • change Bazel;
  • change C++ runtime sources;
  • prune installed headers;
  • add protobuf_BUILD_LIBPROTOBUF_LITE;
  • add telemetry/devtools/docs from the local PoC;
  • support full-runtime consumers in lite-only mode;
  • support protoc, libprotoc, tests, conformance, examples, or libupb when protobuf_BUILD_LIBPROTOBUF=OFF.

CONFIG mode is the primary validated contract:

find_package(protobuf CONFIG REQUIRED)
target_link_libraries(app PRIVATE protobuf::libprotobuf-lite)

Implementation notes

CMakeLists.txt

  • Adds protobuf_BUILD_LIBPROTOBUF, defaulting to ON.
  • Keeps libprotobuf-lite included whenever protobuf_BUILD_PROTOBUF_BINARIES=ON.
  • Includes libprotobuf.cmake only when protobuf_BUILD_LIBPROTOBUF=ON.
  • Rejects unsupported lite-only combinations with explicit FATAL_ERROR messages.

cmake/install.cmake

  • Installs/exports only targets that actually exist.
  • Generates/installs protobuf-lite.pc only when libprotobuf-lite exists.
  • Generates/installs protobuf.pc only when libprotobuf exists.
  • Generates/installs upb.pc only when libupb exists.

Validation

Validated locally on Windows/MSVC.

Default build

cmake -S . -B build-default -Dprotobuf_BUILD_TESTS=OFF
cmake --build build-default --config Release --parallel 1
cmake --install build-default --config Release --prefix install-default

Default install contained:

  • libprotobuf
  • libprotobuf-lite
  • libprotoc
  • protoc
  • libupb
  • protobuf.pc
  • protobuf-lite.pc
  • upb.pc

Lite-only build

cmake -S . -B build-lite-only `
  -Dprotobuf_BUILD_TESTS=OFF `
  -Dprotobuf_BUILD_CONFORMANCE=OFF `
  -Dprotobuf_BUILD_EXAMPLES=OFF `
  -Dprotobuf_BUILD_PROTOC_BINARIES=OFF `
  -Dprotobuf_BUILD_LIBPROTOC=OFF `
  -Dprotobuf_BUILD_LIBUPB=OFF `
  -Dprotobuf_BUILD_LIBPROTOBUF=OFF

cmake --build build-lite-only --config Release --parallel 1
cmake --install build-lite-only --config Release --prefix install-lite-only

Lite-only install contained:

  • libprotobuf-lite
  • protobuf-lite.pc

Lite-only install did not contain:

  • libprotobuf
  • libprotoc
  • protoc
  • libupb
  • protobuf.pc

CONFIG consumer

A minimal external consumer passed with:

find_package(protobuf CONFIG REQUIRED)

if(NOT TARGET protobuf::libprotobuf-lite)
  message(FATAL_ERROR "protobuf::libprotobuf-lite was not exported")
endif()

if(TARGET protobuf::libprotobuf)
  message(FATAL_ERROR "protobuf::libprotobuf should not exist in lite-only install")
endif()

add_executable(protobuf_lite_consumer main.cc)
target_link_libraries(protobuf_lite_consumer PRIVATE protobuf::libprotobuf-lite)

The consumer included:

#include <google/protobuf/message_lite.h>
#include <google/protobuf/stubs/common.h>

int main() {
  google::protobuf::ShutdownProtobufLibrary();
  return 0;
}

Invalid configuration checks

All expected invalid combinations failed at configure time with clear FATAL_ERROR messages:

  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_PROTOC_BINARIES=ON
  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_TESTS=ON
  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_CONFORMANCE=ON
  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_EXAMPLES=ON
  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_LIBUPB=ON
  • protobuf_BUILD_LIBPROTOBUF=OFF + protobuf_BUILD_LIBPROTOC=ON

Local PoC measurements

The earlier local PoC measured the following on Windows/MSVC:

Variant Configure Build Install Install size
Default 21.299s 508.063s 7.699s 113754 KB
Lite-only 49.902s 55.648s 4.363s 21579 KB

The lite-only configure step was slower in that local run, but the build step dominated the total time and dropped from 508.063s to 55.648s.

Related

Resolves #27231

@google-cla

google-cla Bot commented May 14, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@JasonLunn JasonLunn added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 14, 2026
@github-actions github-actions Bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 14, 2026
@esrauchg esrauchg added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 18, 2026
@github-actions github-actions Bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 18, 2026
@dudantas

dudantas commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

@esrauchg sorry, I'm not familiar with this repository checks. Do you need anything else from me?

@rgoldfinger6
rgoldfinger6 requested a review from esrauchg May 19, 2026 14:24
@zhangskz

Copy link
Copy Markdown
Member

Tests seem to be passing (there may have been a spurious failure earlier), @esrauchg will review with comments if additional action is needed.

@dudantas

Copy link
Copy Markdown
Contributor Author

Tests seem to be passing (there may have been a spurious failure earlier), @esrauchg will review with comments if additional action is needed.

Ok, thanks.

@esrauchg esrauchg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for iterating on this!

@copybara-service copybara-service Bot closed this in 7c09017 Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CMake: allow building and installing libprotobuf-lite without libprotobuf

4 participants