From 6a144dafc2b473b32a569d49edf03f69a69768ae Mon Sep 17 00:00:00 2001 From: Fabian Ruffy <5960321+fruffy@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:56:50 +0200 Subject: [PATCH] Pull the libbpf library with FetchContent instead of submodules and a script. (#3970) --- .gitmodules | 4 --- backends/ebpf/CMakeLists.txt | 36 +++++++++++++++++++++--- backends/ebpf/build_libbpf | 6 ---- backends/ebpf/runtime/contrib/.gitignore | 1 + backends/ebpf/runtime/contrib/libbpf | 1 - cmake/Linters.cmake | 1 + tools/ci-build.sh | 5 ---- 7 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 backends/ebpf/runtime/contrib/.gitignore delete mode 160000 backends/ebpf/runtime/contrib/libbpf diff --git a/.gitmodules b/.gitmodules index eedc76ebeff..e69de29bb2d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +0,0 @@ -[submodule "backends/ebpf/runtime/contrib/libbpf"] - path = backends/ebpf/runtime/contrib/libbpf - url = https://github.com/libbpf/libbpf - ignore = dirty diff --git a/backends/ebpf/CMakeLists.txt b/backends/ebpf/CMakeLists.txt index 13c41e822f2..a3114e373a4 100644 --- a/backends/ebpf/CMakeLists.txt +++ b/backends/ebpf/CMakeLists.txt @@ -13,9 +13,34 @@ # limitations under the License. # CMakefile for the EBPF P4-16 back-end. -# To be included in the main P4C compiler CMakefile - -message(STATUS "Start configuring eBPF back end") +message(STATUS "Start configuring the eBPF back end") + +if(NOT APPLE) + # Fetch and declare the libbpf library. Print out download state while setting up libbpf. + set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET}) + set(FETCHCONTENT_QUIET OFF) + fetchcontent_declare( + bpfrepo + URL https://github.com/libbpf/libbpf/archive/refs/tags/v1.2.2.tar.gz + URL_HASH SHA256=32b0c41eabfbbe8e0c8aea784d7495387ff9171b5a338480a8fbaceb9da8d5e5 + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/runtime/contrib/libbpf + USES_TERMINAL_DOWNLOAD TRUE + GIT_PROGRESS TRUE + ) + fetchcontent_makeavailable(bpfrepo) + set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV}) + # Build libbpf as necessary. + add_custom_command( + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/runtime/usr/lib64/libbpf.a + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_libbpf + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Building libbpf..." + ) + add_custom_target(genlibbpf ALL + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/runtime/usr/lib64/libbpf.a + ) + message("Done with setting up libbpf for P4C.") +endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY) @@ -95,6 +120,9 @@ set (P4C_EBPF_DIST_HEADERS p4include/ebpf_model.p4) add_executable(p4c-ebpf ${P4C_EBPF_SRCS}) target_link_libraries (p4c-ebpf ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) add_dependencies(p4c-ebpf ir-generated frontend) +if (NOT APPLE) + add_dependencies(p4c-ebpf genlibbpf) +endif() install (TARGETS p4c-ebpf RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY}) @@ -193,7 +221,7 @@ endif() # check for the libbpf library find_library(LIBBPF NAMES bpf HINTS "${CMAKE_CURRENT_SOURCE_DIR}/runtime/usr/lib64/") if (LIBBPF) - message(STATUS "Found libbpf library") + message(STATUS "Found libbpf library at ${LIBBPF}") else() message(WARNING "Missing the libbpf dependency, disabling kernel tests." " You can install libbpf by running './build_libbpf' in the " diff --git a/backends/ebpf/build_libbpf b/backends/ebpf/build_libbpf index e07a19e53d1..b643fb4addd 100755 --- a/backends/ebpf/build_libbpf +++ b/backends/ebpf/build_libbpf @@ -35,12 +35,6 @@ def main() -> int: libbpf_src_dir = libbpf_dir.joinpath("src") libbpf_build_dir = libbpf_src_dir.joinpath("build") libbpf_target_dir = FILE_DIR.joinpath("runtime") - # Download submodule if it has not been initialized yet - git_cmd = f"cd {FILE_DIR} && git submodule update --init {libbpf_dir}" - # We ignore the error that is thrown here - result = testutils.exec_process(args=git_cmd, timeout=20, shell=True) - if result.returncode != testutils.SUCCESS: - testutils.log.error("Could not initialize libbpf submodule") # Create the libbpf build directory testutils.check_and_create_dir(libbpf_build_dir) # Build libbpf diff --git a/backends/ebpf/runtime/contrib/.gitignore b/backends/ebpf/runtime/contrib/.gitignore new file mode 100644 index 00000000000..cc3ea8dd440 --- /dev/null +++ b/backends/ebpf/runtime/contrib/.gitignore @@ -0,0 +1 @@ +libbpf diff --git a/backends/ebpf/runtime/contrib/libbpf b/backends/ebpf/runtime/contrib/libbpf deleted file mode 160000 index 051a4009f94..00000000000 --- a/backends/ebpf/runtime/contrib/libbpf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 051a4009f94d5633a8f734ca4235f0a78ee90469 diff --git a/cmake/Linters.cmake b/cmake/Linters.cmake index f55f9482009..2220a90fec5 100644 --- a/cmake/Linters.cmake +++ b/cmake/Linters.cmake @@ -95,6 +95,7 @@ file( tools/*.py ) list(FILTER P4C_PYTHON_LINT_LIST EXCLUDE REGEX "backends/p4tools/submodules") +list(FILTER P4C_PYTHON_LINT_LIST EXCLUDE REGEX "backends/ebpf/runtime/contrib/libbpf") list(FILTER P4C_PYTHON_LINT_LIST EXCLUDE REGEX "tools/cpplint.py") add_black_files(${P4C_SOURCE_DIR} "${P4C_PYTHON_LINT_LIST}") diff --git a/tools/ci-build.sh b/tools/ci-build.sh index 2792fa54f78..9a56667d94d 100755 --- a/tools/ci-build.sh +++ b/tools/ci-build.sh @@ -138,11 +138,6 @@ if [[ "${DISTRIB_RELEASE}" == "22.04" ]] ; then sudo pip3 install --upgrade grpcio==1.51.1 fi -# Build libbpf for eBPF tests. -pushd ${P4C_DIR} -backends/ebpf/build_libbpf -popd - # ! ------ BEGIN PTF_EBPF ----------------------------------------------- function install_ptf_ebpf_test_deps() ( P4C_PTF_PACKAGES="gcc-multilib \