|
20 | 20 | import argparse |
21 | 21 | import sys |
22 | 22 |
|
| 23 | + |
| 24 | +class _GnArgs(object): |
| 25 | + """Parsed GN arguments for write_gn_args().""" |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + self.module = None |
| 29 | + self.arg = [] |
| 30 | + self.arg_string = [] |
| 31 | + self.arg_cflags = [] |
| 32 | + |
| 33 | + |
23 | 34 | GN_SPECIAL_SEPARATOR = "+|+" |
24 | 35 | GN_CFLAG_EXCLUDES = [ |
25 | 36 | '-fno-asynchronous-unwind-tables', |
@@ -73,13 +84,53 @@ def write_gn_args(args): |
73 | 84 | key, "{}-".format(GN_SPECIAL_SEPARATOR).join(filtered_value), GN_SPECIAL_SEPARATOR, cflag_excludes)) |
74 | 85 |
|
75 | 86 |
|
| 87 | +def parse_args_tmp_file(path): |
| 88 | + """Parse args.tmp (one token per line). |
| 89 | +
|
| 90 | + argparse treats lines starting with '-' as options when using @file, which |
| 91 | + breaks when a --arg-cflags value is a single compiler flag. |
| 92 | + """ |
| 93 | + args = _GnArgs() |
| 94 | + with open(path, encoding='utf-8') as args_file: |
| 95 | + lines = [line.rstrip('\n\r') for line in args_file] |
| 96 | + |
| 97 | + i = 0 |
| 98 | + while i < len(lines): |
| 99 | + line = lines[i] |
| 100 | + if not line: |
| 101 | + i += 1 |
| 102 | + continue |
| 103 | + if line == '--module': |
| 104 | + i += 1 |
| 105 | + args.module = lines[i] |
| 106 | + i += 1 |
| 107 | + elif line == '--arg-cflags': |
| 108 | + i += 1 |
| 109 | + args.arg_cflags.append([lines[i], lines[i + 1]]) |
| 110 | + i += 2 |
| 111 | + elif line == '--arg-string': |
| 112 | + i += 1 |
| 113 | + args.arg_string.append([lines[i], lines[i + 1]]) |
| 114 | + i += 2 |
| 115 | + elif line == '--arg': |
| 116 | + i += 1 |
| 117 | + args.arg.append([lines[i], lines[i + 1]]) |
| 118 | + i += 2 |
| 119 | + else: |
| 120 | + raise ValueError('Unexpected line in {}: {!r}'.format(path, line)) |
| 121 | + return args |
| 122 | + |
| 123 | + |
76 | 124 | def main(): |
77 | | - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
78 | | - parser.add_argument('--module', action='store') |
79 | | - parser.add_argument('--arg', action='append', nargs=2, default=[]) |
80 | | - parser.add_argument('--arg-string', action='append', nargs=2, default=[]) |
81 | | - parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) |
82 | | - args = parser.parse_args() |
| 125 | + if len(sys.argv) == 2 and sys.argv[1].startswith('@'): |
| 126 | + args = parse_args_tmp_file(sys.argv[1][1:]) |
| 127 | + else: |
| 128 | + parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
| 129 | + parser.add_argument('--module', action='store') |
| 130 | + parser.add_argument('--arg', action='append', nargs=2, default=[]) |
| 131 | + parser.add_argument('--arg-string', action='append', nargs=2, default=[]) |
| 132 | + parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) |
| 133 | + args = parser.parse_args() |
83 | 134 | write_gn_args(args) |
84 | 135 |
|
85 | 136 |
|
|
0 commit comments