1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import sys
18import subprocess
19import shutil
20import os
21
22
23def main():
24    parser = argparse.ArgumentParser()
25    parser.add_argument('--kernel-dir', help='kerner dir')
26    parser.add_argument('--kernel-out-dir', help='header file out dir')
27    parser.add_argument('--target-cpu', help='target cpu')
28    parser.add_argument('--kernel-tools-dir', help='kernel tools dir')
29    options = parser.parse_args()
30    make_tools_inc_dir = os.path.join(options.kernel_out_dir, 'bpf')
31    if not os.path.exists(make_tools_inc_dir):
32        shutil.copytree(options.kernel_tools_dir, make_tools_inc_dir)
33    make_uapi_cmd = ['make', '-C', options.kernel_dir, '-sj', 'headers',
34                     'O={}'.format(options.kernel_out_dir),
35                     'ARCH={}'.format(options.target_cpu)]
36    make_tools_uapi_cmd = ['make', '-C', options.kernel_tools_dir,
37                           'O={}'.format(make_tools_inc_dir),
38                           'ARCH={}'.format(options.target_cpu)]
39    subprocess.run(make_uapi_cmd)
40    subprocess.run(make_tools_uapi_cmd)
41
42if __name__ == '__main__':
43    sys.exit(main())
44