#!/usr/bin/python3 from sys import argv from os import listdir from re import compile as regex_compile include_dir_root_path = "hal_rootstrap_dir" regex_assignment = regex_compile("\s*([-_a-zA-Z0-9]+)\s*=\s*([^\s]*)") regex_declaration = regex_compile("\s*([-_a-zA-Z0-9]+)\s*:\s*(.+)\s*") regex_substitution = regex_compile("\${([-_a-zA-Z0-9]+)}") libs = { "-L${" + include_dir_root_path + "}" } cflags = { "-I${" + include_dir_root_path + "}" } cxxflags = { "-I${" + include_dir_root_path + "}" } def parse_pc(fname): variables = dict() f = open(fname, "r") for line in f: if matches := regex_assignment.findall(line): symbol, value = matches[0][0], matches[0][1] matched_symbols = regex_substitution.findall(value) for matched_symbol in matched_symbols: value = value.replace("${{{}}}".format(matched_symbol), "{}".format(variables[matched_symbol])) variables[symbol] = value continue if matches := regex_declaration.findall(line): symbol, value = matches[0][0], matches[0][1] matched_symbols = regex_substitution.findall(value) for matched_symbol in matched_symbols: value = value.replace("${{{}}}".format(matched_symbol), "{}".format(variables[matched_symbol])) if symbol == "Libs": libs.update(set(map(lambda value: value[:2] + "${" + include_dir_root_path + "}" + value[2:] if value[2] == '/' else value, value.split()))) continue if symbol == "Cflags": cflags.update((map(lambda value: value[:2] + "${" + include_dir_root_path + "}" + value[2:] if value[2] == '/' else value, value.split()))) continue if symbol == "CXXflags": cxxflags.update(list(map(lambda value: value[:2] + "${" + include_dir_root_path + "}" + value[2:] if value[2] == '/' else value, value.split()))) continue f.close() def write_to_pc(fname): f = open(fname, "a") f.write("\n") f.write("Libs: {}\n".format(' '.join(libs))) f.write("Cflags: {}\n".format(' '.join(cflags))) f.write("CXXflags: {}\n".format(' '.join(cxxflags))) f.close() hal_rootstrap_pc_path = argv[1] pc_dir = argv[2] pc_files = listdir(pc_dir) for pc_file in pc_files: parse_pc("{}/{}".format(pc_dir, pc_file)) write_to_pc(hal_rootstrap_pc_path)