-
Notifications
You must be signed in to change notification settings - Fork 448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix some problems with the Protobuf CMake file #4262
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,30 +13,41 @@ macro(p4c_obtain_protobuf) | |
endif() | ||
find_package(Protobuf CONFIG) | ||
if(NOT Protobuf_FOUND) | ||
find_package(Protobuf REQUIRED) | ||
find_package(Protobuf REQUIRED) | ||
endif() | ||
# Protobuf sets the protoc binary to a generator expression, which are problematic. | ||
# They are problematic because they are only evaluated at build time. | ||
# However, we may have scripts that depend on the actual build time during the configure phase. | ||
# Hard code a custom binary instead. | ||
find_program(PROTOC_BINARY protoc) | ||
|
||
if(ENABLE_PROTOBUF_STATIC) | ||
set(CMAKE_FIND_LIBRARY_SUFFIXES ${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}) | ||
endif() | ||
set(PROTOBUF_LIBRARY "protobuf::libprotobuf") | ||
set(PROTOBUF_PROTOC_EXECUTABLE "protobuf::protoc") | ||
else() | ||
# Google introduced another breaking change with protobuf 22.x by adding abseil as a new | ||
# dependency. https://protobuf.dev/news/2022-08-03/#abseil-dep We do not want abseil, so we stay | ||
# with 21.x for now. | ||
set(P4C_PROTOBUF_VERSION "21.12") | ||
message("Fetching Protobuf version ${P4C_PROTOBUF_VERSION} for P4C...") | ||
|
||
# Unity builds do not work for Protobuf... | ||
set(CMAKE_UNITY_BUILD_PREV ${CMAKE_UNITY_BUILD}) | ||
set(CMAKE_UNITY_BUILD OFF) | ||
# Print out download state while setting up Protobuf. | ||
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET}) | ||
set(FETCHCONTENT_QUIET OFF) | ||
# Build Protobuf with position-independent code. | ||
set(CMAKE_POSITION_INDEPENDENT_CODE_PREV ${CMAKE_POSITION_INDEPENDENT_CODE}) | ||
set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
||
set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build tests.") | ||
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "Build libprotoc and protoc compiler.") | ||
set(protobuf_BUILD_PROTOC_BINARIES ON CACHE BOOL "Build libprotoc and protoc compiler.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We build protoc from scratch, because the downloaded, prebuilt binary can interact badly with protoc plugins such as |
||
# Only ever build the static library. It is not safe to link with a local dynamic version. | ||
set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "Build Shared Libraries") | ||
# Unity builds do not work for Protobuf... | ||
set(SAVED_CMAKE_UNITY_BUILD ${CMAKE_UNITY_BUILD}) | ||
set(CMAKE_UNITY_BUILD OFF) | ||
# This is necessary to be able to call FindPackage. | ||
set(protobuf_INSTALL ON CACHE BOOL "Install Protobuf") | ||
|
||
fetchcontent_declare( | ||
protobuf | ||
URL https://github.com/protocolbuffers/protobuf/releases/download/v${P4C_PROTOBUF_VERSION}/protobuf-cpp-3.${P4C_PROTOBUF_VERSION}.tar.gz | ||
|
@@ -45,49 +56,34 @@ macro(p4c_obtain_protobuf) | |
GIT_PROGRESS TRUE | ||
) | ||
|
||
# Derive the target architecture in order to download the right zip. | ||
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64") | ||
set(protobuf_ARCH "aarch_64") | ||
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "amd64") | ||
set(protobuf_ARCH "x86_64") | ||
else() | ||
MESSAGE(FATAL_ERROR "Unsupported host architecture `${CMAKE_HOST_SYSTEM_PROCESSOR}`") | ||
endif() | ||
|
||
# Pull a different protoc binary for MacOS. | ||
# TODO: Should we build from scratch? | ||
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") | ||
fetchcontent_declare( | ||
protoc | ||
URL https://github.com/protocolbuffers/protobuf/releases/download/v${P4C_PROTOBUF_VERSION}/protoc-${P4C_PROTOBUF_VERSION}-osx-${protobuf_ARCH}.zip | ||
USES_TERMINAL_DOWNLOAD TRUE | ||
GIT_PROGRESS TRUE | ||
) | ||
else() | ||
fetchcontent_declare( | ||
protoc | ||
URL https://github.com/protocolbuffers/protobuf/releases/download/v${P4C_PROTOBUF_VERSION}/protoc-${P4C_PROTOBUF_VERSION}-linux-${protobuf_ARCH}.zip | ||
USES_TERMINAL_DOWNLOAD TRUE | ||
GIT_PROGRESS TRUE | ||
) | ||
endif() | ||
|
||
fetchcontent_makeavailable(protoc) | ||
# Exclude Protobuf from the main make install step. We only want to use it locally. | ||
fetchcontent_makeavailable_but_exclude_install(protobuf) | ||
|
||
# Reset unity builds to the previous state... | ||
set(CMAKE_UNITY_BUILD ${SAVED_CMAKE_UNITY_BUILD}) | ||
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV}) | ||
|
||
set(PROTOBUF_PROTOC_EXECUTABLE ${protoc_SOURCE_DIR}/bin/protoc CACHE STRING | ||
"Protoc executable." | ||
) | ||
set(Protobuf_INCLUDE_DIR ${protobuf_SOURCE_DIR}/src/ CACHE STRING "Protobuf include directory.") | ||
set(PROTOBUF_LIBRARY libprotobuf) | ||
# Protobuf source code may trigger warnings which we need to ignore. | ||
# Protobuf and protoc source code may trigger warnings which we need to ignore. | ||
set_target_properties(libprotobuf PROPERTIES COMPILE_FLAGS "-Wno-error") | ||
set_target_properties(libprotoc PROPERTIES COMPILE_FLAGS "-Wno-error") | ||
|
||
# Add the Protobuf directory to our module path for module builds. | ||
list(APPEND CMAKE_MODULE_PATH "${protobuf_BINARY_DIR}/cmake/protobuf") | ||
|
||
message("Done with setting up Protobuf for P4C.") | ||
# Set some Protobuf variables manually until we are able to call FindPackage directly. | ||
set(Protobuf_FOUND TRUE) | ||
set(Protobuf_LIBRARY "protobuf::libprotobuf") | ||
set(Protobuf_PROTOC_LIBRARY "protobuf::libprotoc") | ||
set(Protobuf_PROTOC_EXECUTABLE $<TARGET_FILE:protoc>) | ||
# Protobuf sets the protoc binary to a generator expression, which are problematic. | ||
# They are problematic because they are only evaluated at build time. | ||
# However, we may have scripts that depend on the actual build time during the configure phase. | ||
# Hard code a custom binary which we can use in addition to the generator expression. | ||
set(PROTOC_BINARY ${protobuf_BINARY_DIR}/protoc) | ||
set(Protobuf_INCLUDE_DIR "${protobuf_SOURCE_DIR}/src/") | ||
|
||
# Reset temporary variable modifications. | ||
set(CMAKE_UNITY_BUILD ${CMAKE_UNITY_BUILD_PREV}) | ||
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV}) | ||
set(CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE_PREV}) | ||
endif() | ||
|
||
|
||
message("Done with setting up Protobuf for P4C.") | ||
endmacro(p4c_obtain_protobuf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The canonical Protobuf variable prefix is now
Protobuf_
instead ofPROTOBUF_
.https://cmake.org/cmake/help/latest/module/FindProtobuf.html