3 # Copyright (c) 2013 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 # pylint: disable=F0401
8 import distutils.dir_util
13 from common_function import RemoveUnusedFilesInReleaseMode
15 LIBRARY_PROJECT_NAME = 'xwalk_core_library'
16 XWALK_CORE_SHELL_APK = 'xwalk_core_shell_apk'
18 def AddGeneratorOptions(option_parser):
19 option_parser.add_option('-s', dest='source',
20 help='Source directory of project root.',
22 option_parser.add_option('-t', dest='target',
23 help='Product out target directory.',
27 def CleanLibraryProject(out_directory):
28 out_project_path = os.path.join(out_directory, LIBRARY_PROJECT_NAME)
29 if os.path.exists(out_project_path):
30 for item in os.listdir(out_project_path):
31 sub_path = os.path.join(out_project_path, item)
32 if os.path.isdir(sub_path):
33 shutil.rmtree(sub_path)
34 elif os.path.isfile(sub_path):
38 def CopyProjectFiles(project_source, out_directory):
39 """cp xwalk/build/android/xwalkcore_library_template/<file>
40 out/Release/xwalk_core_library/<file>
43 print 'Copying library project files...'
44 template_folder = os.path.join(project_source, 'xwalk', 'build', 'android',
45 'xwalkcore_library_template')
47 # AndroidManifest.xml from template.
48 'AndroidManifest.xml',
49 # Eclipse project properties from template.
53 # Ant properties file.
56 for f in files_to_copy:
57 source_file = os.path.join(template_folder, f)
58 target_file = os.path.join(out_directory, LIBRARY_PROJECT_NAME, f)
60 shutil.copy2(source_file, target_file)
63 def CopyJavaSources(project_source, out_directory):
64 """cp <path>/java/src/<package>
65 out/Release/xwalk_core_library/src/<package>
68 print 'Copying Java sources...'
69 target_source_directory = os.path.join(
70 out_directory, LIBRARY_PROJECT_NAME, 'src')
71 if not os.path.exists(target_source_directory):
72 os.makedirs(target_source_directory)
74 # FIXME(wang16): There is an assumption here the package names listed
75 # here are all beginned with "org". If the assumption is broken in
76 # future, the logic needs to be adjusted accordingly.
78 # Chromium java sources.
79 'base/android/java/src/org/chromium/base',
80 'content/public/android/java/src/org/chromium/content',
81 'content/public/android/java/src/org/chromium/content_public',
82 'media/base/android/java/src/org/chromium/media',
83 'net/android/java/src/org/chromium/net',
84 'ui/android/java/src/org/chromium/ui',
85 'components/navigation_interception/android/java/'
86 'src/org/chromium/components/navigation_interception',
87 'components/web_contents_delegate_android/android/java/'
88 'src/org/chromium/components/web_contents_delegate_android',
91 'content/public/android/java/resource_map/org/chromium/content/R.java',
92 'ui/android/java/resource_map/org/chromium/ui/R.java',
95 'xwalk/runtime/android/java/src/org/xwalk/core',
96 'xwalk/extensions/android/java/src/org/xwalk/core/extensions',
97 'xwalk/runtime/android/java/src/org/xwalk/runtime/extension',
98 'xwalk/runtime/android/java/'
99 'src/org/xwalk/core/XWalkCookieManager.java',
102 for source in java_srcs_to_copy:
103 # find the src/org in the path
104 slash_org_pos = source.find(r'/org/')
105 if slash_org_pos < 0:
106 raise Exception('Invalid java source path: %s' % source)
107 source_path = os.path.join(project_source, source)
108 package_path = source[slash_org_pos+1:]
109 target_path = os.path.join(target_source_directory, package_path)
110 if os.path.isfile(source_path):
111 if not os.path.isdir(os.path.dirname(target_path)):
112 os.makedirs(os.path.dirname(target_path))
113 shutil.copyfile(source_path, target_path)
115 shutil.copytree(source_path, target_path)
118 def CopyGeneratedSources(out_directory):
119 """cp out/Release/gen/templates/<path>
120 out/Release/xwalk_core_library/src/<path>
121 cp out/Release/xwalk_core_shell_apk/
122 native_libraries_java/NativeLibraries.java
123 out/Release/xwalk_core_library/src/org/
124 chromium/content/app/NativeLibraries.java
127 print 'Copying generated source files...'
128 generated_srcs_to_copy = [
129 'org/chromium/base/ActivityState.java',
130 'org/chromium/base/MemoryPressureLevelList.java',
131 'org/chromium/content/app/NativeLibraries.java',
132 'org/chromium/content/browser/input/PopupItemType.java',
133 'org/chromium/content/browser/PageTransitionTypes.java',
134 'org/chromium/content/browser/SpeechRecognitionError.java',
135 'org/chromium/content/common/ResultCodes.java',
136 'org/chromium/content/common/TopControlsState.java',
137 'org/chromium/media/ImageFormat.java',
138 'org/chromium/net/CertificateMimeType.java',
139 'org/chromium/net/CertVerifyStatusAndroid.java',
140 'org/chromium/net/NetError.java',
141 'org/chromium/net/PrivateKeyType.java',
142 'org/chromium/ui/WindowOpenDisposition.java'
145 for source in generated_srcs_to_copy:
146 source_file = os.path.join(out_directory, 'gen', 'templates', source)
147 target_file = os.path.join(
148 out_directory, LIBRARY_PROJECT_NAME, 'src', source)
149 shutil.copyfile(source_file, target_file)
151 source_file = os.path.join(out_directory, XWALK_CORE_SHELL_APK,
152 'native_libraries_java',
153 'NativeLibraries.java')
154 target_file = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'src', 'org',
155 'chromium', 'content',
156 'app', 'NativeLibraries.java')
157 shutil.copyfile(source_file, target_file)
159 def CopyJSBindingFiles(project_source, out_directory):
160 print 'Copying js binding files...'
161 jsapi_directory = os.path.join(out_directory,
162 LIBRARY_PROJECT_NAME,
165 if not os.path.exists(jsapi_directory):
166 os.makedirs(jsapi_directory)
169 'xwalk/experimental/launch_screen/launch_screen_api.js',
170 'xwalk/experimental/presentation/presentation_api.js',
171 'xwalk/runtime/extension/screen_orientation_api.js',
172 'xwalk/sysapps/device_capabilities/device_capabilities_api.js'
175 # Copy JS binding file to assets/jsapi folder.
176 for jsfile in jsfiles_to_copy:
177 source_file = os.path.join(project_source, jsfile)
178 target_file = os.path.join(jsapi_directory, os.path.basename(source_file))
179 shutil.copyfile(source_file, target_file)
182 def CopyBinaries(out_directory):
183 """cp out/Release/<asset> out/Release/xwalk_core_library/assets/<asset>
184 cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
185 cp out/Release/xwalk_core_shell_apk/libs/*
186 out/Release/xwalk_core_library/libs
189 print 'Copying binaries...'
191 asset_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'assets')
192 if not os.path.exists(asset_directory):
193 os.mkdir(asset_directory)
199 for asset in assets_to_copy:
200 source_file = os.path.join(out_directory, asset)
201 target_file = os.path.join(asset_directory, asset)
202 shutil.copyfile(source_file, target_file)
204 # Copy jar files to libs.
205 libs_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'libs')
206 if not os.path.exists(libs_directory):
207 os.mkdir(libs_directory)
212 'jsr_305_javalib.jar',
215 for lib in libs_to_copy:
216 source_file = os.path.join(out_directory, 'lib.java', lib)
217 target_file = os.path.join(libs_directory, lib)
218 shutil.copyfile(source_file, target_file)
220 # Copy native libraries.
221 source_dir = os.path.join(out_directory, XWALK_CORE_SHELL_APK, 'libs')
222 target_dir = libs_directory
223 distutils.dir_util.copy_tree(source_dir, target_dir)
226 def CopyDirAndPrefixDuplicates(input_dir, output_dir, prefix):
227 """ Copy the files into the output directory. If one file in input_dir folder
228 doesn't exist, copy it directly. If a file exists, copy it and rename the
229 file so that the resources won't be overrided. So all of them could be
230 packaged into the xwalk core library.
232 for root, _, files in os.walk(input_dir):
234 src_file = os.path.join(root, f)
235 relative_path = os.path.relpath(src_file, input_dir)
236 target_file = os.path.join(output_dir, relative_path)
237 target_dir_name = os.path.dirname(target_file)
238 if not os.path.exists(target_dir_name):
239 os.makedirs(target_dir_name)
240 # If the file exists, copy it and rename it with another name to
241 # avoid overwriting the existing one.
242 if os.path.exists(target_file):
243 target_base_name = os.path.basename(target_file)
244 target_base_name = prefix + '_' + target_base_name
245 target_file = os.path.join(target_dir_name, target_base_name)
246 shutil.copyfile(src_file, target_file)
249 def CopyResources(project_source, out_directory):
250 print 'Copying resources...'
251 res_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'res')
252 if os.path.exists(res_directory):
253 shutil.rmtree(res_directory)
255 # All resources should be in specific folders in res_directory.
256 # Since there might be some resource files with same names from
257 # different folders like ui_java, content_java and others,
258 # it's necessary to rename some files to avoid overridding.
260 # (package, prefix) turple.
261 ('ui/android/java/res', 'ui'),
262 ('content/public/android/java/res', 'content'),
263 ('xwalk/runtime/android/java/res', 'xwalk_core'),
266 # For each res, there are two generated res folder in out directory,
267 # they are res_grit and res_v14_compatibility.
268 for res, prefix in res_to_copy:
269 res_path_src = os.path.join(project_source, res)
270 res_path_grit = os.path.join(out_directory,
271 'gen', '%s_java' % prefix, 'res_grit')
272 res_path_v14 = os.path.join(out_directory,
273 'gen', '%s_java' % prefix, 'res_v14_compatibility')
275 for res_path in [res_path_src, res_path_grit, res_path_v14]:
276 CopyDirAndPrefixDuplicates(res_path, res_directory, prefix)
279 def PostCopyLibraryProject(out_directory):
280 print 'Post Copy Library Project...'
281 common_aidl_file = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'src',
282 'org', 'chromium', 'content', 'common',
284 if os.path.exists(common_aidl_file):
285 os.remove(common_aidl_file)
289 print 'Generating XWalkCore Library Project...'
290 option_parser = optparse.OptionParser()
291 AddGeneratorOptions(option_parser)
292 options, _ = option_parser.parse_args(argv)
294 if not os.path.exists(options.source):
295 print 'Source project does not exist, please provide correct directory.'
297 out_directory = options.target
299 # Clean directory for project first.
300 CleanLibraryProject(out_directory)
302 out_project_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME)
303 if not os.path.exists(out_project_directory):
304 os.mkdir(out_project_directory)
306 # Copy Eclipse project files of library project.
307 CopyProjectFiles(options.source, out_directory)
308 # Copy Java sources of chromium and xwalk.
309 CopyJavaSources(options.source, out_directory)
310 CopyGeneratedSources(out_directory)
311 # Copy binaries and resuorces.
312 CopyBinaries(out_directory)
313 CopyResources(options.source, out_directory)
314 # Copy JS API binding files.
315 CopyJSBindingFiles(options.source, out_directory)
316 # Post copy library project.
317 PostCopyLibraryProject(out_directory)
318 # Remove unused files.
319 mode = os.path.basename(os.path.normpath(out_directory))
320 RemoveUnusedFilesInReleaseMode(mode,
321 os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'libs'))
322 print 'Your Android library project has been created at %s' % (
323 out_project_directory)
325 if __name__ == '__main__':
326 sys.exit(main(sys.argv))