1#!/usr/bin/env python3 2# Copyright 2023 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5'''Is used to find all rust files in a crate, and write the result to a 6depfile. Then, used again to read the same depfile and pull out just the 7source files. Lastly, it is also used to write a stamp file at the same 8location as the depfile.''' 9 10import argparse 11import re 12import subprocess 13import sys 14 15FILE_REGEX = re.compile('^(.*):') 16 17 18def main(): 19 parser = argparse.ArgumentParser( 20 description='Collect Rust sources for a crate') 21 parser.add_argument('--stamp', 22 action='store_true', 23 help='Generate a stamp file') 24 parser.add_argument('--generate-depfile', 25 action='store_true', 26 help='Generate a depfile') 27 parser.add_argument('--read-depfile', 28 action='store_true', 29 help='Read the previously generated depfile') 30 args, rest = parser.parse_known_args() 31 32 if (args.stamp): 33 stampfile = rest[0] 34 with open(stampfile, "w") as f: 35 f.write("stamp") 36 elif (args.generate_depfile): 37 rustc = rest[0] 38 crate_root = rest[1] 39 depfile = rest[2] 40 rustflags = rest[3:] 41 42 rustc_args = [ 43 "--emit=dep-info=" + depfile, "-Zdep-info-omit-d-target", crate_root 44 ] 45 subprocess.check_call([rustc] + rustc_args + rustflags) 46 elif (args.read_depfile): 47 depfile = rest[0] 48 try: 49 with open(depfile, "r") as f: 50 files = [FILE_REGEX.match(l) for l in f.readlines()] 51 for f in files: 52 if f: 53 print(f.group(1)) 54 except: 55 pass 56 else: 57 print("ERROR: Unknown action") 58 parser.print_help() 59 return 1 60 return 0 61 62 63if __name__ == '__main__': 64 sys.exit(main()) 65