blob: 2f830fea11fbb320e961aef1f11ec3870f79502a [file] [log] [blame]
Michael Thvedt828a4be2015-08-12 17:45:36 +00001#!/usr/bin/python2.7
2
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Michael Thvedt828a4be2015-08-12 17:45:36 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http:#www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""A wrapper script for J2ObjC transpiler.
18
19This script wraps around J2ObjC transpiler to also output a dependency mapping
20file by scanning the import and include directives of the J2ObjC-translated
21files.
22"""
23
24import argparse
Rumou Duana1a13ae2016-07-26 17:23:26 +000025import errno
Michael Thvedt828a4be2015-08-12 17:45:36 +000026import multiprocessing
27import os
28import Queue
29import re
Rumou Duana1a13ae2016-07-26 17:23:26 +000030import shutil
Michael Thvedt828a4be2015-08-12 17:45:36 +000031import subprocess
Rumou Duan18742ad2015-09-02 23:45:03 +000032import tempfile
Michael Thvedt828a4be2015-08-12 17:45:36 +000033import threading
Rumou Duana1a13ae2016-07-26 17:23:26 +000034import zipfile
Michael Thvedt828a4be2015-08-12 17:45:36 +000035
Rumou Duana1a13ae2016-07-26 17:23:26 +000036_INCLUDE_RE = re.compile('#(include|import) "([^"]+)"')
37_CONST_DATE_TIME = [1980, 1, 1, 0, 0, 0]
Michael Thvedt828a4be2015-08-12 17:45:36 +000038
39
Rumou Duana1a13ae2016-07-26 17:23:26 +000040def RunJ2ObjC(java, jvm_flags, j2objc, main_class, output_file_path,
41 j2objc_args, source_paths, files_to_translate):
Michael Thvedt828a4be2015-08-12 17:45:36 +000042 """Runs J2ObjC transpiler to translate Java source files to ObjC.
43
44 Args:
45 java: The path of the Java executable.
46 jvm_flags: A comma-separated list of flags to pass to JVM.
47 j2objc: The deploy jar of J2ObjC.
48 main_class: The J2ObjC main class to invoke.
Rumou Duana1a13ae2016-07-26 17:23:26 +000049 output_file_path: The output file directory.
Rumou Duan18742ad2015-09-02 23:45:03 +000050 j2objc_args: A list of args to pass to J2ObjC transpiler.
Rumou Duana1a13ae2016-07-26 17:23:26 +000051 source_paths: A list of directories that contain sources to translate.
52 files_to_translate: A list of relative paths (relative to source_paths) that
53 point to sources to translate.
Michael Thvedt828a4be2015-08-12 17:45:36 +000054 Returns:
55 None.
56 """
Googlerf7bc9e52017-06-16 17:06:10 +020057 j2objc_args.extend(['-sourcepath', ':'.join(source_paths)])
58 j2objc_args.extend(['-d', output_file_path])
59 j2objc_args.extend(files_to_translate)
60 param_file_content = ' '.join(j2objc_args)
Rumou Duan18742ad2015-09-02 23:45:03 +000061 fd = None
62 param_filename = None
63 try:
64 fd, param_filename = tempfile.mkstemp(text=True)
Googlerf7bc9e52017-06-16 17:06:10 +020065 os.write(fd, param_file_content)
Rumou Duan18742ad2015-09-02 23:45:03 +000066 finally:
67 if fd:
68 os.close(fd)
69 try:
70 j2objc_cmd = [java]
71 j2objc_cmd.extend(filter(None, jvm_flags.split(',')))
72 j2objc_cmd.extend(['-cp', j2objc, main_class])
Rumou Duan18742ad2015-09-02 23:45:03 +000073 j2objc_cmd.extend(['@%s' % param_filename])
74 subprocess.check_call(j2objc_cmd, stderr=subprocess.STDOUT)
75 finally:
76 if param_filename:
77 os.remove(param_filename)
Michael Thvedt828a4be2015-08-12 17:45:36 +000078
79
Rumou Duana1a13ae2016-07-26 17:23:26 +000080def WriteDepMappingFile(objc_files,
81 objc_file_root,
Michael Thvedt828a4be2015-08-12 17:45:36 +000082 output_dependency_mapping_file,
83 file_open=open):
84 """Scans J2ObjC-translated files and outputs a dependency mapping file.
85
86 The mapping file contains mappings between translated source files and their
87 imported source files scanned from the import and include directives.
88
89 Args:
Rumou Duana1a13ae2016-07-26 17:23:26 +000090 objc_files: A list of ObjC files translated by J2ObjC.
91 objc_file_root: The file path which represents a directory where the
Michael Thvedt828a4be2015-08-12 17:45:36 +000092 generated ObjC files reside.
93 output_dependency_mapping_file: The path of the dependency mapping file to
94 write to.
95 file_open: Reference to the builtin open function so it may be
96 overridden for testing.
Rumou Duan49cdb4b2015-12-04 18:03:44 +000097 Raises:
98 RuntimeError: If spawned threads throw errors during processing.
Michael Thvedt828a4be2015-08-12 17:45:36 +000099 Returns:
100 None.
101 """
102 dep_mapping = dict()
103 input_file_queue = Queue.Queue()
104 output_dep_mapping_queue = Queue.Queue()
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000105 error_message_queue = Queue.Queue()
Rumou Duana1a13ae2016-07-26 17:23:26 +0000106 for objc_file in objc_files:
107 input_file_queue.put(os.path.join(objc_file_root, objc_file))
Michael Thvedt828a4be2015-08-12 17:45:36 +0000108
109 for _ in xrange(multiprocessing.cpu_count()):
110 t = threading.Thread(target=_ReadDepMapping, args=(input_file_queue,
111 output_dep_mapping_queue,
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000112 error_message_queue,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000113 objc_file_root,
Michael Thvedt828a4be2015-08-12 17:45:36 +0000114 file_open))
115 t.start()
116
117 input_file_queue.join()
118
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000119 if not error_message_queue.empty():
120 error_messages = [error_message for error_message in
121 error_message_queue.queue]
122 raise RuntimeError('\n'.join(error_messages))
123
Michael Thvedt828a4be2015-08-12 17:45:36 +0000124 while not output_dep_mapping_queue.empty():
125 entry_file, deps = output_dep_mapping_queue.get()
126 dep_mapping[entry_file] = deps
127
laszlocsomorf11c6bc2018-07-05 01:58:06 -0700128 with file_open(output_dependency_mapping_file, 'w') as f:
129 for entry in sorted(dep_mapping):
130 for dep in dep_mapping[entry]:
131 f.write(entry + ':' + dep + '\n')
Michael Thvedt828a4be2015-08-12 17:45:36 +0000132
133
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000134def _ReadDepMapping(input_file_queue, output_dep_mapping_queue,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000135 error_message_queue, output_root, file_open=open):
Michael Thvedt828a4be2015-08-12 17:45:36 +0000136 while True:
137 try:
138 input_file = input_file_queue.get_nowait()
139 except Queue.Empty:
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000140 # No more work left in the queue.
Michael Thvedt828a4be2015-08-12 17:45:36 +0000141 return
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000142
143 try:
Googlerfc031e22017-04-03 20:31:42 +0000144 deps = set()
145 input_file_name = os.path.splitext(input_file)[0]
146 entry = os.path.relpath(input_file_name, output_root)
147 for file_ext in ['.m', '.h']:
148 with file_open(input_file_name + file_ext, 'r') as f:
149 for line in f:
150 include = _INCLUDE_RE.match(line)
151 if include:
152 include_path = include.group(2)
153 dep = os.path.splitext(include_path)[0]
154 if dep != entry:
155 deps.add(dep)
156
157 output_dep_mapping_queue.put((entry, sorted(deps)))
Rumou Duan49cdb4b2015-12-04 18:03:44 +0000158 except Exception as e: # pylint: disable=broad-except
159 error_message_queue.put(str(e))
160 finally:
161 # We need to mark the task done to prevent blocking the main process
162 # indefinitely.
163 input_file_queue.task_done()
Michael Thvedt828a4be2015-08-12 17:45:36 +0000164
Rumou Duan18742ad2015-09-02 23:45:03 +0000165
Rumou Duan123e1c32016-02-01 16:16:15 +0000166def WriteArchiveSourceMappingFile(compiled_archive_file_path,
167 output_archive_source_mapping_file,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000168 objc_files,
Rumou Duan123e1c32016-02-01 16:16:15 +0000169 file_open=open):
170 """Writes a mapping file between archive file to associated ObjC source files.
171
172 Args:
173 compiled_archive_file_path: The path of the archive file.
174 output_archive_source_mapping_file: A path of the mapping file to write to.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000175 objc_files: A list of ObjC files translated by J2ObjC.
Rumou Duan123e1c32016-02-01 16:16:15 +0000176 file_open: Reference to the builtin open function so it may be
177 overridden for testing.
178 Returns:
179 None.
180 """
181 with file_open(output_archive_source_mapping_file, 'w') as f:
Rumou Duana1a13ae2016-07-26 17:23:26 +0000182 for objc_file in objc_files:
183 f.write(compiled_archive_file_path + ':' + objc_file + '\n')
184
185
Rumou Duan18742ad2015-09-02 23:45:03 +0000186def _ParseArgs(j2objc_args):
187 """Separate arguments passed to J2ObjC into source files and J2ObjC flags.
188
189 Args:
190 j2objc_args: A list of args to pass to J2ObjC transpiler.
191 Returns:
192 A tuple containing source files and J2ObjC flags
193 """
194 source_files = []
195 flags = []
196 is_next_flag_value = False
197 for j2objc_arg in j2objc_args:
198 if j2objc_arg.startswith('-'):
199 flags.append(j2objc_arg)
200 is_next_flag_value = True
201 elif is_next_flag_value:
202 flags.append(j2objc_arg)
203 is_next_flag_value = False
204 else:
205 source_files.append(j2objc_arg)
206 return (source_files, flags)
207
208
Rumou Duana1a13ae2016-07-26 17:23:26 +0000209def _J2ObjcOutputObjcFiles(java_files):
210 """Returns the relative paths of the associated output ObjC source files.
211
212 Args:
213 java_files: The list of Java files to translate.
214 Returns:
215 A list of associated output ObjC source files.
216 """
217 return [os.path.splitext(java_file)[0] + '.m' for java_file in java_files]
218
219
Rumou Duane7fd5392017-01-09 20:33:33 +0000220def UnzipSourceJarSources(source_jars):
221 """Unzips the source jars containing Java source files.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000222
223 Args:
Rumou Duane7fd5392017-01-09 20:33:33 +0000224 source_jars: The list of input Java source jars.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000225 Returns:
226 A tuple of the temporary output root and a list of root-relative paths of
227 unzipped Java files
228 """
Rumou Duane7fd5392017-01-09 20:33:33 +0000229 srcjar_java_files = []
230 if source_jars:
Rumou Duana1a13ae2016-07-26 17:23:26 +0000231 tmp_input_root = tempfile.mkdtemp()
Rumou Duane7fd5392017-01-09 20:33:33 +0000232 for source_jar in source_jars:
233 zip_ref = zipfile.ZipFile(source_jar, 'r')
234 zip_entries = []
Rumou Duana1a13ae2016-07-26 17:23:26 +0000235
Rumou Duane7fd5392017-01-09 20:33:33 +0000236 for file_entry in zip_ref.namelist():
237 # We only care about Java source files.
238 if file_entry.endswith('.java'):
239 zip_entries.append(file_entry)
Rumou Duana1a13ae2016-07-26 17:23:26 +0000240
Rumou Duane7fd5392017-01-09 20:33:33 +0000241 zip_ref.extractall(tmp_input_root, zip_entries)
242 zip_ref.close()
243 srcjar_java_files.extend(zip_entries)
244
245 return (tmp_input_root, srcjar_java_files)
Rumou Duana1a13ae2016-07-26 17:23:26 +0000246 else:
247 return None
248
249
250def RenameGenJarObjcFileRootInFileContent(tmp_objc_file_root,
251 j2objc_source_paths,
252 gen_src_jar, genjar_objc_files,
253 execute=subprocess.check_call):
254 """Renames references to temporary root inside ObjC sources from gen srcjar.
255
256 Args:
257 tmp_objc_file_root: The temporary output root containing ObjC sources.
258 j2objc_source_paths: The source paths used by J2ObjC.
259 gen_src_jar: The path of the gen srcjar.
260 genjar_objc_files: The list of ObjC sources translated from the gen srcjar.
261 execute: The function used to execute shell commands.
262 Returns:
263 None.
264 """
265 if genjar_objc_files:
266 abs_genjar_objc_source_files = [
267 os.path.join(tmp_objc_file_root, genjar_objc_file)
268 for genjar_objc_file in genjar_objc_files
269 ]
270 abs_genjar_objc_header_files = [
271 os.path.join(tmp_objc_file_root,
272 os.path.splitext(genjar_objc_file)[0] + '.h')
273 for genjar_objc_file in genjar_objc_files
274 ]
275
276 # We execute a command to change all references of the temporary Java root
277 # where we unzipped the gen srcjar sources, to the actual gen srcjar that
278 # contains the original Java sources.
279 cmd = [
280 'sed',
281 '-i',
282 '-e',
283 's|%s/|%s::|g' % (j2objc_source_paths[1], gen_src_jar)
284 ]
285 cmd.extend(abs_genjar_objc_source_files)
286 cmd.extend(abs_genjar_objc_header_files)
287 execute(cmd, stderr=subprocess.STDOUT)
288
289
Rumou Duand7cdc552016-11-03 19:42:48 +0000290def MoveObjcFileToFinalOutputRoot(objc_files,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000291 tmp_objc_file_root,
292 final_objc_file_root,
293 suffix,
294 os_module=os,
295 shutil_module=shutil):
Rumou Duand7cdc552016-11-03 19:42:48 +0000296 """Moves ObjC files from temporary location to the final output location.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000297
298 Args:
Rumou Duand7cdc552016-11-03 19:42:48 +0000299 objc_files: The list of objc files to move.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000300 tmp_objc_file_root: The temporary output root containing ObjC sources.
301 final_objc_file_root: The final output root.
Rumou Duand7cdc552016-11-03 19:42:48 +0000302 suffix: The suffix of the files to move.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000303 os_module: The os python module.
304 shutil_module: The shutil python module.
305 Returns:
306 None.
307 """
308 for objc_file in objc_files:
309 file_with_suffix = os_module.path.splitext(objc_file)[0] + suffix
310 dest_path = os_module.path.join(
311 final_objc_file_root, file_with_suffix)
312 dest_path_dir = os_module.path.dirname(dest_path)
313
314 if not os_module.path.isdir(dest_path_dir):
315 try:
316 os_module.makedirs(dest_path_dir)
317 except OSError as e:
318 if e.errno != errno.EEXIST or not os_module.path.isdir(dest_path_dir):
319 raise
320
Rumou Duand7cdc552016-11-03 19:42:48 +0000321 shutil_module.move(
Rumou Duana1a13ae2016-07-26 17:23:26 +0000322 os_module.path.join(tmp_objc_file_root, file_with_suffix),
323 dest_path)
324
325
326def PostJ2ObjcFileProcessing(normal_objc_files, genjar_objc_files,
327 tmp_objc_file_root, final_objc_file_root,
328 j2objc_source_paths, gen_src_jar,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000329 output_gen_source_dir, output_gen_header_dir):
330 """Performs cleanups on ObjC files and moves them to final output location.
331
332 Args:
333 normal_objc_files: The list of objc files translated from normal Java files.
334 genjar_objc_files: The list of ObjC sources translated from the gen srcjar.
335 tmp_objc_file_root: The temporary output root containing ObjC sources.
336 final_objc_file_root: The final output root.
337 j2objc_source_paths: The source paths used by J2ObjC.
338 gen_src_jar: The path of the gen srcjar.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000339 output_gen_source_dir: The final output directory of ObjC source files
340 translated from gen srcjar. Maybe null.
341 output_gen_header_dir: The final output directory of ObjC header files
342 translated from gen srcjar. Maybe null.
343 Returns:
344 None.
345 """
346 RenameGenJarObjcFileRootInFileContent(tmp_objc_file_root,
347 j2objc_source_paths,
348 gen_src_jar,
349 genjar_objc_files)
Rumou Duand7cdc552016-11-03 19:42:48 +0000350 MoveObjcFileToFinalOutputRoot(normal_objc_files,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000351 tmp_objc_file_root,
352 final_objc_file_root,
353 '.m')
Rumou Duand7cdc552016-11-03 19:42:48 +0000354 MoveObjcFileToFinalOutputRoot(normal_objc_files,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000355 tmp_objc_file_root,
356 final_objc_file_root,
357 '.h')
358
Rumou Duana1a13ae2016-07-26 17:23:26 +0000359 if output_gen_source_dir:
Rumou Duand7cdc552016-11-03 19:42:48 +0000360 MoveObjcFileToFinalOutputRoot(
Rumou Duana1a13ae2016-07-26 17:23:26 +0000361 genjar_objc_files,
362 tmp_objc_file_root,
363 output_gen_source_dir,
364 '.m')
365
366 if output_gen_header_dir:
Rumou Duand7cdc552016-11-03 19:42:48 +0000367 MoveObjcFileToFinalOutputRoot(
Rumou Duana1a13ae2016-07-26 17:23:26 +0000368 genjar_objc_files,
369 tmp_objc_file_root,
370 output_gen_header_dir,
371 '.h')
372
373
374def GenerateJ2objcMappingFiles(normal_objc_files,
375 genjar_objc_files,
376 tmp_objc_file_root,
377 output_dependency_mapping_file,
378 output_archive_source_mapping_file,
379 compiled_archive_file_path):
380 """Generates J2ObjC mapping files.
381
382 Args:
383 normal_objc_files: The list of objc files translated from normal Java files.
384 genjar_objc_files: The list of ObjC sources translated from the gen srcjar.
385 tmp_objc_file_root: The temporary output root containing ObjC sources.
386 output_dependency_mapping_file: The path of the dependency mapping file to
387 write to.
388 output_archive_source_mapping_file: A path of the mapping file to write to.
389 compiled_archive_file_path: The path of the archive file.
390 Returns:
391 None.
392 """
393 WriteDepMappingFile(normal_objc_files + genjar_objc_files,
394 tmp_objc_file_root,
395 output_dependency_mapping_file)
396
397 if output_archive_source_mapping_file:
398 WriteArchiveSourceMappingFile(compiled_archive_file_path,
399 output_archive_source_mapping_file,
400 normal_objc_files + genjar_objc_files)
401
402
403def main():
Rumou Duanab16dd62015-08-18 21:52:08 +0000404 parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
Michael Thvedt828a4be2015-08-12 17:45:36 +0000405 parser.add_argument(
406 '--java',
407 required=True,
408 help='The path to the Java executable.')
409 parser.add_argument(
410 '--jvm_flags',
Googlercc28a1c2017-08-23 18:37:08 +0200411 default='-Xss4m,-XX:+UseParallelGC',
Michael Thvedt828a4be2015-08-12 17:45:36 +0000412 help='A comma-separated list of flags to pass to the JVM.')
413 parser.add_argument(
414 '--j2objc',
415 required=True,
416 help='The path to the J2ObjC deploy jar.')
417 parser.add_argument(
418 '--main_class',
419 required=True,
420 help='The main class of the J2ObjC deploy jar to execute.')
Rumou Duana1a13ae2016-07-26 17:23:26 +0000421 # TODO(rduan): Remove, no longer needed.
Michael Thvedt828a4be2015-08-12 17:45:36 +0000422 parser.add_argument(
423 '--translated_source_files',
Rumou Duand7cdc552016-11-03 19:42:48 +0000424 required=False,
Michael Thvedt828a4be2015-08-12 17:45:36 +0000425 help=('A comma-separated list of file paths where J2ObjC will write the '
426 'translated files to.'))
427 parser.add_argument(
428 '--output_dependency_mapping_file',
429 required=True,
430 help='The file path of the dependency mapping file to write to.')
431 parser.add_argument(
Rumou Duana1a13ae2016-07-26 17:23:26 +0000432 '--objc_file_path', '-d',
Michael Thvedt828a4be2015-08-12 17:45:36 +0000433 required=True,
434 help=('The file path which represents a directory where the generated '
435 'ObjC files reside.'))
Rumou Duan123e1c32016-02-01 16:16:15 +0000436 parser.add_argument(
437 '--output_archive_source_mapping_file',
438 help='The file path of the mapping file containing mappings between the '
439 'translated source files and the to-be-generated archive file '
440 'compiled from those source files. --compile_archive_file_path must '
441 'be specified if this option is specified.')
442 parser.add_argument(
443 '--compiled_archive_file_path',
444 required=False,
445 help=('The archive file path that will be produced by ObjC compile action'
446 ' later'))
Rumou Duane7fd5392017-01-09 20:33:33 +0000447 # TODO(rduan): Remove this flag once it is fully replaced by flag --src_jars.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000448 parser.add_argument(
449 '--gen_src_jar',
450 required=False,
451 help='The jar containing Java sources generated by annotation processor.')
452 parser.add_argument(
Rumou Duane7fd5392017-01-09 20:33:33 +0000453 '--src_jars',
454 required=False,
jingwen4a74c522018-11-20 11:57:15 -0800455 help='The list of Java source jars containing Java sources to translate.')
Rumou Duane7fd5392017-01-09 20:33:33 +0000456 parser.add_argument(
Rumou Duana1a13ae2016-07-26 17:23:26 +0000457 '--output_gen_source_dir',
458 required=False,
459 help='The output directory of ObjC source files translated from the gen'
460 ' srcjar')
461 parser.add_argument(
462 '--output_gen_header_dir',
463 required=False,
464 help='The output directory of ObjC header files translated from the gen'
465 ' srcjar')
Michael Thvedt828a4be2015-08-12 17:45:36 +0000466
Rumou Duana1a13ae2016-07-26 17:23:26 +0000467 args, pass_through_args = parser.parse_known_args()
468 normal_java_files, j2objc_flags = _ParseArgs(pass_through_args)
Rumou Duane7fd5392017-01-09 20:33:33 +0000469 srcjar_java_files = []
Rumou Duana1a13ae2016-07-26 17:23:26 +0000470 j2objc_source_paths = [os.getcwd()]
471
Rumou Duane7fd5392017-01-09 20:33:33 +0000472 # Unzip the source jars, so J2ObjC can translate the contained sources.
Rumou Duana1a13ae2016-07-26 17:23:26 +0000473 # Also add the temporary directory containing the unzipped sources as a source
474 # path for J2ObjC, so it can find these sources.
Rumou Duane7fd5392017-01-09 20:33:33 +0000475 source_jars = []
476 if args.gen_src_jar:
477 source_jars.append(args.gen_src_jar)
478 if args.src_jars:
479 source_jars.extend(args.src_jars.split(','))
480
481 srcjar_source_tuple = UnzipSourceJarSources(source_jars)
482 if srcjar_source_tuple:
483 j2objc_source_paths.append(srcjar_source_tuple[0])
484 srcjar_java_files = srcjar_source_tuple[1]
Rumou Duana1a13ae2016-07-26 17:23:26 +0000485
486 # Run J2ObjC over the normal input Java files and unzipped gen jar Java files.
487 # The output is stored in a temporary directory.
488 tmp_objc_file_root = tempfile.mkdtemp()
Rumou Duan6773c152017-03-24 17:12:25 +0000489
490 # If we do not generate the header mapping from J2ObjC, we still
491 # need to specify --output-header-mapping, as it signals to J2ObjC that we
492 # are using source paths as import paths, not package paths.
493 # TODO(rduan): Make another flag in J2ObjC to specify using source paths.
494 if '--output-header-mapping' not in j2objc_flags:
495 j2objc_flags.extend(['--output-header-mapping', '/dev/null'])
496
Michael Thvedt828a4be2015-08-12 17:45:36 +0000497 RunJ2ObjC(args.java,
498 args.jvm_flags,
499 args.j2objc,
500 args.main_class,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000501 tmp_objc_file_root,
502 j2objc_flags,
503 j2objc_source_paths,
Rumou Duane7fd5392017-01-09 20:33:33 +0000504 normal_java_files + srcjar_java_files)
Rumou Duan123e1c32016-02-01 16:16:15 +0000505
Rumou Duana1a13ae2016-07-26 17:23:26 +0000506 # Calculate the relative paths of generated objc files.
507 normal_objc_files = _J2ObjcOutputObjcFiles(normal_java_files)
Rumou Duane7fd5392017-01-09 20:33:33 +0000508 genjar_objc_files = _J2ObjcOutputObjcFiles(srcjar_java_files)
Rumou Duana1a13ae2016-07-26 17:23:26 +0000509
510 # Generate J2ObjC mapping files needed for distributed builds.
511 GenerateJ2objcMappingFiles(normal_objc_files,
512 genjar_objc_files,
513 tmp_objc_file_root,
514 args.output_dependency_mapping_file,
515 args.output_archive_source_mapping_file,
516 args.compiled_archive_file_path)
517
Rumou Duand7cdc552016-11-03 19:42:48 +0000518 # Post J2ObjC-run processing, involving file editing, zipping and moving
Rumou Duana1a13ae2016-07-26 17:23:26 +0000519 # files to their final output locations.
520 PostJ2ObjcFileProcessing(
521 normal_objc_files,
522 genjar_objc_files,
523 tmp_objc_file_root,
524 args.objc_file_path,
525 j2objc_source_paths,
526 args.gen_src_jar,
Rumou Duana1a13ae2016-07-26 17:23:26 +0000527 args.output_gen_source_dir,
528 args.output_gen_header_dir)
529
530if __name__ == '__main__':
531 main()