Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / build / android / generate_xwalk_core_library.py
1 #!/usr/bin/env python
2 #
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
7
8 import distutils.dir_util
9 import optparse
10 import os
11 import shutil
12 import sys
13 from common_function import RemoveUnusedFilesInReleaseMode
14
15 LIBRARY_PROJECT_NAME = 'xwalk_core_library'
16 XWALK_CORE_SHELL_APK = 'xwalk_core_shell_apk'
17
18 def AddGeneratorOptions(option_parser):
19   option_parser.add_option('-s', dest='source',
20                            help='Source directory of project root.',
21                            type='string')
22   option_parser.add_option('-t', dest='target',
23                            help='Product out target directory.',
24                            type='string')
25
26
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):
35         os.remove(sub_path)
36
37
38 def CopyProjectFiles(project_source, out_directory):
39   """cp xwalk/build/android/xwalkcore_library_template/<file>
40         out/Release/xwalk_core_library/<file>
41   """
42
43   print 'Copying library project files...'
44   template_folder = os.path.join(project_source, 'xwalk', 'build', 'android',
45                                  'xwalkcore_library_template')
46   files_to_copy = [
47       # AndroidManifest.xml from template.
48       'AndroidManifest.xml',
49       # Eclipse project properties from template.
50       'project.properties',
51       # Ant build file.
52       'build.xml',
53       # Ant properties file.
54       'ant.properties',
55   ]
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)
59
60     shutil.copy2(source_file, target_file)
61
62
63 def CopyJavaSources(project_source, out_directory):
64   """cp <path>/java/src/<package>
65         out/Release/xwalk_core_library/src/<package>
66   """
67
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)
73
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.
77   java_srcs_to_copy = [
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',
89
90       # R.javas
91       'content/public/android/java/resource_map/org/chromium/content/R.java',
92       'ui/android/java/resource_map/org/chromium/ui/R.java',
93
94       # XWalk java sources.
95       'xwalk/runtime/android/core/src/org/xwalk/core',
96       'xwalk/extensions/android/java/src/org/xwalk/core/extensions',
97   ]
98
99   for source in java_srcs_to_copy:
100     # find the src/org in the path
101     slash_org_pos = source.find(r'/org/')
102     if slash_org_pos < 0:
103       raise Exception('Invalid java source path: %s' % source)
104     source_path = os.path.join(project_source, source)
105     package_path = source[slash_org_pos+1:]
106     target_path = os.path.join(target_source_directory, package_path)
107     if os.path.isfile(source_path):
108       if not os.path.isdir(os.path.dirname(target_path)):
109         os.makedirs(os.path.dirname(target_path))
110       shutil.copyfile(source_path, target_path)
111     else:
112       shutil.copytree(source_path, target_path)
113
114
115 def CopyGeneratedSources(out_directory):
116   """cp out/Release/gen/templates/<path>
117         out/Release/xwalk_core_library/src/<path>
118      cp out/Release/xwalk_core_shell_apk/
119             native_libraries_java/NativeLibraries.java
120         out/Release/xwalk_core_library/src/org/
121             chromium/content/app/NativeLibraries.java
122   """
123
124   print 'Copying generated source files...'
125   generated_srcs_to_copy = [
126       'org/chromium/base/ActivityState.java',
127       'org/chromium/base/MemoryPressureLevelList.java',
128       'org/chromium/content/app/NativeLibraries.java',
129       'org/chromium/content/browser/input/PopupItemType.java',
130       'org/chromium/content/browser/PageTransitionTypes.java',
131       'org/chromium/content/browser/SpeechRecognitionError.java',
132       'org/chromium/content/common/ResultCodes.java',
133       'org/chromium/content/common/TopControlsState.java',
134       'org/chromium/media/ImageFormat.java',
135       'org/chromium/net/CertificateMimeType.java',
136       'org/chromium/net/CertVerifyStatusAndroid.java',
137       'org/chromium/net/NetError.java',
138       'org/chromium/net/PrivateKeyType.java',
139       'org/chromium/ui/WindowOpenDisposition.java'
140   ]
141
142   for source in generated_srcs_to_copy:
143     source_file = os.path.join(out_directory, 'gen', 'templates', source)
144     target_file = os.path.join(
145         out_directory, LIBRARY_PROJECT_NAME, 'src', source)
146     shutil.copyfile(source_file, target_file)
147
148   source_file = os.path.join(out_directory, XWALK_CORE_SHELL_APK,
149                              'native_libraries_java',
150                              'NativeLibraries.java')
151   target_file = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'src', 'org',
152                              'chromium', 'content',
153                              'app', 'NativeLibraries.java')
154   shutil.copyfile(source_file, target_file)
155
156 def CopyJSBindingFiles(project_source, out_directory):
157   print 'Copying js binding files...'
158   jsapi_directory = os.path.join(out_directory,
159                                  LIBRARY_PROJECT_NAME,
160                                  'res',
161                                  'raw')
162   if not os.path.exists(jsapi_directory):
163     os.makedirs(jsapi_directory)
164
165   jsfiles_to_copy = [
166       'xwalk/experimental/launch_screen/launch_screen_api.js',
167       'xwalk/experimental/presentation/presentation_api.js',
168       'xwalk/runtime/extension/screen_orientation_api.js',
169       'xwalk/sysapps/device_capabilities/device_capabilities_api.js'
170   ]
171
172   # Copy JS binding file to assets/jsapi folder.
173   for jsfile in jsfiles_to_copy:
174     source_file = os.path.join(project_source, jsfile)
175     target_file = os.path.join(jsapi_directory, os.path.basename(source_file))
176     shutil.copyfile(source_file, target_file)
177
178
179 def CopyBinaries(out_directory):
180   """cp out/Release/<asset> out/Release/xwalk_core_library/assets/<asset>
181      cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
182      cp out/Release/xwalk_core_shell_apk/libs/*
183         out/Release/xwalk_core_library/libs
184   """
185
186   print 'Copying binaries...'
187   # Copy assets.
188   asset_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'assets')
189   if not os.path.exists(asset_directory):
190     os.mkdir(asset_directory)
191
192   assets_to_copy = [
193       'xwalk.pak',
194   ]
195
196   for asset in assets_to_copy:
197     source_file = os.path.join(out_directory, asset)
198     target_file = os.path.join(asset_directory, asset)
199     shutil.copyfile(source_file, target_file)
200
201   # Copy jar files to libs.
202   libs_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'libs')
203   if not os.path.exists(libs_directory):
204     os.mkdir(libs_directory)
205
206   libs_to_copy = [
207       'eyesfree_java.jar',
208       'guava_javalib.jar',
209       'jsr_305_javalib.jar',
210   ]
211
212   for lib in libs_to_copy:
213     source_file = os.path.join(out_directory, 'lib.java', lib)
214     target_file = os.path.join(libs_directory, lib)
215     shutil.copyfile(source_file, target_file)
216
217   # Copy native libraries.
218   source_dir = os.path.join(out_directory, XWALK_CORE_SHELL_APK, 'libs')
219   target_dir = libs_directory
220   distutils.dir_util.copy_tree(source_dir, target_dir)
221
222
223 def CopyDirAndPrefixDuplicates(input_dir, output_dir, prefix):
224   """ Copy the files into the output directory. If one file in input_dir folder
225   doesn't exist, copy it directly. If a file exists, copy it and rename the
226   file so that the resources won't be overrided. So all of them could be
227   packaged into the xwalk core library.
228   """
229   for root, _, files in os.walk(input_dir):
230     for f in files:
231       src_file = os.path.join(root, f)
232       relative_path = os.path.relpath(src_file, input_dir)
233       target_file = os.path.join(output_dir, relative_path)
234       target_dir_name = os.path.dirname(target_file)
235       if not os.path.exists(target_dir_name):
236         os.makedirs(target_dir_name)
237       # If the file exists, copy it and rename it with another name to
238       # avoid overwriting the existing one.
239       if os.path.exists(target_file):
240         target_base_name = os.path.basename(target_file)
241         target_base_name = prefix + '_' + target_base_name
242         target_file = os.path.join(target_dir_name, target_base_name)
243       shutil.copyfile(src_file, target_file)
244
245
246 def CopyResources(project_source, out_directory):
247   print 'Copying resources...'
248   res_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'res')
249   if os.path.exists(res_directory):
250     shutil.rmtree(res_directory)
251
252   # All resources should be in specific folders in res_directory.
253   # Since there might be some resource files with same names from
254   # different folders like ui_java, content_java and others,
255   # it's necessary to rename some files to avoid overridding.
256   res_to_copy = [
257       # (package, prefix) turple.
258       ('ui/android/java/res', 'ui'),
259       ('content/public/android/java/res', 'content'),
260       ('xwalk/runtime/android/java/res', 'xwalk_core'),
261   ]
262
263   # For each res, there are two generated res folder in out directory,
264   # they are res_grit and res_v14_compatibility.
265   for res, prefix in res_to_copy:
266     res_path_src = os.path.join(project_source, res)
267     res_path_grit = os.path.join(out_directory,
268         'gen', '%s_java' % prefix, 'res_grit')
269     res_path_v14 = os.path.join(out_directory,
270         'gen', '%s_java' % prefix, 'res_v14_compatibility')
271
272     for res_path in [res_path_src, res_path_grit, res_path_v14]:
273       CopyDirAndPrefixDuplicates(res_path, res_directory, prefix)
274
275
276 def PostCopyLibraryProject(out_directory):
277   print 'Post Copy Library Project...'
278   common_aidl_file = os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'src',
279                                   'org', 'chromium', 'content', 'common',
280                                   'common.aidl')
281   if os.path.exists(common_aidl_file):
282     os.remove(common_aidl_file)
283
284
285 def main(argv):
286   print 'Generating XWalkCore Library Project...'
287   option_parser = optparse.OptionParser()
288   AddGeneratorOptions(option_parser)
289   options, _ = option_parser.parse_args(argv)
290
291   if not os.path.exists(options.source):
292     print 'Source project does not exist, please provide correct directory.'
293     sys.exit(1)
294   out_directory = options.target
295
296   # Clean directory for project first.
297   CleanLibraryProject(out_directory)
298
299   out_project_directory = os.path.join(out_directory, LIBRARY_PROJECT_NAME)
300   if not os.path.exists(out_project_directory):
301     os.mkdir(out_project_directory)
302
303   # Copy Eclipse project files of library project.
304   CopyProjectFiles(options.source, out_directory)
305   # Copy Java sources of chromium and xwalk.
306   CopyJavaSources(options.source, out_directory)
307   CopyGeneratedSources(out_directory)
308   # Copy binaries and resuorces.
309   CopyBinaries(out_directory)
310   CopyResources(options.source, out_directory)
311   # Copy JS API binding files.
312   CopyJSBindingFiles(options.source, out_directory)
313   # Post copy library project.
314   PostCopyLibraryProject(out_directory)
315   # Remove unused files.
316   mode = os.path.basename(os.path.normpath(out_directory))
317   RemoveUnusedFilesInReleaseMode(mode,
318         os.path.join(out_directory, LIBRARY_PROJECT_NAME, 'libs'))
319   print 'Your Android library project has been created at %s' % (
320       out_project_directory)
321
322 if __name__ == '__main__':
323   sys.exit(main(sys.argv))