build: support KNI cross-compilation
[dpdk.git] / kernel / linux / meson.build
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright(c) 2018 Intel Corporation
3
4 subdirs = ['kni']
5
6 kernel_build_dir = get_option('kernel_dir')
7 kernel_install_dir = ''
8 install = not meson.is_cross_build()
9 cross_args = []
10
11 if not meson.is_cross_build()
12         # native build
13         kernel_version = run_command('uname', '-r').stdout().strip()
14         kernel_install_dir = '/lib/modules/' + kernel_version + '/extra/dpdk'
15         if kernel_build_dir == ''
16                 # use default path for native builds
17                 kernel_build_dir = '/lib/modules/' + kernel_version + '/build'
18         endif
19
20         # test running make in kernel directory, using "make kernelversion"
21         make_returncode = run_command('make', '-sC', kernel_build_dir,
22                         'kernelversion').returncode()
23         if make_returncode != 0
24                 # backward compatibility:
25                 # the headers could still be in the 'build' subdir
26                 if not kernel_build_dir.endswith('build') and not kernel_build_dir.endswith('build/')
27                         kernel_build_dir = join_paths(kernel_build_dir, 'build')
28                         make_returncode = run_command('make', '-sC', kernel_build_dir,
29                                         'kernelversion').returncode()
30                 endif
31         endif
32
33         if make_returncode != 0
34                 error('Cannot compile kernel modules as requested - are kernel headers installed?')
35         endif
36
37         # DO ACTUAL MODULE BUILDING
38         foreach d:subdirs
39                 subdir(d)
40         endforeach
41
42         subdir_done()
43 endif
44
45 # cross build
46 # if we are cross-compiling we need kernel_build_dir specified
47 if kernel_build_dir == ''
48         error('Need "kernel_dir" option for kmod compilation when cross-compiling')
49 endif
50 cross_compiler = find_program('c').path()
51 if cross_compiler.endswith('gcc')
52         cross_prefix = run_command([py3, '-c', 'print("' + cross_compiler + '"[:-3])']).stdout().strip()
53 elif cross_compiler.endswith('clang')
54         cross_prefix = ''
55         found_target = false
56         # search for '-target' and use the arg that follows
57         # (i.e. the value of '-target') as cross_prefix
58         foreach cross_c_arg : meson.get_cross_property('c_args')
59                 if found_target and cross_prefix == ''
60                         cross_prefix = cross_c_arg
61                 endif
62                 if cross_c_arg == '-target'
63                         found_target = true
64                 endif
65         endforeach
66         if cross_prefix == ''
67                 error('Didn\'t find -target and its value in' +
68                       ' c_args in input cross-file.')
69         endif
70         linker = 'lld'
71         foreach cross_c_link_arg : meson.get_cross_property('c_link_args')
72                 if cross_c_link_arg.startswith('-fuse-ld')
73                         linker = cross_c_link_arg.split('=')[1]
74                 endif
75         endforeach
76         cross_args += ['CC=@0@'.format(cross_compiler), 'LD=ld.@0@'.format(linker)]
77 else
78         error('Unsupported cross compiler: @0@'.format(cross_compiler))
79 endif
80
81 cross_arch = host_machine.cpu_family()
82 if host_machine.cpu_family() == 'aarch64'
83         cross_arch = 'arm64'
84 endif
85
86 cross_args += ['ARCH=@0@'.format(cross_arch),
87         'CROSS_COMPILE=@0@'.format(cross_prefix)]
88
89 # DO ACTUAL MODULE BUILDING
90 foreach d:subdirs
91         subdir(d)
92 endforeach