-
Notifications
You must be signed in to change notification settings - Fork 444
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
Install Protobuf using FetchContent. #4056
Changes from all commits
791e743
f2eaba1
cfa0f90
cbb3ab0
f05f58b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
macro(p4c_obtain_protobuf) | ||
option( | ||
P4C_USE_PREINSTALLED_PROTOBUF | ||
"Look for a preinstalled version of Protobuf in the system instead of installing a prebuilt binary using FetchContent." | ||
OFF | ||
) | ||
|
||
# If P4C_USE_PREINSTALLED_PROTOBUF is ON just try to find a preinstalled version of Protobuf. | ||
if(P4C_USE_PREINSTALLED_PROTOBUF) | ||
if(ENABLE_PROTOBUF_STATIC) | ||
set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) | ||
set(CMAKE_FIND_LIBRARY_SUFFIXES .a) | ||
endif() | ||
find_package(Protobuf 3.0.0 REQUIRED) | ||
if(ENABLE_PROTOBUF_STATIC) | ||
set(CMAKE_FIND_LIBRARY_SUFFIXES ${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}) | ||
endif() | ||
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...") | ||
|
||
# Print out download state while setting up Protobuf. | ||
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET}) | ||
set(FETCHCONTENT_QUIET OFF) | ||
set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build tests.") | ||
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "Build libprotoc and protoc compiler.") | ||
# 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) | ||
fetchcontent_declare( | ||
protobuf | ||
URL https://github.com/protocolbuffers/protobuf/releases/download/v${P4C_PROTOBUF_VERSION}/protobuf-cpp-3.${P4C_PROTOBUF_VERSION}.tar.gz | ||
URL_HASH SHA256=4eab9b524aa5913c6fffb20b2a8abf5ef7f95a80bc0701f3a6dbb4c607f73460 | ||
USES_TERMINAL_DOWNLOAD TRUE | ||
GIT_PROGRESS TRUE | ||
) | ||
# 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-x86_64.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-x86_64.zip | ||
URL_HASH SHA256=3a4c1e5f2516c639d3079b1586e703fc7bcfa2136d58bda24d1d54f949c315e8 | ||
USES_TERMINAL_DOWNLOAD TRUE | ||
GIT_PROGRESS TRUE | ||
) | ||
endif() | ||
|
||
fetchcontent_makeavailable(protoc) | ||
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. should protoc be installed? 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. This just downloads the binary packages, the installation of protoc takes a bit. |
||
# 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) | ||
message("Done with setting up Protobuf for P4C.") | ||
endif() | ||
endmacro(p4c_obtain_protobuf) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,15 @@ if [[ ! -x $BREW ]]; then | |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | ||
fi | ||
|
||
# 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. | ||
PROTOBUF_LIB="protobuf@21" | ||
BOOST_LIB="[email protected]" | ||
|
||
$BREW update | ||
$BREW install autoconf automake bdw-gc bison ${BOOST_LIB} ccache cmake \ | ||
libtool openssl pkg-config python coreutils grep ${PROTOBUF_LIB} | ||
libtool openssl pkg-config python coreutils grep | ||
|
||
# Prefer Homebrew's bison, grep, and protobuf over the macOS-provided version | ||
$BREW link --force bison grep ${PROTOBUF_LIB} ${BOOST_LIB} | ||
# Prefer Homebrew's bison and grep over the macOS-provided version | ||
$BREW link --force bison grep ${BOOST_LIB} | ||
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile | ||
echo 'export PATH="/usr/local/opt/${PROTOBUF_LIB}/bin:$PATH"' >> ~/.bash_profile | ||
echo 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile | ||
export PATH="/usr/local/opt/bison/bin:$PATH" | ||
export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH" | ||
|
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.
@fruffy This seems to break the build for architectures other than x86_64 (e.g., aarch64).
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.
Afaik we do not support or make guarantees for any architecture other than
x86_64
. But if there is a need this should be easily patchable.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.
p4c is also used on ARM64 platforms like P4EDGE and P4Pi. It would be great if we could also enable support for the
aarch64
architecture.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.
Using the option
P4C_USE_PREINSTALLED_PROTOBUF=ON
will use the system's Protobuf version. In that case things should compile as usual until we have a patch for these types of architectures in place.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.
Thank you Fabian!
Some platforms are not connected to the internet and FetchContent would fail when building p4c. As a workaround, we can run
cmake
on a system with internet access and includebuild/_deps
with the source code.Do you have any advice on how to enable building p4c offline?
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.
I dont think that building without internet is a supported usecase.
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.
There are ways to run FetchContent fully disconnected https://cmake.org/cmake/help/latest/module/FetchContent.html#command:fetchcontent_populate
At least I have been in scenarios where I did not have internet and was able to build as usual.
Now, populating
build/deps
in the beginning is a different story. But that is no different that runninggit submodule init --update
. I do not have a good suggestion there yet other than using the PREINSTALLED options.