Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / chromium / scripts / c99conv.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import os
8 import re
9 import sys
10 import subprocess
11
12
13 # Path to root FFmpeg directory.  Used as the CWD for executing all commands.
14 FFMPEG_ROOT = os.path.abspath(os.path.join(
15     os.path.dirname(__file__), '..', '..'))
16
17 # Path to the C99 to C89 converter.
18 CONVERTER_EXECUTABLE = os.path.abspath(os.path.join(
19     FFMPEG_ROOT, 'chromium', 'binaries', 'c99conv.exe'))
20
21 # $CC to use.   We use to support GOMA, but by switching to stdout preprocessing
22 # its twice as fast to just preprocess locally (compilation may still be GOMA).
23 DEFAULT_CC = 'cl.exe'
24
25 # Disable spammy warning related to av_restrict, upstream needs to fix.
26 DISABLED_WARNINGS = ['-wd4005']
27
28
29 def main():
30   if len(sys.argv) < 3:
31     print 'C99 to C89 Converter Wrapper'
32     print '  usage: c99conv.py <input file> <output file> [-I <include> ...]'
33     sys.exit(1)
34
35   input_file = os.path.abspath(sys.argv[1])
36   # Keep the preprocessed output file in the same directory so GOMA will work
37   # without complaining about unknown paths.
38   preprocessed_output_file = input_file + '_preprocessed.c'
39   output_file = os.path.abspath(sys.argv[2])
40
41   # Run the preprocessor command.  All of these settings are pulled from the
42   # CFLAGS section of the "config.mak" created after running build_ffmpeg.sh.
43   #
44   # cl.exe is extremely inefficient (upwards of 30k writes) when asked to
45   # preprocess to file (-P) so instead ask for output to go to stdout (-E) and
46   # write it out ourselves afterward.  See http://crbug.com/172368.
47   p = subprocess.Popen(
48       [DEFAULT_CC, '-E', '-nologo', '-DCOMPILING_avcodec=1',
49        '-DCOMPILING_avutil=1', '-DCOMPILING_avformat=1', '-D_USE_MATH_DEFINES',
50        '-Dinline=__inline', '-Dstrtoll=_strtoi64', '-U__STRICT_ANSI__',
51        '-D_ISOC99_SOURCE', '-D_LARGEFILE_SOURCE', '-DHAVE_AV_CONFIG_H',
52        '-Dstrtod=avpriv_strtod', '-Dsnprintf=avpriv_snprintf',
53        '-D_snprintf=avpriv_snprintf', '-Dvsnprintf=avpriv_vsnprintf',
54        '-FIstdlib.h'] +
55       sys.argv[3:] +
56       DISABLED_WARNINGS +
57       ['-I', '.', '-I', FFMPEG_ROOT, '-I', 'chromium/config',
58        '-I', 'chromium/include/win', input_file],
59       cwd=FFMPEG_ROOT, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
60       # universal_newlines ensures whitespace is correct for #line directives.
61       universal_newlines=True)
62   stdout, stderr = p.communicate()
63
64   # Abort if any error occurred.
65   if p.returncode != 0:
66     print stdout, stderr
67     if os.path.isfile(preprocessed_output_file):
68       os.unlink(preprocessed_output_file)
69     sys.exit(p.returncode)
70
71   with open(preprocessed_output_file, 'w') as f:
72     # Write out stdout but skip the filename print out that MSVC forces for
73     # every cl.exe execution as well as ridiculous amounts of white space; saves
74     # ~64mb of output over the entire conversion!  Care must be taken while
75     # trimming whitespace to keep #line directives accurate or stack traces will
76     # be inaccurate.
77     f.write(re.sub('\s+\n(#line|#pragma)', r'\n\1',
78                    stdout[len(os.path.basename(input_file)):]))
79
80   # Run the converter command.  Note: the input file must have a '.c' extension
81   # or the converter will crash.  libclang does some funky detection based on
82   # the file extension.
83   p = subprocess.Popen(
84       [CONVERTER_EXECUTABLE, preprocessed_output_file, output_file],
85       cwd=FFMPEG_ROOT)
86   p.wait()
87   os.unlink(preprocessed_output_file)
88   sys.exit(p.returncode)
89
90
91 if __name__ == '__main__':
92   main()