Advertisement
Guest User

webkitfixincludes.python

a guest
Mar 27th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # This script will add include paths to many include directives in
  4. # webkit so that include paths can be removed from the compiler
  5. # command. Note, it will not generate a working build. A lot of
  6. # handfixing is necessary to fix it.
  7.  
  8. # TODO:
  9. #  Make it work.
  10. #  Fix .. and maybe ../.. so they're relative the right directory for each file.
  11. #  Fix platform dependant includes.
  12. #  And more
  13.  
  14. import os.path
  15. import re
  16. from glob import glob
  17.  
  18. '''A map of file names pointing to the array of directories in which they were found'''
  19. files_map = {
  20. }
  21.  
  22. INCLUDE_RE = re.compile(r'#include\s+["<"]([^">]+)[">]')
  23. SOURCE_EXTENSION_RE = re.compile(r'\.(cpp|h)')
  24.  
  25. # Include paths ripped out from third_party/WebKit/Source/WebCore/WebCore.gyp/WebCore.gyp
  26. INCLUDE_PATHS = [
  27.     '../',
  28.     '../..',
  29.     '../Modules/battery',
  30.     '../Modules/filesystem',
  31.     '../Modules/filesystem/chromium',
  32.     '../Modules/gamepad',
  33.     '../Modules/geolocation',
  34.     '../Modules/intents',
  35.     '../Modules/indexeddb',
  36.     '../Modules/indexeddb/chromium',
  37.     '../Modules/mediasource',
  38.     '../Modules/mediastream',
  39.     '../Modules/navigatorcontentutils',
  40.     '../Modules/notifications',
  41.     '../Modules/proximity',
  42.     '../Modules/quota',
  43.     '../Modules/speech',
  44.     '../Modules/webaudio',
  45.     '../Modules/webdatabase',
  46.     '../Modules/webdatabase/chromium',
  47.     '../Modules/websockets',
  48.     '../accessibility',
  49.     '../accessibility/chromium',
  50.     '../bindings',
  51.     '../bindings/generic',
  52.     '../bindings/v8',
  53.     '../bindings/v8/custom',
  54.     '../bridge',
  55.     '../bridge/jni',
  56.     '../bridge/jni/v8',
  57.     '../css',
  58.     '../dom',
  59.     '../dom/default',
  60.     '../editing',
  61.     '../fileapi',
  62.     '../history',
  63.     '../html',
  64.     '../html/canvas',
  65.     '../html/parser',
  66.     '../html/shadow',
  67.     '../html/track',
  68.     '../inspector',
  69.     '../loader',
  70.     '../loader/appcache',
  71.     '../loader/archive',
  72.     '../loader/archive/cf',
  73.     '../loader/archive/mhtml',
  74.     '../loader/cache',
  75.     '../loader/icon',
  76.     '../mathml',
  77.     '../page',
  78.     '../page/animation',
  79.     '../page/chromium',
  80.     '../page/scrolling',
  81.     '../page/scrolling/chromium',
  82.     '../platform',
  83.     '../platform/animation',
  84.     '../platform/audio',
  85.     '../platform/audio/chromium',
  86.     '../platform/chromium',
  87.     '../platform/chromium/support',
  88.     '../platform/graphics',
  89.     '../platform/graphics/chromium',
  90.     '../platform/graphics/chromium/cc',
  91.     '../platform/graphics/cpu/arm',
  92.     '../platform/graphics/cpu/arm/filters',
  93.     '../platform/graphics/filters',
  94.     '../platform/graphics/filters/skia',
  95.     '../platform/graphics/gpu',
  96.     '../platform/graphics/opentype',
  97.     '../platform/graphics/skia',
  98.     '../platform/graphics/transforms',
  99.     '../platform/image-decoders',
  100.     '../platform/image-decoders/bmp',
  101.     '../platform/image-decoders/gif',
  102.     '../platform/image-decoders/ico',
  103.     '../platform/image-decoders/jpeg',
  104.     '../platform/image-decoders/png',
  105.     '../platform/image-decoders/skia',
  106.     '../platform/image-decoders/webp',
  107.     '../platform/image-encoders/skia',
  108.     '../platform/leveldb',
  109.     '../platform/mediastream',
  110.     '../platform/mediastream/chromium',
  111.     '../platform/mock',
  112.     '../platform/network',
  113.     '../platform/network/chromium',
  114.     '../platform/sql',
  115.     '../platform/text',
  116.     '../platform/text/transcoder',
  117.     '../plugins',
  118.     '../plugins/chromium',
  119.     '../rendering',
  120.     '../rendering/mathml',
  121.     '../rendering/style',
  122.     '../rendering/svg',
  123.     '../storage',
  124.     '../svg',
  125.     '../svg/animation',
  126.     '../svg/graphics',
  127.     '../svg/graphics/filters',
  128.     '../svg/properties',
  129.     '../../ThirdParty/glu',
  130.     '../workers',
  131.     '../workers/chromium',
  132.     '../xml',
  133.     '../xml/parser',
  134.     # os==mac
  135.     '../platform/audio/mac',
  136.     '../platform/cocoa',
  137.     '../platform/graphics/cg',
  138.     '../platform/graphics/cocoa',
  139.     '../platform/graphics/mac',
  140.     '../platform/mac',
  141.     '../platform/text/mac',
  142.     '../platform/graphics/harfbuzz',
  143.     '../platform/graphics/harfbuzz/ng',
  144.     # os==win
  145.     '../page/win',
  146.     '../platform/audio/win',
  147.     '../platform/graphics/win',
  148.     '../platform/text/win',
  149.     '../platform/win',
  150.     # os==x11 or android or linux_embedded
  151.     '../platform/graphics/harfbuzz',
  152.     '../platform/graphics/harfbuzz/ng',
  153. ]
  154.  
  155. def generate_filename_to_dirpath_map():
  156.   for inc_path in INCLUDE_PATHS:
  157.     headers = glob(inc_path + '/*.h')
  158.     for header in headers:
  159.       filename = os.path.basename(header)
  160.       dirpath = os.path.dirname(os.path.relpath(header, '../..')).replace('\\', '/')
  161.       if filename in files_map:
  162.         files_map[filename].append(dirpath)
  163.       else:
  164.         files_map[filename] = [dirpath]
  165.  
  166. def lookup_filename_in_map(filename, dirpath):
  167.   if filename in files_map:
  168.     # If the included file is in the same directory then don't change include
  169.     # path. Mostly done due to copying of some files from
  170.     # third_party/WebKit/Source/WebCore/platform/graphics/ into
  171.     # global_intermediate directory where that would cause problems.
  172.     if dirpath in files_map[filename]:
  173.       return filename
  174.     else:
  175.       return files_map[filename][0] + '/' + filename
  176.   else:
  177.     return filename
  178.  
  179. def replace_includes(dirpath, contents):
  180.   '''Normalizes include paths in contents'''
  181.   results = []
  182.   for m in INCLUDE_RE.finditer(contents):
  183.     results.append([m.group(1), m.start(1)])
  184.  
  185.   if not len(results):
  186.     return None
  187.  
  188.   string_list = []
  189.   current_offset = 0
  190.  
  191.   for (name, offset) in results:
  192.     string_list.append(contents[current_offset:offset])
  193.     string_list.append(lookup_filename_in_map(name, dirpath))
  194.     current_offset = offset + len(name)
  195.  
  196.   if current_offset < len(contents):
  197.     string_list.append(contents[current_offset:])
  198.  
  199.   return ''.join(string_list)
  200.  
  201. if __name__ == '__main__':
  202.   generate_filename_to_dirpath_map()
  203.  
  204.   # A path to the directory in which source files will be modified recursively.
  205.   scanned_dir = '../..'
  206.   # A path to the directory which will be used as a root
  207.   # for modified #include paths. Relative to this script.
  208.   include_root = '../..'
  209.  
  210.   for dirpath, dirnames, filenames in os.walk(scanned_dir):
  211.     for fname in filenames:
  212.       if SOURCE_EXTENSION_RE.search(fname):
  213.         with open(os.path.join(dirpath, fname), 'r+U') as f:
  214.           contents = replace_includes(os.path.relpath(dirpath, include_root), f.read())
  215.           if contents is not None:
  216.             f.truncate(0)
  217.             f.seek(0)
  218.             f.write(contents)
  219.             print "Modified %s" % fname
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement