Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # This script will add include paths to many include directives in
- # webkit so that include paths can be removed from the compiler
- # command. Note, it will not generate a working build. A lot of
- # handfixing is necessary to fix it.
- # Run it inside third_party/WebKit/Source/WebCore/WebCore.gyp (yes,
- # it's a folder)
- #
- # TODO:
- # Make it work.
- # And more
- # Add ../../../ to includes in files in webkit/source/platform/chromium/public that inlude WebCore/... something.
- from __future__ import print_function
- import os.path
- import re
- from glob import glob
- """A map of file names pointing to the array of directories in which they were found"""
- files_map = {
- }
- INCLUDE_RE = re.compile(r'#(?:include|import)\s+["<]([^">]+)[">]')
- SOURCE_EXTENSION_RE = re.compile(r"\.(cpp|h|gperf|y|includes|mm)")
- # Include paths ripped out from third_party/WebKit/Source/WebCore/WebCore.gyp/WebCore.gyp
- INCLUDE_PATHS = {
- "generic": [
- "../Modules/battery",
- "../Modules/filesystem",
- "../Modules/filesystem/chromium",
- "../Modules/gamepad",
- "../Modules/geolocation",
- "../Modules/intents",
- "../Modules/indexeddb",
- "../Modules/indexeddb/chromium",
- "../Modules/mediasource",
- "../Modules/mediastream",
- "../Modules/navigatorcontentutils",
- "../Modules/notifications",
- "../Modules/proximity",
- "../Modules/quota",
- "../Modules/speech",
- "../Modules/webaudio",
- "../Modules/webdatabase",
- "../Modules/webdatabase/chromium",
- "../Modules/websockets",
- "../accessibility",
- "../accessibility/chromium",
- "../bindings",
- "../bindings/generic",
- "../bindings/v8",
- "../bindings/v8/custom",
- "../bridge",
- "../bridge/jni",
- "../bridge/jni/v8",
- "../css",
- "../dom",
- "../dom/default",
- "../dom/default/chromium",
- "../editing",
- "../fileapi",
- "../history",
- "../html",
- "../html/canvas",
- "../html/parser",
- "../html/shadow",
- "../html/track",
- "../inspector",
- "../loader",
- "../loader/appcache",
- "../loader/archive",
- "../loader/archive/cf",
- "../loader/archive/mhtml",
- "../loader/cache",
- "../loader/icon",
- "../mathml",
- "../page",
- "../page/animation",
- "../page/chromium",
- "../page/scrolling",
- "../page/scrolling/chromium",
- "../platform",
- "../platform/animation",
- "../platform/audio",
- "../platform/audio/chromium",
- "../platform/chromium",
- "../platform/chromium/support",
- "../platform/graphics",
- "../platform/graphics/chromium",
- "../platform/graphics/chromium/cc",
- "../platform/graphics/cpu/arm",
- "../platform/graphics/cpu/arm/filters",
- "../platform/graphics/filters",
- "../platform/graphics/filters/skia",
- "../platform/graphics/gpu",
- "../platform/graphics/opentype",
- "../platform/graphics/skia",
- "../platform/graphics/transforms",
- "../platform/image-decoders",
- "../platform/image-decoders/bmp",
- "../platform/image-decoders/gif",
- "../platform/image-decoders/ico",
- "../platform/image-decoders/jpeg",
- "../platform/image-decoders/png",
- "../platform/image-decoders/skia",
- "../platform/image-decoders/webp",
- "../platform/image-encoders/skia",
- "../platform/leveldb",
- "../platform/mediastream",
- "../platform/mediastream/chromium",
- "../platform/mock",
- "../platform/network",
- "../platform/network/chromium",
- "../platform/sql",
- "../platform/text",
- "../platform/text/transcoder",
- "../plugins",
- "../plugins/chromium",
- "../rendering",
- "../rendering/mathml",
- "../rendering/style",
- "../rendering/svg",
- "../storage",
- "../svg",
- "../svg/animation",
- "../svg/graphics",
- "../svg/graphics/filters",
- "../svg/properties",
- "../../ThirdParty/glu",
- "../workers",
- "../workers/chromium",
- "../xml",
- "../xml/parser",
- ],
- "webkit_generic": [
- # These are for WebKit projects. If this doesn't work we may have
- # to build two databases, one for WebCore and one for WebKit.
- "../../WebKit/chromium/public",
- "../../WebKit/chromium/src",
- ],
- "mac": [
- # os==mac
- "../platform/audio/mac",
- "../platform/cocoa",
- "../platform/graphics/cg",
- "../platform/graphics/cocoa",
- "../platform/graphics/mac",
- "../platform/mac",
- "../platform/text/mac",
- "../platform/graphics/harfbuzz",
- "../platform/graphics/harfbuzz/ng",
- ],
- "win": [
- # os==win
- "../page/win",
- "../platform/audio/win",
- "../platform/graphics/win",
- "../platform/text/win",
- "../platform/win",
- ],
- "x11": [
- # os==x11 or android or linux_embedded
- "../platform/graphics/harfbuzz",
- "../platform/graphics/harfbuzz/ng",
- ],
- }
- # Parts of WebKit/Source that should not be converted.
- PRESERVE_PATHS = [
- "ThirdParty/glu",
- "WebCore/platform/graphics/avfoundation",
- "WebCore/platform/graphics/ca",
- "WebCore/platform/graphics/cairo",
- # "WebCore/platform/graphics/mac",
- "WebCore/platform/graphics/win",
- "WebCore/platform/network/cf",
- "WebKit/blackberry",
- "WebKit/cf",
- "WebKit/efl",
- "WebKit/gtk",
- "WebKit/mac",
- "WebKit/qt",
- "WebKit/win",
- "WebKit/wince",
- "WebKit/wx",
- "WebKit2",
- ]
- def report_duplicates():
- print("Headers appearing more than once in the path:")
- for header_name in files_map.keys():
- path_list = files_map[header_name]
- if len(path_list) > 1:
- print("%s: %s" % (header_name, str(path_list)))
- def generate_filename_to_dirpath_map():
- sections = INCLUDE_PATHS.keys()
- if "generic" in sections:
- sections.remove("generic")
- if "webkit_generic" in sections:
- sections.remove("webkit_generic")
- for prefix in ["generic", "webkit_generic"] + sections:
- for inc_path in INCLUDE_PATHS[prefix]:
- # print("At %s:" % inc_path)
- add_files_to_dirpath_map(prefix, inc_path)
- # report_duplicates()
- def add_files_to_dirpath_map(prefix, inc_path):
- headers = glob(inc_path + "/*.h")
- # if inc_path == "../platform/graphics":
- # print(headers)
- for header in headers:
- filename = os.path.basename(header)
- dirpath = os.path.dirname(os.path.relpath(header, "../..")).replace("\\", "/")
- if filename in files_map:
- files_map[filename].append((prefix, dirpath))
- else:
- files_map[filename] = [(prefix, dirpath)]
- if "HashTools" in filename:
- print("Found header %s(%d)" % (filename, len(files_map[filename])))
- def lookup_filename_in_map(header_filename, dirpath, includer_filename):
- if includer_filename == "CrossProcessFontLoading.mm":
- print("mooo: " + header_filename)
- if header_filename == "../graphics/FontPlatformData.h" and includer_filename == "CrossProcessFontLoading.mm":
- print("Hej")
- # This file is in the same directory as CrossProcessFontLoading.mm
- # but just #import "FontPlatformData.h" seems to find the wrong
- # file.
- return "WebCore/platform/graphics/FontPlatformData.h"
- if header_filename in files_map:
- # If the included file is in the same directory then don't change include
- # path. Mostly done due to copying of some files from
- # third_party/WebKit/Source/WebCore/platform/graphics/ into
- # global_intermediate directory where that would cause problems.
- dirpath = dirpath.replace("\\", "/")
- # Special case #1 WebSockets.
- if header_filename == "WebSocket.h" and dirpath == "WebKit/chromium/src":
- # There are two WebSocket.h and these files are compiled with
- # different paths than the other file so replacing path would be
- # wrong.
- return "WebSocket.h"
- if header_filename == "WebLayer.h" and dirpath == "Platform/chromium/public":
- # "Platform/chromium/public" isn't in the include path (but
- # . is) so the right WebLayer.h is not recorded and we'll pick
- # one in the mac gfx directory
- return "WebLayer.h"
- if header_filename == "WebFontCache.h" and includer_filename == "WebCore/platform/mac":
- # FIXME: This is not working due to a typo in the
- # condition. Does that means it's not needed?
- # "Platform/chromium/public" isn't in the include path (but
- # . is) so the right WebLayer.h is not recorded and we'll pick
- # one in the mac gfx directory
- return "WebLayer.h"
- # if "WebFontCache" in header_filename:
- # print("Map for %s: %s" % (header_filename,
- # str(files_map[header_filename])))
- # print("dirpath: " + dirpath)
- path_types, header_paths = zip(*files_map[header_filename])
- if dirpath in header_paths and not (includer_filename.endswith(".y") or
- includer_filename.endswith(".gperf") or
- includer_filename.endswith("includes")):
- # Searching folder "."
- # FIXME: This is not right, it's possible dirpath wasn't in the
- # list of paths to search, and "." should still find it.
- # print("1")
- # Returning just header_filename means that things should work
- # every time "." is in the include path which should be
- # always. Returning dirpath + "/" + header_filename might look
- # better but dirpath might not always be in the include path if
- # this is a header included from another project.
- return header_filename
- elif os.path.dirname(dirpath) in header_paths:
- # Searching folder ".."
- # FIXME: This is not right, it's possible dirpath/.. wasn't in the
- # list of paths to search, and ".." should still find it.
- # print("2")
- return os.path.dirname(dirpath) + "/" + header_filename
- elif os.path.dirname(os.path.dirname(dirpath)) in header_paths:
- # Searching folder "../.."
- # FIXME: This is not right, it's possible dirpath/../.. wasn't in the
- # list of paths to search, and "../.." should still find it.
- # print("3")
- return os.path.dirname(os.path.dirname(dirpath)) + "/" + header_filename
- else:
- if path_types[0] == "generic":
- # print("4")
- return header_paths[0] + "/" + header_filename
- elif path_types[0] == "webkit_generic" and ("WebKit" in dirpath or
- len(path_types) == 1):
- return header_paths[0] + "/" + header_filename
- else:
- if path_types[0] == "webkit_generic":
- path_types = path_types[1:]
- header_paths = header_paths[1:]
- # DAMN! A platform dependant include.
- # FIXME
- if "Win" in includer_filename:
- for (path_type, header_path) in zip(path_types, header_paths):
- if path_type == "win":
- return header_path + "/" + header_filename
- elif ("Mac" in includer_filename or
- includer_filename.endswith(".mm") or
- (header_filename == "SoftLinking.h" and
- "XSL" in includer_filename)):
- for (path_type, header_path) in zip(path_types, header_paths):
- if path_type == "mac":
- return header_path + "/" + header_filename
- if len(set(header_paths)) > 1:
- print("Include of "+ header_filename + " in " + dirpath + "/" +
- includer_filename + " will have a result depending on " +
- "platform. Please handle this manually. Options are " +
- str(files_map[header_filename]))
- return header_paths[0] + "/" + header_filename
- else:
- # print("5. Including unknown header")
- return header_filename
- def replace_includes(content_location_path, contents, includer_filename):
- """Normalizes include paths in contents"""
- results = []
- for m in INCLUDE_RE.finditer(contents):
- results.append([m.group(1), m.start(1)])
- if includer_filename == "CrossProcessFontLoading.mm":
- print("nwww: " + m.group(0) + ": " + m.group(1))
- if not len(results):
- return None
- string_list = []
- current_offset = 0
- for (name, offset) in results:
- string_list.append(contents[current_offset:offset])
- replacement = lookup_filename_in_map(name, content_location_path,
- includer_filename)
- # if "HashTools.h" in name:
- # print("Replacing %s with %s in %s/%s" % (name, replacement, content_location_path, includer_filename))
- string_list.append(replacement)
- current_offset = offset + len(name)
- if current_offset < len(contents):
- string_list.append(contents[current_offset:])
- return "".join(string_list)
- if __name__ == "__main__":
- generate_filename_to_dirpath_map()
- # A path to the directory in which source files will be modified recursively.
- scanned_dir = "../.."
- # A path to the directory which will be used as a root
- # for modified #include paths. Relative to this script.
- include_root = "../.."
- for dirpath, dirnames, filenames in os.walk(scanned_dir):
- # print(dirpath)
- # if dirpath == "../..\WebCore\platform\graphics\mac":
- # print(str(filenames))
- # for fname in filenames:
- # if "ComplexTextControllerCoreText.mm" in fname:
- # print("Found %s in %s" % (fname, dirpath))
- skip = False
- for preserve_dir in PRESERVE_PATHS:
- adjusted_dirpath = dirpath.replace("\\", "/")
- if preserve_dir in adjusted_dirpath:
- skip = True
- break
- if skip:
- # TODO: Clear dirnames to save a few ms.
- # print("Skipping %s because it's in PRESERVE_PATHS." % dirpath)
- continue
- for fname in filenames:
- # if "ComplexTextControllerCoreText.mm" in fname:
- # print("Yes, %s is in %s" % (fname, dirpath))
- if fname in ["FontBlackBerry.cpp", "RenderThemeSafari.cpp"]:
- continue
- # if "ComplexTextControllerCoreText.mm" in fname:
- # print("Found %s" % fname)
- if (SOURCE_EXTENSION_RE.search(fname) or
- fname == "create-html-entity-table"):
- with open(os.path.join(dirpath, fname), "r+U") as f:
- original = f.read()
- maybe_modified = False
- modified = original
- # Special hacks for special brokenness
- if fname.startswith("WebGL"):
- # "." is not in the search path for includes done with
- # #include <file>, only for #include "file".
- modified = modified.replace("<WebGLRenderingContext.h>",
- "\"WebGLRenderingContext.h\"")
- maybe_modified = True
- contents = replace_includes(os.path.relpath(dirpath, include_root),
- modified,
- fname)
- if contents is not None:
- maybe_modified = True
- modified = contents
- #continue # TEMPORARY continue WHILE TESTING SCRIPT FIXME TODO XXX
- if maybe_modified and modified != original:
- f.truncate(0)
- f.seek(0)
- f.write(contents)
- # if "includes" in fname:
- # print "Modified %s" % fname
- else:
- pass
- # if "includes" in fname:
- # print "Did not modify %s" % fname
Advertisement
Add Comment
Please, Sign In to add comment