diff --git a/compiler_opt/tools/extract_ir_lib.py b/compiler_opt/tools/extract_ir_lib.py index 2f9d8620..c1d2a54b 100644 --- a/compiler_opt/tools/extract_ir_lib.py +++ b/compiler_opt/tools/extract_ir_lib.py @@ -207,14 +207,19 @@ def extract(self, def convert_compile_command_to_objectfile( command: Dict[str, str], output_dir: str) -> Optional[TrainingIRExtractor]: obj_base_dir = command['directory'] - cmd = command['command'] + if 'arguments' in command: + cmd_parts = command['arguments'] + elif 'command' in command: + cmd_parts = command['command'].split() + else: + logging.info('compile_commands element has no command and arguments') + return None - cmd_parts = cmd.split() try: obj_index = cmd_parts.index('-o') + 1 except ValueError: # This could happen if there are non-clang commands in compile_commands.json - logging.info('Command has no -o option: %s', cmd) + logging.info('Command has no -o option: %s', ' '.join(cmd_parts)) return None obj_rel_path = cmd_parts[obj_index] # TODO(mtrofin): is the obj_base_dir correct for thinlto index bc files? diff --git a/compiler_opt/tools/extract_ir_test.py b/compiler_opt/tools/extract_ir_test.py index b7004948..8811134a 100644 --- a/compiler_opt/tools/extract_ir_test.py +++ b/compiler_opt/tools/extract_ir_test.py @@ -42,6 +42,25 @@ def test_one_conversion(self): '/corpus/destination/path/lib/bar.o.thinlto.bc') # pytype: enable=attribute-error + def test_one_conversion_arguments_style(self): + obj = extract_ir_lib.convert_compile_command_to_objectfile( + { + 'directory': '/output/directory', + 'arguments': + ['-cc1', '-c', '/some/path/lib/foo/bar.cc', '-o', 'lib/bar.o'], + 'file': '/some/path/lib/foo/bar.cc' + }, '/corpus/destination/path') + self.assertIsNotNone(obj) + # pytype: disable=attribute-error + # Pytype complains about obj being None + self.assertEqual(obj.input_obj(), '/output/directory/lib/bar.o') + self.assertEqual(obj.relative_output_path(), 'lib/bar.o') + self.assertEqual(obj.cmd_file(), '/corpus/destination/path/lib/bar.o.cmd') + self.assertEqual(obj.bc_file(), '/corpus/destination/path/lib/bar.o.bc') + self.assertEqual(obj.thinlto_index_file(), + '/corpus/destination/path/lib/bar.o.thinlto.bc') + # pytype: enable=attribute-error + def test_arr_conversion(self): res = extract_ir_lib.load_from_compile_commands([{ 'directory': '/output/directory',