blob: c159d5074190b92fa389d1fb99565f44a5df8945 [file] [log] [blame]
kma@webrtc.org0d321da2012-05-26 01:05:27 +00001#!/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.org31eae472012-11-07 22:34:16 +000011"""This script is a tool to generate special header files from input
12C source files.
kma@webrtc.org0d321da2012-05-26 01:05:27 +000013
kma@webrtc.org31eae472012-11-07 22:34:16 +000014It first assembles the input source files to generate intermediate assembly
15files (*.s). Then it parses the .s files and finds declarations of variables
kma@webrtc.org0d321da2012-05-26 01:05:27 +000016whose names start with the string specified as the third argument in the
kma@webrtc.org31eae472012-11-07 22:34:16 +000017command-line, translates the variable names and values into constant defines
18and writes them into header files.
kma@webrtc.org0d321da2012-05-26 01:05:27 +000019"""
20
kma@webrtc.org31eae472012-11-07 22:34:16 +000021import os
kma@webrtc.org2f9bd242013-02-23 04:16:59 +000022import re
kma@webrtc.org31eae472012-11-07 22:34:16 +000023import subprocess
kma@webrtc.org2f9bd242013-02-23 04:16:59 +000024import sys
kma@webrtc.org31eae472012-11-07 22:34:16 +000025from optparse import OptionParser
kma@webrtc.org0d321da2012-05-26 01:05:27 +000026
27def main(argv):
kma@webrtc.org31eae472012-11-07 22:34:16 +000028 parser = OptionParser()
leozwang@webrtc.org63a243a2012-12-05 07:12:15 +000029 usage = 'Usage: %prog [options] input_filename'
kma@webrtc.org31eae472012-11-07 22:34:16 +000030 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.org0d321da2012-05-26 01:05:27 +000037
kma@webrtc.org31eae472012-11-07 22:34:16 +000038 # Generate complete intermediate and header file names.
leozwang@webrtc.org63a243a2012-12-05 07:12:15 +000039 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.org0d321da2012-05-26 01:05:27 +000044
kma@webrtc.org31eae472012-11-07 22:34:16 +000045 # Set the shell command with the compiler and options inputs.
leozwang@webrtc.org63a243a2012-12-05 07:12:15 +000046 compiler_command = (options.compiler + " " + options.options + " " +
47 input_filename + " -o " + interim_filename)
kma@webrtc.org2f9bd242013-02-23 04:16:59 +000048
kma@webrtc.org31eae472012-11-07 22:34:16 +000049 # Run the shell command and generate the intermediate file.
50 subprocess.check_call(compiler_command, shell=True)
51
leozwang@webrtc.org63a243a2012-12-05 07:12:15 +000052 interim_file = open(interim_filename) # The intermediate file.
53 out_file = open(out_filename, 'w') # The output header file.
kma@webrtc.org31eae472012-11-07 22:34:16 +000054
55 # Generate the output header file.
kma@webrtc.org2f9bd242013-02-23 04:16:59 +000056 while True:
57 line = interim_file.readline()
58 if not line: break
kma@webrtc.org31eae472012-11-07 22:34:16 +000059 if line.startswith(options.pattern):
kma@webrtc.org2f9bd242013-02-23 04:16:59 +000060 # 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.org0d321da2012-05-26 01:05:27 +000069
leozwang@webrtc.org63a243a2012-12-05 07:12:15 +000070 interim_file.close()
71 out_file.close()
kma@webrtc.org0d321da2012-05-26 01:05:27 +000072
73if __name__ == "__main__":
74 main(sys.argv[1:])