-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathresponse_file.py
More file actions
101 lines (79 loc) · 3.42 KB
/
response_file.py
File metadata and controls
101 lines (79 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2013 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
import logging
import os
import shlex
import tempfile
# Do not import shared.py so that file_packager.py can run without setting up LLVM_ROOT.
from .utils import WINDOWS
DEBUG = int(os.environ.get('EMCC_DEBUG', '0'))
def create_response_file_contents(args):
"""Create response file contents based on list of arguments.
"""
escape_chars = ['\\', '\"']
# When calling llvm-ar on Linux and macOS, single quote characters ' should be escaped.
if not WINDOWS:
escape_chars += ['\'']
def escape(arg):
for char in escape_chars:
arg = arg.replace(char, '\\' + char)
return arg
args = [escape(a) for a in args]
contents = ''
# Arguments containing spaces need to be quoted.
for arg in args:
if ' ' in arg:
arg = '"%s"' % arg
contents += arg + '\n'
return contents
def expand_response_file(arg):
"""Reads a response file, and returns the list of cmdline params found in the
file.
The encoding that the response filename should be read with can be specified
as a suffix to the file, e.g. "foo.rsp.utf-8" or "foo.rsp.cp1252". If not
specified, first UTF-8 and then Python locale.getpreferredencoding() are
attempted.
The parameter `arg` is the command line argument to be expanded."""
if arg.startswith('@'):
response_filename = arg[1:]
elif arg.startswith('-Wl,@'):
response_filename = arg[5:]
else:
response_filename = None
# Is the argument is not a response file, or if the file does not exist
# just return original argument.
if not response_filename or not os.path.exists(response_filename):
return [arg]
# Guess encoding based on the file suffix
components = os.path.basename(response_filename).split('.')
encoding_suffix = components[-1].lower()
if len(components) > 1 and (encoding_suffix.startswith(('utf', 'cp', 'iso')) or encoding_suffix in {'ascii', 'latin-1'}):
guessed_encoding = encoding_suffix
else:
# On windows, recent version of CMake emit rsp files containing
# a BOM. Using 'utf-8-sig' works on files both with and without
# a BOM.
guessed_encoding = 'utf-8-sig'
try:
# First try with the guessed encoding
with open(response_filename, encoding=guessed_encoding) as f:
args = f.read()
except (ValueError, LookupError): # UnicodeDecodeError is a subclass of ValueError, and Python raises either a ValueError or a UnicodeDecodeError on decode errors. LookupError is raised if guessed encoding is not an encoding.
if DEBUG:
logging.warning(f'failed to parse response file {response_filename} with guessed encoding "{guessed_encoding}". Trying default system encoding...')
# If that fails, try with the Python default locale.getpreferredencoding()
with open(response_filename) as f: # noqa: PLW1514
args = f.read()
args = shlex.split(args)
if DEBUG:
logging.warning(f'read response file {response_filename}: {args}')
# Response file can be recursive so call substitute_response_files on the arguments
return substitute_response_files(args)
def substitute_response_files(args):
"""Substitute any response files found in args with their contents."""
new_args = []
for arg in args:
new_args += expand_response_file(arg)
return new_args