Skip to content
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

Merged
merged 5 commits into from
Jul 25, 2023
Merged

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Jul 9, 2023

This PR install Protocol Buffers by pulling binaries with CMake Fetchcontent instead of relying on the system's version of Protobuf. This ensures that we have better control over the version of Protobuf that P4C is built with. The goal is to avoid build errors that occur frequently because of Protobuf.

Unfortunately, no prebuilt libraries exist for libprotobuf and libprotobuf-lite, so we build from scratch. Thankfully, there is not much to build and the impact on the installation time is negligible.

Partially prompted by the discussion in #4055.

Also fixes #2110, fixes #2856, fixes #3543.

@fruffy fruffy force-pushed the fruffy/protobuf_fetchcontent branch 6 times, most recently from 269dd1a to e400d7d Compare July 10, 2023 15:20
@fruffy fruffy marked this pull request as ready for review July 10, 2023 15:24
@fruffy fruffy changed the title [Wip] Install Protobuf using FetchContent. Install Protobuf using FetchContent. Jul 10, 2023
@fruffy fruffy requested a review from jafingerhut July 10, 2023 15:26
CMakeLists.txt Outdated
@@ -126,6 +130,8 @@ if (BUILD_STATIC_RELEASE)
# Do not bring in dynamic libstdcc and libgcc
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++ -Wl,-z,muldefs")
add_definitions(-DP4C_STATIC_BUILD)
else ()
set(BUILD_SHARED_LIBS ON)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, why is this used? I think this would interfere with all internal libraries we have in the build -- they would all be shared now, which is probably not what we want (or do we?). That would mean that we would need to distribute not only p4c binary, but also all its library dependencies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Internal libraries are already linked statically since we only build them this way, e.g.
https://github.com/p4lang/p4c/blob/main/ir/CMakeLists.txt#L66

The intention was to allow Protobuf to be linked dynamically to reduce binary size as per the discussion in #4055. But it is probably more prudent to always package it in to avoid lookup issues even though that may mean we get a huge binary.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. How much does the protobuf increase binary size? But I would still suggest using static protobuf.

)
endif()

fetchcontent_makeavailable(protoc)
Copy link
Contributor

Choose a reason for hiding this comment

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

should protoc be installed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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. make install will not include protoc since it is not a CMake package.

echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/${PROTOBUF_LIB}/bin:$PATH"' >> ~/.bash_profile
eecho 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile
Copy link
Contributor

Choose a reason for hiding this comment

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

I know you did not change this code, but unless there is some nonstandard eecho on macOS, this is a typo and therefore does not work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will do this in a separate PR.

@fruffy fruffy force-pushed the fruffy/protobuf_fetchcontent branch from bc940ad to f05f58b Compare July 19, 2023 13:09
Copy link
Contributor

@pkotikal pkotikal left a comment

Choose a reason for hiding this comment

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

Thank you!

@fruffy fruffy merged commit 56aa810 into main Jul 25, 2023
@fruffy fruffy deleted the fruffy/protobuf_fetchcontent branch July 25, 2023 18:58
else()
fetchcontent_declare(
protoc
URL https://github.com/protocolbuffers/protobuf/releases/download/v${P4C_PROTOBUF_VERSION}/protoc-${P4C_PROTOBUF_VERSION}-linux-x86_64.zip
Copy link
Member

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).

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

@rst0git rst0git Aug 10, 2023

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 include build/_deps with the source code.

Do you have any advice on how to enable building p4c offline?

Copy link
Contributor

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.

Copy link
Collaborator Author

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 running git submodule init --update. I do not have a good suggestion there yet other than using the PREINSTALLED options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants