1*795d594fSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*795d594fSAndroid Build Coastguard Worker# 3*795d594fSAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project 4*795d594fSAndroid Build Coastguard Worker# 5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*795d594fSAndroid Build Coastguard Worker# 9*795d594fSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*795d594fSAndroid Build Coastguard Worker# 11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*795d594fSAndroid Build Coastguard Worker# limitations under the License. 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker# This script looks through compiled object file (stored human readable text), 18*795d594fSAndroid Build Coastguard Worker# and looks for the compile-time constants (added through custom "asm" block). 19*795d594fSAndroid Build Coastguard Worker# For example: .ascii ">>OBJECT_ALIGNMENT_MASK $7 $0<<" 20*795d594fSAndroid Build Coastguard Worker# 21*795d594fSAndroid Build Coastguard Worker# It will transform each such line to #define which is usabe in assembly code. 22*795d594fSAndroid Build Coastguard Worker# For example: #define OBJECT_ALIGNMENT_MASK 0x7 23*795d594fSAndroid Build Coastguard Worker# 24*795d594fSAndroid Build Coastguard Worker# Usage: make_header.py out/soong/.intermediates/.../asm_defines.o 25*795d594fSAndroid Build Coastguard Worker# 26*795d594fSAndroid Build Coastguard Worker 27*795d594fSAndroid Build Coastguard Workerimport argparse 28*795d594fSAndroid Build Coastguard Workerimport re 29*795d594fSAndroid Build Coastguard Workerimport sys 30*795d594fSAndroid Build Coastguard Worker 31*795d594fSAndroid Build Coastguard Workerdef convert(input): 32*795d594fSAndroid Build Coastguard Worker """Find all defines in the compiler generated assembly and convert them to #define pragmas""" 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker asm_define_re = re.compile(r'">>(\w+) (?:\$|#)?([-0-9]+) (?:\$|#)?(0|1)<<"') 35*795d594fSAndroid Build Coastguard Worker asm_defines = asm_define_re.findall(input) 36*795d594fSAndroid Build Coastguard Worker if not asm_defines: 37*795d594fSAndroid Build Coastguard Worker raise RuntimeError("Failed to find any asm defines in the input") 38*795d594fSAndroid Build Coastguard Worker 39*795d594fSAndroid Build Coastguard Worker # Convert the found constants to #define pragmas. 40*795d594fSAndroid Build Coastguard Worker # In case the C++ compiler decides to reorder the AsmDefinesFor_${name} functions, 41*795d594fSAndroid Build Coastguard Worker # we don't want the order of the .h file to change from one compilation to another. 42*795d594fSAndroid Build Coastguard Worker # Sorting ensures deterministic order of the #defines. 43*795d594fSAndroid Build Coastguard Worker output = [] 44*795d594fSAndroid Build Coastguard Worker for name, value, negative_value in sorted(asm_defines): 45*795d594fSAndroid Build Coastguard Worker value = int(value) 46*795d594fSAndroid Build Coastguard Worker if value < 0 and negative_value == "0": 47*795d594fSAndroid Build Coastguard Worker # Overflow - uint64_t constant was pretty printed as negative value. 48*795d594fSAndroid Build Coastguard Worker value += 2 ** 64 # Python will use arbitrary precision arithmetic. 49*795d594fSAndroid Build Coastguard Worker output.append("#define {0} {1:#x}".format(name, value)) 50*795d594fSAndroid Build Coastguard Worker return "\n".join(output) 51*795d594fSAndroid Build Coastguard Worker 52*795d594fSAndroid Build Coastguard Workerif __name__ == "__main__": 53*795d594fSAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 54*795d594fSAndroid Build Coastguard Worker parser.add_argument('input', help="Object file as text") 55*795d594fSAndroid Build Coastguard Worker args = parser.parse_args() 56*795d594fSAndroid Build Coastguard Worker print(convert(open(args.input, "r").read())) 57