generate-feature-defines-files 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. import os
  3. import re
  4. import sys
  5. def read_feature_defines_override(feature_defines):
  6. feature_defines_overriding_file = 'WebKitFeatureOverrides.txt'
  7. if not os.path.exists(feature_defines_overriding_file):
  8. return
  9. print("The following feature defines were overriden:")
  10. with open(feature_defines_overriding_file) as f:
  11. match_iter = re.findall(r"((?:ENABLE_)\w+)=(0|1)", f.read())
  12. for match in match_iter:
  13. feature = match[0]
  14. value = int(match[1])
  15. if feature in feature_defines and value != feature_defines[feature]:
  16. print("\t{0}: {1} => {2}".format(feature, feature_defines[feature], value))
  17. feature_defines[feature] = value
  18. def write_file_if_contents_changed(filename, contents):
  19. if os.path.exists(filename):
  20. with open(filename, 'r') as f:
  21. old_contents = f.read()
  22. if old_contents == contents:
  23. return
  24. with open(filename, 'w') as f:
  25. f.write(contents)
  26. def write_feature_defines_header(feature_defines):
  27. contents = ''
  28. for (feature, value) in feature_defines.items():
  29. contents += '#define {0} {1}\n'.format(feature, value)
  30. write_file_if_contents_changed("WebKitFeatures.h", contents)
  31. def write_flattened_feature_defines_file(feature_defines):
  32. contents = ''
  33. for (feature, value) in feature_defines.items():
  34. contents += '{0}={1}\n'.format(feature, value)
  35. write_file_if_contents_changed("WebKitFeatures.txt", contents)
  36. def generate_feature_defines_files(default_features):
  37. build_dir = os.getcwd()
  38. feature_defines = {}
  39. for feature_define in default_features:
  40. (feature, value) = feature_define.split("=")
  41. feature_defines[feature] = int(value)
  42. read_feature_defines_override(feature_defines)
  43. write_feature_defines_header(feature_defines)
  44. write_flattened_feature_defines_file(feature_defines)
  45. if __name__=='__main__':
  46. generate_feature_defines_files(sys.argv[1:])