From 8c8a2855ddfbaf934c10fede60b6d0452b7cbfa3 Mon Sep 17 00:00:00 2001 From: Derek G Foster Date: Sat, 30 Sep 2023 15:44:35 -0700 Subject: [PATCH 1/2] Overhaul cmake interface to Tofino SDE - Replace the tofino-driver.cmake include file with FindTofinoDriver.cmake, a find-module that allows us to use `find_package(TofinoDriver)` to define namespaced cmake targets for the Tofino SDE. - Define an `add_tofino_target_libraries()` function that provides an abstract interface to set the properties a target needs in order to compile and link with the Tofino SDE. - Update krnlmon submodule reference. Signed-off-by: Derek G Foster --- CMakeLists.txt | 2 +- cmake/FindTofinoDriver.cmake | 117 +++++++++++++++++++++++++++++++++++ cmake/tofino-driver.cmake | 51 --------------- infrap4d/CMakeLists.txt | 2 +- 4 files changed, 119 insertions(+), 53 deletions(-) create mode 100644 cmake/FindTofinoDriver.cmake delete mode 100644 cmake/tofino-driver.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f72cf83e..5b62b8cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,7 +149,7 @@ if(DPDK_TARGET) elseif(ES2K_TARGET) find_package(Es2kDriver) elseif(TOFINO_TARGET) - include(tofino-driver) + find_package(TofinoDriver) endif() if(WITH_OVSP4RT) diff --git a/cmake/FindTofinoDriver.cmake b/cmake/FindTofinoDriver.cmake new file mode 100644 index 00000000..2a67ea3b --- /dev/null +++ b/cmake/FindTofinoDriver.cmake @@ -0,0 +1,117 @@ +# FindTofinoDriver.cmake - import Tofino P4 Driver (SDE). +# +# Copyright 2022-2023 Intel Corporation +# SPDX-License-Identifier: Apache 2.0 +# + +# This module requires that SDE_INSTALL_DIR be defined. +# It needs additional work to make it a proper CMake find-module. + +#----------------------------------------------------------------------- +# Define SDE_INCLUDE_DIR +#----------------------------------------------------------------------- +find_path(SDE_INCLUDE_DIR + NAMES "bf_switchd/bf_switchd.h" + PATHS ${SDE_INSTALL_DIR}/include +) +if(NOT SDE_INCLUDE_DIR) + message(FATAL_ERROR "bf_switchd.h not found") +endif() +mark_as_advanced(SDE_INCLUDE_DIR) + +#----------------------------------------------------------------------- +# Find libraries +#----------------------------------------------------------------------- +find_library(LIBDRIVER driver) +if(NOT LIBDRIVER) + message(FATAL_ERROR "sde::driver library not found") +endif() +mark_as_advanced(LIBDRIVER) + +find_library(LIBTDI tdi) +if(NOT LIBTDI) + message(FATAL_ERROR "sde::tdi library not found") +endif() +mark_as_advanced(LIBTDI) + +find_library(LIBTDI_JSON_PARSER tdi_json_parser) +if(NOT LIBTDI_JSON_PARSER) + message(FATAL_ERROR "sde::tdi_json_parser library not found") +endif() +mark_as_advanced(LIBTDI_JSON_PARSER) + +find_library(LIBTARGET_SYS target_sys) +if(NOT LIBTARGET_SYS) + message(FATAL_ERROR "sde::target_sys library not found") +endif() +mark_as_advanced(LIBTARGET_SYS) + +find_library(LIBTARGET_UTILS target_utils) +if(NOT LIBTARGET_UTILS) + message(FATAL_ERROR "sde::target_utils library not found") +endif() +mark_as_advanced(LIBTARGET_UTILS) + +#----------------------------------------------------------------------- +# Get version number +#----------------------------------------------------------------------- +if(EXISTS ${SDE_INSTALL_DIR}/share/VERSION) + file(READ ${SDE_INSTALL_DIR}/share/VERSION _sde_version) + string(STRIP "${_sde_version}" _sde_version) + if(NOT _sde_version STREQUAL "") + set(SDE_VERSION "${_sde_version}" CACHE STRING "Tofino SDE version") + mark_as_advanced(SDE_VERSION) + endif() + unset(_sde_version) +endif() + +#----------------------------------------------------------------------- +# Define library targets +#----------------------------------------------------------------------- +add_library(sde::driver UNKNOWN IMPORTED) +set_target_properties(sde::driver PROPERTIES + IMPORTED_LOCATION ${LIBDRIVER} + INTERFACE_INCLUDE_DIRECTORIES ${SDE_INCLUDE_DIR} + IMPORTED_LINK_INTERFACE_LANGUAGES C) + +add_library(sde::tdi UNKNOWN IMPORTED) +set_target_properties(sde::tdi PROPERTIES + IMPORTED_LOCATION ${LIBTDI} + INTERFACE_INCLUDE_DIRECTORIES ${SDE_INCLUDE_DIR} + IMPORTED_LINK_INTERFACE_LANGUAGES CXX) + +add_library(sde::tdi_json_parser UNKNOWN IMPORTED) +set_target_properties(sde::tdi_json_parser PROPERTIES + IMPORTED_LOCATION ${LIBTDI_JSON_PARSER} + INTERFACE_INCLUDE_DIRECTORIES ${SDE_INCLUDE_DIR} + IMPORTED_LINK_INTERFACE_LANGUAGES CXX) + +add_library(sde::target_sys UNKNOWN IMPORTED) +set_target_properties(sde::target_sys PROPERTIES + IMPORTED_LOCATION ${LIBTARGET_SYS} + INTERFACE_INCLUDE_DIRECTORIES ${SDE_INCLUDE_DIR} + IMPORTED_LINK_INTERFACE_LANGUAGES C) + +add_library(sde::target_utils UNKNOWN IMPORTED) +set_target_properties(sde::target_utils PROPERTIES + IMPORTED_LOCATION ${LIBTARGET_UTILS} + INTERFACE_INCLUDE_DIRECTORIES ${SDE_INCLUDE_DIR} + IMPORTED_LINK_INTERFACE_LANGUAGES C) + +#----------------------------------------------------------------------- +# Define SDE_LIBRARY_DIRS +#----------------------------------------------------------------------- +set(SDE_LIBRARY_DIRS ${SDE_INSTALL_DIR}/lib) + +#----------------------------------------------------------------------- +# Apply SDE properties to target +#----------------------------------------------------------------------- +function(add_tofino_target_libraries TGT) + target_link_libraries(${TGT} PUBLIC + sde::driver + sde::target_sys + sde::tdi + sde::tdi_json_parser + ) + target_link_directories(${TGT} PUBLIC ${SDE_LIBRARY_DIRS}) +endfunction() diff --git a/cmake/tofino-driver.cmake b/cmake/tofino-driver.cmake deleted file mode 100644 index 8eb2c5b4..00000000 --- a/cmake/tofino-driver.cmake +++ /dev/null @@ -1,51 +0,0 @@ -# Definitions for the Tofino P4 driver package. -# -# Copyright 2022-2023 Intel Corporation -# SPDX-License-Identifier: Apache 2.0 -# - -# The definitions are encapsulated in a function to limit pollution of the -# global namespace. -function(define_tofino_driver _LIBS) - - ############# - # Libraries # - ############# - - # driver - find_library(LIBDRIVER driver REQUIRED) - add_library(driver SHARED IMPORTED) - set_property(TARGET driver PROPERTY IMPORTED_LOCATION ${LIBDRIVER}) - - # target_sys - find_library(LIBTARGET_SYS target_sys REQUIRED) - add_library(target_sys SHARED IMPORTED) - set_property(TARGET target_sys - PROPERTY IMPORTED_LOCATION ${LIBTARGET_SYS}) - - # tdi - find_library(LIBTDI tdi REQUIRED) - add_library(tdi SHARED IMPORTED) - set_property(TARGET tdi PROPERTY IMPORTED_LOCATION ${LIBTDI}) - - # tdi_json_parser - find_library(LIBTDI_JSON_PARSER tdi_json_parser REQUIRED) - add_library(tdi_json_parser SHARED IMPORTED) - set_property(TARGET tdi_json_parser - PROPERTY IMPORTED_LOCATION ${LIBTDI_JSON_PARSER}) - - ############# - # Variables # - ############# - - set(${_LIBS} - driver - tdi - tdi_json_parser - target_sys - PARENT_SCOPE - ) - -endfunction(define_tofino_driver) - -define_tofino_driver(DRIVER_SDK_LIBS) diff --git a/infrap4d/CMakeLists.txt b/infrap4d/CMakeLists.txt index 7188a520..4630d963 100644 --- a/infrap4d/CMakeLists.txt +++ b/infrap4d/CMakeLists.txt @@ -36,7 +36,7 @@ if(DPDK_TARGET) elseif(ES2K_TARGET) add_es2k_target_libraries(infrap4d) elseif(TOFINO_TARGET) - target_link_libraries(infrap4d PUBLIC ${DRIVER_SDK_LIBS}) + add_tofino_target_libraries(infrap4d) endif() if(WITH_KRNLMON) From 7ba9aaa757dbbeb4393483c5fa8ea91b563a8d4d Mon Sep 17 00:00:00 2001 From: Derek G Foster Date: Mon, 2 Oct 2023 19:48:28 -0700 Subject: [PATCH 2/2] Correct copyright date Signed-off-by: Derek G Foster --- cmake/FindTofinoDriver.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/FindTofinoDriver.cmake b/cmake/FindTofinoDriver.cmake index 2a67ea3b..35fff683 100644 --- a/cmake/FindTofinoDriver.cmake +++ b/cmake/FindTofinoDriver.cmake @@ -1,6 +1,6 @@ # FindTofinoDriver.cmake - import Tofino P4 Driver (SDE). # -# Copyright 2022-2023 Intel Corporation +# Copyright 2023 Intel Corporation # SPDX-License-Identifier: Apache 2.0 #