xref: /netbsd-src/external/bsd/libpcap/dist/cmake/Modules/Finddpdk.cmake (revision f73a5f05f638a81353839a8e4bfc90d1189181e7)
1748408edSchristos# Try to find dpdk
2748408edSchristos#
3748408edSchristos# Once done, this will define
4748408edSchristos#
5748408edSchristos# dpdk_FOUND
6748408edSchristos# dpdk_INCLUDE_DIRS
7748408edSchristos# dpdk_LIBRARIES
8748408edSchristos# dpdk_STATIC_LIBRARIES
9748408edSchristos# dpdk_LIBS_STATIC
10748408edSchristos# dpdk_REQUIRES_PRIVATE
11748408edSchristos# dpdk_PACKAGE_NAME
12748408edSchristos
13748408edSchristos#
14748408edSchristos# We only try to find DPDK using pkg-config; DPDK is *SO*
15748408edSchristos# complicated - DPDK 19.02, for example, has about 117(!)
16748408edSchristos# libraries, and the precise set of libraries required has
17748408edSchristos# changed over time  - so attempting to guess which libraries
18748408edSchristos# you need, and hardcoding that in an attempt to find the
19748408edSchristos# libraries without DPDK, rather than relying on DPDK to
20748408edSchristos# tell you, with a .pc file, what libraries are needed,
21748408edSchristos# is *EXTREMELY* fragile and has caused some bug reports,
22748408edSchristos# so we're just not going to do it.
23748408edSchristos#
24748408edSchristos# If that causes a problem, the only thing we will do is
25748408edSchristos# accept an alternative way of finding the appropriate
26748408edSchristos# library set for the installed version of DPDK that is
27748408edSchristos# as robust as pkg-config (i.e., it had better work as well
28748408edSchristos# as pkg-config with *ALL* versions of DPDK that provide a
29748408edSchristos# libdpdk.pc file).
30748408edSchristos#
31748408edSchristos# If dpdk_ROOT is set, add ${dpdk_ROOT}/pkgconfig
32748408edSchristos# to PKG_CONFIG_PATH, so we look for the .pc file there,
33748408edSchristos# first.
34748408edSchristos#
35748408edSchristosif(PKG_CONFIG_FOUND)
36748408edSchristos  set(save_PKG_CONFIG_PATH $ENV{PKG_CONFIG_PATH})
37748408edSchristos  if(dpdk_ROOT)
38748408edSchristos    set(ENV{PKG_CONFIG_PATH} "${dpdk_ROOT}/pkgconfig:$ENV{PKG_CONFIG_PATH}")
39748408edSchristos  endif()
40748408edSchristos  pkg_check_modules(dpdk QUIET libdpdk)
41748408edSchristos  if(dpdk_FOUND)
42748408edSchristos    #
43748408edSchristos    # Get link information for DPDK.
44748408edSchristos    #
45748408edSchristos    pkg_get_link_info(dpdk libdpdk)
46748408edSchristos  endif()
47748408edSchristos  set(ENV{PKG_CONFIG_PATH} "${save_PKG_CONFIG_PATH}")
48748408edSchristosendif()
49748408edSchristos
50748408edSchristosmark_as_advanced(dpdk_INCLUDE_DIRS dpdk_LIBRARIES dpdk_STATIC_LIBRARIES dpdk_REQUIRES_PRIVATE)
51748408edSchristos
52748408edSchristosinclude(FindPackageHandleStandardArgs)
53748408edSchristosfind_package_handle_standard_args(dpdk DEFAULT_MSG
54748408edSchristos  dpdk_INCLUDE_DIRS
55748408edSchristos  dpdk_LIBRARIES)
56748408edSchristos
57748408edSchristosif(dpdk_FOUND)
58748408edSchristos  #
59748408edSchristos  # This depends on CMake support for "imported targets",
60748408edSchristos  # which are not supported until CMake 3.19.
61748408edSchristos  #
62748408edSchristos  # Ubuntu 20.04 provides CMake 3.16.3, so we are *NOT*
63748408edSchristos  # going to require CMake 3.19.  If you want to use
64748408edSchristos  # Shiny New Features(TM), wait until all the OSes on
65748408edSchristos  # which a build might conceivably be done, and that
66748408edSchristos  # provide CMake, provide 3.19 or later.
67748408edSchristos  #
68748408edSchristos  # Just don't do this stuff on earlier versions.  If that
69748408edSchristos  # breaks something, figure out a way to do it *without*
70748408edSchristos  # "imported targets", and either do this that way, or,
71748408edSchristos  # at least, do it that way on older versions of CMake.
72748408edSchristos  #
73748408edSchristos  # (One good thing about autotools is that only the builders
74748408edSchristos  # of a package, and people doing configure-script development,
75748408edSchristos  # have to care about the autoconf etc. version; you don't
76748408edSchristos  # even need to have autotools installed in order to be able
77748408edSchristos  # to run an autotools-generated configure script, you just
78748408edSchristos  # need an environment UN*Xy enough, and modern enough, to
79748408edSchristos  # run the stuff in the script.
80748408edSchristos  #
81748408edSchristos  # This is *NOT* the case for CMake; not only do you need
82748408edSchristos  # CMake in order to build a package using CMake, you need
83748408edSchristos  # a version recent enough to run the stuff the package's
84748408edSchristos  # CMake files use.
85748408edSchristos  #
86748408edSchristos  # Please keep this in mind when changing any CMake files,
87748408edSchristos  # and keep in mind what versions of CMake come with, for
88748408edSchristos  # example, commonly-used versions of commonly-used
89*f73a5f05Schristos  # Linux distributions.)
90748408edSchristos  #
91748408edSchristos  if(NOT CMAKE_VERSION VERSION_LESS 3.19)
92748408edSchristos    if(NOT TARGET dpdk::cflags)
93748408edSchristos       if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|AMD64")
94748408edSchristos        set(rte_cflags "-march=core2")
95748408edSchristos      elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM")
96748408edSchristos        set(rte_cflags "-march=armv7-a")
97748408edSchristos      elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|AARCH64")
98748408edSchristos        set(rte_cflags "-march=armv8-a+crc")
99748408edSchristos      endif()
100748408edSchristos      add_library(dpdk::cflags INTERFACE IMPORTED)
101748408edSchristos      if (rte_cflags)
102748408edSchristos        set_target_properties(dpdk::cflags PROPERTIES
103748408edSchristos          INTERFACE_COMPILE_OPTIONS "${rte_cflags}")
104748408edSchristos      endif()
105748408edSchristos    endif()
106748408edSchristos
107748408edSchristos    if(NOT TARGET dpdk::dpdk)
108748408edSchristos      add_library(dpdk::dpdk INTERFACE IMPORTED)
109748408edSchristos      find_package(Threads QUIET)
110748408edSchristos      list(APPEND dpdk_LIBRARIES
111748408edSchristos        Threads::Threads
112748408edSchristos        dpdk::cflags)
113748408edSchristos      set_target_properties(dpdk::dpdk PROPERTIES
114748408edSchristos        INTERFACE_LINK_LIBRARIES "${dpdk_LIBRARIES}"
115748408edSchristos        INTERFACE_INCLUDE_DIRECTORIES "${dpdk_INCLUDE_DIRS}")
116748408edSchristos    endif()
117748408edSchristos  endif()
118748408edSchristosendif()
119