1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Copyright 2023 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17"""
18
19import os
20import json
21
22from util.log_util import LogUtil
23
24
25def parse_cflags(line, cflags_check_list, whitelist):
26    cflags_list = []
27    for flag in line.split():
28        if flag.startswith('-Wno-') and flag not in whitelist:
29            cflags_list.append(flag)
30        elif flag in cflags_check_list:
31            cflags_list.append(flag)
32    return cflags_list
33
34
35def parse_ldflags(line, ldflags_check_list, whitelist):
36    ldflags_list = []
37    for flag in line.split():
38        if flag in ldflags_check_list and flag not in whitelist:
39            ldflags_list.append(flag)
40    return ldflags_list
41
42
43def parse_ninja_file(root_path, ninja_file):
44    flag = list()
45    whitelist, ldflags_check_list, cflags_check_list = read_check_list(root_path)
46    with open(ninja_file, 'r') as file:
47        for line in file:
48            if line.startswith('cflags') or line.startswith('cflags_cc') or line.startswith('cflags_c'):
49                flag = parse_cflags(line, cflags_check_list, whitelist)
50            elif line.startswith('ldflags'):
51                flag = parse_ldflags(line, ldflags_check_list, whitelist)
52            elif line.startswith('label_name'):
53                label_name = line.split()[2]
54            elif line.startswith('target_out_dir'):
55                target_out_dir = line.split()[2]
56    if flag:
57        build_target = '{}:{}'.format(target_out_dir.replace('obj/', ''), label_name)
58        LogUtil.hb_info('build target "{}" used illegal build parameters {}'.format(build_target, flag))
59
60
61def collect_ninja_file_path(obj_path):
62    ninja_file_list = []
63    for root, dirs, files in os.walk(obj_path):
64        for file_name in files:
65            if file_name.endswith('.ninja'):
66                ninja_file_list.append(os.path.join(root, file_name))
67    return ninja_file_list
68
69
70def read_ohos_config(root_path):
71    file_path = os.path.join(root_path, 'out', 'ohos_config.json')
72    with open(file_path, 'r') as file:
73        file_json = json.load(file)
74        out_path = file_json.get('out_path')
75    return out_path
76
77
78def read_check_list(root_path):
79    file_path = os.path.join(root_path, 'build', 'hb', 'util', 'post_gn', 'check_list.json')
80    with open(file_path, 'r') as file:
81        file_json = json.load(file)
82        whitelist = file_json.get('whitelist')
83        ldflags_check_list = file_json.get('ldflags_check_list')
84        cflags_check_list = file_json.get('cflags_check_list')
85    return whitelist, ldflags_check_list, cflags_check_list
86
87
88def check_compilation_parameters(root_path):
89    out_path = read_ohos_config(root_path)
90    ninja_file_list = collect_ninja_file_path(os.path.join(out_path, 'obj'))
91    for ninja_file in ninja_file_list:
92        parse_ninja_file(root_path, ninja_file)
93    return 0
94