xref: /freebsd/contrib/libpcap/cmake/Modules/Finddpdk.cmake (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
1*6f9cba8fSJoseph Mingrone# Try to find dpdk
2*6f9cba8fSJoseph Mingrone#
3*6f9cba8fSJoseph Mingrone# Once done, this will define
4*6f9cba8fSJoseph Mingrone#
5*6f9cba8fSJoseph Mingrone# dpdk_FOUND
6*6f9cba8fSJoseph Mingrone# dpdk_INCLUDE_DIRS
7*6f9cba8fSJoseph Mingrone# dpdk_LIBRARIES
8*6f9cba8fSJoseph Mingrone# dpdk_STATIC_LIBRARIES
9*6f9cba8fSJoseph Mingrone# dpdk_LIBS_STATIC
10*6f9cba8fSJoseph Mingrone# dpdk_REQUIRES_PRIVATE
11*6f9cba8fSJoseph Mingrone# dpdk_PACKAGE_NAME
12*6f9cba8fSJoseph Mingrone
13*6f9cba8fSJoseph Mingrone#
14*6f9cba8fSJoseph Mingrone# We only try to find DPDK using pkg-config; DPDK is *SO*
15*6f9cba8fSJoseph Mingrone# complicated - DPDK 19.02, for example, has about 117(!)
16*6f9cba8fSJoseph Mingrone# libraries, and the precise set of libraries required has
17*6f9cba8fSJoseph Mingrone# changed over time  - so attempting to guess which libraries
18*6f9cba8fSJoseph Mingrone# you need, and hardcoding that in an attempt to find the
19*6f9cba8fSJoseph Mingrone# libraries without DPDK, rather than relying on DPDK to
20*6f9cba8fSJoseph Mingrone# tell you, with a .pc file, what libraries are needed,
21*6f9cba8fSJoseph Mingrone# is *EXTREMELY* fragile and has caused some bug reports,
22*6f9cba8fSJoseph Mingrone# so we're just not going to do it.
23*6f9cba8fSJoseph Mingrone#
24*6f9cba8fSJoseph Mingrone# If that causes a problem, the only thing we will do is
25*6f9cba8fSJoseph Mingrone# accept an alternative way of finding the appropriate
26*6f9cba8fSJoseph Mingrone# library set for the installed version of DPDK that is
27*6f9cba8fSJoseph Mingrone# as robust as pkg-config (i.e., it had better work as well
28*6f9cba8fSJoseph Mingrone# as pkg-config with *ALL* versions of DPDK that provide a
29*6f9cba8fSJoseph Mingrone# libdpdk.pc file).
30*6f9cba8fSJoseph Mingrone#
31*6f9cba8fSJoseph Mingrone# If dpdk_ROOT is set, add ${dpdk_ROOT}/pkgconfig
32*6f9cba8fSJoseph Mingrone# to PKG_CONFIG_PATH, so we look for the .pc file there,
33*6f9cba8fSJoseph Mingrone# first.
34*6f9cba8fSJoseph Mingrone#
35*6f9cba8fSJoseph Mingroneif(PKG_CONFIG_FOUND)
36*6f9cba8fSJoseph Mingrone  set(save_PKG_CONFIG_PATH $ENV{PKG_CONFIG_PATH})
37*6f9cba8fSJoseph Mingrone  if(dpdk_ROOT)
38*6f9cba8fSJoseph Mingrone    set(ENV{PKG_CONFIG_PATH} "${dpdk_ROOT}/pkgconfig:$ENV{PKG_CONFIG_PATH}")
39*6f9cba8fSJoseph Mingrone  endif()
40*6f9cba8fSJoseph Mingrone  pkg_check_modules(dpdk QUIET libdpdk)
41*6f9cba8fSJoseph Mingrone  if(dpdk_FOUND)
42*6f9cba8fSJoseph Mingrone    #
43*6f9cba8fSJoseph Mingrone    # Get link information for DPDK.
44*6f9cba8fSJoseph Mingrone    #
45*6f9cba8fSJoseph Mingrone    pkg_get_link_info(dpdk libdpdk)
46*6f9cba8fSJoseph Mingrone  endif()
47*6f9cba8fSJoseph Mingrone  set(ENV{PKG_CONFIG_PATH} "${save_PKG_CONFIG_PATH}")
48*6f9cba8fSJoseph Mingroneendif()
49*6f9cba8fSJoseph Mingrone
50*6f9cba8fSJoseph Mingronemark_as_advanced(dpdk_INCLUDE_DIRS dpdk_LIBRARIES dpdk_STATIC_LIBRARIES dpdk_REQUIRES_PRIVATE)
51*6f9cba8fSJoseph Mingrone
52*6f9cba8fSJoseph Mingroneinclude(FindPackageHandleStandardArgs)
53*6f9cba8fSJoseph Mingronefind_package_handle_standard_args(dpdk DEFAULT_MSG
54*6f9cba8fSJoseph Mingrone  dpdk_INCLUDE_DIRS
55*6f9cba8fSJoseph Mingrone  dpdk_LIBRARIES)
56*6f9cba8fSJoseph Mingrone
57*6f9cba8fSJoseph Mingroneif(dpdk_FOUND)
58*6f9cba8fSJoseph Mingrone  #
59*6f9cba8fSJoseph Mingrone  # This depends on CMake support for "imported targets",
60*6f9cba8fSJoseph Mingrone  # which are not supported until CMake 3.19.
61*6f9cba8fSJoseph Mingrone  #
62*6f9cba8fSJoseph Mingrone  # Ubuntu 20.04 provides CMake 3.16.3, so we are *NOT*
63*6f9cba8fSJoseph Mingrone  # going to require CMake 3.19.  If you want to use
64*6f9cba8fSJoseph Mingrone  # Shiny New Features(TM), wait until all the OSes on
65*6f9cba8fSJoseph Mingrone  # which a build might conceivably be done, and that
66*6f9cba8fSJoseph Mingrone  # provide CMake, provide 3.19 or later.
67*6f9cba8fSJoseph Mingrone  #
68*6f9cba8fSJoseph Mingrone  # Just don't do this stuff on earlier versions.  If that
69*6f9cba8fSJoseph Mingrone  # breaks something, figure out a way to do it *without*
70*6f9cba8fSJoseph Mingrone  # "imported targets", and either do this that way, or,
71*6f9cba8fSJoseph Mingrone  # at least, do it that way on older versions of CMake.
72*6f9cba8fSJoseph Mingrone  #
73*6f9cba8fSJoseph Mingrone  # (One good thing about autotools is that only the builders
74*6f9cba8fSJoseph Mingrone  # of a package, and people doing configure-script development,
75*6f9cba8fSJoseph Mingrone  # have to care about the autoconf etc. version; you don't
76*6f9cba8fSJoseph Mingrone  # even need to have autotools installed in order to be able
77*6f9cba8fSJoseph Mingrone  # to run an autotools-generated configure script, you just
78*6f9cba8fSJoseph Mingrone  # need an environment UN*Xy enough, and modern enough, to
79*6f9cba8fSJoseph Mingrone  # run the stuff in the script.
80*6f9cba8fSJoseph Mingrone  #
81*6f9cba8fSJoseph Mingrone  # This is *NOT* the case for CMake; not only do you need
82*6f9cba8fSJoseph Mingrone  # CMake in order to build a package using CMake, you need
83*6f9cba8fSJoseph Mingrone  # a version recent enough to run the stuff the package's
84*6f9cba8fSJoseph Mingrone  # CMake files use.
85*6f9cba8fSJoseph Mingrone  #
86*6f9cba8fSJoseph Mingrone  # Please keep this in mind when changing any CMake files,
87*6f9cba8fSJoseph Mingrone  # and keep in mind what versions of CMake come with, for
88*6f9cba8fSJoseph Mingrone  # example, commonly-used versions of commonly-used
89*6f9cba8fSJoseph Mingrone  # Linux distributiions.)
90*6f9cba8fSJoseph Mingrone  #
91*6f9cba8fSJoseph Mingrone  if(NOT CMAKE_VERSION VERSION_LESS 3.19)
92*6f9cba8fSJoseph Mingrone    if(NOT TARGET dpdk::cflags)
93*6f9cba8fSJoseph Mingrone       if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|AMD64")
94*6f9cba8fSJoseph Mingrone        set(rte_cflags "-march=core2")
95*6f9cba8fSJoseph Mingrone      elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM")
96*6f9cba8fSJoseph Mingrone        set(rte_cflags "-march=armv7-a")
97*6f9cba8fSJoseph Mingrone      elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|AARCH64")
98*6f9cba8fSJoseph Mingrone        set(rte_cflags "-march=armv8-a+crc")
99*6f9cba8fSJoseph Mingrone      endif()
100*6f9cba8fSJoseph Mingrone      add_library(dpdk::cflags INTERFACE IMPORTED)
101*6f9cba8fSJoseph Mingrone      if (rte_cflags)
102*6f9cba8fSJoseph Mingrone        set_target_properties(dpdk::cflags PROPERTIES
103*6f9cba8fSJoseph Mingrone          INTERFACE_COMPILE_OPTIONS "${rte_cflags}")
104*6f9cba8fSJoseph Mingrone      endif()
105*6f9cba8fSJoseph Mingrone    endif()
106*6f9cba8fSJoseph Mingrone
107*6f9cba8fSJoseph Mingrone    if(NOT TARGET dpdk::dpdk)
108*6f9cba8fSJoseph Mingrone      add_library(dpdk::dpdk INTERFACE IMPORTED)
109*6f9cba8fSJoseph Mingrone      find_package(Threads QUIET)
110*6f9cba8fSJoseph Mingrone      list(APPEND dpdk_LIBRARIES
111*6f9cba8fSJoseph Mingrone        Threads::Threads
112*6f9cba8fSJoseph Mingrone        dpdk::cflags)
113*6f9cba8fSJoseph Mingrone      set_target_properties(dpdk::dpdk PROPERTIES
114*6f9cba8fSJoseph Mingrone        INTERFACE_LINK_LIBRARIES "${dpdk_LIBRARIES}"
115*6f9cba8fSJoseph Mingrone        INTERFACE_INCLUDE_DIRECTORIES "${dpdk_INCLUDE_DIRS}")
116*6f9cba8fSJoseph Mingrone    endif()
117*6f9cba8fSJoseph Mingrone  endif()
118*6f9cba8fSJoseph Mingroneendif()
119