kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +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 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 11 | """This script is a tool to generate special header files from input |
| 12 | C source files. |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 13 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 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 |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 16 | whose names start with the string specified as the third argument in the |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 17 | command-line, translates the variable names and values into constant defines |
| 18 | and writes them into header files. |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 19 | """ |
| 20 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 21 | import os |
kma@webrtc.org | 2f9bd24 | 2013-02-23 04:16:59 +0000 | [diff] [blame] | 22 | import re |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 23 | import subprocess |
kma@webrtc.org | 2f9bd24 | 2013-02-23 04:16:59 +0000 | [diff] [blame] | 24 | import sys |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 25 | from optparse import OptionParser |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 26 | |
| 27 | def main(argv): |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 28 | parser = OptionParser() |
leozwang@webrtc.org | 63a243a | 2012-12-05 07:12:15 +0000 | [diff] [blame] | 29 | usage = 'Usage: %prog [options] input_filename' |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 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() |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 37 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 38 | # Generate complete intermediate and header file names. |
leozwang@webrtc.org | 63a243a | 2012-12-05 07:12:15 +0000 | [diff] [blame] | 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' |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 44 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 45 | # Set the shell command with the compiler and options inputs. |
leozwang@webrtc.org | 63a243a | 2012-12-05 07:12:15 +0000 | [diff] [blame] | 46 | compiler_command = (options.compiler + " " + options.options + " " + |
| 47 | input_filename + " -o " + interim_filename) |
kma@webrtc.org | 2f9bd24 | 2013-02-23 04:16:59 +0000 | [diff] [blame] | 48 | |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 49 | # Run the shell command and generate the intermediate file. |
| 50 | subprocess.check_call(compiler_command, shell=True) |
| 51 | |
leozwang@webrtc.org | 63a243a | 2012-12-05 07:12:15 +0000 | [diff] [blame] | 52 | interim_file = open(interim_filename) # The intermediate file. |
| 53 | out_file = open(out_filename, 'w') # The output header file. |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 54 | |
| 55 | # Generate the output header file. |
kma@webrtc.org | 2f9bd24 | 2013-02-23 04:16:59 +0000 | [diff] [blame] | 56 | while True: |
| 57 | line = interim_file.readline() |
| 58 | if not line: break |
kma@webrtc.org | 31eae47 | 2012-11-07 22:34:16 +0000 | [diff] [blame] | 59 | if line.startswith(options.pattern): |
kma@webrtc.org | 2f9bd24 | 2013-02-23 04:16:59 +0000 | [diff] [blame] | 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) |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 69 | |
leozwang@webrtc.org | 63a243a | 2012-12-05 07:12:15 +0000 | [diff] [blame] | 70 | interim_file.close() |
| 71 | out_file.close() |
kma@webrtc.org | 0d321da | 2012-05-26 01:05:27 +0000 | [diff] [blame] | 72 | |
| 73 | if __name__ == "__main__": |
| 74 | main(sys.argv[1:]) |