Guest User

Untitled

a guest
Apr 15th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.13 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. # Run it inside third_party/WebKit/Source/WebCore/WebCore.gyp (yes,
  9. # it's a folder)
  10. #
  11. # TODO:
  12. #  Make it work.
  13. #  And more
  14. #  Add ../../../ to includes in files in webkit/source/platform/chromium/public that inlude WebCore/... something.
  15.  
  16. from __future__ import print_function
  17. import os.path
  18. import re
  19. from glob import glob
  20.  
  21. """A map of file names pointing to the array of directories in which they were found"""
  22. files_map = {
  23. }
  24.  
  25. INCLUDE_RE = re.compile(r'#(?:include|import)\s+["<]([^">]+)[">]')
  26. SOURCE_EXTENSION_RE = re.compile(r"\.(cpp|h|gperf|y|includes|mm)")
  27.  
  28. # Include paths ripped out from third_party/WebKit/Source/WebCore/WebCore.gyp/WebCore.gyp
  29. INCLUDE_PATHS = {
  30.   "generic": [
  31.     "../Modules/battery",
  32.     "../Modules/filesystem",
  33.     "../Modules/filesystem/chromium",
  34.     "../Modules/gamepad",
  35.     "../Modules/geolocation",
  36.     "../Modules/intents",
  37.     "../Modules/indexeddb",
  38.     "../Modules/indexeddb/chromium",
  39.     "../Modules/mediasource",
  40.     "../Modules/mediastream",
  41.     "../Modules/navigatorcontentutils",
  42.     "../Modules/notifications",
  43.     "../Modules/proximity",
  44.     "../Modules/quota",
  45.     "../Modules/speech",
  46.     "../Modules/webaudio",
  47.     "../Modules/webdatabase",
  48.     "../Modules/webdatabase/chromium",
  49.     "../Modules/websockets",
  50.     "../accessibility",
  51.     "../accessibility/chromium",
  52.     "../bindings",
  53.     "../bindings/generic",
  54.     "../bindings/v8",
  55.     "../bindings/v8/custom",
  56.     "../bridge",
  57.     "../bridge/jni",
  58.     "../bridge/jni/v8",
  59.     "../css",
  60.     "../dom",
  61.     "../dom/default",
  62.     "../dom/default/chromium",
  63.     "../editing",
  64.     "../fileapi",
  65.     "../history",
  66.     "../html",
  67.     "../html/canvas",
  68.     "../html/parser",
  69.     "../html/shadow",
  70.     "../html/track",
  71.     "../inspector",
  72.     "../loader",
  73.     "../loader/appcache",
  74.     "../loader/archive",
  75.     "../loader/archive/cf",
  76.     "../loader/archive/mhtml",
  77.     "../loader/cache",
  78.     "../loader/icon",
  79.     "../mathml",
  80.     "../page",
  81.     "../page/animation",
  82.     "../page/chromium",
  83.     "../page/scrolling",
  84.     "../page/scrolling/chromium",
  85.     "../platform",
  86.     "../platform/animation",
  87.     "../platform/audio",
  88.     "../platform/audio/chromium",
  89.     "../platform/chromium",
  90.     "../platform/chromium/support",
  91.     "../platform/graphics",
  92.     "../platform/graphics/chromium",
  93.     "../platform/graphics/chromium/cc",
  94.     "../platform/graphics/cpu/arm",
  95.     "../platform/graphics/cpu/arm/filters",
  96.     "../platform/graphics/filters",
  97.     "../platform/graphics/filters/skia",
  98.     "../platform/graphics/gpu",
  99.     "../platform/graphics/opentype",
  100.     "../platform/graphics/skia",
  101.     "../platform/graphics/transforms",
  102.     "../platform/image-decoders",
  103.     "../platform/image-decoders/bmp",
  104.     "../platform/image-decoders/gif",
  105.     "../platform/image-decoders/ico",
  106.     "../platform/image-decoders/jpeg",
  107.     "../platform/image-decoders/png",
  108.     "../platform/image-decoders/skia",
  109.     "../platform/image-decoders/webp",
  110.     "../platform/image-encoders/skia",
  111.     "../platform/leveldb",
  112.     "../platform/mediastream",
  113.     "../platform/mediastream/chromium",
  114.     "../platform/mock",
  115.     "../platform/network",
  116.     "../platform/network/chromium",
  117.     "../platform/sql",
  118.     "../platform/text",
  119.     "../platform/text/transcoder",
  120.     "../plugins",
  121.     "../plugins/chromium",
  122.     "../rendering",
  123.     "../rendering/mathml",
  124.     "../rendering/style",
  125.     "../rendering/svg",
  126.     "../storage",
  127.     "../svg",
  128.     "../svg/animation",
  129.     "../svg/graphics",
  130.     "../svg/graphics/filters",
  131.     "../svg/properties",
  132.     "../../ThirdParty/glu",
  133.     "../workers",
  134.     "../workers/chromium",
  135.     "../xml",
  136.     "../xml/parser",
  137.     ],
  138.   "webkit_generic": [
  139.     # These are for WebKit projects. If this doesn't work we may have
  140.     # to build two databases, one for WebCore and one for WebKit.
  141.     "../../WebKit/chromium/public",
  142.     "../../WebKit/chromium/src",
  143.     ],
  144.   "mac": [
  145.     # os==mac
  146.     "../platform/audio/mac",
  147.     "../platform/cocoa",
  148.     "../platform/graphics/cg",
  149.     "../platform/graphics/cocoa",
  150.     "../platform/graphics/mac",
  151.     "../platform/mac",
  152.     "../platform/text/mac",
  153.     "../platform/graphics/harfbuzz",
  154.     "../platform/graphics/harfbuzz/ng",
  155.     ],
  156.   "win": [
  157.     # os==win
  158.     "../page/win",
  159.     "../platform/audio/win",
  160.     "../platform/graphics/win",
  161.     "../platform/text/win",
  162.     "../platform/win",
  163.     ],
  164.   "x11": [
  165.     # os==x11 or android or linux_embedded
  166.     "../platform/graphics/harfbuzz",
  167.     "../platform/graphics/harfbuzz/ng",
  168.     ],
  169. }
  170.  
  171. # Parts of WebKit/Source that should not be converted.
  172. PRESERVE_PATHS = [
  173.     "ThirdParty/glu",
  174.     "WebCore/platform/graphics/avfoundation",
  175.     "WebCore/platform/graphics/ca",
  176.     "WebCore/platform/graphics/cairo",
  177. #    "WebCore/platform/graphics/mac",
  178.     "WebCore/platform/graphics/win",
  179.     "WebCore/platform/network/cf",
  180.     "WebKit/blackberry",
  181.     "WebKit/cf",
  182.     "WebKit/efl",
  183.     "WebKit/gtk",
  184.     "WebKit/mac",
  185.     "WebKit/qt",
  186.     "WebKit/win",
  187.     "WebKit/wince",
  188.     "WebKit/wx",
  189.     "WebKit2",
  190.     ]
  191.  
  192. def report_duplicates():
  193.   print("Headers appearing more than once in the path:")
  194.   for header_name in files_map.keys():
  195.     path_list = files_map[header_name]
  196.     if len(path_list) > 1:
  197.       print("%s: %s" % (header_name, str(path_list)))
  198.  
  199. def generate_filename_to_dirpath_map():
  200.   sections = INCLUDE_PATHS.keys()
  201.   if "generic" in sections:
  202.     sections.remove("generic")
  203.   if "webkit_generic" in sections:
  204.     sections.remove("webkit_generic")
  205.   for prefix in ["generic", "webkit_generic"] + sections:
  206.     for inc_path in INCLUDE_PATHS[prefix]:
  207.       # print("At %s:" % inc_path)
  208.       add_files_to_dirpath_map(prefix, inc_path)
  209.  
  210.   # report_duplicates()
  211.  
  212. def add_files_to_dirpath_map(prefix, inc_path):
  213.     headers = glob(inc_path + "/*.h")
  214.     # if inc_path == "../platform/graphics":
  215.     #   print(headers)
  216.  
  217.     for header in headers:
  218.       filename = os.path.basename(header)
  219.       dirpath = os.path.dirname(os.path.relpath(header, "../..")).replace("\\", "/")
  220.       if filename in files_map:
  221.         files_map[filename].append((prefix, dirpath))
  222.       else:
  223.         files_map[filename] = [(prefix, dirpath)]
  224.       if "HashTools" in filename:
  225.         print("Found header %s(%d)" % (filename, len(files_map[filename])))
  226.  
  227. def lookup_filename_in_map(header_filename, dirpath, includer_filename):
  228.   if includer_filename == "CrossProcessFontLoading.mm":
  229.     print("mooo: " + header_filename)
  230.   if header_filename == "../graphics/FontPlatformData.h" and includer_filename == "CrossProcessFontLoading.mm":
  231.     print("Hej")
  232.     # This file is in the same directory as CrossProcessFontLoading.mm
  233.     # but just #import "FontPlatformData.h" seems to find the wrong
  234.     # file.
  235.     return "WebCore/platform/graphics/FontPlatformData.h"
  236.  
  237.   if header_filename in files_map:
  238.     # If the included file is in the same directory then don't change include
  239.     # path. Mostly done due to copying of some files from
  240.     # third_party/WebKit/Source/WebCore/platform/graphics/ into
  241.     # global_intermediate directory where that would cause problems.
  242.     dirpath = dirpath.replace("\\", "/")
  243.  
  244.     # Special case #1 WebSockets.
  245.     if header_filename == "WebSocket.h" and dirpath == "WebKit/chromium/src":
  246.       # There are two WebSocket.h and these files are compiled with
  247.       # different paths than the other file so replacing path would be
  248.       # wrong.
  249.       return "WebSocket.h"
  250.     if header_filename == "WebLayer.h" and dirpath == "Platform/chromium/public":
  251.       # "Platform/chromium/public" isn't in the include path (but
  252.       # . is) so the right WebLayer.h is not recorded and we'll pick
  253.       # one in the mac gfx directory
  254.       return "WebLayer.h"
  255.     if header_filename == "WebFontCache.h" and includer_filename == "WebCore/platform/mac":
  256.       # FIXME: This is not working due to a typo in the
  257.       # condition. Does that means it's not needed?
  258.  
  259.       # "Platform/chromium/public" isn't in the include path (but
  260.       # . is) so the right WebLayer.h is not recorded and we'll pick
  261.       # one in the mac gfx directory
  262.       return "WebLayer.h"
  263.  
  264.     # if "WebFontCache" in header_filename:
  265.     #   print("Map for %s: %s" % (header_filename,
  266.     #                             str(files_map[header_filename])))
  267.     #   print("dirpath: " + dirpath)
  268.  
  269.     path_types, header_paths = zip(*files_map[header_filename])
  270.     if dirpath in header_paths and not (includer_filename.endswith(".y") or
  271.                                         includer_filename.endswith(".gperf") or
  272.                                         includer_filename.endswith("includes")):
  273.       # Searching folder "."
  274.       # FIXME: This is not right, it's possible dirpath wasn't in the
  275.       # list of paths to search, and "." should still find it.
  276.       #      print("1")
  277.  
  278.       # Returning just header_filename means that things should work
  279.       # every time "." is in the include path which should be
  280.       # always. Returning dirpath + "/" + header_filename might look
  281.       # better but dirpath might not always be in the include path if
  282.       # this is a header included from another project.
  283.       return header_filename
  284.     elif os.path.dirname(dirpath) in header_paths:
  285.       # Searching folder ".."
  286.       # FIXME: This is not right, it's possible dirpath/.. wasn't in the
  287.       # list of paths to search, and ".." should still find it.
  288. #      print("2")
  289.       return os.path.dirname(dirpath) + "/" + header_filename
  290.     elif os.path.dirname(os.path.dirname(dirpath)) in header_paths:
  291.       # Searching folder "../.."
  292.       # FIXME: This is not right, it's possible dirpath/../.. wasn't in the
  293.       # list of paths to search, and "../.." should still find it.
  294. #      print("3")
  295.       return os.path.dirname(os.path.dirname(dirpath)) + "/" + header_filename
  296.     else:
  297.       if path_types[0] == "generic":
  298.         #      print("4")
  299.         return header_paths[0] + "/" + header_filename
  300.       elif path_types[0] == "webkit_generic" and ("WebKit" in dirpath or
  301.                                                   len(path_types) == 1):
  302.         return header_paths[0] + "/" + header_filename
  303.       else:
  304.         if path_types[0] == "webkit_generic":
  305.           path_types = path_types[1:]
  306.           header_paths = header_paths[1:]
  307.  
  308.         # DAMN! A platform dependant include.
  309.         # FIXME
  310.         if "Win" in includer_filename:
  311.           for (path_type, header_path) in zip(path_types, header_paths):
  312.             if path_type == "win":
  313.               return header_path + "/" + header_filename
  314.         elif ("Mac" in includer_filename or
  315.               includer_filename.endswith(".mm") or
  316.               (header_filename == "SoftLinking.h" and
  317.                "XSL" in includer_filename)):
  318.           for (path_type, header_path) in zip(path_types, header_paths):
  319.             if path_type == "mac":
  320.               return header_path + "/" + header_filename
  321.  
  322.         if len(set(header_paths)) > 1:
  323.           print("Include of "+ header_filename + " in " + dirpath + "/" +
  324.                 includer_filename + " will have a result depending on " +
  325.                 "platform. Please handle this manually. Options are " +
  326.                 str(files_map[header_filename]))
  327.         return header_paths[0] + "/" + header_filename
  328.   else:
  329. #    print("5. Including unknown header")
  330.     return header_filename
  331.  
  332. def replace_includes(content_location_path, contents, includer_filename):
  333.   """Normalizes include paths in contents"""
  334.   results = []
  335.   for m in INCLUDE_RE.finditer(contents):
  336.     results.append([m.group(1), m.start(1)])
  337.     if includer_filename == "CrossProcessFontLoading.mm":
  338.       print("nwww: " + m.group(0) + ": " + m.group(1))
  339.  
  340.   if not len(results):
  341.     return None
  342.  
  343.   string_list = []
  344.   current_offset = 0
  345.  
  346.   for (name, offset) in results:
  347.     string_list.append(contents[current_offset:offset])
  348.     replacement = lookup_filename_in_map(name, content_location_path,
  349.                                          includer_filename)
  350.     # if "HashTools.h" in name:
  351.     #   print("Replacing %s with %s in %s/%s" % (name, replacement, content_location_path, includer_filename))
  352.     string_list.append(replacement)
  353.     current_offset = offset + len(name)
  354.  
  355.   if current_offset < len(contents):
  356.     string_list.append(contents[current_offset:])
  357.  
  358.   return "".join(string_list)
  359.  
  360. if __name__ == "__main__":
  361.   generate_filename_to_dirpath_map()
  362.  
  363.   # A path to the directory in which source files will be modified recursively.
  364.   scanned_dir = "../.."
  365.   # A path to the directory which will be used as a root
  366.   # for modified #include paths. Relative to this script.
  367.   include_root = "../.."
  368.  
  369.   for dirpath, dirnames, filenames in os.walk(scanned_dir):
  370.     # print(dirpath)
  371.     # if dirpath == "../..\WebCore\platform\graphics\mac":
  372.     #   print(str(filenames))
  373.     # for fname in filenames:
  374.       # if "ComplexTextControllerCoreText.mm" in fname:
  375.       #   print("Found %s in %s" % (fname, dirpath))
  376.     skip = False
  377.     for preserve_dir in PRESERVE_PATHS:
  378.       adjusted_dirpath = dirpath.replace("\\", "/")
  379.       if preserve_dir in adjusted_dirpath:
  380.         skip = True
  381.         break
  382.     if skip:
  383.       # TODO: Clear dirnames to save a few ms.
  384. #      print("Skipping %s because it's in PRESERVE_PATHS." % dirpath)
  385.       continue
  386.  
  387.     for fname in filenames:
  388.       # if "ComplexTextControllerCoreText.mm" in fname:
  389.       #   print("Yes, %s is in %s" % (fname, dirpath))
  390.       if fname in ["FontBlackBerry.cpp", "RenderThemeSafari.cpp"]:
  391.         continue
  392.       # if "ComplexTextControllerCoreText.mm" in fname:
  393.       #   print("Found %s" % fname)
  394.       if (SOURCE_EXTENSION_RE.search(fname) or
  395.           fname == "create-html-entity-table"):
  396.         with open(os.path.join(dirpath, fname), "r+U") as f:
  397.           original = f.read()
  398.           maybe_modified = False
  399.           modified = original
  400.           # Special hacks for special brokenness
  401.           if fname.startswith("WebGL"):
  402.             # "." is not in the search path for includes done with
  403.             # #include <file>, only for #include "file".
  404.             modified = modified.replace("<WebGLRenderingContext.h>",
  405.                                         "\"WebGLRenderingContext.h\"")
  406.             maybe_modified = True
  407.           contents = replace_includes(os.path.relpath(dirpath, include_root),
  408.                                       modified,
  409.                                       fname)
  410.           if contents is not None:
  411.             maybe_modified = True
  412.             modified = contents
  413.           #continue # TEMPORARY continue WHILE TESTING SCRIPT FIXME TODO XXX
  414.           if maybe_modified and modified != original:
  415.             f.truncate(0)
  416.             f.seek(0)
  417.             f.write(contents)
  418.             # if "includes" in fname:
  419.             #   print "Modified %s" % fname
  420.           else:
  421.             pass
  422.             # if "includes" in fname:
  423.             #   print "Did not modify %s" % fname
Advertisement
Add Comment
Please, Sign In to add comment