Lines Matching refs:self
136 def __init__(self, runner, outfile_name: str): argument
138 self.runner = runner
139 self.debug = runner.args.debug
140 self.cargo_dir = '' # directory of my Cargo.toml
141 self.outfile = None # open file handle of outfile_name during dump*
142 self.outfile_name = outfile_name # path to BUILD.gn
144 self.module_type = '' # lib,crate_name,test etc.
145 self.root_pkg_name = '' # parent package name of a sub/test packge
147 self.error_infos = '' # all errors found during parsing
148 self.line = '' # original rustc command line parameters
149 self.line_num = 1 # runner told input source line number
151 self.cap_lints = ''
152 self.crate_name = ''
153 …self.edition = '2015' # cargo default is 2015, you can specify the edition as 2018 or 2…
154 self.emit_list = '' # --emit=dep-info,metadata,link
155 self.main_src = ''
156 self.target = ''
157 self.cfgs = list()
158 self.core_deps = list() # first part of self.deps elements
159 self.crate_types = list()
160 self.deps = list()
161 self.features = list()
162 self.ignore_options = list()
163 self.srcs = list() # main_src or merged multiple source files
164 self.shared_libs = list() # -l dylib=wayland-client, -l z
165 self.static_libs = list() # -l static=host_cpuid
167 self.cargo_pkg_version = '' # value extracted from Cargo.toml version field
168 self.cargo_pkg_authors = '' # value extracted from Cargo.toml authors field
169 self.cargo_pkg_name = '' # value extracted from Cargo.toml name field
170 self.cargo_pkg_description = '' # value extracted from Cargo.toml description field
172 self.build_root = ''
173 self.checked_out_files = False # to check only once
174 self.build_script_outputs = [] # output files generated by build.rs
176 def write(self, s: str): argument
178 self.outfile.write(s + '\n')
180 def parse_rustc(self, line_num: int, line: str): argument
182 self.line_num = line_num
183 self.line = line
185 self.parse_args(args)
186 if not self.crate_name:
187 self.error_infos += 'ERROR: missing --crate-name\n'
188 if not self.crate_types:
189 if 'test' in self.cfgs:
190 self.crate_types.append('test')
192 self.error_infos += 'ERROR: missing --crate-type or --test\n'
193 elif len(self.crate_types) > 1:
194 if 'lib' in self.crate_types and 'rlib' in self.crate_types:
195 self.error_infos += 'ERROR: cannot generate both lib and rlib crate types\n'
196 if 'test' in self.crate_types:
197 self.error_infos += 'ERROR: cannot handle both --crate-type and --test\n'
198 if not self.main_src:
199 self.error_infos += 'ERROR: missing main source file\n'
201 self.srcs.append(self.main_src)
202 if self.cargo_dir:
203 self.get_root_pkg_name()
204 if not self.root_pkg_name:
205 self.root_pkg_name = self.crate_name
208 if not self.skip_crate():
209 if not self.runner.args.no_pkg_info:
210 self.find_pkg_info()
211 self.find_build_root()
212 if self.runner.args.copy_out:
213 self.copy_out_files()
214 elif self.find_out_files() and self.has_used_out_dir():
215 self.copy_out_files()
217 self.cfgs = sorted(set(self.cfgs))
218 self.core_deps = sorted(set(self.core_deps))
219 self.crate_types = sorted(set(self.crate_types))
220 self.deps = sorted(set(self.deps))
221 self.features = sorted(set(self.features))
222 self.ignore_options = sorted(set(self.ignore_options))
223 self.static_libs = sorted(set(self.static_libs))
224 self.shared_libs = sorted(set(self.shared_libs))
225 self.decide_module_type()
226 return self
228 def parse_args(self, args): argument
234 self.crate_name = args[num]
237 self.crate_types.append(args[num])
240 self.deal_cfg(args[num])
243 self.add_ignore_options_flag(args[num]) # codegen options
245 self.add_ignore_options_flag(arg[2:])
248 self.cap_lints = args[num]
250 self.edition = arg.replace('--edition=', '')
252 self.emit_list = arg.replace('--emit=', '')
255 self.deal_extern(args[num])
261 self.set_root_pkg_name(args[num])
264 self.deal_static_and_dylib(args[num])
269 self.target = args[num]
271 self.crate_types.append('test')
273 self.set_main_src(args[num])
275 self.error_infos += 'ERROR: unknown ' + arg + '\n'
278 def deal_cfg(self, arg: str): argument
284 self.features.append(feature)
286 self.cfgs.append(arg)
288 def add_ignore_options_flag(self, flag: str): argument
297 self.ignore_options.append(flag)
299 def deal_extern(self, arg: str): argument
301 self.deps.append(deps)
302 self.core_deps.append(re.sub(' = .*', '', deps))
304 def set_root_pkg_name(self, arg: str): argument
307 self.root_pkg_name = re.sub('^.*/', '',
310 self.root_pkg_name = re.sub('^.*/', '',
312 self.root_pkg_name = remove_version_suffix(self.root_pkg_name)
314 def deal_static_and_dylib(self, arg: str): argument
316 self.static_libs.append(re.sub('static=', '', arg))
318 self.shared_libs.append(re.sub('dylib=', '', arg))
320 self.shared_libs.append(arg)
322 def set_main_src(self, arg: str): argument
323 self.main_src = re.sub(r'^/[^ ]*/registry/src/', '.../', arg)
324 self.main_src = re.sub(r'^\.\.\./github.com-[0-9a-f]*/', '.../', self.main_src)
325 self.find_cargo_dir()
326 if self.cargo_dir:
327 if self.runner.args.no_subdir:
329 self.outfile_name = '/dev/null'
330 elif not self.runner.args.one_file:
332 self.outfile_name = os.path.join(self.cargo_dir, 'BUILD.gn')
333 self.main_src = self.main_src[len(self.cargo_dir) + 1:]
335 def find_cargo_dir(self): argument
337 if not is_dependent_path(self.main_src):
338 dir_name = os.path.dirname(self.main_src)
344 self.cargo_dir = dir_name
348 def skip_crate(self): argument
353 if (is_build_script(self.crate_name) or
354 self.crate_name in excluded_crates):
355 return self.crate_name
356 if is_dependent_path(self.main_src):
360 def get_root_pkg_name(self): argument
363 if self.cargo_dir:
365 os.path.join('.', self.cargo_dir), 'Cargo.toml')
369 self.root_pkg_name = get_designated_pkg_info(infile, 'name')
372 def find_pkg_info(self): argument
375 if self.cargo_dir:
377 os.path.join('.', self.cargo_dir), 'Cargo.toml')
381 if self.root_pkg_name:
382 self.cargo_pkg_name = self.root_pkg_name
384 self.cargo_pkg_name = get_designated_pkg_info(infile, 'name')
386 self.cargo_pkg_version = get_designated_pkg_info(infile, 'version')
390 self.cargo_pkg_description = pkg_description
399 self.cargo_pkg_authors = authors
401 def find_build_root(self): argument
404 if self.cargo_dir:
406 os.path.join('.', self.cargo_dir), 'Cargo.toml')
410 self.build_root = get_designated_pkg_info(infile, 'build')
411 if not self.build_root:
413 if self.cargo_dir:
414 build_rs_path = os.path.join(os.path.join('.', self.cargo_dir), 'build.rs')
416 self.build_root = 'build.rs'
418 def find_out_files(self): argument
421 TARGET_TEMP + '/*/*/build/' + self.root_pkg_name + '-*/out/*')
424 TARGET_TEMP + '/*/build/' + self.root_pkg_name + '-*/out/*')
427 def has_used_out_dir(self): argument
430 if self.cargo_dir:
431 … cmd = ['grep', '-rl', '--exclude', os.path.join(self.cargo_dir, 'build.rs'), '--include', \
435 def copy_out_files(self): argument
437 if self.checked_out_files:
439 self.checked_out_files = True
440 cargo_out_files = self.find_out_files()
443 if self.cargo_dir:
444 out_path = os.path.join(self.cargo_dir, out_path)
450 self.build_script_outputs = sorted(out_files)
452 def decide_module_type(self): argument
454 crate_type = self.crate_types[0] if self.crate_types else ''
455 self.decide_one_module_type(crate_type)
457 def decide_one_module_type(self, crate_type: str): argument
460 self.module_type = self.crate_name
462 self.module_type = 'lib'
464 self.module_type = 'test'
466 self.module_type = ''
468 def merge_crate(self, other, outfile_name: str): argument
473 should_merge = self.is_should_merge(other)
476 should_merge_test = self.merge_test(other)
478 self.runner.init_gn_file(outfile_name)
481 self.outfile = outfile
483 self.execute_merge(other, should_merge_test)
487 def is_should_merge(self, other): argument
488 return (self.crate_name == other.crate_name and
489 self.crate_types == other.crate_types and
490 self.main_src == other.main_src and
491 self.root_pkg_name == other.root_pkg_name and
492 not self.skip_crate() and self.is_same_flags(other))
494 def merge_test(self, other): argument
499 return (self.crate_types == other.crate_types and self.crate_types == ['test'] and
500 self.root_pkg_name == other.root_pkg_name and not self.skip_crate() and
502 (len(self.srcs) > 1 or (self.crate_name == get_base_name(self.main_src))) and
503 self.is_same_flags(other))
505 def is_same_flags(self, other): argument
506 return (not self.error_infos and not other.error_infos and
507 self.cap_lints == other.cap_lints and self.cfgs == other.cfgs and
508 self.core_deps == other.core_deps and self.edition == other.edition and
509 self.emit_list == other.emit_list and self.features == other.features and
510 self.ignore_options == other.ignore_options and
511 self.static_libs == other.static_libs and
512 self.shared_libs == other.shared_libs)
514 def execute_merge(self, other, should_merge_test): argument
516 if self.debug:
517 self.write('\n// Before merge definition(self):')
518 self.dump_debug_info()
519 self.write('\n// Before merge definition(other):')
521 if not self.target:
523 self.target = other.target
524 self.decide_module_type()
526 if (self.runner.should_ignore_test(self.main_src) and
527 not self.runner.should_ignore_test(other.main_src)):
528 self.main_src = other.main_src
529 self.srcs.append(other.main_src)
530 self.crate_name = pkg_to_crate_name(self.root_pkg_name)
531 if self.debug:
532 self.write('\n// After merge definition:')
533 self.dump_debug_info()
535 def dump(self): argument
537 self.runner.init_gn_file(self.outfile_name)
538 with open(self.outfile_name, 'a') as outfile:
539 self.outfile = outfile
540 if self.error_infos:
541 self.dump_line()
542 self.write(self.error_infos)
543 elif self.skip_crate():
544 self.dump_skip_crate(self.skip_crate())
546 if self.debug:
547 self.dump_debug_info()
548 self.dump_gn_module()
550 def dump_debug_info(self): argument
554 self.write('//%12s = %s' % (name, value))
558 self.write(fmt % v)
564 self.dump_line()
565 dump('crate_name', self.crate_name)
566 dump('crate_types', self.crate_types)
567 opt_dump('edition', self.edition)
568 opt_dump('emit_list', self.emit_list)
569 dump('main_src', self.main_src)
570 dump('module_type', self.module_type)
571 opt_dump('target', self.target)
572 opt_dump('cap_lints', self.cap_lints)
573 dump_list('// cfg = %s', self.cfgs)
574 dump_list('// cfg = \'feature "%s"\'', self.features)
575 dump_list('// codegen = %s', self.ignore_options)
576 dump_list('// deps = %s', self.deps)
577 dump_list('// -l (dylib) = %s', self.shared_libs)
578 dump_list('// -l static = %s', self.static_libs)
580 def dump_line(self): argument
581 self.write('\n// Line ' + str(self.line_num) + ' ' + self.line)
583 def dump_skip_crate(self, kind: str): argument
584 if self.debug:
585 self.write('\n// IGNORED: ' + kind + ' ' + self.main_src)
586 return self
588 def dump_gn_module(self): argument
590 if len(self.crate_types) == 1:
591 self.dump_single_type_gn_module()
593 if 'test' in self.crate_types:
594 self.write('\nERROR: multiple crate types cannot include test type')
597 for crate_type in self.crate_types:
598 self.decide_one_module_type(crate_type)
599 self.dump_one_gn_module(crate_type)
601 def dump_single_type_gn_module(self): argument
603 crate_type = self.crate_types[0]
605 self.dump_one_gn_module(crate_type)
608 self.srcs = [
609 src for src in self.srcs if not self.runner.should_ignore_test(src)]
610 if len(self.srcs) > 1:
611 self.srcs = sorted(set(self.srcs))
612 saved_srcs = self.srcs
614 self.srcs = [src]
615 saved_main_src = self.main_src
616 self.main_src = src
617 self.decide_one_module_type(crate_type)
618 self.dump_one_gn_module(crate_type)
619 self.main_src = saved_main_src
620 self.srcs = saved_srcs
622 def dump_one_gn_module(self, crate_type: str): argument
624 if not self.module_type:
625 self.write('\nERROR: unknown crate_type ' + crate_type)
627 self.write('\nohos_cargo_crate("' + self.module_type + '") {')
628 self.dump_gn_first_properties(crate_type)
629 self.dump_gn_core_properties()
630 self.write('}')
632 def dump_gn_first_properties(self, crate_type: str): argument
634 self.write(' crate_name = "' + self.crate_name + '"')
638 self.write(' crate_type = "' + crate_type + '"')
639 if self.main_src:
640 self.write(' crate_root = "' + self.main_src + '"')
641 if self.crate_name.startswith('lib'):
642 self.write(' output_name = "lib' + self.crate_name + '"')
643 self.write('')
645 def dump_gn_core_properties(self): argument
646 self.dump_sources_list()
647 if self.edition:
648 self.write(' edition = "' + self.edition + '"')
649 if not self.runner.args.no_pkg_info:
650 if self.cargo_pkg_version:
651 self.write(' cargo_pkg_version = "' +
652 self.cargo_pkg_version + '"')
653 if self.cargo_pkg_authors:
654 self.write(' cargo_pkg_authors = "' +
655 self.cargo_pkg_authors + '"')
656 if self.cargo_pkg_name:
657 self.write(' cargo_pkg_name = "' +
658 self.cargo_pkg_name + '"')
659 if self.cargo_pkg_description:
660 self.write(' cargo_pkg_description = "' +
661 self.cargo_pkg_description + '"')
662 if self.deps:
663 self.dump_gn_deps()
664 if self.build_root and self.root_pkg_name in self.runner.build_deps:
665 self.dump_gn_build_deps()
666 self.dump_gn_property_list('features', '"%s"', self.features)
667 if self.build_root:
668 self.write(' build_root = "' + self.build_root + '"')
670 build_sources.append(self.build_root)
671 self.dump_gn_property_list('build_sources', '"%s"', build_sources)
672 if self.build_script_outputs:
673 self.dump_gn_property_list(
674 'build_script_outputs', '"%s"', self.build_script_outputs)
676 def dump_sources_list(self): argument
678 if len(self.srcs) > 1:
679 srcs = sorted(set(self.srcs)) # make a copy and dedup
680 for num in range(len(self.srcs)):
683 srcs = [self.main_src]
684 self.dump_gn_property_list('sources', '"%s"', srcs)
686 def dump_gn_deps(self): argument
690 for lib in self.deps:
696 if lib_name in self.runner.args.dependency_blocklist:
700 rust_lib = self.get_rust_lib(lib_name)
708 self.dump_gn_property_list('deps', '"%s"', rust_deps)
710 def dump_gn_build_deps(self): argument
714 build_deps = self.runner.build_deps.get(self.root_pkg_name)
723 if lib_name in self.runner.args.dependency_blocklist:
727 rust_lib = self.get_rust_lib(lib_name)
734 self.dump_gn_property_list('build_deps', '"%s"', rust_build_deps)
736 def dump_gn_property_list(self, name: str, fmt, values): argument
740 self.write(' ' + name + ' = [')
741 self.dump_gn_property_list_items(fmt, values)
742 self.write(' ]')
744 self.write(' ' + name + ' = [' +
747 def dump_gn_property_list_items(self, fmt, values): argument
750 self.write(' ' + (fmt % escape_quotes(v)) + ',')
752 def get_rust_lib(self, lib_name: str): argument
756 deps_libname = self.runner.deps_libname_map.get(crate_name)
765 def __init__(self, args): argument
766 self.gn_files = set() # Remember all output BUILD.gn files.
767 self.root_pkg_name = '' # name of package in ./Cargo.toml
768 self.args = args
769 self.dry_run = not args.run
770 self.skip_cargo = args.skipcargo
771 self.cargo_path = ['./cargo'] # path to cargo
772 self.crates = list() # all crates
773 self.error_infos = '' # all error infos
774 self.test_error_infos = '' # all test error infos
775 self.warning_files = set() # all warning files
776 self.set_cargo_path()
779 self.cargo = ['clean'] + args.cargo
782 self.cargo = ['clean', 'build --target x86_64-unknown-linux-gnu']
783 self.empty_tests = set()
784 self.empty_unittests = False
785 self.build_deps = {}
786 self.deps_libname_map = {}
788 def set_cargo_path(self): argument
790 if self.args.cargo_bin:
791 self.cargo_path = [os.path.join(self.args.cargo_bin, 'cargo')]
792 if os.path.isfile(self.cargo_path):
793 print('INFO: using cargo in ' + self.args.cargo_bin)
796 sys.exit('ERROR: cannot find cargo in ' + self.args.cargo_bin)
801 def run_cargo(self): argument
803 if self.skip_cargo:
804 return self
809 return self
813 if not self.dry_run:
816 if not self.args.use_cargo_lock and have_cargo_lock:
820 os.environ['PATH'] = os.path.dirname(self.cargo_path) + ':' + save_path
823 if self.args.add_workspace:
832 self.deal_cargo_cmd(cargo_out)
837 if not self.dry_run:
841 elif not self.args.use_cargo_lock: # restore saved Cargo.lock
844 return self
846 def deal_cargo_cmd(self, cargo_out: str): argument
847 cargo_cmd_v_flag = ['-vv'] if self.args.vv else ['-v']
850 for cargo in self.cargo:
851 cargo_cmd = [self.cargo_path] + cargo_cmd_v_flag
854 if self.args.features is not None:
856 if self.args.features:
857 features += [' --features ', self.args.features]
859 if self.args.rustflags and cargo != 'clean':
860 cargo_cmd = ['RUSTFLAGS="' + self.args.rustflags] + cargo_cmd
861 self.run_cargo_cmd(cargo_cmd, cargo_out)
863 def run_cargo_cmd(self, cargo_cmd: str, cargo_out: str): argument
864 if self.dry_run:
874 def generate_gn(self): argument
878 if self.dry_run:
881 self.find_root_pkg()
883 self.parse(cargo_out, 'BUILD.gn')
884 self.crates.sort(key=get_crate_name)
885 for crate in self.crates:
887 if self.error_infos:
888 self.append_to_gn('\n' + errors_line + '\n' + self.error_infos)
889 if self.test_error_infos:
890 self.append_to_gn('\n// Errors when listing tests:\n' +
891 self.test_error_infos)
892 return self
894 def find_root_pkg(self): argument
901 def parse(self, infile: str, outfile_name: str): argument
917 self.add_empty_test(current_test_name)
921 self.add_deps_libname_map(compiling_pat.match(line).group(1))
923 self.parse_cargo_out(infile, outfile_name)
925 def add_empty_test(self, name: str): argument
927 self.empty_unittests = True
929 self.empty_tests.add(name)
931 def add_deps_libname_map(self, line: str): argument
934 self.deps_libname_map[pkg_to_crate_name(line_list[0])] = line_list[0]
936 def parse_cargo_out(self, infile: str, outfile_name: str): argument
953 rustc_line = self.assert_empty_rustc_line(rustc_line)
958 self.add_crate(Crate(self, outfile_name).parse_rustc(line_num, args_line))
959 self.assert_empty_rustc_line(rustc_line)
961 new_rustc_line = self.deal_rustc_command(
966 self.warning_files.add(file_path)
967 self.assert_empty_rustc_line(rustc_line)
969 if not self.args.ignore_cargo_errors:
970 self.add_error_infos(in_tests, line)
976 def assert_empty_rustc_line(self, line: str): argument
979 self.append_to_gn('ERROR -vv line: ' + line)
982 def append_to_gn(self, line: str): argument
983 self.init_gn_file('BUILD.gn')
988 def init_gn_file(self, name: str): argument
990 if name in self.gn_files:
992 self.gn_files.add(name)
1000 def add_error_infos(self, in_tests: str, line: str): argument
1002 self.test_error_infos += '// '.join(line)
1004 self.error_infos += line
1006 def deal_rustc_command(self, line_num: str, rustc_line: str, line: str, outfile_name: str): argument
1017 self.add_crate(Crate(self, outfile_name).parse_rustc(line_num, args))
1019 self.assert_empty_rustc_line(new_rustc_line)
1022 def add_crate(self, new_crate): argument
1027 self.build_deps[new_crate.root_pkg_name] = new_crate.deps
1030 if self.args.debug:
1031 self.crates.append(new_crate)
1033 for crate in self.crates:
1038 self.crates.append(new_crate)
1040 def should_ignore_test(self, src): argument
1044 return (src in self.empty_tests or src in self.args.test_blocklist or
1045 (self.empty_unittests and
1046 src in [c.main_src for c in self.crates if c.crate_types != ['test']]))