9b93f6af58d8f6e156f67207dc7c1050f0a1c0db
[WebKit-https.git] / Tools / gtk / override-feature-defines
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import shutil
6 import sys
7
8 def copy_feature_defines_makefile():
9     src_path = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am.in')
10     dest_path = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am')
11     shutil.copy(src_path, dest_path)
12
13 def adjust_feature_defines_makefile(build_dir, feature_defines_overriding):
14     feature_defines_makefile_file = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am')
15     with open(feature_defines_makefile_file) as f:
16         feature_defines_makefile = f.read()
17
18     overriden_feature_defines = []
19     def override_feature_define(match):
20         matched_feature_define = match.group('feature')
21         if matched_feature_define not in feature_defines_overriding:
22             return match.string[match.start():match.end()]
23
24         if int(match.group('default_value')) is not feature_defines_overriding[matched_feature_define]:
25             overriden_feature_defines.append(matched_feature_define)
26         return "%s=%d" % (matched_feature_define, feature_defines_overriding[matched_feature_define])
27
28     feature_defines_makefile = re.sub(r"(?P<feature>(?:ENABLE_)\w+)=(?P<default_value>0|1)", override_feature_define, feature_defines_makefile)
29     with open(feature_defines_makefile_file, 'w') as f:
30         f.write(feature_defines_makefile)
31
32     if overriden_feature_defines:
33         print "The following feature defines were overriden:\n%s" % ', '.join(overriden_feature_defines)
34
35 def override_feature_defines(build_dir):
36     copy_feature_defines_makefile()
37
38     feature_defines_overriding_file = os.path.join(build_dir, 'feature-defines-overriding.txt')
39     if not os.path.exists(feature_defines_overriding_file):
40         return
41
42     with open(feature_defines_overriding_file) as f:
43         match_iter = re.findall(r"((?:ENABLE_)\w+)=(0|1)", f.read())
44
45     feature_defines_overriding = {}
46     for match in match_iter:
47         feature_defines_overriding[match[0]] = int(match[1])
48
49     adjust_feature_defines_makefile(build_dir, feature_defines_overriding)
50
51 if __name__=='__main__':
52     override_feature_defines(sys.argv[1])