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 | """ |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 57 | source_file_manifest_content = ' '.join(files_to_translate) |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 58 | fd = None |
| 59 | param_filename = None |
| 60 | try: |
| 61 | fd, param_filename = tempfile.mkstemp(text=True) |
| 62 | os.write(fd, source_file_manifest_content) |
| 63 | finally: |
| 64 | if fd: |
| 65 | os.close(fd) |
| 66 | try: |
| 67 | j2objc_cmd = [java] |
| 68 | j2objc_cmd.extend(filter(None, jvm_flags.split(','))) |
| 69 | j2objc_cmd.extend(['-cp', j2objc, main_class]) |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 70 | j2objc_cmd.extend(j2objc_args) |
| 71 | j2objc_cmd.extend(['-sourcepath', ':'.join(source_paths)]) |
| 72 | j2objc_cmd.extend(['-d', output_file_path]) |
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 | |
| 128 | f = file_open(output_dependency_mapping_file, 'w') |
| 129 | for entry in sorted(dep_mapping): |
| 130 | for dep in dep_mapping[entry]: |
| 131 | f.write(entry + ':' + dep + '\n') |
| 132 | f.close() |
| 133 | |
| 134 | |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 135 | def _ReadDepMapping(input_file_queue, output_dep_mapping_queue, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 136 | error_message_queue, output_root, file_open=open): |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 137 | while True: |
| 138 | try: |
| 139 | input_file = input_file_queue.get_nowait() |
| 140 | except Queue.Empty: |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 141 | # No more work left in the queue. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 142 | return |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 143 | |
| 144 | try: |
| 145 | deps = [] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 146 | entry = os.path.relpath(os.path.splitext(input_file)[0], output_root) |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 147 | with file_open(input_file, 'r') as f: |
| 148 | for line in f: |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 149 | include = _INCLUDE_RE.match(line) |
Rumou Duan | 49cdb4b | 2015-12-04 18:03:44 +0000 | [diff] [blame] | 150 | if include: |
| 151 | include_path = include.group(2) |
| 152 | dep = os.path.splitext(include_path)[0] |
| 153 | if dep != entry: |
| 154 | deps.append(dep) |
| 155 | output_dep_mapping_queue.put((entry, deps)) |
| 156 | except Exception as e: # pylint: disable=broad-except |
| 157 | error_message_queue.put(str(e)) |
| 158 | finally: |
| 159 | # We need to mark the task done to prevent blocking the main process |
| 160 | # indefinitely. |
| 161 | input_file_queue.task_done() |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 162 | |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 163 | |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 164 | def WriteArchiveSourceMappingFile(compiled_archive_file_path, |
| 165 | output_archive_source_mapping_file, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 166 | objc_files, |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 167 | file_open=open): |
| 168 | """Writes a mapping file between archive file to associated ObjC source files. |
| 169 | |
| 170 | Args: |
| 171 | compiled_archive_file_path: The path of the archive file. |
| 172 | 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] | 173 | objc_files: A list of ObjC files translated by J2ObjC. |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 174 | file_open: Reference to the builtin open function so it may be |
| 175 | overridden for testing. |
| 176 | Returns: |
| 177 | None. |
| 178 | """ |
| 179 | with file_open(output_archive_source_mapping_file, 'w') as f: |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 180 | for objc_file in objc_files: |
| 181 | f.write(compiled_archive_file_path + ':' + objc_file + '\n') |
| 182 | |
| 183 | |
Rumou Duan | 18742ad | 2015-09-02 23:45:03 +0000 | [diff] [blame] | 184 | def _ParseArgs(j2objc_args): |
| 185 | """Separate arguments passed to J2ObjC into source files and J2ObjC flags. |
| 186 | |
| 187 | Args: |
| 188 | j2objc_args: A list of args to pass to J2ObjC transpiler. |
| 189 | Returns: |
| 190 | A tuple containing source files and J2ObjC flags |
| 191 | """ |
| 192 | source_files = [] |
| 193 | flags = [] |
| 194 | is_next_flag_value = False |
| 195 | for j2objc_arg in j2objc_args: |
| 196 | if j2objc_arg.startswith('-'): |
| 197 | flags.append(j2objc_arg) |
| 198 | is_next_flag_value = True |
| 199 | elif is_next_flag_value: |
| 200 | flags.append(j2objc_arg) |
| 201 | is_next_flag_value = False |
| 202 | else: |
| 203 | source_files.append(j2objc_arg) |
| 204 | return (source_files, flags) |
| 205 | |
| 206 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 207 | def _J2ObjcOutputObjcFiles(java_files): |
| 208 | """Returns the relative paths of the associated output ObjC source files. |
| 209 | |
| 210 | Args: |
| 211 | java_files: The list of Java files to translate. |
| 212 | Returns: |
| 213 | A list of associated output ObjC source files. |
| 214 | """ |
| 215 | return [os.path.splitext(java_file)[0] + '.m' for java_file in java_files] |
| 216 | |
| 217 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 218 | def UnzipSourceJarSources(source_jars): |
| 219 | """Unzips the source jars containing Java source files. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 220 | |
| 221 | Args: |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 222 | source_jars: The list of input Java source jars. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 223 | Returns: |
| 224 | A tuple of the temporary output root and a list of root-relative paths of |
| 225 | unzipped Java files |
| 226 | """ |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 227 | srcjar_java_files = [] |
| 228 | if source_jars: |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 229 | tmp_input_root = tempfile.mkdtemp() |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 230 | for source_jar in source_jars: |
| 231 | zip_ref = zipfile.ZipFile(source_jar, 'r') |
| 232 | zip_entries = [] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 233 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 234 | for file_entry in zip_ref.namelist(): |
| 235 | # We only care about Java source files. |
| 236 | if file_entry.endswith('.java'): |
| 237 | zip_entries.append(file_entry) |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 238 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 239 | zip_ref.extractall(tmp_input_root, zip_entries) |
| 240 | zip_ref.close() |
| 241 | srcjar_java_files.extend(zip_entries) |
| 242 | |
| 243 | return (tmp_input_root, srcjar_java_files) |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 244 | else: |
| 245 | return None |
| 246 | |
| 247 | |
| 248 | def RenameGenJarObjcFileRootInFileContent(tmp_objc_file_root, |
| 249 | j2objc_source_paths, |
| 250 | gen_src_jar, genjar_objc_files, |
| 251 | execute=subprocess.check_call): |
| 252 | """Renames references to temporary root inside ObjC sources from gen srcjar. |
| 253 | |
| 254 | Args: |
| 255 | tmp_objc_file_root: The temporary output root containing ObjC sources. |
| 256 | j2objc_source_paths: The source paths used by J2ObjC. |
| 257 | gen_src_jar: The path of the gen srcjar. |
| 258 | genjar_objc_files: The list of ObjC sources translated from the gen srcjar. |
| 259 | execute: The function used to execute shell commands. |
| 260 | Returns: |
| 261 | None. |
| 262 | """ |
| 263 | if genjar_objc_files: |
| 264 | abs_genjar_objc_source_files = [ |
| 265 | os.path.join(tmp_objc_file_root, genjar_objc_file) |
| 266 | for genjar_objc_file in genjar_objc_files |
| 267 | ] |
| 268 | abs_genjar_objc_header_files = [ |
| 269 | os.path.join(tmp_objc_file_root, |
| 270 | os.path.splitext(genjar_objc_file)[0] + '.h') |
| 271 | for genjar_objc_file in genjar_objc_files |
| 272 | ] |
| 273 | |
| 274 | # We execute a command to change all references of the temporary Java root |
| 275 | # where we unzipped the gen srcjar sources, to the actual gen srcjar that |
| 276 | # contains the original Java sources. |
| 277 | cmd = [ |
| 278 | 'sed', |
| 279 | '-i', |
| 280 | '-e', |
| 281 | 's|%s/|%s::|g' % (j2objc_source_paths[1], gen_src_jar) |
| 282 | ] |
| 283 | cmd.extend(abs_genjar_objc_source_files) |
| 284 | cmd.extend(abs_genjar_objc_header_files) |
| 285 | execute(cmd, stderr=subprocess.STDOUT) |
| 286 | |
| 287 | |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 288 | def MoveObjcFileToFinalOutputRoot(objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 289 | tmp_objc_file_root, |
| 290 | final_objc_file_root, |
| 291 | suffix, |
| 292 | os_module=os, |
| 293 | shutil_module=shutil): |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 294 | """Moves ObjC files from temporary location to the final output location. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 295 | |
| 296 | Args: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 297 | objc_files: The list of objc files to move. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 298 | tmp_objc_file_root: The temporary output root containing ObjC sources. |
| 299 | final_objc_file_root: The final output root. |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 300 | suffix: The suffix of the files to move. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 301 | os_module: The os python module. |
| 302 | shutil_module: The shutil python module. |
| 303 | Returns: |
| 304 | None. |
| 305 | """ |
| 306 | for objc_file in objc_files: |
| 307 | file_with_suffix = os_module.path.splitext(objc_file)[0] + suffix |
| 308 | dest_path = os_module.path.join( |
| 309 | final_objc_file_root, file_with_suffix) |
| 310 | dest_path_dir = os_module.path.dirname(dest_path) |
| 311 | |
| 312 | if not os_module.path.isdir(dest_path_dir): |
| 313 | try: |
| 314 | os_module.makedirs(dest_path_dir) |
| 315 | except OSError as e: |
| 316 | if e.errno != errno.EEXIST or not os_module.path.isdir(dest_path_dir): |
| 317 | raise |
| 318 | |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 319 | shutil_module.move( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 320 | os_module.path.join(tmp_objc_file_root, file_with_suffix), |
| 321 | dest_path) |
| 322 | |
| 323 | |
| 324 | def PostJ2ObjcFileProcessing(normal_objc_files, genjar_objc_files, |
| 325 | tmp_objc_file_root, final_objc_file_root, |
| 326 | j2objc_source_paths, gen_src_jar, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 327 | output_gen_source_dir, output_gen_header_dir): |
| 328 | """Performs cleanups on ObjC files and moves them to final output location. |
| 329 | |
| 330 | Args: |
| 331 | normal_objc_files: The list of objc files translated from normal Java files. |
| 332 | genjar_objc_files: The list of ObjC sources translated from the gen srcjar. |
| 333 | tmp_objc_file_root: The temporary output root containing ObjC sources. |
| 334 | final_objc_file_root: The final output root. |
| 335 | j2objc_source_paths: The source paths used by J2ObjC. |
| 336 | gen_src_jar: The path of the gen srcjar. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 337 | output_gen_source_dir: The final output directory of ObjC source files |
| 338 | translated from gen srcjar. Maybe null. |
| 339 | output_gen_header_dir: The final output directory of ObjC header files |
| 340 | translated from gen srcjar. Maybe null. |
| 341 | Returns: |
| 342 | None. |
| 343 | """ |
| 344 | RenameGenJarObjcFileRootInFileContent(tmp_objc_file_root, |
| 345 | j2objc_source_paths, |
| 346 | gen_src_jar, |
| 347 | genjar_objc_files) |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 348 | MoveObjcFileToFinalOutputRoot(normal_objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 349 | tmp_objc_file_root, |
| 350 | final_objc_file_root, |
| 351 | '.m') |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 352 | MoveObjcFileToFinalOutputRoot(normal_objc_files, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 353 | tmp_objc_file_root, |
| 354 | final_objc_file_root, |
| 355 | '.h') |
| 356 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 357 | if output_gen_source_dir: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 358 | MoveObjcFileToFinalOutputRoot( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 359 | genjar_objc_files, |
| 360 | tmp_objc_file_root, |
| 361 | output_gen_source_dir, |
| 362 | '.m') |
| 363 | |
| 364 | if output_gen_header_dir: |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 365 | MoveObjcFileToFinalOutputRoot( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 366 | genjar_objc_files, |
| 367 | tmp_objc_file_root, |
| 368 | output_gen_header_dir, |
| 369 | '.h') |
| 370 | |
| 371 | |
| 372 | def GenerateJ2objcMappingFiles(normal_objc_files, |
| 373 | genjar_objc_files, |
| 374 | tmp_objc_file_root, |
| 375 | output_dependency_mapping_file, |
| 376 | output_archive_source_mapping_file, |
| 377 | compiled_archive_file_path): |
| 378 | """Generates J2ObjC mapping files. |
| 379 | |
| 380 | Args: |
| 381 | normal_objc_files: The list of objc files translated from normal Java files. |
| 382 | genjar_objc_files: The list of ObjC sources translated from the gen srcjar. |
| 383 | tmp_objc_file_root: The temporary output root containing ObjC sources. |
| 384 | output_dependency_mapping_file: The path of the dependency mapping file to |
| 385 | write to. |
| 386 | output_archive_source_mapping_file: A path of the mapping file to write to. |
| 387 | compiled_archive_file_path: The path of the archive file. |
| 388 | Returns: |
| 389 | None. |
| 390 | """ |
| 391 | WriteDepMappingFile(normal_objc_files + genjar_objc_files, |
| 392 | tmp_objc_file_root, |
| 393 | output_dependency_mapping_file) |
| 394 | |
| 395 | if output_archive_source_mapping_file: |
| 396 | WriteArchiveSourceMappingFile(compiled_archive_file_path, |
| 397 | output_archive_source_mapping_file, |
| 398 | normal_objc_files + genjar_objc_files) |
| 399 | |
| 400 | |
| 401 | def main(): |
Rumou Duan | ab16dd6 | 2015-08-18 21:52:08 +0000 | [diff] [blame] | 402 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 403 | parser.add_argument( |
| 404 | '--java', |
| 405 | required=True, |
| 406 | help='The path to the Java executable.') |
| 407 | parser.add_argument( |
| 408 | '--jvm_flags', |
Googler | 1672c76 | 2016-12-21 16:15:02 +0000 | [diff] [blame] | 409 | default='-Xss4m', |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 410 | help='A comma-separated list of flags to pass to the JVM.') |
| 411 | parser.add_argument( |
| 412 | '--j2objc', |
| 413 | required=True, |
| 414 | help='The path to the J2ObjC deploy jar.') |
| 415 | parser.add_argument( |
| 416 | '--main_class', |
| 417 | required=True, |
| 418 | help='The main class of the J2ObjC deploy jar to execute.') |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 419 | # TODO(rduan): Remove, no longer needed. |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 420 | parser.add_argument( |
| 421 | '--translated_source_files', |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 422 | required=False, |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 423 | help=('A comma-separated list of file paths where J2ObjC will write the ' |
| 424 | 'translated files to.')) |
| 425 | parser.add_argument( |
| 426 | '--output_dependency_mapping_file', |
| 427 | required=True, |
| 428 | help='The file path of the dependency mapping file to write to.') |
| 429 | parser.add_argument( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 430 | '--objc_file_path', '-d', |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 431 | required=True, |
| 432 | help=('The file path which represents a directory where the generated ' |
| 433 | 'ObjC files reside.')) |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 434 | parser.add_argument( |
| 435 | '--output_archive_source_mapping_file', |
| 436 | help='The file path of the mapping file containing mappings between the ' |
| 437 | 'translated source files and the to-be-generated archive file ' |
| 438 | 'compiled from those source files. --compile_archive_file_path must ' |
| 439 | 'be specified if this option is specified.') |
| 440 | parser.add_argument( |
| 441 | '--compiled_archive_file_path', |
| 442 | required=False, |
| 443 | help=('The archive file path that will be produced by ObjC compile action' |
| 444 | ' later')) |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 445 | # 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] | 446 | parser.add_argument( |
| 447 | '--gen_src_jar', |
| 448 | required=False, |
| 449 | help='The jar containing Java sources generated by annotation processor.') |
| 450 | parser.add_argument( |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 451 | '--src_jars', |
| 452 | required=False, |
| 453 | help='The list of Java source jars containg Java sources to translate.') |
| 454 | parser.add_argument( |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 455 | '--output_gen_source_dir', |
| 456 | required=False, |
| 457 | help='The output directory of ObjC source files translated from the gen' |
| 458 | ' srcjar') |
| 459 | parser.add_argument( |
| 460 | '--output_gen_header_dir', |
| 461 | required=False, |
| 462 | help='The output directory of ObjC header files translated from the gen' |
| 463 | ' srcjar') |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 464 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 465 | args, pass_through_args = parser.parse_known_args() |
| 466 | normal_java_files, j2objc_flags = _ParseArgs(pass_through_args) |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 467 | srcjar_java_files = [] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 468 | j2objc_source_paths = [os.getcwd()] |
| 469 | |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 470 | # Unzip the source jars, so J2ObjC can translate the contained sources. |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 471 | # Also add the temporary directory containing the unzipped sources as a source |
| 472 | # path for J2ObjC, so it can find these sources. |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 473 | source_jars = [] |
| 474 | if args.gen_src_jar: |
| 475 | source_jars.append(args.gen_src_jar) |
| 476 | if args.src_jars: |
| 477 | source_jars.extend(args.src_jars.split(',')) |
| 478 | |
| 479 | srcjar_source_tuple = UnzipSourceJarSources(source_jars) |
| 480 | if srcjar_source_tuple: |
| 481 | j2objc_source_paths.append(srcjar_source_tuple[0]) |
| 482 | srcjar_java_files = srcjar_source_tuple[1] |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 483 | |
| 484 | # Run J2ObjC over the normal input Java files and unzipped gen jar Java files. |
| 485 | # The output is stored in a temporary directory. |
| 486 | tmp_objc_file_root = tempfile.mkdtemp() |
Michael Thvedt | 828a4be | 2015-08-12 17:45:36 +0000 | [diff] [blame] | 487 | RunJ2ObjC(args.java, |
| 488 | args.jvm_flags, |
| 489 | args.j2objc, |
| 490 | args.main_class, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 491 | tmp_objc_file_root, |
| 492 | j2objc_flags, |
| 493 | j2objc_source_paths, |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 494 | normal_java_files + srcjar_java_files) |
Rumou Duan | 123e1c3 | 2016-02-01 16:16:15 +0000 | [diff] [blame] | 495 | |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 496 | # Calculate the relative paths of generated objc files. |
| 497 | normal_objc_files = _J2ObjcOutputObjcFiles(normal_java_files) |
Rumou Duan | e7fd539 | 2017-01-09 20:33:33 +0000 | [diff] [blame] | 498 | genjar_objc_files = _J2ObjcOutputObjcFiles(srcjar_java_files) |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 499 | |
| 500 | # Generate J2ObjC mapping files needed for distributed builds. |
| 501 | GenerateJ2objcMappingFiles(normal_objc_files, |
| 502 | genjar_objc_files, |
| 503 | tmp_objc_file_root, |
| 504 | args.output_dependency_mapping_file, |
| 505 | args.output_archive_source_mapping_file, |
| 506 | args.compiled_archive_file_path) |
| 507 | |
Rumou Duan | d7cdc55 | 2016-11-03 19:42:48 +0000 | [diff] [blame] | 508 | # Post J2ObjC-run processing, involving file editing, zipping and moving |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 509 | # files to their final output locations. |
| 510 | PostJ2ObjcFileProcessing( |
| 511 | normal_objc_files, |
| 512 | genjar_objc_files, |
| 513 | tmp_objc_file_root, |
| 514 | args.objc_file_path, |
| 515 | j2objc_source_paths, |
| 516 | args.gen_src_jar, |
Rumou Duan | a1a13ae | 2016-07-26 17:23:26 +0000 | [diff] [blame] | 517 | args.output_gen_source_dir, |
| 518 | args.output_gen_header_dir) |
| 519 | |
| 520 | if __name__ == '__main__': |
| 521 | main() |