13
13
DEBUG = int (os .environ .get ('EMCC_DEBUG' , '0' ))
14
14
15
15
16
- def create_response_file (args , directory ):
17
- """Routes the given cmdline param list in args into a new response file and
18
- returns the filename to it.
16
+ def create_response_file_contents (args ):
17
+ """Create response file contents based on list of arguments.
19
18
"""
20
-
21
- response_fd , response_filename = tempfile .mkstemp (prefix = 'emscripten_' , suffix = '.rsp.utf-8' , dir = directory , text = True )
22
-
23
- # Backslashes and other special chars need to be escaped in the response file.
24
19
escape_chars = ['\\ ' , '\" ' ]
25
20
# When calling llvm-ar on Linux and macOS, single quote characters ' should be escaped.
26
21
if not WINDOWS :
@@ -40,6 +35,18 @@ def escape(arg):
40
35
arg = '"%s"' % arg
41
36
contents += arg + '\n '
42
37
38
+ return contents
39
+
40
+
41
+ def create_response_file (args , directory ):
42
+ """Routes the given cmdline param list in args into a new response file and
43
+ returns the filename to it.
44
+ """
45
+ # Backslashes and other special chars need to be escaped in the response file.
46
+ contents = create_response_file_contents (args )
47
+
48
+ response_fd , response_filename = tempfile .mkstemp (prefix = 'emscripten_' , suffix = '.rsp.utf-8' , dir = directory , text = True )
49
+
43
50
with os .fdopen (response_fd , 'w' , encoding = 'utf-8' ) as f :
44
51
f .write (contents )
45
52
@@ -54,7 +61,7 @@ def escape(arg):
54
61
return response_filename
55
62
56
63
57
- def read_response_file ( response_filename ):
64
+ def expand_response_file ( arg ):
58
65
"""Reads a response file, and returns the list of cmdline params found in the
59
66
file.
60
67
@@ -63,12 +70,19 @@ def read_response_file(response_filename):
63
70
specified, first UTF-8 and then Python locale.getpreferredencoding() are
64
71
attempted.
65
72
66
- The parameter response_filename may start with '@'."""
67
- if response_filename .startswith ('@' ):
68
- response_filename = response_filename [1 :]
73
+ The parameter `arg` is the command line argument to be expanded."""
74
+
75
+ if arg .startswith ('@' ):
76
+ response_filename = arg [1 :]
77
+ elif arg .startswith ('-Wl,@' ):
78
+ response_filename = arg [5 :]
79
+ else :
80
+ response_filename = None
69
81
70
- if not os .path .exists (response_filename ):
71
- raise OSError ("response file not found: %s" % response_filename )
82
+ # Is the argument is not a response file, or if the file does not exist
83
+ # just return orginal argument.
84
+ if not response_filename or not os .path .exists (response_filename ):
85
+ return [arg ]
72
86
73
87
# Guess encoding based on the file suffix
74
88
components = os .path .basename (response_filename ).split ('.' )
@@ -97,20 +111,13 @@ def read_response_file(response_filename):
97
111
if DEBUG :
98
112
logging .warning (f'read response file { response_filename } : { args } ' )
99
113
100
- return args
114
+ # Response file can be recursive so call substitute_response_files on the arguments
115
+ return substitute_response_files (args )
101
116
102
117
103
118
def substitute_response_files (args ):
104
119
"""Substitute any response files found in args with their contents."""
105
120
new_args = []
106
121
for arg in args :
107
- if arg .startswith ('@' ):
108
- new_args += read_response_file (arg )
109
- elif arg .startswith ('-Wl,@' ):
110
- for a in read_response_file (arg [5 :]):
111
- if a .startswith ('-' ):
112
- a = '-Wl,' + a
113
- new_args .append (a )
114
- else :
115
- new_args .append (arg )
122
+ new_args += expand_response_file (arg )
116
123
return new_args
0 commit comments