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
14 from xml.dom.minidom import Document
16 LIBRARY_PROJECT_NAME = 'xwalk_core_library'
17 XWALK_CORE_SHELL_APK = 'xwalk_core_shell_apk'
19 def AddGeneratorOptions(option_parser):
20 option_parser.add_option('-s', dest='source',
21 help='Source directory of project root.',
23 option_parser.add_option('-t', dest='target',
24 help='Product out target directory.',
28 def CleanLibraryProject(out_dir):
29 out_project_path = os.path.join(out_dir, LIBRARY_PROJECT_NAME)
30 if os.path.exists(out_project_path):
31 for item in os.listdir(out_project_path):
32 sub_path = os.path.join(out_project_path, item)
33 if os.path.isdir(sub_path):
34 shutil.rmtree(sub_path)
35 elif os.path.isfile(sub_path):
39 def CopyProjectFiles(project_source, out_dir):
40 """cp xwalk/build/android/xwalkcore_library_template/<file>
41 out/Release/xwalk_core_library/<file>
44 print 'Copying library project files...'
45 template_dir = os.path.join(project_source, 'xwalk', 'build', 'android',
46 'xwalkcore_library_template')
48 # AndroidManifest.xml from template.
49 'AndroidManifest.xml',
50 # Eclipse project properties from template.
54 # Ant properties file.
57 for f in files_to_copy:
58 source_file = os.path.join(template_dir, f)
59 target_file = os.path.join(out_dir, LIBRARY_PROJECT_NAME, f)
61 shutil.copy2(source_file, target_file)
64 def CopyJSBindingFiles(project_source, out_dir):
65 print 'Copying js binding files...'
66 jsapi_dir = os.path.join(out_dir,
70 if not os.path.exists(jsapi_dir):
71 os.makedirs(jsapi_dir)
74 'xwalk/experimental/launch_screen/launch_screen_api.js',
75 'xwalk/experimental/presentation/presentation_api.js',
76 'xwalk/runtime/extension/screen_orientation_api.js',
77 'xwalk/sysapps/device_capabilities/device_capabilities_api.js'
80 # Copy JS binding file to assets/jsapi folder.
81 for jsfile in jsfiles_to_copy:
82 source_file = os.path.join(project_source, jsfile)
83 target_file = os.path.join(jsapi_dir, os.path.basename(source_file))
84 shutil.copyfile(source_file, target_file)
87 def CopyBinaries(out_dir):
88 """cp out/Release/<pak> out/Release/xwalk_core_library/res/raw/<pak>
89 cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
90 cp out/Release/xwalk_core_shell_apk/libs/*
91 out/Release/xwalk_core_library/libs
94 print 'Copying binaries...'
96 res_raw_dir = os.path.join(
97 out_dir, LIBRARY_PROJECT_NAME, 'res', 'raw')
98 res_value_dir = os.path.join(
99 out_dir, LIBRARY_PROJECT_NAME, 'res', 'values')
100 if not os.path.exists(res_raw_dir):
101 os.mkdir(res_raw_dir)
102 if not os.path.exists(res_value_dir):
103 os.mkdir(res_value_dir)
110 pak_list_xml = Document()
111 resources_node = pak_list_xml.createElement('resources')
112 string_array_node = pak_list_xml.createElement('string-array')
113 string_array_node.setAttribute('name', 'xwalk_resources_list')
114 pak_list_xml.appendChild(resources_node)
115 resources_node.appendChild(string_array_node)
116 for pak in paks_to_copy:
117 source_file = os.path.join(out_dir, pak)
118 target_file = os.path.join(res_raw_dir, pak)
119 shutil.copyfile(source_file, target_file)
120 item_node = pak_list_xml.createElement('item')
121 item_node.appendChild(pak_list_xml.createTextNode(pak))
122 string_array_node.appendChild(item_node)
123 pak_list_file = open(os.path.join(res_value_dir,
124 'xwalk_resources_list.xml'), 'w')
125 pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
126 pak_list_file.close()
128 # Copy jar files to libs.
129 libs_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs')
130 if not os.path.exists(libs_dir):
134 'xwalk_core_library_java.jar',
137 for lib in libs_to_copy:
138 source_file = os.path.join(out_dir, 'lib.java', lib)
139 target_file = os.path.join(libs_dir, lib)
140 shutil.copyfile(source_file, target_file)
142 # Copy native libraries.
143 source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
144 target_dir = libs_dir
145 distutils.dir_util.copy_tree(source_dir, target_dir)
148 def CopyDirAndPrefixDuplicates(input_dir, output_dir, prefix):
149 """ Copy the files into the output directory. If one file in input_dir folder
150 doesn't exist, copy it directly. If a file exists, copy it and rename the
151 file so that the resources won't be overrided. So all of them could be
152 packaged into the xwalk core library.
154 for root, _, files in os.walk(input_dir):
156 src_file = os.path.join(root, f)
157 relative_path = os.path.relpath(src_file, input_dir)
158 target_file = os.path.join(output_dir, relative_path)
159 target_dir_name = os.path.dirname(target_file)
160 if not os.path.exists(target_dir_name):
161 os.makedirs(target_dir_name)
162 # If the file exists, copy it and rename it with another name to
163 # avoid overwriting the existing one.
164 if os.path.exists(target_file):
165 target_base_name = os.path.basename(target_file)
166 target_base_name = prefix + '_' + target_base_name
167 target_file = os.path.join(target_dir_name, target_base_name)
168 shutil.copyfile(src_file, target_file)
171 def CopyResources(project_source, out_dir):
172 print 'Copying resources...'
173 res_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'res')
174 if os.path.exists(res_dir):
175 shutil.rmtree(res_dir)
177 # All resources should be in specific folders in res_directory.
178 # Since there might be some resource files with same names from
179 # different folders like ui_java, content_java and others,
180 # it's necessary to rename some files to avoid overridding.
182 # (package, prefix) turple.
183 ('ui/android/java/res', 'ui'),
184 ('content/public/android/java/res', 'content'),
185 ('xwalk/runtime/android/java/res', 'xwalk_core'),
188 # For each res, there are two generated res folder in out directory,
189 # they are res_grit and res_v14_compatibility.
190 for res, prefix in res_to_copy:
191 res_path_src = os.path.join(project_source, res)
192 res_path_grit = os.path.join(out_dir,
193 'gen', '%s_java' % prefix, 'res_grit')
194 res_path_v14 = os.path.join(out_dir,
195 'gen', '%s_java' % prefix, 'res_v14_compatibility')
197 for res_path in [res_path_src, res_path_grit, res_path_v14]:
198 CopyDirAndPrefixDuplicates(res_path, res_dir, prefix)
201 def PostCopyLibraryProject(out_dir):
202 print 'Post Copy Library Project...'
204 'org/chromium/content/common/common.aidl',
205 'org/chromium/net/IRemoteAndroidKeyStoreInterface.aidl',
207 for aidl in aidls_to_remove:
208 aidl_file = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'src', aidl)
209 if os.path.exists(aidl_file):
214 print 'Generating XWalkCore Library Project...'
215 option_parser = optparse.OptionParser()
216 AddGeneratorOptions(option_parser)
217 options, _ = option_parser.parse_args(argv)
219 if not os.path.exists(options.source):
220 print 'Source project does not exist, please provide correct directory.'
222 out_dir = options.target
224 # Clean directory for project first.
225 CleanLibraryProject(out_dir)
227 out_project_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME)
228 if not os.path.exists(out_project_dir):
229 os.mkdir(out_project_dir)
231 # Copy Eclipse project files of library project.
232 CopyProjectFiles(options.source, out_dir)
233 # Copy binaries and resuorces.
234 CopyResources(options.source, out_dir)
235 CopyBinaries(out_dir)
236 # Copy JS API binding files.
237 CopyJSBindingFiles(options.source, out_dir)
238 # Post copy library project.
239 PostCopyLibraryProject(out_dir)
240 # Remove unused files.
241 mode = os.path.basename(os.path.normpath(out_dir))
242 RemoveUnusedFilesInReleaseMode(mode,
243 os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs'))
244 # Create empty src directory
245 src_dir = os.path.join(out_project_dir, 'src')
246 if not os.path.isdir(src_dir):
248 print 'Your Android library project has been created at %s' % (
251 if __name__ == '__main__':
252 sys.exit(main(sys.argv))