Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.7 |
| 2 | |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 4 | # |
| 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 | |
| 19 | This script wraps around J2ObjC transpiler to also output a dependency mapping |
| 20 | file by scanning the import and include directives of the J2ObjC-translated |
| 21 | files. |
| 22 | """ |
| 23 | |
| 24 | import argparse |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 25 | import errno |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 26 | import multiprocessing |
| 27 | import os |
| 28 | import Queue |
| 29 | import re |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 30 | import shutil |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 31 | import subprocess |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 32 | import tempfile |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 33 | import threading |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 34 | import zipfile |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 35 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 36 | _INCLUDE_RE = re.compile('#(include|import) "([^"]+)"') |
| 37 | _CONST_DATE_TIME = [1980, 1, 1, 0, 0, 0] |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 38 | |
| 39 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 40 | def RunJ2ObjC(java, jvm_flags, j2objc, main_class, output_file_path, |
| 41 | j2objc_args, source_paths, files_to_translate): |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 42 | """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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 49 | output_file_path: The output file directory. |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 50 | j2objc_args: A list of args to pass to J2ObjC transpiler. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 51 | 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 54 | Returns: |
| 55 | None. |
| 56 | """ |
Googler | f7bc9e5 | 2017-06-16 17:06:10 +0200 | [diff] [blame] | 57 | 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 Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 61 | fd = None |
| 62 | param_filename = None |
| 63 | try: |
| 64 | fd, param_filename = tempfile.mkstemp(text=True) |
Googler | f7bc9e5 | 2017-06-16 17:06:10 +0200 | [diff] [blame] | 65 | os.write(fd, param_file_content) |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 66 | 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 Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 73 | 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 78 | |
| 79 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 80 | def WriteDepMappingFile(objc_files, |
| 81 | objc_file_root, |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 82 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 90 | objc_files: A list of ObjC files translated by J2ObjC. |
| 91 | objc_file_root: The file path which represents a directory where the |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 92 | 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 Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 97 | Raises: |
| 98 | RuntimeError: If spawned threads throw errors during processing. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 99 | Returns: |
| 100 | None. |
| 101 | """ |
| 102 | dep_mapping = dict() |
| 103 | input_file_queue = Queue.Queue() |
| 104 | output_dep_mapping_queue = Queue.Queue() |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 105 | error_message_queue = Queue.Queue() |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 106 | for objc_file in objc_files: |
| 107 | input_file_queue.put(os.path.join(objc_file_root, objc_file)) |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 108 | |
| 109 | for _ in xrange(multiprocessing.cpu_count()): |
| 110 | t = threading.Thread(target=_ReadDepMapping, args=(input_file_queue, |
| 111 | output_dep_mapping_queue, |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 112 | error_message_queue, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 113 | objc_file_root, |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 114 | file_open)) |
| 115 | t.start() |
| 116 | |
| 117 | input_file_queue.join() |
| 118 | |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 119 | 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 124 | while not output_dep_mapping_queue.empty(): |
| 125 | entry_file, deps = output_dep_mapping_queue.get() |
| 126 | dep_mapping[entry_file] = deps |
| 127 | |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 128 | 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 132 | |
| 133 | |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 134 | def _ReadDepMapping(input_file_queue, output_dep_mapping_queue, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 135 | error_message_queue, output_root, file_open=open): |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 136 | while True: |
| 137 | try: |
| 138 | input_file = input_file_queue.get_nowait() |
| 139 | except Queue.Empty: |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 140 | # No more work left in the queue. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 141 | return |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 142 | |
| 143 | try: |
Googler | fc031e2 | 2017-04-03 20:31:42 +0000 | [diff] [blame] | 144 | 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 Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 158 | 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 164 | |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 165 | |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 166 | def WriteArchiveSourceMappingFile(compiled_archive_file_path, |
| 167 | output_archive_source_mapping_file, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 168 | objc_files, |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 169 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 175 | objc_files: A list of ObjC files translated by J2ObjC. |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 176 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 182 | for objc_file in objc_files: |
| 183 | f.write(compiled_archive_file_path + ':' + objc_file + '\n') |
| 184 | |
| 185 | |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 186 | def _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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 209 | def _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 Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 220 | def UnzipSourceJarSources(source_jars): |
| 221 | """Unzips the source jars containing Java source files. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 222 | |
| 223 | Args: |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 224 | source_jars: The list of input Java source jars. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 225 | Returns: |
| 226 | A tuple of the temporary output root and a list of root-relative paths of |
| 227 | unzipped Java files |
| 228 | """ |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 229 | srcjar_java_files = [] |
| 230 | if source_jars: |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 231 | tmp_input_root = tempfile.mkdtemp() |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 232 | for source_jar in source_jars: |
| 233 | zip_ref = zipfile.ZipFile(source_jar, 'r') |
| 234 | zip_entries = [] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 235 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 236 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 240 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 241 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 246 | else: |
| 247 | return None |
| 248 | |
| 249 | |
| 250 | def 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 Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 290 | def MoveObjcFileToFinalOutputRoot(objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 291 | tmp_objc_file_root, |
| 292 | final_objc_file_root, |
| 293 | suffix, |
| 294 | os_module=os, |
| 295 | shutil_module=shutil): |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 296 | """Moves ObjC files from temporary location to the final output location. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 297 | |
| 298 | Args: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 299 | objc_files: The list of objc files to move. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 300 | tmp_objc_file_root: The temporary output root containing ObjC sources. |
| 301 | final_objc_file_root: The final output root. |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 302 | suffix: The suffix of the files to move. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 303 | 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 Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 321 | shutil_module.move( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 322 | os_module.path.join(tmp_objc_file_root, file_with_suffix), |
| 323 | dest_path) |
| 324 | |
| 325 | |
| 326 | def PostJ2ObjcFileProcessing(normal_objc_files, genjar_objc_files, |
| 327 | tmp_objc_file_root, final_objc_file_root, |
| 328 | j2objc_source_paths, gen_src_jar, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 329 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 339 | 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 Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 350 | MoveObjcFileToFinalOutputRoot(normal_objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 351 | tmp_objc_file_root, |
| 352 | final_objc_file_root, |
| 353 | '.m') |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 354 | MoveObjcFileToFinalOutputRoot(normal_objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 355 | tmp_objc_file_root, |
| 356 | final_objc_file_root, |
| 357 | '.h') |
| 358 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 359 | if output_gen_source_dir: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 360 | MoveObjcFileToFinalOutputRoot( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 361 | genjar_objc_files, |
| 362 | tmp_objc_file_root, |
| 363 | output_gen_source_dir, |
| 364 | '.m') |
| 365 | |
| 366 | if output_gen_header_dir: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 367 | MoveObjcFileToFinalOutputRoot( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 368 | genjar_objc_files, |
| 369 | tmp_objc_file_root, |
| 370 | output_gen_header_dir, |
| 371 | '.h') |
| 372 | |
| 373 | |
| 374 | def 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 | |
| 403 | def main(): |
Rumou Duan | ab16dd6 | 2015-08-18 21:52:08 +0000 | [diff] [blame] | 404 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 405 | parser.add_argument( |
| 406 | '--java', |
| 407 | required=True, |
| 408 | help='The path to the Java executable.') |
| 409 | parser.add_argument( |
| 410 | '--jvm_flags', |
Googler | cc28a1c | 2017-08-23 18:37:08 +0200 | [diff] [blame] | 411 | default='-Xss4m,-XX:+UseParallelGC', |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 412 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 421 | # TODO(rduan): Remove, no longer needed. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 422 | parser.add_argument( |
| 423 | '--translated_source_files', |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 424 | required=False, |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 425 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 432 | '--objc_file_path', '-d', |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 433 | required=True, |
| 434 | help=('The file path which represents a directory where the generated ' |
| 435 | 'ObjC files reside.')) |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 436 | 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 Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 447 | # TODO(rduan): Remove this flag once it is fully replaced by flag --src_jars. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 448 | 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 Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 453 | '--src_jars', |
| 454 | required=False, |
jingwen | 4a74c52 | 2018-11-20 11:57:15 -0800 | [diff] [blame] | 455 | help='The list of Java source jars containing Java sources to translate.') |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 456 | parser.add_argument( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 457 | '--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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 466 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 467 | args, pass_through_args = parser.parse_known_args() |
| 468 | normal_java_files, j2objc_flags = _ParseArgs(pass_through_args) |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 469 | srcjar_java_files = [] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 470 | j2objc_source_paths = [os.getcwd()] |
| 471 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 472 | # Unzip the source jars, so J2ObjC can translate the contained sources. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 473 | # Also add the temporary directory containing the unzipped sources as a source |
| 474 | # path for J2ObjC, so it can find these sources. |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 475 | 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 485 | |
| 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 Duan | 6773c15 | 2017-03-24 17:12:25 +0000 | [diff] [blame] | 489 | |
| 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 Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 497 | RunJ2ObjC(args.java, |
| 498 | args.jvm_flags, |
| 499 | args.j2objc, |
| 500 | args.main_class, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 501 | tmp_objc_file_root, |
| 502 | j2objc_flags, |
| 503 | j2objc_source_paths, |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 504 | normal_java_files + srcjar_java_files) |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 505 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 506 | # Calculate the relative paths of generated objc files. |
| 507 | normal_objc_files = _J2ObjcOutputObjcFiles(normal_java_files) |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 508 | genjar_objc_files = _J2ObjcOutputObjcFiles(srcjar_java_files) |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 509 | |
| 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 Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 518 | # Post J2ObjC-run processing, involving file editing, zipping and moving |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 519 | # 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 Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 527 | args.output_gen_source_dir, |
| 528 | args.output_gen_header_dir) |
| 529 | |
| 530 | if __name__ == '__main__': |
| 531 | main() |