xref: /dpdk/kernel/linux/meson.build (revision ecb904cc)
13f708560SBruce Richardson# SPDX-License-Identifier: BSD-3-Clause
23f708560SBruce Richardson# Copyright(c) 2018 Intel Corporation
33f708560SBruce Richardson
456bb5841SThomas Monjalonsubdirs = ['kni']
53f708560SBruce Richardson
63b4f41a1SJuraj Linkeškernel_build_dir = get_option('kernel_dir')
7c28e2165SAman Singhkernel_source_dir = get_option('kernel_dir')
83b4f41a1SJuraj Linkeškernel_install_dir = ''
93b4f41a1SJuraj Linkešinstall = not meson.is_cross_build()
103b4f41a1SJuraj Linkešcross_args = []
1189200580SBruce Richardson
123b4f41a1SJuraj Linkešif not meson.is_cross_build()
133b4f41a1SJuraj Linkeš    # native build
14*ecb904ccSBruce Richardson    kernel_version = run_command('uname', '-r', check: true).stdout().strip()
153b4f41a1SJuraj Linkeš    kernel_install_dir = '/lib/modules/' + kernel_version + '/extra/dpdk'
163b4f41a1SJuraj Linkeš    if kernel_build_dir == ''
173b4f41a1SJuraj Linkeš        # use default path for native builds
183b4f41a1SJuraj Linkeš        kernel_build_dir = '/lib/modules/' + kernel_version + '/build'
1989200580SBruce Richardson    endif
20c28e2165SAman Singh    if kernel_source_dir == ''
21c28e2165SAman Singh        # use default path for native builds
22c28e2165SAman Singh        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
23c28e2165SAman Singh    endif
2489200580SBruce Richardson
2589200580SBruce Richardson    # test running make in kernel directory, using "make kernelversion"
263b4f41a1SJuraj Linkeš    make_returncode = run_command('make', '-sC', kernel_build_dir,
27*ecb904ccSBruce Richardson            'kernelversion', check: true).returncode()
2889200580SBruce Richardson    if make_returncode != 0
293b4f41a1SJuraj Linkeš        # backward compatibility:
303b4f41a1SJuraj Linkeš        # the headers could still be in the 'build' subdir
313b4f41a1SJuraj Linkeš        if not kernel_build_dir.endswith('build') and not kernel_build_dir.endswith('build/')
323b4f41a1SJuraj Linkeš            kernel_build_dir = join_paths(kernel_build_dir, 'build')
333b4f41a1SJuraj Linkeš            make_returncode = run_command('make', '-sC', kernel_build_dir,
34*ecb904ccSBruce Richardson                    'kernelversion', check: true).returncode()
353b4f41a1SJuraj Linkeš        endif
363b4f41a1SJuraj Linkeš    endif
373b4f41a1SJuraj Linkeš
383b4f41a1SJuraj Linkeš    if make_returncode != 0
39455be5b4SAnatoly Burakov        error('Cannot compile kernel modules as requested - are kernel headers installed?')
4041dec89fSBruce Richardson    endif
4189200580SBruce Richardson
4289200580SBruce Richardson    # DO ACTUAL MODULE BUILDING
433f708560SBruce Richardson    foreach d:subdirs
443f708560SBruce Richardson        subdir(d)
453f708560SBruce Richardson    endforeach
463b4f41a1SJuraj Linkeš
473b4f41a1SJuraj Linkeš    subdir_done()
483b4f41a1SJuraj Linkešendif
493b4f41a1SJuraj Linkeš
503b4f41a1SJuraj Linkeš# cross build
513b4f41a1SJuraj Linkeš# if we are cross-compiling we need kernel_build_dir specified
523b4f41a1SJuraj Linkešif kernel_build_dir == ''
533b4f41a1SJuraj Linkeš    error('Need "kernel_dir" option for kmod compilation when cross-compiling')
543b4f41a1SJuraj Linkešendif
553b4f41a1SJuraj Linkešcross_compiler = find_program('c').path()
563b4f41a1SJuraj Linkešif cross_compiler.endswith('gcc')
57*ecb904ccSBruce Richardson    cross_prefix = run_command([py3, '-c', 'print("' + cross_compiler + '"[:-3])'],
58*ecb904ccSBruce Richardson            check: true).stdout().strip()
593b4f41a1SJuraj Linkešelif cross_compiler.endswith('clang')
603b4f41a1SJuraj Linkeš    cross_prefix = ''
613b4f41a1SJuraj Linkeš    found_target = false
623b4f41a1SJuraj Linkeš    # search for '-target' and use the arg that follows
633b4f41a1SJuraj Linkeš    # (i.e. the value of '-target') as cross_prefix
643b4f41a1SJuraj Linkeš    foreach cross_c_arg : meson.get_cross_property('c_args')
653b4f41a1SJuraj Linkeš        if found_target and cross_prefix == ''
663b4f41a1SJuraj Linkeš            cross_prefix = cross_c_arg
673b4f41a1SJuraj Linkeš        endif
683b4f41a1SJuraj Linkeš        if cross_c_arg == '-target'
693b4f41a1SJuraj Linkeš            found_target = true
703b4f41a1SJuraj Linkeš        endif
713b4f41a1SJuraj Linkeš    endforeach
723b4f41a1SJuraj Linkeš    if cross_prefix == ''
738dcb898cSBruce Richardson        error('Did not find -target and its value in c_args in input cross-file.')
743b4f41a1SJuraj Linkeš    endif
753b4f41a1SJuraj Linkeš    linker = 'lld'
763b4f41a1SJuraj Linkeš    foreach cross_c_link_arg : meson.get_cross_property('c_link_args')
773b4f41a1SJuraj Linkeš        if cross_c_link_arg.startswith('-fuse-ld')
783b4f41a1SJuraj Linkeš            linker = cross_c_link_arg.split('=')[1]
793b4f41a1SJuraj Linkeš        endif
803b4f41a1SJuraj Linkeš    endforeach
813b4f41a1SJuraj Linkeš    cross_args += ['CC=@0@'.format(cross_compiler), 'LD=ld.@0@'.format(linker)]
823b4f41a1SJuraj Linkešelse
833b4f41a1SJuraj Linkeš    error('Unsupported cross compiler: @0@'.format(cross_compiler))
843b4f41a1SJuraj Linkešendif
853b4f41a1SJuraj Linkeš
863b4f41a1SJuraj Linkešcross_arch = host_machine.cpu_family()
873b4f41a1SJuraj Linkešif host_machine.cpu_family() == 'aarch64'
883b4f41a1SJuraj Linkeš    cross_arch = 'arm64'
893b4f41a1SJuraj Linkešendif
903b4f41a1SJuraj Linkeš
913b4f41a1SJuraj Linkešcross_args += ['ARCH=@0@'.format(cross_arch),
923b4f41a1SJuraj Linkeš        'CROSS_COMPILE=@0@'.format(cross_prefix)]
933b4f41a1SJuraj Linkeš
943b4f41a1SJuraj Linkeš# DO ACTUAL MODULE BUILDING
953b4f41a1SJuraj Linkešforeach d:subdirs
963b4f41a1SJuraj Linkeš    subdir(d)
973b4f41a1SJuraj Linkešendforeach
98