From 791e743b635232eb0fa4545ac226cb29f0fe0ca4 Mon Sep 17 00:00:00 2001 From: fruffy Date: Sun, 9 Jul 2023 18:44:39 -0400 Subject: [PATCH 1/5] Install Protobuf using FetchContent. --- .github/workflows/ci-test.yml | 11 +-- CMakeLists.txt | 15 ++-- backends/dpdk/CMakeLists.txt | 3 + .../p4tools/modules/testgen/CMakeLists.txt | 8 +-- .../testgen/targets/bmv2/CMakeLists.txt | 6 ++ cmake/Protobuf.cmake | 70 +++++++++++++++++++ control-plane/CMakeLists.txt | 3 + tools/ci-build.sh | 2 +- tools/install_mac_deps.sh | 11 +-- 9 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 cmake/Protobuf.cmake diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index b9cfc5baee..f7f1fe672a 100644 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -202,18 +202,9 @@ jobs: echo "/usr/local/opt/bison/bin" >> $GITHUB_PATH - name: Build (MacOS) - # for some reason, brew does not install protobuf in a way cmake would be - # able to find it. Also, the library needs to be specified manually in - # order for the compiler to be correctly linked with protobuf. - # Also, there are no static protobuf libs in homebrew. - # See also https://github.com/Homebrew/homebrew-core/pull/122277 run: | - PROTOBUF=`brew --prefix --installed protobuf@21` ./bootstrap.sh -DENABLE_GC=ON -DCMAKE_BUILD_TYPE=RELEASE \ - -DCMAKE_UNITY_BUILD=ON \ - -DProtobuf_INCLUDE_DIR=$PROTOBUF/include \ - -DProtobuf_LIBRARY=$PROTOBUF/lib/libprotobuf.dylib \ - -DENABLE_PROTOBUF_STATIC=OFF + -DCMAKE_UNITY_BUILD=ON make -Cbuild -j2 - name: Run tests (MacOS) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40361f1c69..0ee027c433 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,19 +128,14 @@ if (BUILD_STATIC_RELEASE) add_definitions(-DP4C_STATIC_BUILD) endif () -# required tools and libraries +# Required tools and libraries. find_package (PythonInterp 3 REQUIRED) find_package (FLEX 2.0 REQUIRED) find_package (BISON 3.0 REQUIRED) -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 () -# The boost graph headers are optional and only required by the graphs backend +include(Protobuf) +p4c_obtain_protobuf() + +# The boost graph headers are optional and only required by the graphs back end. find_package (Boost QUIET COMPONENTS graph) if (Boost_FOUND) set (HAVE_LIBBOOST_GRAPH 1) diff --git a/backends/dpdk/CMakeLists.txt b/backends/dpdk/CMakeLists.txt index 71441a1c17..29f20b2ae6 100644 --- a/backends/dpdk/CMakeLists.txt +++ b/backends/dpdk/CMakeLists.txt @@ -46,6 +46,9 @@ add_custom_command( ) add_library(dpdk_runtime STATIC ${DPDK_P4RUNTIME_INFO_GEN_SRCS}) +target_include_directories(dpdk_runtime + SYSTEM BEFORE PUBLIC ${Protobuf_INCLUDE_DIR} +) set(P4C_DPDK_SOURCES ../bmv2/common/lower.cpp diff --git a/backends/p4tools/modules/testgen/CMakeLists.txt b/backends/p4tools/modules/testgen/CMakeLists.txt index 23cf886f1d..1db051166c 100644 --- a/backends/p4tools/modules/testgen/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/CMakeLists.txt @@ -74,12 +74,13 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(inja) -# Testgen libraries. +# Testgen libraries and includes. Defined here such that targets can extend them. set( TESTGEN_LIBS PRIVATE p4tools-common PUBLIC inja ) +set(TESTGEN_INCLUDES) file(GLOB testgen_targets RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/targets ${CMAKE_CURRENT_SOURCE_DIR}/targets/*) foreach(ext ${testgen_targets}) @@ -104,10 +105,6 @@ endforeach(ext) # Propagate def files set by target extensions upwards. set(IR_DEF_FILES ${IR_DEF_FILES} PARENT_SCOPE) -# Convert the list of files into #includes -foreach(include_file ${include_files}) -endforeach() - # Fill the template configure_file(register.h.in register.h) @@ -115,6 +112,7 @@ configure_file(register.h.in register.h) add_p4tools_library(testgen ${TESTGEN_SOURCES}) # Make sure the testgen library and anything that links to it see both the Z3 and Inja includes. target_link_libraries(testgen PUBLIC p4tools-common inja) +target_include_directories(testgen ${TESTGEN_INCLUDES}) # The executable. add_p4tools_executable(p4testgen main.cpp) diff --git a/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt b/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt index 234adee0ba..53bc1239a4 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/targets/bmv2/CMakeLists.txt @@ -36,3 +36,9 @@ set( TESTGEN_LIBS ${TESTGEN_LIBS} PARENT_SCOPE ) + +set( + TESTGEN_INCLUDES ${TESTGEN_INCLUDES} + SYSTEM BEFORE PUBLIC ${Protobuf_INCLUDE_DIR} + PARENT_SCOPE +) diff --git a/cmake/Protobuf.cmake b/cmake/Protobuf.cmake new file mode 100644 index 0000000000..9ef941f8d9 --- /dev/null +++ b/cmake/Protobuf.cmake @@ -0,0 +1,70 @@ +macro(p4c_obtain_protobuf) + option( + TOOLS_USE_PREINSTALLED_PROTOBUF + "Look for a preinstalled version of Protobuf instead of installing a prebuilt binary using FetchContent." + OFF + ) + + if(TOOLS_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 P4Tools...") + + # Print out download state while setting up Z3. + 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.") + # 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 binary for MacOS. + 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(protobuf protoc) + # 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) diff --git a/control-plane/CMakeLists.txt b/control-plane/CMakeLists.txt index 441aeb516d..15ef916dc1 100644 --- a/control-plane/CMakeLists.txt +++ b/control-plane/CMakeLists.txt @@ -95,6 +95,9 @@ set (CONTROLPLANE_HDRS ) add_library(controlplane-gen OBJECT ${P4RUNTIME_GEN_SRCS}) +target_include_directories(controlplane-gen + SYSTEM BEFORE PUBLIC ${Protobuf_INCLUDE_DIR} +) include_directories (${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) # Silence -Warray-bounds as the root issue is out of our control: https://github.com/protocolbuffers/protobuf/issues/7140 diff --git a/tools/ci-build.sh b/tools/ci-build.sh index a4231d800f..7c01633cbe 100755 --- a/tools/ci-build.sh +++ b/tools/ci-build.sh @@ -96,7 +96,7 @@ if [[ "${DISTRIB_RELEASE}" == "18.04" ]] || [[ "$(which simple_switch 2> /dev/nu # Use GCC 9 from https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test sudo apt-get update && sudo apt-get install -y software-properties-common sudo add-apt-repository -uy ppa:ubuntu-toolchain-r/test - P4C_DEPS+=" libprotobuf-dev protobuf-compiler gcc-9 g++-9" + P4C_DEPS+=" gcc-9 g++-9" export CC=gcc-9 export CXX=g++-9 else diff --git a/tools/install_mac_deps.sh b/tools/install_mac_deps.sh index 96df877b25..27cb514375 100755 --- a/tools/install_mac_deps.sh +++ b/tools/install_mac_deps.sh @@ -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="boost@1.76" $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" From f2eaba1df29fd11846d204b1db20b8b784fdb05e Mon Sep 17 00:00:00 2001 From: fruffy Date: Mon, 10 Jul 2023 10:03:22 -0400 Subject: [PATCH 2/5] Comments/Cleanup. --- README.md | 26 +++++++++++--------------- cmake/Protobuf.cmake | 14 ++++++++------ tools/install_fedora_deps.sh | 2 -- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 36160debc9..beafb91294 100644 --- a/README.md +++ b/README.md @@ -191,8 +191,9 @@ sudo dpkg -i /path/to/package.deb library. Default is ON. - `-DENABLE_GTESTS=ON|OFF`. Enable building and running GTest unit tests. Default is ON. - - `-DENABLE_PROTOBUF_STATIC=ON|OFF`. Enable the use of static - protobuf libraries. Default is ON. + - `-DP4C_USE_PREINSTALLED_PROTOBUF=ON|OFF`. Try to find a system version of Protobuf instead of a CMake version. + - `-DENABLE_PROTOBUF_STATIC=ON|OFF`. Enable the use of static protobuf libraries. Default is ON. + Only has an effect when `P4C_USE_PREINSTALLED_PROTOBUF` is enabled. - `-DENABLE_MULTITHREAD=ON|OFF`. Use multithreading. Default is OFF. - `-DBUILD_LINK_WITH_GOLD=ON|OFF`. Use Gold linker for build if available. @@ -271,18 +272,16 @@ For documentation building: `p4c` also depends on Google Protocol Buffers (Protobuf). `p4c` requires version 3.0 or higher, so the packaged version provided in Ubuntu 20.04 **should** -work. However, all our CI testing is done with a more recent version of Protobuf -(at the moment, 3.18.1), which we install from source. If you are experiencing -issues with the Protobuf version shipped with your OS distribution, we recommend -that we install Protobuf 3.18.1 from source. You can find instructions -[here](https://github.com/protocolbuffers/protobuf/blob/v3.18.1/src/README.md). -After cloning Protobuf and before you build, check-out version 3.18.1: +work. However, P4C typically installs its own version of Protobuf using CMake's Fetchcontent module +(at the moment, 3.21.12). If you are experiencing issues with the Protobuf version shipped with your OS distribution, we recommend that to install Protobuf 3.21.12 from source. You can find instructions +[here](https://github.com/protocolbuffers/protobuf/blob/v3.21.12/src/README.md). +After cloning Protobuf and before you build, check-out version 3.21.12: -`git checkout v3.18.1` +`git checkout v3.21.12` Please note that while all Protobuf versions newer than 3.0 should work for `p4c` itself, you may run into trouble with some extensions and other p4lang -projects unless you install version 3.18.1. +projects unless you install version 3.21.12. ### CMake p4c requires a CMake version of at least 3.16.3 or higher. On older systems, a newer version of CMake can be installed using `pip3 install --user cmake==3.16.3`. We have a CI test on Ubuntu 18.04 that uses this option, but there is no guarantee that this will lead to a successful build. @@ -292,7 +291,7 @@ p4c requires a CMake version of at least 3.16.3 or higher. On older systems, a n ```bash sudo dnf install -y cmake g++ git automake libtool gc-devel bison flex \ libfl-devel gmp-devel boost-devel boost-iostreams boost-graph llvm pkg-config \ -python3 python3-pip tcpdump protobuf-devel protobuf-static +python3 python3-pip tcpdump sudo pip3 install -r requirements.txt ``` @@ -343,10 +342,7 @@ Installing on macOS: ``` Homebrew offers a `protobuf` formula. It installs version 3.2, which should work for p4c itself but may cause problems with some extensions. It's - preferable to install Protocol Buffers 3.0 from source using the instructions - [here](https://github.com/google/protobuf/blob/master/src/README.md). Check - out the newest tag in the 3.0 series (`v3.0.2` as of this writing) before you - build. + preferable to use the version of Protobuf which is supplied with CMake's fetchcontent (3.21.12). The `protobuf` formula requires the following CMake variables to be set, otherwise CMake does not find the libraries or fails in linking. It is likely diff --git a/cmake/Protobuf.cmake b/cmake/Protobuf.cmake index 9ef941f8d9..bf6e8464d9 100644 --- a/cmake/Protobuf.cmake +++ b/cmake/Protobuf.cmake @@ -1,11 +1,12 @@ macro(p4c_obtain_protobuf) option( - TOOLS_USE_PREINSTALLED_PROTOBUF - "Look for a preinstalled version of Protobuf instead of installing a prebuilt binary using FetchContent." + P4C_USE_PREINSTALLED_PROTOBUF + "Look for a preinstalled version of Protobuf in the system instead of installing a prebuilt binary using FetchContent." OFF ) - if(TOOLS_USE_PREINSTALLED_PROTOBUF) + # 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) @@ -19,9 +20,9 @@ macro(p4c_obtain_protobuf) # 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 P4Tools...") + message("Fetching Protobuf version ${P4C_PROTOBUF_VERSION} for P4C...") - # Print out download state while setting up Z3. + # 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.") @@ -36,7 +37,8 @@ macro(p4c_obtain_protobuf) USES_TERMINAL_DOWNLOAD TRUE GIT_PROGRESS TRUE ) - # Pull a different binary for MacOS. + # Pull a different protoc binary for MacOS. + # TODO: Should we build from scratch? if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") fetchcontent_declare( protoc diff --git a/tools/install_fedora_deps.sh b/tools/install_fedora_deps.sh index 9e279cb62a..1b1dd1df2d 100755 --- a/tools/install_fedora_deps.sh +++ b/tools/install_fedora_deps.sh @@ -38,8 +38,6 @@ sudo dnf install -y -q \ openssl-devel \ pkg-config \ procps-ng \ - protobuf-devel \ - protobuf-static \ python3 \ python3-pip \ python3-thrift \ From cfa0f900ee41dcec14b9f531aa41ce2a68cc2d5d Mon Sep 17 00:00:00 2001 From: fruffy Date: Mon, 10 Jul 2023 17:08:15 -0400 Subject: [PATCH 3/5] Exclude Protobuf from the install step. --- cmake/Protobuf.cmake | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmake/Protobuf.cmake b/cmake/Protobuf.cmake index bf6e8464d9..8840cad95b 100644 --- a/cmake/Protobuf.cmake +++ b/cmake/Protobuf.cmake @@ -57,7 +57,14 @@ macro(p4c_obtain_protobuf) ) endif() - fetchcontent_makeavailable(protobuf protoc) + fetchcontent_makeavailable(protoc) + # Exclude Protobuf from the main make install step. We only want to use it locally. + FetchContent_GetProperties(protobuf) + if(NOT protobuf_POPULATED) + FetchContent_Populate(protobuf) + add_subdirectory(${protobuf_SOURCE_DIR} ${protobuf_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() + # Reset unity builds to the previous state... set(CMAKE_UNITY_BUILD ${SAVED_CMAKE_UNITY_BUILD}) set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV}) From cbb3ab0fb0624e9363b440344cd63a6132888a35 Mon Sep 17 00:00:00 2001 From: fruffy Date: Tue, 11 Jul 2023 09:43:07 -0400 Subject: [PATCH 4/5] Introduce a helper function, always build with position-independent code. --- CMakeLists.txt | 6 ++++++ backends/p4tools/modules/testgen/CMakeLists.txt | 2 +- cmake/P4CUtils.cmake | 9 +++++++++ cmake/Protobuf.cmake | 9 ++------- control-plane/CMakeLists.txt | 3 ++- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ee027c433..6c9e19a202 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,10 @@ endif() if (CMAKE_UNITY_BUILD) set(CMAKE_UNITY_BUILD_BATCH_SIZE 10 CACHE UNINITIALIZED "Set the unity build batch size.") endif () + +# Always build position-independent code. This is important when linking with Protobuf. +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + # set the required options for a static release build if (BUILD_STATIC_RELEASE) message(STATUS "Building static release binaries") @@ -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) endif () # Required tools and libraries. diff --git a/backends/p4tools/modules/testgen/CMakeLists.txt b/backends/p4tools/modules/testgen/CMakeLists.txt index 1db051166c..27bce80105 100644 --- a/backends/p4tools/modules/testgen/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/CMakeLists.txt @@ -72,7 +72,7 @@ FetchContent_Declare( GIT_TAG 3741c73ba78babd2ed88f2acf2fcd6dafdb878e8 GIT_PROGRESS TRUE ) -FetchContent_MakeAvailable(inja) +fetchcontent_makeavailable_but_exclude_install(inja) # Testgen libraries and includes. Defined here such that targets can extend them. set( diff --git a/cmake/P4CUtils.cmake b/cmake/P4CUtils.cmake index ad2913fd42..83b91f50eb 100644 --- a/cmake/P4CUtils.cmake +++ b/cmake/P4CUtils.cmake @@ -405,3 +405,12 @@ macro(p4c_add_xfail_reason tag reason) endif() endforeach() endmacro(p4c_add_xfail_reason) + +# Effectively emulates fetchcontent_makeavailable but does not add the module to install. +macro(fetchcontent_makeavailable_but_exclude_install content) + FetchContent_GetProperties(${content}) + if(NOT ${content}_POPULATED) + FetchContent_Populate(${content}) + add_subdirectory(${${content}_SOURCE_DIR} ${${content}_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() +endmacro(fetchcontent_makeavailable_but_exclude_install) diff --git a/cmake/Protobuf.cmake b/cmake/Protobuf.cmake index 8840cad95b..f0eb6f5602 100644 --- a/cmake/Protobuf.cmake +++ b/cmake/Protobuf.cmake @@ -43,8 +43,7 @@ macro(p4c_obtain_protobuf) 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 + USES_TERMINAL_DOWNLOAD TRUE GIT_PROGRESS TRUE ) else() @@ -59,11 +58,7 @@ macro(p4c_obtain_protobuf) fetchcontent_makeavailable(protoc) # Exclude Protobuf from the main make install step. We only want to use it locally. - FetchContent_GetProperties(protobuf) - if(NOT protobuf_POPULATED) - FetchContent_Populate(protobuf) - add_subdirectory(${protobuf_SOURCE_DIR} ${protobuf_BINARY_DIR} EXCLUDE_FROM_ALL) - endif() + fetchcontent_makeavailable_but_exclude_install(protobuf) # Reset unity builds to the previous state... set(CMAKE_UNITY_BUILD ${SAVED_CMAKE_UNITY_BUILD}) diff --git a/control-plane/CMakeLists.txt b/control-plane/CMakeLists.txt index 15ef916dc1..e8ea27c939 100644 --- a/control-plane/CMakeLists.txt +++ b/control-plane/CMakeLists.txt @@ -98,11 +98,12 @@ 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}) include_directories (${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) # Silence -Warray-bounds as the root issue is out of our control: https://github.com/protocolbuffers/protobuf/issues/7140 set_source_files_properties (${P4RUNTIME_GEN_SRCS} PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter -Wno-array-bounds") add_library (controlplane STATIC ${CONTROLPLANE_SRCS} ) -target_link_libraries (controlplane ${PROTOBUF_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} controlplane-gen) +target_link_libraries (controlplane ${CMAKE_THREAD_LIBS_INIT} controlplane-gen) add_dependencies (controlplane mkP4configdir ir-generated controlplane-gen frontend-parser-gen) From f05f58beb6cf78eebd4110d98d9cf32c48871b4b Mon Sep 17 00:00:00 2001 From: fruffy Date: Tue, 11 Jul 2023 10:16:20 -0400 Subject: [PATCH 5/5] Make sure the BMv2 back end library is built statically, like all other P4C libraries. Revert dynamic libs. Force static local Protobuf. --- CMakeLists.txt | 2 -- backends/bmv2/CMakeLists.txt | 2 +- cmake/Protobuf.cmake | 2 ++ 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c9e19a202..b6f3d840bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,8 +130,6 @@ 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) endif () # Required tools and libraries. diff --git a/backends/bmv2/CMakeLists.txt b/backends/bmv2/CMakeLists.txt index 8b8bc909eb..cb65307324 100644 --- a/backends/bmv2/CMakeLists.txt +++ b/backends/bmv2/CMakeLists.txt @@ -82,7 +82,7 @@ set (BMV2_BACKEND_COMMON_HDRS set (IR_DEF_FILES ${IR_DEF_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/bmv2.def PARENT_SCOPE) -add_library(bmv2backend ${P4C_STATIC_BUILD} ${BMV2_BACKEND_COMMON_SRCS}) +add_library(bmv2backend STATIC ${BMV2_BACKEND_COMMON_SRCS}) add_dependencies(bmv2backend ir-generated frontend) add_executable(p4c-bm2-ss ${BMV2_SIMPLE_SWITCH_SRCS}) diff --git a/cmake/Protobuf.cmake b/cmake/Protobuf.cmake index f0eb6f5602..aaff03f638 100644 --- a/cmake/Protobuf.cmake +++ b/cmake/Protobuf.cmake @@ -27,6 +27,8 @@ macro(p4c_obtain_protobuf) 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)