Upstream version 5.34.92.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/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',
100   ]
101
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)
114     else:
115       shutil.copytree(source_path, target_path)
116
117
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
125   """
126
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'
143   ]
144
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)
150
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)
158
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,
163                                  'assets',
164                                  'jsapi')
165   if not os.path.exists(jsapi_directory):
166     os.makedirs(jsapi_directory)
167
168   jsfiles_to_copy = [
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'
173   ]
174
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)
180
181
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
187   """
188
189   print 'Copying binaries...'
190   # Copy assets.
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)
194
195   assets_to_copy = [
196       'xwalk.pak',
197   ]
198
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)
203
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)
208
209   libs_to_copy = [
210       'eyesfree_java.jar',
211       'guava_javalib.jar',
212       'jsr_305_javalib.jar',
213   ]
214
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)
219
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)
224
225
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.
231   """
232   for root, _, files in os.walk(input_dir):
233     for f in files:
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)
247
248
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)
254
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.
259   res_to_copy = [
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'),
264   ]
265
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')
274
275     for res_path in [res_path_src, res_path_grit, res_path_v14]:
276       CopyDirAndPrefixDuplicates(res_path, res_directory, prefix)
277
278
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',
283                                   'common.aidl')
284   if os.path.exists(common_aidl_file):
285     os.remove(common_aidl_file)
286
287
288 def main(argv):
289   print 'Generating XWalkCore Library Project...'
290   option_parser = optparse.OptionParser()
291   AddGeneratorOptions(option_parser)
292   options, _ = option_parser.parse_args(argv)
293
294   if not os.path.exists(options.source):
295     print 'Source project does not exist, please provide correct directory.'
296     sys.exit(1)
297   out_directory = options.target
298
299   # Clean directory for project first.
300   CleanLibraryProject(out_directory)
301
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)
305
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)
324
325 if __name__ == '__main__':
326   sys.exit(main(sys.argv))