ee12318d4fac40af2e139abc15d8c09ed150c47b
[dpdk.git] / config / meson.build
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright(c) 2017-2019 Intel Corporation
3
4 # check the OS is supported, rather than going any further
5 supported_exec_envs = ['freebsd', 'linux', 'windows']
6 exec_env = host_machine.system()
7 if not supported_exec_envs.contains(exec_env)
8     error('unsupported system type "@0@"'.format(exec_env))
9 endif
10
11 # define a handy variable for checking which OS we have.
12 # gives us "is_windows", "is_freebsd" and "is_linux"
13 foreach env:supported_exec_envs
14     set_variable('is_' + env, exec_env == env)
15 endforeach
16
17 # MS linker requires special treatment.
18 # TODO: use cc.get_linker_id() with Meson >= 0.54
19 is_ms_linker = is_windows and (cc.get_id() == 'clang')
20
21 # set the major version, which might be used by drivers and libraries
22 # depending on the configuration options
23 pver = meson.project_version().split('.')
24 major_version = '@0@.@1@'.format(pver.get(0), pver.get(1))
25 abi_version = run_command(find_program('cat', 'more'), abi_version_file,
26         check: true).stdout().strip()
27
28 # Libraries have the abi_version as the filename extension
29 # and have the soname be all but the final part of the abi_version.
30 # e.g. v20.1 => librte_foo.so.20.1
31 #    sonames => librte_foo.so.20
32 so_version = abi_version.split('.')[0]
33
34 # extract all version information into the build configuration
35 dpdk_conf.set('RTE_VER_YEAR', pver.get(0).to_int())
36 dpdk_conf.set('RTE_VER_MONTH', pver.get(1).to_int())
37 if pver.get(2).contains('-rc')
38     rc_ver = pver.get(2).split('-rc')
39     dpdk_conf.set('RTE_VER_MINOR', rc_ver.get(0).to_int())
40     dpdk_conf.set_quoted('RTE_VER_SUFFIX', '-rc')
41     dpdk_conf.set('RTE_VER_RELEASE', rc_ver.get(1).to_int())
42 else
43     dpdk_conf.set('RTE_VER_MINOR', pver.get(2).to_int())
44     dpdk_conf.set_quoted('RTE_VER_SUFFIX', '')
45 # for actual, non-rc releases, set the release value to 99 to ensure releases
46 # have higher version numbers than their respective release candidates
47     dpdk_conf.set('RTE_VER_RELEASE', 99)
48 endif
49
50 pmd_subdir_opt = get_option('drivers_install_subdir')
51 if pmd_subdir_opt.contains('<VERSION>')
52     pmd_subdir_opt = abi_version.join(pmd_subdir_opt.split('<VERSION>'))
53 endif
54 driver_install_path = join_paths(get_option('libdir'), pmd_subdir_opt)
55 eal_pmd_path = join_paths(get_option('prefix'), driver_install_path)
56
57 # driver .so files often depend upon the bus drivers for their connect bus,
58 # e.g. ixgbe depends on librte_bus_pci. This means that the bus drivers need
59 # to be in the library path, so symlink the drivers from the main lib directory.
60 if not is_windows
61     meson.add_install_script('../buildtools/symlink-drivers-solibs.sh',
62             get_option('libdir'), pmd_subdir_opt)
63 elif meson.version().version_compare('>=0.55.0')
64     # 0.55.0 is required to use external program with add_install_script
65     meson.add_install_script(py3,
66             files('../buildtools/symlink-drivers-solibs.py'),
67             get_option('libdir'), pmd_subdir_opt, get_option('bindir'))
68 endif
69
70 # init disable/enable driver lists that will be populated in different places
71 disable_drivers = ''
72 enable_drivers = ''
73
74 platform = get_option('platform')
75
76 # set the cpu_instruction_set and cflags for it
77 if meson.is_cross_build()
78     cpu_instruction_set = host_machine.cpu()
79 else
80     cpu_instruction_set = get_option('cpu_instruction_set')
81     machine = get_option('machine')
82     if machine != 'auto'
83         warning('The "machine" option is deprecated. ' +
84                 'Please use "cpu_instruction_set" instead.')
85         if cpu_instruction_set != 'auto'
86             error('Setting both "machine" and ' +
87                 '"cpu_instruction_set" is unsupported.')
88         endif
89         cpu_instruction_set = machine
90         if cpu_instruction_set == 'default'
91             cpu_instruction_set = 'generic'
92         endif
93     endif
94 endif
95
96 if platform == 'native'
97     if cpu_instruction_set == 'auto'
98         cpu_instruction_set = 'native'
99     endif
100 elif platform == 'generic'
101     if cpu_instruction_set == 'auto'
102         cpu_instruction_set = 'generic'
103     endif
104 endif
105
106 # cpu_instruction_set 'generic' is special, it selects the per arch agreed
107 # common minimal baseline needed for DPDK. cpu_instruction_set 'default' is
108 # also supported with the same meaning for backwards compatibility.
109 # That might not be the most optimized, but the most portable version while
110 # still being able to support the CPU features required for DPDK.
111 # This can be bumped up by the DPDK project, but it can never be an
112 # invariant like 'native'
113 if cpu_instruction_set == 'generic'
114     if host_machine.cpu_family().startswith('x86')
115         # matches the old pre-meson build systems generic cpu_instruction_set
116         cpu_instruction_set = 'corei7'
117     elif host_machine.cpu_family().startswith('arm')
118         cpu_instruction_set = 'armv7-a'
119     elif host_machine.cpu_family().startswith('aarch')
120         # arm64 manages generic config in config/arm/meson.build
121         cpu_instruction_set = 'generic'
122     elif host_machine.cpu_family().startswith('ppc')
123         cpu_instruction_set = 'power8'
124     endif
125 endif
126
127 dpdk_conf.set('RTE_MACHINE', cpu_instruction_set)
128 machine_args = []
129
130 # ppc64 does not support -march= at all, use -mcpu and -mtune for that
131 if host_machine.cpu_family().startswith('ppc')
132     machine_args += '-mcpu=' + cpu_instruction_set
133     machine_args += '-mtune=' + cpu_instruction_set
134 else
135     machine_args += '-march=' + cpu_instruction_set
136 endif
137
138 toolchain = cc.get_id()
139 dpdk_conf.set_quoted('RTE_TOOLCHAIN', toolchain)
140 dpdk_conf.set('RTE_TOOLCHAIN_' + toolchain.to_upper(), 1)
141
142 dpdk_conf.set('RTE_ARCH_64', cc.sizeof('void *') == 8)
143 dpdk_conf.set('RTE_ARCH_32', cc.sizeof('void *') == 4)
144
145 if not is_windows
146     add_project_link_arguments('-Wl,--no-as-needed', language: 'c')
147 endif
148
149 # use pthreads if available for the platform
150 if not is_windows
151     add_project_link_arguments('-pthread', language: 'c')
152     dpdk_extra_ldflags += '-pthread'
153 endif
154
155 # on some OS, maths functions are in a separate library
156 if cc.find_library('m', required : false).found()
157     # some libs depend on maths lib
158     add_project_link_arguments('-lm', language: 'c')
159     dpdk_extra_ldflags += '-lm'
160 endif
161
162 if is_linux
163     link_lib = 'dl'
164 else
165     link_lib = ''
166 endif
167
168 # if link_lib is empty, do not add it to project properties
169 if link_lib != ''
170     add_project_link_arguments('-l' + link_lib, language: 'c')
171     dpdk_extra_ldflags += '-l' + link_lib
172 endif
173
174 # check for libraries used in multiple places in DPDK
175 has_libnuma = 0
176 find_libnuma = true
177 if meson.is_cross_build() and not meson.get_cross_property('numa', true)
178     # don't look for libnuma if explicitly disabled in cross build
179     find_libnuma = false
180 endif
181 if find_libnuma
182     numa_dep = cc.find_library('numa', required: false)
183     if numa_dep.found() and cc.has_header('numaif.h')
184         dpdk_conf.set10('RTE_HAS_LIBNUMA', true)
185         has_libnuma = 1
186         add_project_link_arguments('-lnuma', language: 'c')
187         dpdk_extra_ldflags += '-lnuma'
188     endif
189 endif
190
191 has_libfdt = 0
192 fdt_dep = cc.find_library('libfdt', required: false)
193 if fdt_dep.found() and cc.has_header('fdt.h')
194     dpdk_conf.set10('RTE_HAS_LIBFDT', true)
195     has_libfdt = 1
196     add_project_link_arguments('-lfdt', language: 'c')
197     dpdk_extra_ldflags += '-lfdt'
198 endif
199
200 libexecinfo = cc.find_library('libexecinfo', required: false)
201 if libexecinfo.found() and cc.has_header('execinfo.h')
202     add_project_link_arguments('-lexecinfo', language: 'c')
203     dpdk_extra_ldflags += '-lexecinfo'
204 endif
205
206 libarchive = dependency('libarchive', required: false, method: 'pkg-config')
207 if libarchive.found()
208     dpdk_conf.set('RTE_HAS_LIBARCHIVE', 1)
209     # Push libarchive link dependency at the project level to support
210     # statically linking dpdk apps. Details at:
211     # https://inbox.dpdk.org/dev/20210605004024.660267a1@sovereign/
212     add_project_link_arguments('-larchive', language: 'c')
213     dpdk_extra_ldflags += '-larchive'
214 endif
215
216 # check for libbsd
217 libbsd = dependency('libbsd', required: false, method: 'pkg-config')
218 if libbsd.found()
219     dpdk_conf.set('RTE_USE_LIBBSD', 1)
220 endif
221
222 jansson_dep = dependency('jansson', required: false, method: 'pkg-config')
223 if jansson_dep.found()
224     dpdk_conf.set('RTE_HAS_JANSSON', 1)
225 endif
226
227 # check for pcap
228 pcap_dep = dependency('libpcap', required: false, method: 'pkg-config')
229 pcap_lib = is_windows ? 'wpcap' : 'pcap'
230 if not pcap_dep.found()
231     # pcap got a pkg-config file only in 1.9.0
232     pcap_dep = cc.find_library(pcap_lib, required: false)
233 endif
234 if pcap_dep.found() and cc.has_header('pcap.h', dependencies: pcap_dep)
235     dpdk_conf.set('RTE_HAS_LIBPCAP', 1)
236     dpdk_extra_ldflags += '-l@0@'.format(pcap_lib)
237 endif
238
239 # for clang 32-bit compiles we need libatomic for 64-bit atomic ops
240 if cc.get_id() == 'clang' and dpdk_conf.get('RTE_ARCH_64') == false
241     atomic_dep = cc.find_library('atomic', required: true)
242     add_project_link_arguments('-latomic', language: 'c')
243     dpdk_extra_ldflags += '-latomic'
244 endif
245
246 # add -include rte_config to cflags
247 add_project_arguments('-include', 'rte_config.h', language: 'c')
248
249 # enable extra warnings and disable any unwanted warnings
250 warning_flags = [
251         # -Wall is added by meson by default, so add -Wextra only
252         '-Wextra',
253
254         # additional warnings in alphabetical order
255         '-Wcast-qual',
256         '-Wdeprecated',
257         '-Wformat',
258         '-Wformat-nonliteral',
259         '-Wformat-security',
260         '-Wmissing-declarations',
261         '-Wmissing-prototypes',
262         '-Wnested-externs',
263         '-Wold-style-definition',
264         '-Wpointer-arith',
265         '-Wsign-compare',
266         '-Wstrict-prototypes',
267         '-Wundef',
268         '-Wwrite-strings',
269
270         # globally disabled warnings
271         '-Wno-address-of-packed-member',
272         '-Wno-packed-not-aligned',
273         '-Wno-missing-field-initializers',
274 ]
275 if cc.get_id() == 'gcc' and cc.version().version_compare('>=10.0')
276 # FIXME: Bugzilla 396
277     warning_flags += '-Wno-zero-length-bounds'
278 endif
279 if not dpdk_conf.get('RTE_ARCH_64')
280 # for 32-bit, don't warn about casting a 32-bit pointer to 64-bit int - it's fine!!
281     warning_flags += '-Wno-pointer-to-int-cast'
282 endif
283 if cc.get_id() == 'intel'
284     warning_ids = [181, 188, 2203, 2279, 2557, 3179, 3656]
285     foreach i:warning_ids
286         warning_flags += '-diag-disable=@0@'.format(i)
287     endforeach
288 endif
289 foreach arg: warning_flags
290     if cc.has_argument(arg)
291         add_project_arguments(arg, language: 'c')
292     endif
293 endforeach
294
295 # set other values pulled from the build options
296 dpdk_conf.set('RTE_MAX_ETHPORTS', get_option('max_ethports'))
297 dpdk_conf.set('RTE_LIBEAL_USE_HPET', get_option('use_hpet'))
298 dpdk_conf.set('RTE_ENABLE_TRACE_FP', get_option('enable_trace_fp'))
299 # values which have defaults which may be overridden
300 dpdk_conf.set('RTE_MAX_VFIO_GROUPS', 64)
301 dpdk_conf.set('RTE_DRIVER_MEMPOOL_BUCKET_SIZE_KB', 64)
302 dpdk_conf.set('RTE_LIBRTE_DPAA2_USE_PHYS_IOVA', true)
303 if dpdk_conf.get('RTE_ARCH_64')
304     dpdk_conf.set('RTE_MAX_MEM_MB', 524288)
305 else # for 32-bit we need smaller reserved memory areas
306     dpdk_conf.set('RTE_MAX_MEM_MB', 2048)
307 endif
308 if get_option('mbuf_refcnt_atomic')
309     dpdk_conf.set('RTE_MBUF_REFCNT_ATOMIC', true)
310 endif
311
312 compile_time_cpuflags = []
313 subdir(arch_subdir)
314 dpdk_conf.set('RTE_COMPILE_TIME_CPUFLAGS', ','.join(compile_time_cpuflags))
315
316 # apply cross-specific options
317 if meson.is_cross_build()
318     # configure RTE_MAX_LCORE and RTE_MAX_NUMA_NODES from cross file
319     cross_max_lcores = meson.get_cross_property('max_lcores', 0)
320     if cross_max_lcores != 0
321         message('Setting RTE_MAX_LCORE from cross file')
322         dpdk_conf.set('RTE_MAX_LCORE', cross_max_lcores)
323     endif
324     cross_max_numa_nodes = meson.get_cross_property('max_numa_nodes', 0)
325     if cross_max_numa_nodes != 0
326         message('Setting RTE_MAX_NUMA_NODES from cross file')
327         dpdk_conf.set('RTE_MAX_NUMA_NODES', cross_max_numa_nodes)
328     endif
329 endif
330
331 max_lcores = get_option('max_lcores')
332 if max_lcores == 'detect'
333     # discovery makes sense only for non-cross builds
334     if meson.is_cross_build()
335         error('Discovery of max_lcores is not supported for cross-compilation.')
336     endif
337     # overwrite the default value with discovered values
338     max_lcores = run_command(get_cpu_count_cmd, check: true).stdout().to_int()
339     min_lcores = 2
340     # DPDK must be built for at least 2 cores
341     if max_lcores < min_lcores
342         message('Found less than @0@ cores, building for @0@ cores'.format(min_lcores))
343         max_lcores = min_lcores
344     else
345         message('Found @0@ cores'.format(max_lcores))
346     endif
347     dpdk_conf.set('RTE_MAX_LCORE', max_lcores)
348 elif max_lcores != 'default'
349     # overwrite the default value from arch_subdir with user input
350     dpdk_conf.set('RTE_MAX_LCORE', max_lcores.to_int())
351 endif
352
353 max_numa_nodes = get_option('max_numa_nodes')
354 if max_numa_nodes == 'detect'
355     # discovery makes sense only for non-cross builds
356     if meson.is_cross_build()
357         error('Discovery of max_numa_nodes not supported for cross-compilation.')
358     endif
359     # overwrite the default value with discovered values
360     max_numa_nodes = run_command(get_numa_count_cmd).stdout().to_int()
361     message('Found @0@ numa nodes'.format(max_numa_nodes))
362     dpdk_conf.set('RTE_MAX_NUMA_NODES', max_numa_nodes)
363 elif max_numa_nodes != 'default'
364     # overwrite the default value from arch_subdir with user input
365     dpdk_conf.set('RTE_MAX_NUMA_NODES', max_numa_nodes.to_int())
366 endif
367
368 # check that CPU and NUMA counts are set
369 if not dpdk_conf.has('RTE_MAX_LCORE')
370     error('Number of CPU cores not specified.')
371 endif
372 if not dpdk_conf.has('RTE_MAX_NUMA_NODES')
373     error('Number of NUMA nodes not specified.')
374 endif
375
376 # set the install path for the drivers
377 dpdk_conf.set_quoted('RTE_EAL_PMD_PATH', eal_pmd_path)
378
379 install_headers(['rte_config.h'],
380         subdir: get_option('include_subdir_arch'))
381
382 # enable VFIO only if it is linux OS
383 dpdk_conf.set('RTE_EAL_VFIO', is_linux)
384
385 # specify -D_GNU_SOURCE unconditionally
386 add_project_arguments('-D_GNU_SOURCE', language: 'c')
387
388 # specify -D__BSD_VISIBLE for FreeBSD
389 if is_freebsd
390     add_project_arguments('-D__BSD_VISIBLE', language: 'c')
391 endif
392
393 if is_windows
394     # VirtualAlloc2() is available since Windows 10 / Server 2019.
395     # It's essential for EAL, so we don't support older versions.
396     add_project_arguments('-D_WIN32_WINNT=0x0A00', language: 'c')
397
398     # Use MinGW-w64 stdio, because DPDK assumes ANSI-compliant formatting.
399     if cc.get_id() == 'gcc'
400         add_project_arguments('-D__USE_MINGW_ANSI_STDIO', language: 'c')
401     endif
402
403     # Disable secure CRT deprecated warnings for clang
404     if cc.get_id() == 'clang'
405         add_project_arguments('-D_CRT_SECURE_NO_WARNINGS', language: 'c')
406     endif
407 endif
408
409 if get_option('b_lto')
410     if cc.has_argument('-ffat-lto-objects')
411         add_project_arguments('-ffat-lto-objects', language: 'c')
412     else
413         error('compiler does not support fat LTO objects - please turn LTO off')
414     endif
415     # workaround for gcc bug 81440
416     if cc.get_id() == 'gcc' and cc.version().version_compare('<8.0')
417         add_project_arguments('-Wno-lto-type-mismatch', language: 'c')
418         add_project_link_arguments('-Wno-lto-type-mismatch', language: 'c')
419     endif
420 endif
421
422 if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined'
423     if is_windows
424         error('ASan is not supported on windows')
425     endif
426
427     if cc.get_id() == 'gcc'
428         asan_dep = cc.find_library('asan', required: true)
429         if (not cc.links('int main(int argc, char *argv[]) { return 0; }',
430                 dependencies: asan_dep))
431             error('broken dependency, "libasan"')
432         endif
433         add_project_link_arguments('-lasan', language: 'c')
434         dpdk_extra_ldflags += '-lasan'
435     endif
436
437     if is_linux and dpdk_conf.get('RTE_ARCH_64')
438         dpdk_conf.set10('RTE_MALLOC_ASAN', true)
439     endif
440 endif
441
442 if get_option('default_library') == 'both'
443     error( '''
444  Unsupported value "both" for "default_library" option.
445
446  NOTE: DPDK always builds both shared and static libraries.  Please set
447  "default_library" to either "static" or "shared" to select default linkage
448  for apps and any examples.''')
449 endif