2015-08-22 18:25:48 -07:00
|
|
|
include(GNUInstallDirs)
|
|
|
|
|
|
2023-04-24 10:41:12 -07:00
|
|
|
foreach(_target IN LISTS protobuf_ABSL_USED_TARGETS)
|
2024-02-21 19:35:47 -08:00
|
|
|
# shared abseil on windows breaks the absl::foo -> absl_foo replacement logic -
|
|
|
|
|
# preempt this by a more specific replace (harmless if it doesn't apply); see GH-15883
|
|
|
|
|
string(REPLACE "absl::abseil_dll" "abseil_dll" _modified_target ${_target})
|
|
|
|
|
string(REPLACE :: _ _modified_target ${_modified_target})
|
2023-04-24 10:41:12 -07:00
|
|
|
list(APPEND _pc_targets ${_modified_target})
|
|
|
|
|
endforeach()
|
|
|
|
|
list(APPEND _pc_targets "utf8_range")
|
|
|
|
|
|
2023-05-05 16:24:31 -07:00
|
|
|
set(_protobuf_PC_REQUIRES "")
|
|
|
|
|
set(_sep "")
|
2023-05-07 09:23:49 -07:00
|
|
|
foreach (_target IN LISTS _pc_targets)
|
2023-05-05 16:24:31 -07:00
|
|
|
string(CONCAT _protobuf_PC_REQUIRES "${_protobuf_PC_REQUIRES}" "${_sep}" "${_target}")
|
|
|
|
|
set(_sep " ")
|
|
|
|
|
endforeach ()
|
2023-05-07 09:22:27 -07:00
|
|
|
set(_protobuf_PC_CFLAGS)
|
|
|
|
|
if (protobuf_BUILD_SHARED_LIBS)
|
|
|
|
|
set(_protobuf_PC_CFLAGS -DPROTOBUF_USE_DLLS)
|
|
|
|
|
endif ()
|
2023-04-24 10:41:12 -07:00
|
|
|
|
2024-12-03 19:07:04 -08:00
|
|
|
if(CMAKE_BUILD_TYPE STREQUAL Debug)
|
|
|
|
|
# attach debug postfix only in debug mode
|
|
|
|
|
set(protobuf_LIBRARY_POSTFIX ${protobuf_DEBUG_POSTFIX})
|
|
|
|
|
endif()
|
cmake: support narrowly constrained lite-only runtime builds (#27407)
## Summary
Implements the narrowly constrained lite-only runtime build discussed in #27231.
This adds one new CMake option:
```cmake
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:
```proto
option optimize_for = LITE_RUNTIME;
```
and the target consumer only links:
```cmake
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:
```cmake
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
```powershell
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
```powershell
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:
```cmake
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:
```cpp
#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
Closes #27407
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27407 from dudantas:dudantas/cmake-lite-only-runtime-pr 2c146c1aacb7453c61895898ef1ab660b9149294
PiperOrigin-RevId: 925038366
2026-06-01 19:01:31 -07:00
|
|
|
if (TARGET libprotobuf)
|
|
|
|
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/protobuf.pc.cmake
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc @ONLY)
|
|
|
|
|
endif ()
|
|
|
|
|
if (TARGET libprotobuf-lite)
|
|
|
|
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/protobuf-lite.pc.cmake
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc @ONLY)
|
|
|
|
|
endif ()
|
|
|
|
|
if (TARGET libupb)
|
2024-07-12 16:08:22 -07:00
|
|
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/upb.pc.cmake
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/upb.pc @ONLY)
|
|
|
|
|
endif ()
|
2017-07-21 18:57:07 +02:00
|
|
|
|
cmake: support narrowly constrained lite-only runtime builds (#27407)
## Summary
Implements the narrowly constrained lite-only runtime build discussed in #27231.
This adds one new CMake option:
```cmake
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:
```proto
option optimize_for = LITE_RUNTIME;
```
and the target consumer only links:
```cmake
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:
```cmake
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
```powershell
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
```powershell
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:
```cmake
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:
```cpp
#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
Closes #27407
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27407 from dudantas:dudantas/cmake-lite-only-runtime-pr 2c146c1aacb7453c61895898ef1ab660b9149294
PiperOrigin-RevId: 925038366
2026-06-01 19:01:31 -07:00
|
|
|
set(_protobuf_libraries)
|
|
|
|
|
foreach (_library libprotobuf-lite libprotobuf libprotoc libupb)
|
|
|
|
|
if (TARGET ${_library})
|
|
|
|
|
list(APPEND _protobuf_libraries ${_library})
|
|
|
|
|
endif()
|
|
|
|
|
endforeach()
|
2017-11-13 15:15:39 -08:00
|
|
|
|
|
|
|
|
foreach(_library ${_protobuf_libraries})
|
2018-05-22 21:52:07 +02:00
|
|
|
if (UNIX AND NOT APPLE)
|
|
|
|
|
set_property(TARGET ${_library}
|
|
|
|
|
PROPERTY INSTALL_RPATH "$ORIGIN")
|
|
|
|
|
elseif (APPLE)
|
|
|
|
|
set_property(TARGET ${_library}
|
|
|
|
|
PROPERTY INSTALL_RPATH "@loader_path")
|
|
|
|
|
endif()
|
2015-08-22 18:25:48 -07:00
|
|
|
install(TARGETS ${_library} EXPORT protobuf-targets
|
|
|
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${_library}
|
|
|
|
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library}
|
|
|
|
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library})
|
|
|
|
|
endforeach()
|
|
|
|
|
|
2017-11-13 15:15:39 -08:00
|
|
|
if (protobuf_BUILD_PROTOC_BINARIES)
|
2024-03-19 20:13:17 -07:00
|
|
|
set(_protobuf_binaries protoc)
|
2017-11-13 15:15:39 -08:00
|
|
|
install(TARGETS protoc EXPORT protobuf-targets
|
2021-09-22 23:25:29 +02:00
|
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc
|
|
|
|
|
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
|
2024-03-19 20:13:17 -07:00
|
|
|
if (protobuf_BUILD_LIBUPB)
|
2025-05-30 09:04:54 -07:00
|
|
|
foreach (generator upb upbdefs upb_minitable)
|
2024-03-19 20:13:17 -07:00
|
|
|
list(APPEND _protobuf_binaries protoc-gen-${generator})
|
|
|
|
|
install(TARGETS protoc-gen-${generator} EXPORT protobuf-targets
|
|
|
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT upb-generators
|
|
|
|
|
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT upb-generators)
|
|
|
|
|
endforeach ()
|
|
|
|
|
endif ()
|
|
|
|
|
foreach (binary IN LISTS _protobuf_binaries)
|
|
|
|
|
if (UNIX AND NOT APPLE)
|
|
|
|
|
set_property(TARGET ${binary}
|
|
|
|
|
PROPERTY INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
|
|
|
|
|
elseif (APPLE)
|
|
|
|
|
set_property(TARGET ${binary}
|
|
|
|
|
PROPERTY INSTALL_RPATH "@loader_path/../lib")
|
|
|
|
|
endif ()
|
|
|
|
|
endforeach ()
|
cmake: support narrowly constrained lite-only runtime builds (#27407)
## Summary
Implements the narrowly constrained lite-only runtime build discussed in #27231.
This adds one new CMake option:
```cmake
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:
```proto
option optimize_for = LITE_RUNTIME;
```
and the target consumer only links:
```cmake
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:
```cmake
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
```powershell
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
```powershell
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:
```cmake
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:
```cpp
#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
Closes #27407
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27407 from dudantas:dudantas/cmake-lite-only-runtime-pr 2c146c1aacb7453c61895898ef1ab660b9149294
PiperOrigin-RevId: 925038366
2026-06-01 19:01:31 -07:00
|
|
|
endif ()
|
2015-08-22 18:25:48 -07:00
|
|
|
|
cmake: support narrowly constrained lite-only runtime builds (#27407)
## Summary
Implements the narrowly constrained lite-only runtime build discussed in #27231.
This adds one new CMake option:
```cmake
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:
```proto
option optimize_for = LITE_RUNTIME;
```
and the target consumer only links:
```cmake
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:
```cmake
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
```powershell
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
```powershell
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:
```cmake
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:
```cpp
#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
Closes #27407
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27407 from dudantas:dudantas/cmake-lite-only-runtime-pr 2c146c1aacb7453c61895898ef1ab660b9149294
PiperOrigin-RevId: 925038366
2026-06-01 19:01:31 -07:00
|
|
|
if (TARGET libprotobuf-lite)
|
|
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc
|
|
|
|
|
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
|
|
|
|
endif ()
|
|
|
|
|
if (TARGET libprotobuf)
|
|
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc
|
|
|
|
|
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
|
|
|
|
endif ()
|
|
|
|
|
if (TARGET libupb)
|
2024-07-12 16:08:22 -07:00
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/upb.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
|
|
|
|
endif ()
|
2017-07-21 18:57:07 +02:00
|
|
|
|
2022-07-25 20:36:47 -07:00
|
|
|
include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
|
|
|
|
|
set(protobuf_HEADERS
|
|
|
|
|
${libprotobuf_hdrs}
|
2024-12-26 08:55:09 -08:00
|
|
|
${libprotoc_public_hdrs}
|
2022-07-25 20:36:47 -07:00
|
|
|
${plugin_proto_proto_srcs}
|
2026-07-31 16:17:28 -07:00
|
|
|
${release_all_options_protos_files}
|
2022-07-25 20:36:47 -07:00
|
|
|
)
|
2024-03-19 20:13:17 -07:00
|
|
|
if (protobuf_BUILD_LIBUPB)
|
|
|
|
|
list(APPEND protobuf_HEADERS ${libupb_hdrs})
|
|
|
|
|
# Manually install the bootstrap headers
|
|
|
|
|
install(
|
|
|
|
|
FILES
|
|
|
|
|
${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/descriptor.upb.h
|
|
|
|
|
${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/descriptor.upb_minitable.h
|
2026-07-30 11:17:22 -07:00
|
|
|
${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/json_enumvalue_options.upb.h
|
|
|
|
|
${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/json_enumvalue_options.upb_minitable.h
|
2024-03-19 20:13:17 -07:00
|
|
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/google/protobuf
|
|
|
|
|
COMPONENT protobuf-headers
|
|
|
|
|
)
|
|
|
|
|
endif ()
|
2024-12-20 14:01:16 -08:00
|
|
|
set(protobuf_STRIP_PREFIXES
|
|
|
|
|
"/src"
|
|
|
|
|
"/java/core/src/main/resources"
|
2026-03-27 00:47:19 -07:00
|
|
|
"/csharp"
|
2024-12-20 14:01:16 -08:00
|
|
|
"/go"
|
|
|
|
|
"/"
|
|
|
|
|
)
|
2022-07-25 20:36:47 -07:00
|
|
|
foreach(_header ${protobuf_HEADERS})
|
2024-12-20 14:01:16 -08:00
|
|
|
foreach(_strip_prefix ${protobuf_STRIP_PREFIXES})
|
|
|
|
|
string(FIND ${_header} "${protobuf_SOURCE_DIR}${_strip_prefix}" _find_src)
|
|
|
|
|
if(_find_src GREATER -1)
|
|
|
|
|
set(_from_dir "${protobuf_SOURCE_DIR}${_strip_prefix}")
|
|
|
|
|
break()
|
|
|
|
|
endif()
|
|
|
|
|
endforeach()
|
|
|
|
|
|
2024-02-08 17:08:45 -08:00
|
|
|
# Escape _from_dir for regex special characters in the directory name.
|
2024-02-12 11:12:08 -08:00
|
|
|
string(REGEX REPLACE "([$^.[|*+?()]|])" "\\\\\\1" _from_dir_regexp "${_from_dir}")
|
2023-11-29 20:27:57 -08:00
|
|
|
# On some platforms `_form_dir` ends up being just "protobuf", which can
|
|
|
|
|
# easily match multiple times in our paths. We force it to only replace
|
|
|
|
|
# prefixes to avoid this case.
|
2024-02-08 17:08:45 -08:00
|
|
|
string(REGEX REPLACE "^${_from_dir_regexp}" "" _header ${_header})
|
2023-10-12 14:33:40 -07:00
|
|
|
get_filename_component(_extract_from "${_from_dir}/${_header}" ABSOLUTE)
|
2017-03-10 16:32:19 -08:00
|
|
|
get_filename_component(_extract_name ${_header} NAME)
|
2022-03-10 21:05:27 +01:00
|
|
|
get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" DIRECTORY)
|
2022-07-25 20:36:47 -07:00
|
|
|
install(FILES "${_extract_from}"
|
|
|
|
|
DESTINATION "${_extract_to}"
|
|
|
|
|
COMPONENT protobuf-headers
|
|
|
|
|
RENAME "${_extract_name}")
|
2016-05-26 18:04:32 -07:00
|
|
|
endforeach()
|
2015-08-22 18:25:48 -07:00
|
|
|
|
2016-06-06 10:59:58 -07:00
|
|
|
# Install configuration
|
2022-06-22 10:43:13 -07:00
|
|
|
set(_install_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
|
|
|
|
|
set(_build_cmakedir_desc "Directory relative to CMAKE_CURRENT_BINARY_DIR for cmake configuration files")
|
2020-11-21 16:41:51 +08:00
|
|
|
set(_exampledir_desc "Directory relative to CMAKE_INSTALL_DATA to install examples")
|
2022-06-22 10:43:13 -07:00
|
|
|
set(_protobuf_subdir_desc "Subdirectory in which to install cmake configuration files")
|
2024-07-08 19:50:42 -07:00
|
|
|
set(protobuf_CMAKE_SUBDIR "cmake/protobuf" CACHE STRING "${_protobuf_subdir_desc}")
|
|
|
|
|
set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/${protobuf_CMAKE_SUBDIR}" CACHE STRING "${_install_cmakedir_desc}")
|
|
|
|
|
set(CMAKE_INSTALL_EXAMPLEDIR "${CMAKE_INSTALL_DATADIR}/protobuf/examples" CACHE STRING "${_exampledir_desc}")
|
2022-06-22 10:43:13 -07:00
|
|
|
set(CMAKE_BUILD_CMAKEDIR "${CMAKE_CURRENT_BINARY_DIR}/${protobuf_CMAKE_SUBDIR}" CACHE STRING "${_build_cmakedir_desc}")
|
|
|
|
|
mark_as_advanced(protobuf_CMAKE_SUBDIR)
|
|
|
|
|
mark_as_advanced(CMAKE_BUILD_CMAKEDIR)
|
2016-06-01 17:00:08 +03:00
|
|
|
mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
|
2020-11-21 16:41:51 +08:00
|
|
|
mark_as_advanced(CMAKE_INSTALL_EXAMPLEDIR)
|
2015-08-22 18:25:48 -07:00
|
|
|
|
2022-03-08 00:00:00 +00:00
|
|
|
configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-config.cmake.in
|
2022-06-22 10:43:13 -07:00
|
|
|
${CMAKE_BUILD_CMAKEDIR}/protobuf-config.cmake @ONLY)
|
2022-03-08 00:00:00 +00:00
|
|
|
configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-config-version.cmake.in
|
2022-06-22 10:43:13 -07:00
|
|
|
${CMAKE_BUILD_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
|
2022-03-08 00:00:00 +00:00
|
|
|
configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-module.cmake.in
|
2022-06-22 10:43:13 -07:00
|
|
|
${CMAKE_BUILD_CMAKEDIR}/protobuf-module.cmake @ONLY)
|
2022-03-08 00:00:00 +00:00
|
|
|
configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake
|
2022-06-22 10:43:13 -07:00
|
|
|
${CMAKE_BUILD_CMAKEDIR}/protobuf-options.cmake @ONLY)
|
2022-08-23 20:32:36 +02:00
|
|
|
configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake
|
|
|
|
|
${CMAKE_BUILD_CMAKEDIR}/protobuf-generate.cmake @ONLY)
|
2016-06-01 17:00:08 +03:00
|
|
|
|
2016-06-06 10:59:58 -07:00
|
|
|
# Allows the build directory to be used as a find directory.
|
2017-11-13 15:15:39 -08:00
|
|
|
|
2016-06-06 10:59:58 -07:00
|
|
|
install(EXPORT protobuf-targets
|
2016-05-19 14:52:04 -07:00
|
|
|
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
|
2016-06-06 10:59:58 -07:00
|
|
|
NAMESPACE protobuf::
|
2022-06-22 10:43:13 -07:00
|
|
|
COMPONENT protobuf-export
|
|
|
|
|
)
|
2022-05-26 14:11:37 -07:00
|
|
|
|
2022-06-22 10:43:13 -07:00
|
|
|
install(DIRECTORY ${CMAKE_BUILD_CMAKEDIR}/
|
2022-05-26 14:11:37 -07:00
|
|
|
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
|
2016-06-06 10:59:58 -07:00
|
|
|
COMPONENT protobuf-export
|
2022-05-26 14:11:37 -07:00
|
|
|
PATTERN protobuf-targets.cmake EXCLUDE
|
2016-06-06 10:59:58 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
option(protobuf_INSTALL_EXAMPLES "Install the examples folder" OFF)
|
|
|
|
|
if(protobuf_INSTALL_EXAMPLES)
|
2022-03-08 00:00:00 +00:00
|
|
|
install(DIRECTORY examples/
|
2020-11-21 16:41:51 +08:00
|
|
|
DESTINATION "${CMAKE_INSTALL_EXAMPLEDIR}"
|
2016-06-06 10:59:58 -07:00
|
|
|
COMPONENT protobuf-examples)
|
|
|
|
|
endif()
|
2023-02-01 11:32:39 -08:00
|
|
|
|
|
|
|
|
if (protobuf_INSTALL_TESTS)
|
|
|
|
|
install(TARGETS gmock EXPORT protobuf-targets
|
|
|
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
|
|
|
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
|
|
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
|
|
|
|
endif()
|