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/sysapps/device_capabilities/device_capabilities_api.js'
79 # Copy JS binding file to assets/jsapi folder.
80 for jsfile in jsfiles_to_copy:
81 source_file = os.path.join(project_source, jsfile)
82 target_file = os.path.join(jsapi_dir, os.path.basename(source_file))
83 shutil.copyfile(source_file, target_file)
86 def CopyBinaries(out_dir):
87 """cp out/Release/<pak> out/Release/xwalk_core_library/res/raw/<pak>
88 cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
89 cp out/Release/xwalk_core_shell_apk/libs/*
90 out/Release/xwalk_core_library/libs
93 print 'Copying binaries...'
95 res_raw_dir = os.path.join(
96 out_dir, LIBRARY_PROJECT_NAME, 'res', 'raw')
97 res_value_dir = os.path.join(
98 out_dir, LIBRARY_PROJECT_NAME, 'res', 'values')
99 if not os.path.exists(res_raw_dir):
100 os.mkdir(res_raw_dir)
101 if not os.path.exists(res_value_dir):
102 os.mkdir(res_value_dir)
109 pak_list_xml = Document()
110 resources_node = pak_list_xml.createElement('resources')
111 string_array_node = pak_list_xml.createElement('string-array')
112 string_array_node.setAttribute('name', 'xwalk_resources_list')
113 pak_list_xml.appendChild(resources_node)
114 resources_node.appendChild(string_array_node)
115 for pak in paks_to_copy:
116 source_file = os.path.join(out_dir, pak)
117 target_file = os.path.join(res_raw_dir, pak)
118 shutil.copyfile(source_file, target_file)
119 item_node = pak_list_xml.createElement('item')
120 item_node.appendChild(pak_list_xml.createTextNode(pak))
121 string_array_node.appendChild(item_node)
122 pak_list_file = open(os.path.join(res_value_dir,
123 'xwalk_resources_list.xml'), 'w')
124 pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
125 pak_list_file.close()
127 # Copy jar files to libs.
128 libs_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs')
129 if not os.path.exists(libs_dir):
133 'xwalk_core_library_java.jar',
136 for lib in libs_to_copy:
137 source_file = os.path.join(out_dir, 'lib.java', lib)
138 target_file = os.path.join(libs_dir, lib)
139 shutil.copyfile(source_file, target_file)
141 # Copy native libraries.
142 source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
143 target_dir = libs_dir
144 distutils.dir_util.copy_tree(source_dir, target_dir)
147 def CopyDirAndPrefixDuplicates(input_dir, output_dir, prefix):
148 """ Copy the files into the output directory. If one file in input_dir folder
149 doesn't exist, copy it directly. If a file exists, copy it and rename the
150 file so that the resources won't be overrided. So all of them could be
151 packaged into the xwalk core library.
153 for root, _, files in os.walk(input_dir):
155 src_file = os.path.join(root, f)
156 relative_path = os.path.relpath(src_file, input_dir)
157 target_file = os.path.join(output_dir, relative_path)
158 target_dir_name = os.path.dirname(target_file)
159 if not os.path.exists(target_dir_name):
160 os.makedirs(target_dir_name)
161 # If the file exists, copy it and rename it with another name to
162 # avoid overwriting the existing one.
163 if os.path.exists(target_file):
164 target_base_name = os.path.basename(target_file)
165 target_base_name = prefix + '_' + target_base_name
166 target_file = os.path.join(target_dir_name, target_base_name)
167 shutil.copyfile(src_file, target_file)
170 def CopyResources(project_source, out_dir):
171 print 'Copying resources...'
172 res_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'res')
173 if os.path.exists(res_dir):
174 shutil.rmtree(res_dir)
176 # All resources should be in specific folders in res_directory.
177 # Since there might be some resource files with same names from
178 # different folders like ui_java, content_java and others,
179 # it's necessary to rename some files to avoid overridding.
181 # (package, prefix) turple.
182 ('ui/android/java/res', 'ui'),
183 ('content/public/android/java/res', 'content'),
184 ('xwalk/runtime/android/java/res', 'xwalk_core'),
187 # For each res, there are two generated res folder in out directory,
188 # they are res_grit and res_v14_compatibility.
189 for res, prefix in res_to_copy:
190 res_path_src = os.path.join(project_source, res)
191 res_path_grit = os.path.join(out_dir,
192 'gen', '%s_java' % prefix, 'res_grit')
193 res_path_v14 = os.path.join(out_dir,
194 'gen', '%s_java' % prefix, 'res_v14_compatibility')
196 for res_path in [res_path_src, res_path_grit, res_path_v14]:
197 CopyDirAndPrefixDuplicates(res_path, res_dir, prefix)
200 def PostCopyLibraryProject(out_dir):
201 print 'Post Copy Library Project...'
203 'org/chromium/content/common/common.aidl',
204 'org/chromium/net/IRemoteAndroidKeyStoreInterface.aidl',
206 for aidl in aidls_to_remove:
207 aidl_file = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'src', aidl)
208 if os.path.exists(aidl_file):
213 print 'Generating XWalkCore Library Project...'
214 option_parser = optparse.OptionParser()
215 AddGeneratorOptions(option_parser)
216 options, _ = option_parser.parse_args(argv)
218 if not os.path.exists(options.source):
219 print 'Source project does not exist, please provide correct directory.'
221 out_dir = options.target
223 # Clean directory for project first.
224 CleanLibraryProject(out_dir)
226 out_project_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME)
227 if not os.path.exists(out_project_dir):
228 os.mkdir(out_project_dir)
230 # Copy Eclipse project files of library project.
231 CopyProjectFiles(options.source, out_dir)
232 # Copy binaries and resuorces.
233 CopyResources(options.source, out_dir)
234 CopyBinaries(out_dir)
235 # Copy JS API binding files.
236 CopyJSBindingFiles(options.source, out_dir)
237 # Post copy library project.
238 PostCopyLibraryProject(out_dir)
239 # Remove unused files.
240 mode = os.path.basename(os.path.normpath(out_dir))
241 RemoveUnusedFilesInReleaseMode(mode,
242 os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs'))
243 # Create empty src directory
244 src_dir = os.path.join(out_project_dir, 'src')
245 if not os.path.isdir(src_dir):
247 readme = os.path.join(src_dir, 'README.md')
248 open(readme, 'w').write(
249 "# Source folder for xwalk_core_library\n"
250 "## Why it's empty\n"
251 "xwalk_core_library doesn't contain java sources.\n"
252 "## Why put me here\n"
253 "To make archives keep the folder, "
254 "the src directory is needed to build an apk by ant.")
255 print 'Your Android library project has been created at %s' % (
258 if __name__ == '__main__':
259 sys.exit(main(sys.argv))