Skip to content

Commit

Permalink
Setup upb installation with cmake.
Browse files Browse the repository at this point in the history
This installs a upb's static library, headers, and three protoc plugin binaries (upb, upbdefs, upb_minitable).  These will now be enabled by default, but can be disabled by setting protobuf_BUILD_LIBUPB=OFF.

To qualify this, we hook into our existing install test infrastructure which attempts to build and run our tests without any of the installation artifacts available from the source tree.  Public headers are deleted, and builds of exported libraries/binaries are disabled.

PiperOrigin-RevId: 617376961
  • Loading branch information
mkruskal-google authored and copybara-github committed Mar 20, 2024
1 parent 83b7ca2 commit 6898451
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 35 deletions.
24 changes: 14 additions & 10 deletions .github/workflows/test_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ jobs:
command: >
-c "set -ex;
sccache -z;
cmake . -DWITH_PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_WITH_ZLIB=OFF ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake . -DWITH_PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }}
-Dprotobuf_BUILD_LIBUPB=OFF -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14
-Dprotobuf_WITH_ZLIB=OFF ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake --build . --parallel 20;
ctest --parallel 20;
sccache -s"
Expand Down Expand Up @@ -151,7 +153,7 @@ jobs:
command: >-
/test.sh ${{ matrix.flags}} ${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_BUILD_TESTS=ON -Dprotobuf_USE_EXTERNAL_GTEST=ON
-Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_BUILD_LIBUPB=ON
-Dprotobuf_ABSL_PROVIDER=package
linux-cmake-install:
name: Linux CMake Install
Expand All @@ -175,7 +177,9 @@ jobs:
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake:3.13.3-63dd26c0c7a808d92673a3e52e848189d4ab0f17
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
command: >-
/install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_BUILD_SHARED_LIBS=ON \&\&
/install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package
-Dprotobuf_BUILD_SHARED_LIBS=ON \&\&
/test.sh
${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_REMOVE_INSTALLED_HEADERS=ON
Expand Down Expand Up @@ -250,7 +254,7 @@ jobs:
-c 'set -ex;
cd /workspace;
sccache -z;
cmake . -Dprotobuf_BUILD_LIBUPB=ON ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake . ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake --build . --parallel 20;
ctest --verbose --parallel 20;
sccache -s'
Expand Down Expand Up @@ -278,7 +282,7 @@ jobs:
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
command: >-
/test.sh ${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_BUILD_CONFORMANCE=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_BUILD_LIBUPB=ON
-Dprotobuf_BUILD_CONFORMANCE=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14
linux-cmake-32-bit:
name: Linux CMake 32-bit
Expand Down Expand Up @@ -306,7 +310,7 @@ jobs:
/bin/bash -cex '
cd /workspace;
sccache -z;
cmake . -DCMAKE_CXX_STANDARD=14 -Dprotobuf_BUILD_LIBUPB=ON ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake . -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake --build . --parallel 20;
ctest --verbose --parallel 20;
sccache -s'
Expand Down Expand Up @@ -370,30 +374,30 @@ jobs:
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF
-Dprotobuf_BUILD_EXAMPLES=ON -Dprotobuf_BUILD_LIBUPB=ON
-Dprotobuf_BUILD_EXAMPLES=ON
vsversion: '2022'
cache-prefix: windows-2022-cmake
- name: Windows CMake 2019
os: windows-2019
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF
-Dprotobuf_BUILD_EXAMPLES=ON -Dprotobuf_BUILD_LIBUPB=ON
-Dprotobuf_BUILD_EXAMPLES=ON
vsversion: '2019'
cache-prefix: windows-2019-cmake
# windows-2019 has python3.7 installed, which is incompatible with the latest gcloud
python-version: '3.9'
- name: Windows CMake 32-bit
os: windows-2022
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF -Dprotobuf_BUILD_LIBUPB=ON
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
vsversion: '2022'
windows-arch: 'win32'
cache-prefix: windows-2022-win32-cmake
- name: Windows CMake Shared
os: windows-2022
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF -Dprotobuf_BUILD_LIBUPB=ON
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=ON
vsversion: '2022'
cache-prefix: windows-2022-cmake
Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
option(protobuf_BUILD_PROTOBUF_BINARIES "Build protobuf libraries and protoc compiler" ON)
option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
option(protobuf_BUILD_LIBUPB "Build libupb" OFF)
option(protobuf_BUILD_LIBUPB "Build libupb" ON)
option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "")
option(protobuf_ALLOW_CCACHE "Adjust build flags to allow for ccache support." OFF)
Expand Down Expand Up @@ -316,10 +316,10 @@ if (protobuf_BUILD_PROTOBUF_BINARIES)
endif ()
if (protobuf_BUILD_LIBUPB)
include(${protobuf_SOURCE_DIR}/cmake/libupb.cmake)
include(${protobuf_SOURCE_DIR}/cmake/upb_generators.cmake)
if (NOT DEFINED protobuf_LIB_UPB)
set(protobuf_LIB_UPB libupb)
endif ()
include(${protobuf_SOURCE_DIR}/cmake/upb_generators.cmake)
endif ()
if (protobuf_BUILD_PROTOC_BINARIES)
include(${protobuf_SOURCE_DIR}/cmake/protoc.cmake)
Expand All @@ -334,6 +334,7 @@ else ()
set(protobuf_LIB_PROTOC protobuf::libprotoc)
set(protobuf_LIB_PROTOBUF protobuf::libprotobuf)
set(protobuf_LIB_PROTOBUF_LITE protobuf::libprotobuf-lite)
set(protobuf_LIB_UPB protobuf::libupb)
message(STATUS "CMake installation of Protobuf found.")
endif ()
endif ()
Expand Down
43 changes: 32 additions & 11 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ set(_protobuf_libraries libprotobuf-lite libprotobuf)
if (protobuf_BUILD_LIBPROTOC)
list(APPEND _protobuf_libraries libprotoc)
endif (protobuf_BUILD_LIBPROTOC)
if (protobuf_BUILD_LIBUPB)
list(APPEND _protobuf_libraries libupb)
endif ()

foreach(_library ${_protobuf_libraries})
set_property(TARGET ${_library}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${protobuf_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if (UNIX AND NOT APPLE)
set_property(TARGET ${_library}
PROPERTY INSTALL_RPATH "$ORIGIN")
Expand All @@ -49,16 +48,27 @@ foreach(_library ${_protobuf_libraries})
endforeach()

if (protobuf_BUILD_PROTOC_BINARIES)
set(_protobuf_binaries protoc)
install(TARGETS protoc EXPORT protobuf-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
if (UNIX AND NOT APPLE)
set_property(TARGET protoc
PROPERTY INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
elseif (APPLE)
set_property(TARGET protoc
PROPERTY INSTALL_RPATH "@loader_path/../lib")
endif()
if (protobuf_BUILD_LIBUPB)
foreach (generator upb upbdefs upb_minitable)
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 ()
endif (protobuf_BUILD_PROTOC_BINARIES)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Expand All @@ -73,6 +83,17 @@ set(protobuf_HEADERS
${plugin_proto_proto_srcs}
${java_features_proto_proto_srcs}
)
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
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/google/protobuf
COMPONENT protobuf-headers
)
endif ()
foreach(_header ${protobuf_HEADERS})
string(FIND ${_header} "${protobuf_SOURCE_DIR}/src" _find_src)
string(FIND ${_header} "${protobuf_SOURCE_DIR}" _find_nosrc)
Expand Down
5 changes: 4 additions & 1 deletion cmake/libprotobuf-lite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
target_link_libraries(libprotobuf-lite PRIVATE log)
endif()
target_include_directories(libprotobuf-lite PUBLIC ${protobuf_SOURCE_DIR}/src)
target_include_directories(libprotobuf-lite PUBLIC
$<BUILD_INTERFACE:${protobuf_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(libprotobuf-lite PUBLIC ${protobuf_ABSL_USED_TARGETS})
protobuf_configure_target(libprotobuf-lite)
if(protobuf_BUILD_SHARED_LIBS)
Expand Down
5 changes: 4 additions & 1 deletion cmake/libprotobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
target_link_libraries(libprotobuf PRIVATE log)
endif()
target_include_directories(libprotobuf PUBLIC ${protobuf_SOURCE_DIR}/src)
target_include_directories(libprotobuf PUBLIC
$<BUILD_INTERFACE:${protobuf_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(libprotobuf PUBLIC ${protobuf_ABSL_USED_TARGETS})
protobuf_configure_target(libprotobuf)
if(protobuf_BUILD_SHARED_LIBS)
Expand Down
5 changes: 4 additions & 1 deletion cmake/libupb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ add_library(libupb STATIC
${protobuf_version_rc_file}
)

target_include_directories(libupb PUBLIC ${bootstrap_cmake_dir})
target_include_directories(libupb PUBLIC
$<BUILD_INTERFACE:${bootstrap_cmake_dir}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

protobuf_configure_target(libupb)

Expand Down
14 changes: 8 additions & 6 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ if (protobuf_BUILD_LIBUPB)
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
IMPORT_DIRS ${protobuf_SOURCE_DIR}
PLUGIN protoc-gen-${generator}=$<TARGET_FILE:protoc-gen-${generator}>
DEPENDENCIES $<TARGET_FILE:protoc-gen-${generator}>
PLUGIN protoc-gen-${generator}=$<TARGET_FILE:protobuf::protoc-gen-${generator}>
DEPENDENCIES $<TARGET_FILE:protobuf::protoc-gen-${generator}>
)
set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
endforeach()
Expand All @@ -219,8 +219,8 @@ if (protobuf_BUILD_LIBUPB)
${upb_test_util_files})

target_link_libraries(upb-test
libprotobuf
libupb
${protobuf_LIB_PROTOBUF}
${protobuf_LIB_UPB}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock_main)
Expand All @@ -239,7 +239,9 @@ add_custom_target(restore-installed-headers)

file(GLOB_RECURSE _local_hdrs
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.inc")
"${PROJECT_SOURCE_DIR}/src/*.inc"
"${PROJECT_SOURCE_DIR}/upb/*.h"
)

# Exclude the bootstrapping that are directly used by tests.
set(_exclude_hdrs
Expand All @@ -250,7 +252,7 @@ set(_exclude_hdrs

# Exclude test library headers.
list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
${compiler_test_utils_hdrs})
${compiler_test_utils_hdrs} ${upb_test_util_files})
foreach(_hdr ${_exclude_hdrs})
list(REMOVE_ITEM _local_hdrs ${_hdr})
endforeach()
Expand Down
7 changes: 4 additions & 3 deletions cmake/upb_generators.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ foreach(generator upb upbdefs upb_minitable)
${bootstrap_sources}
${protobuf_version_rc_file}
)
target_include_directories(protoc-gen-${generator} PUBLIC ${bootstrap_cmake_dir})
target_include_directories(protoc-gen-${generator} PRIVATE ${bootstrap_cmake_dir})
target_link_libraries(protoc-gen-${generator}
libprotobuf
libupb
${protobuf_LIB_PROTOBUF}
${protobuf_LIB_UPB}
${protobuf_ABSL_USED_TARGETS}
)
set_target_properties(protoc-gen-${generator} PROPERTIES VERSION ${protobuf_VERSION})
add_executable(protobuf::protoc-gen-${generator} ALIAS protoc-gen-${generator})
endforeach()
1 change: 1 addition & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ cc_dist_library(
name = "upb",
tags = ["manual"],
deps = [
"//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
"//upb/json",
"//upb/message:compare",
"//upb/message:copy",
Expand Down

0 comments on commit 6898451

Please sign in to comment.