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

Fix some problems with the Protobuf CMake file #4262

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Set the required options for a (mostly) static release build.
if(BUILD_STATIC_RELEASE_SANS_GLIBC)
message(STATUS "Building static release binaries")
message(WARNING "glibc is linked dynamically in current static builds.")
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
# Link Boost statically
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ add_custom_target (dpdk_runtime_dir
# placed in the build directory inside `control-plane` directory
add_custom_command(
OUTPUT ${DPDK_P4RUNTIME_INFO_GEN_SRCS} ${DPDK_P4RUNTIME_INFO_GEN_HDRS}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
Copy link
Collaborator Author

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 of PROTOBUF_.

https://cmake.org/cmake/help/latest/module/FindProtobuf.html

--proto_path ${DPDK_P4RUNTIME_DIR}
--proto_path ${P4RUNTIME_STD_DIR}
--cpp_out ${CMAKE_CURRENT_BINARY_DIR}/p4/config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function(validate_protobuf testfile testfolder)
file(APPEND ${testfile} "for item in \${txtpbfiles[@]}\n")
file(APPEND ${testfile} "do\n")
file(APPEND ${testfile} "\techo \"Found \${item}\"\n")
file(APPEND ${testfile} "\t${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_LIST_DIR}/../proto --proto_path=${P4RUNTIME_STD_DIR} --proto_path=${P4C_SOURCE_DIR}/control-plane --encode=p4testgen.TestCase p4testgen.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "\t${PROTOC_BINARY} -I${Protobuf_INCLUDE_DIR} -I${CMAKE_CURRENT_LIST_DIR}/../proto -I${P4RUNTIME_STD_DIR} -I${P4C_SOURCE_DIR}/control-plane --encode=p4testgen.TestCase p4testgen.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "done\n")
endfunction(validate_protobuf)

Expand All @@ -60,7 +60,7 @@ function(validate_protobuf_ir testfile testfolder)
file(APPEND ${testfile} "for item in \${txtpbfiles[@]}\n")
file(APPEND ${testfile} "do\n")
file(APPEND ${testfile} "\techo \"Found \${item}\"\n")
file(APPEND ${testfile} "\t${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_LIST_DIR}/../proto --proto_path=${P4RUNTIME_STD_DIR} --proto_path=${P4C_SOURCE_DIR} --proto_path=${P4C_SOURCE_DIR}/control-plane --encode=p4testgen_ir.TestCase p4testgen_ir.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "\t${PROTOC_BINARY} -I${Protobuf_INCLUDE_DIR} -I${CMAKE_CURRENT_LIST_DIR}/../proto -I${P4RUNTIME_STD_DIR} -I${P4C_SOURCE_DIR} -I${P4C_SOURCE_DIR}/control-plane --encode=p4testgen_ir.TestCase p4testgen_ir.proto < \${item} > /dev/null\n")
file(APPEND ${testfile} "done\n")
endfunction(validate_protobuf_ir)

Expand Down
88 changes: 42 additions & 46 deletions cmake/Protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Copy link
Collaborator Author

@fruffy fruffy Nov 27, 2023

Choose a reason for hiding this comment

The 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 grpc_cpp_plugin.

# 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
Expand All @@ -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)
6 changes: 3 additions & 3 deletions control-plane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ set (P4RUNTIME_GEN_PYTHON "control-plane")
add_custom_target (mkP4configdir
${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/p4/config)
add_custom_command(OUTPUT ${P4RUNTIME_GEN_SRCS} ${P4RUNTIME_GEN_HDRS}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
-I ${P4RUNTIME_STD_DIR} -I${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
-I${P4RUNTIME_STD_DIR} -I${CMAKE_CURRENT_SOURCE_DIR} -I${Protobuf_INCLUDE_DIR}
vlstill marked this conversation as resolved.
Show resolved Hide resolved
--cpp_out ${CMAKE_CURRENT_BINARY_DIR}
--python_out ${CMAKE_CURRENT_BINARY_DIR}
--experimental_allow_proto3_optional
Expand Down Expand Up @@ -96,7 +96,7 @@ add_library(controlplane-gen OBJECT ${P4RUNTIME_GEN_SRCS})
target_include_directories(controlplane-gen
SYSTEM BEFORE PUBLIC ${Protobuf_INCLUDE_DIR}
)
target_link_libraries (controlplane-gen PUBLIC ${PROTOBUF_LIBRARY})
target_link_libraries (controlplane-gen PUBLIC ${Protobuf_LIBRARY})
# Needed for the correct import of google/status.pb.cc
target_include_directories(
controlplane-gen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
Expand Down
Loading