michaelbai@google.com | 0d03514 | 2014-02-10 19:26:26 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | """This script is a tool to generate special header files from input |
| 12 | C source files. |
| 13 | |
| 14 | It first assembles the input source files to generate intermediate assembly |
| 15 | files (*.s). Then it parses the .s files and finds declarations of variables |
| 16 | whose names start with the string specified as the third argument in the |
| 17 | command-line, translates the variable names and values into constant defines |
| 18 | and writes them into header files. |
| 19 | """ |
| 20 | |
| 21 | import os |
| 22 | import re |
| 23 | import subprocess |
| 24 | import sys |
| 25 | from optparse import OptionParser |
| 26 | |
| 27 | def main(argv): |
| 28 | parser = OptionParser() |
| 29 | usage = 'Usage: %prog [options] input_filename' |
| 30 | parser.set_usage(usage) |
| 31 | parser.add_option('--compiler', default = 'gcc', help = 'compiler name') |
| 32 | parser.add_option('--options', default = '-S', help = 'compiler options') |
| 33 | parser.add_option('--pattern', default = 'offset_', help = 'A match pattern' |
| 34 | ' used for searching the relevant constants.') |
| 35 | parser.add_option('--dir', default = '.', help = 'output directory') |
| 36 | (options, args) = parser.parse_args() |
| 37 | |
| 38 | # Generate complete intermediate and header file names. |
| 39 | input_filename = args[0] |
| 40 | output_root = (options.dir + '/' + |
| 41 | os.path.splitext(os.path.basename(input_filename))[0]) |
| 42 | interim_filename = output_root + '.s' |
| 43 | out_filename = output_root + '.h' |
| 44 | |
| 45 | # Set the shell command with the compiler and options inputs. |
| 46 | compiler_command = (options.compiler + " " + options.options + " " + |
| 47 | input_filename + " -o " + interim_filename) |
| 48 | |
| 49 | # Run the shell command and generate the intermediate file. |
| 50 | subprocess.check_call(compiler_command, shell=True) |
| 51 | |
| 52 | interim_file = open(interim_filename) # The intermediate file. |
| 53 | out_file = open(out_filename, 'w') # The output header file. |
| 54 | |
| 55 | # Generate the output header file. |
| 56 | while True: |
| 57 | line = interim_file.readline() |
| 58 | if not line: break |
| 59 | if line.startswith(options.pattern): |
| 60 | # Find name of the next constant and write to the output file. |
| 61 | const_name = re.sub(r'^_', '', line.split(':')[0]) |
| 62 | out_file.write('#define %s ' % const_name) |
| 63 | |
| 64 | # Find value of the constant we just found and write to the output file. |
| 65 | line = interim_file.readline() |
| 66 | const_value = filter(str.isdigit, line.split(' ')[0]) |
| 67 | if const_value != '': |
| 68 | out_file.write('%s\n' % const_value) |
| 69 | |
| 70 | interim_file.close() |
| 71 | out_file.close() |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | main(sys.argv[1:]) |