1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-24 17:01:50 +03:00

Support response files

This commit is contained in:
Rui Ueyama 2020-12-20 16:17:24 +09:00
parent fa46609297
commit ed93e6c6e4
3 changed files with 64 additions and 3 deletions

51
main.cc
View File

@ -805,14 +805,61 @@ static std::function<void()> fork_child() {
return [=]() { write(pipefd[1], (char []){1}, 1); };
}
static std::vector<std::string_view> read_response_file(std::string_view path) {
std::vector<std::string_view> vec;
MemoryMappedFile mb = must_open_input_file(std::string(path));
auto read_quoted = [&](int i, char quote) {
std::string *buf = new std::string;
while (i < mb.size && mb.data[i] != quote) {
if (mb.data[i] == '\\') {
buf->append(1, mb.data[i + 1]);
i += 2;
} else {
buf->append(1, mb.data[i++]);
}
}
if (i >= mb.size)
error(std::string(path) + ": premature end of input");
vec.push_back(std::string_view(*buf));
return i + 1;
};
auto read_unquoted = [&](int i) {
std::string *buf = new std::string;
while (i < mb.size && !isspace(mb.data[i]))
buf->append(1, mb.data[i++]);
vec.push_back(std::string_view(*buf));
return i;
};
for (int i = 0; i < mb.size;) {
if (isspace(mb.data[i]))
i++;
else if (mb.data[i] == '\'')
i = read_quoted(i + 1, '\'');
else if (mb.data[i] == '\"')
i = read_quoted(i + 1, '\"');
else
i = read_unquoted(i);
}
munmap(mb.data, mb.size);
return vec;
}
int main(int argc, char **argv) {
// Main
Timer t_all("all");
// Parse command line options
std::vector<std::string_view> arg_vector;
for (int i = 1; i < argc; i++)
arg_vector.push_back(argv[i]);
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '@')
append(arg_vector, read_response_file(argv[i] + 1));
else
arg_vector.push_back(argv[i]);
}
config.thread_count =
tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism);

2
mold.h
View File

@ -991,7 +991,7 @@ inline void write_vector(u8 *buf, const std::vector<T> &vec) {
}
template <typename T, typename U>
inline void append(std::vector<T> &vec1, std::vector<U> &vec2) {
inline void append(std::vector<T> &vec1, std::vector<U> vec2) {
vec1.insert(vec1.end(), vec2.begin(), vec2.end());
}

14
test/response-file.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
set -e
echo -n "Testing $(basename -s .sh $0) ..."
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
echo '.globl _start; _start: jmp loop' | cc -o $t/a.o -c -x assembler -
echo '.globl loop; loop: jmp loop' | cc -o $t/b.o -c -x assembler -
echo "-o $t/exe '$t/a.o' $t/b.o" > $t/rsp
../mold -static @$t/rsp
objdump -d $t/exe > /dev/null
file $t/exe | grep -q ELF
echo ' OK'