Skip to content

Commit

Permalink
Formalize logic to import Stratum dependencies
Browse files Browse the repository at this point in the history
- Consolidate the find_package() commands for the packages
  used by Stratum into StratumDependencies.cmake, and include
  the latter from the top-level listfile.

- Replace library names with namespaced library targets.

- Define WITH_OPENSSL variable to specify whether to link against
  OpenSSL.

- Display Abseil and gRPC package versions

- Rework logic to find the paths to the host protobuf compiler
  and the gRPC C++ plugin.

- Set the ADVANCED attribute on cache variables that should not
  normally be displayed in cmake GUIs.

Signed-off-by: Derek G Foster <[email protected]>
  • Loading branch information
ffoulkes committed Oct 3, 2023
1 parent cdd28a5 commit ce6aa9c
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 63 deletions.
24 changes: 11 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ set(PB_OUT_DIR "${CMAKE_BINARY_DIR}/pb.out" CACHE PATH
"Path to generated Protobuf files")
file(MAKE_DIRECTORY ${PB_OUT_DIR})

############################
# Target selection options #
############################

include(SelectTdiTarget)

###################
# Feature toggles #
###################

option(SET_RPATH "Set RPATH in libraries and executables" OFF)

option(WITH_KRNLMON "Enable Kernel Monitor support" ON)
option(WITH_OVSP4RT "Enable OVS support" ON)

############################
# Target selection options #
############################

include(SelectTdiTarget)

if(TOFINO_TARGET)
set(WITH_KRNLMON OFF)
set(WITH_OVSP4RT OFF)
Expand All @@ -76,6 +75,10 @@ endif()
cmake_print_variables(WITH_KRNLMON)
cmake_print_variables(WITH_OVSP4RT)

if(WITH_OVSP4RT AND OVS_INSTALL_DIR STREQUAL "")
message(FATAL_ERROR "OVS_INSTALL_DIR (OVS_INSTALL) not defined!")
endif()

###########################
# Global compiler options #
###########################
Expand Down Expand Up @@ -140,9 +143,7 @@ cmake_print_variables(CMAKE_PREFIX_PATH)
# External packages #
#####################

find_package(absl CONFIG REQUIRED)
set(GFLAGS_USE_TARGET_NAMESPACE TRUE)
find_package(gflags CONFIG REQUIRED)
include(StratumDependencies)

if(DPDK_TARGET)
include(dpdk-driver)
Expand All @@ -153,9 +154,6 @@ elseif(TOFINO_TARGET)
endif()

if(WITH_OVSP4RT)
if(OVS_INSTALL_DIR STREQUAL "")
message(FATAL_ERROR "OVS_INSTALL_DIR (OVS_INSTALL) not defined!")
endif()
find_package(OVS)
endif()

Expand Down
16 changes: 8 additions & 8 deletions clients/gnmi-ctl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ set_install_rpath(gnmi-ctl ${EXEC_ELEMENT} ${DEP_ELEMENT})
target_link_libraries(gnmi-ctl
PUBLIC
stratum_static
gnmi_proto grpc_proto
grpc protobuf gflags grpc++
pthread re2
gnmi_proto
grpc_proto
gflags::gflags_shared
gRPC::grpc
gRPC::grpc++
gRPC::re2
protobuf::libprotobuf
pthread
)

target_include_directories(gnmi-ctl
Expand All @@ -30,11 +35,6 @@ target_include_directories(gnmi-ctl
${PB_OUT_DIR}
)

add_dependencies(gnmi-ctl
gnmi_proto
grpc_proto
)

if(DPDK_TARGET)
install(TARGETS gnmi-ctl RUNTIME)
endif()
16 changes: 8 additions & 8 deletions clients/sgnmi_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ set_install_rpath(sgnmi_cli ${EXEC_ELEMENT} ${DEP_ELEMENT})
target_link_libraries(sgnmi_cli
PUBLIC
stratum_static
gnmi_proto grpc_proto
grpc protobuf gflags grpc++
pthread re2
gnmi_proto
grpc_proto
gflags::gflags_shared
gRPC::grpc
gRPC::grpc++
gRPC::re2
protobuf::libprotobuf
pthread
)

if(HAVE_POSIX_AIO)
Expand All @@ -32,9 +37,4 @@ target_include_directories(sgnmi_cli
${PB_OUT_DIR}
)

add_dependencies(sgnmi_cli
gnmi_proto
grpc_proto
)

install(TARGETS sgnmi_cli RUNTIME)
84 changes: 84 additions & 0 deletions cmake/StratumDependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Import Stratum dependencies.
#
# Copyright 2022-2023 Intel Corporation
# SPDX-License-Identifier: Apache 2.0
#

include_guard(GLOBAL)

#-----------------------------------------------------------------------
# Google C++ libraries (Abseil).
#-----------------------------------------------------------------------
find_package(absl CONFIG REQUIRED)
mark_as_advanced(absl_DIR)

message(STATUS "Found Abseil version ${absl_VERSION}")

if(absl_VERSION VERSION_GREATER_EQUAL "20230125")
add_compile_definitions(ABSL_LEGACY_THREAD_ANNOTATIONS)
endif()

#-----------------------------------------------------------------------
# Google command-line flags library (gflags).
#-----------------------------------------------------------------------
# By default, gflags does not namespace its targets.
set(GFLAGS_USE_TARGET_NAMESPACE TRUE)

find_package(gflags CONFIG REQUIRED)
mark_as_advanced(gflags_DIR)

#-----------------------------------------------------------------------
# Google logging library (glog).
#-----------------------------------------------------------------------
find_package(glog CONFIG REQUIRED)
mark_as_advanced(glog_DIR)

#-----------------------------------------------------------------------
# Google Protocol Buffers (protobuf).
# We must import the Protobuf package before gRPC.
#-----------------------------------------------------------------------
find_package(Protobuf CONFIG REQUIRED)
mark_as_advanced(Protobuf_DIR)

#-----------------------------------------------------------------------
# Google RPC (gRPC).
#-----------------------------------------------------------------------
find_package(gRPC CONFIG REQUIRED)
mark_as_advanced(gRPC_DIR)
mark_as_advanced(c-ares_DIR)

message(STATUS "Found gRPC version ${gRPC_VERSION}")

#-----------------------------------------------------------------------
# Protobuf compiler.
# Runs on the development system.
#-----------------------------------------------------------------------
find_program(HOST_PROTOC "protoc")
mark_as_advanced(HOST_PROTOC)

if(HOST_PROTOC)
message(STATUS "Found protoc: ${HOST_PROTOC}")
else()
message(FATAL_ERROR "protoc not found")
endif()

#-----------------------------------------------------------------------
# gRPC plugin for Protobuf compiler.
# Runs on the development system.
#-----------------------------------------------------------------------
find_program(HOST_GRPC_CPP_PLUGIN "grpc_cpp_plugin")
mark_as_advanced(HOST_GRPC_CPP_PLUGIN)

if(HOST_GRPC_CPP_PLUGIN)
message(STATUS "Found grpc_cpp_plugin: ${HOST_GRPC_CPP_PLUGIN}")
else()
message(FATAL_ERROR "grpc_cpp_plugin not found")
endif()

#-----------------------------------------------------------------------
# SSL/TLS library (OpenSSL).
#-----------------------------------------------------------------------
find_package(OpenSSL REQUIRED)
mark_as_advanced(OpenSSL_DIR)

set(WITH_OPENSSL TRUE)
2 changes: 1 addition & 1 deletion stratum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ set(STRATUM_SOURCE_DIR
mark_as_advanced(STRATUM_SOURCE_DIR)

set(PB_OUT_DIR "${CMAKE_BINARY_DIR}/pb.out" CACHE PATH
"Path to generated Protobuf files")
"Path to directory for generated Protobuf files")
mark_as_advanced(PB_OUT_DIR)
file(MAKE_DIRECTORY ${PB_OUT_DIR})

Expand Down
38 changes: 5 additions & 33 deletions stratum/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,6 @@
# SPDX-License-Identifier: Apache 2.0
#

include(FindProtobuf)

#############################
# Find Protobuf executables #
#############################

# The Protobuf compiler and gRPC plugin are host tools, even if we're
# cross-compiling. NO_CMAKE_FIND_ROOT_PATH ensures that we do not
# search the cross-compiled binaries for the Protobuf executables.

find_package(Protobuf REQUIRED NO_CMAKE_FIND_ROOT_PATH)
set(HOST_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})

find_package(gRPC NO_CMAKE_FIND_ROOT_PATH)

if(gRPC AND TARGET(gRPC::grpc_cpp_plugin))
get_property(HOST_GRPC_CPP_PLUGIN
TARGET gRPC::grpc_cpp_plugin
PROPERTY IMPORTED_LOCATION_NOCONFIG)
else()
find_program(HOST_GRPC_CPP_PLUGIN "grpc_cpp_plugin" REQUIRED
NO_CMAKE_FIND_ROOT_PATH)
mark_as_advanced(HOST_GRPC_CPP_PLUGIN)
if(NOT HOST_GRPC_CPP_PLUGIN)
message(FATAL_ERROR "grpc_cpp_plugin not found")
endif()
endif()

cmake_print_variables(HOST_PROTOC)
cmake_print_variables(HOST_GRPC_CPP_PLUGIN)

#############################
# Define Generate functions #
#############################
Expand Down Expand Up @@ -76,7 +45,7 @@ add_library(grpc_proto SHARED

target_include_directories(grpc_proto PRIVATE ${PB_OUT_DIR})

target_link_libraries(grpc_proto PUBLIC protobuf)
target_link_libraries(grpc_proto PUBLIC protobuf::libprotobuf)

set_install_rpath(grpc_proto ${DEP_ELEMENT})

Expand Down Expand Up @@ -117,7 +86,8 @@ target_include_directories(p4runtime_proto PRIVATE ${PB_OUT_DIR})
target_link_libraries(p4runtime_proto
PUBLIC
grpc_proto
absl::synchronization
protobuf::libprotobuf
absl::synchronization
)

set_install_rpath(p4runtime_proto $ORIGIN ${DEP_ELEMENT})
Expand Down Expand Up @@ -204,4 +174,6 @@ add_library(stratum_proto SHARED
$<TARGET_OBJECTS:stratum_proto2_o>
)

target_link_libraries(stratum_proto PUBLIC protobuf::libprotobuf)

install(TARGETS stratum_proto LIBRARY)
1 change: 1 addition & 0 deletions stratum/proto/gnmi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ target_include_directories(gnmi_proto PRIVATE ${PB_OUT_DIR})
target_link_libraries(gnmi_proto
PUBLIC
grpc_proto
protobuf::libprotobuf
absl::hash
absl::strings
absl::synchronization
Expand Down
2 changes: 2 additions & 0 deletions stratum/proto/openconfig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ add_library(openconfig_proto SHARED
$<TARGET_OBJECTS:openconfig_goog_bcm_proto_o>
)

target_link_libraries(openconfig_proto PUBLIC protobuf::libprotobuf)

install(TARGETS openconfig_proto LIBRARY)

0 comments on commit ce6aa9c

Please sign in to comment.