• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2022 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 os
18import sys
19
20from util import build_utils
21
22
23def parse_args(args):
24    parser = argparse.ArgumentParser()
25    build_utils.add_depfile_option(parser)
26
27    parser.add_argument('--clang-path', help='path to clang')
28    parser.add_argument('--include-dirs', nargs='+', help='path to header files')
29    parser.add_argument('--output-file', help='path to .o file')
30    parser.add_argument('--input-file', nargs='+', help='path to .c file')
31
32    options = parser.parse_args(args)
33    return options
34
35def bpf_compile(options, cmd: str):
36    my_env = None
37    for f in options.input_file:
38        cmd.extend(['-c', f])
39        cmd.extend(['-o', options.output_file])
40        build_utils.check_output(cmd, env=my_env)
41
42def main(args):
43    options = parse_args(args)
44    cmd = [options.clang_path]
45    cmd.extend(['-v', '-g', '-c', '-O2', '-target', 'bpf'])
46    for include_dir in options.include_dirs:
47        cmd.extend(['-I', include_dir])
48
49    outputs = [options.output_file]
50
51    build_utils.call_and_write_depfile_if_stale(
52        lambda: bpf_compile(options, cmd),
53        options,
54        depfile_deps=([options.clang_path]),
55        input_paths=(options.input_file + [options.clang_path]),
56        output_paths=(outputs),
57        input_strings=cmd,
58        force=False,
59        add_pydeps=False
60    )
61
62if __name__ == '__main__':
63    sys.exit(main(sys.argv[1:]))
64