2017-11-29 03:58:45 +08:00
|
|
|
//===- Args.cpp -----------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-29 03:58:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Common/Args.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2018-08-23 07:52:13 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-11-29 03:58:45 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
|
2019-01-31 04:46:18 +08:00
|
|
|
// TODO(sbc): Remove this once CGOptLevel can be set completely based on bitcode
|
|
|
|
// function metadata.
|
|
|
|
CodeGenOpt::Level lld::args::getCGOptLevel(int optLevelLTO) {
|
|
|
|
if (optLevelLTO == 3)
|
|
|
|
return CodeGenOpt::Aggressive;
|
|
|
|
assert(optLevelLTO < 3);
|
|
|
|
return CodeGenOpt::Default;
|
|
|
|
}
|
|
|
|
|
2020-07-31 05:28:45 +08:00
|
|
|
static int64_t getInteger(opt::InputArgList &args, unsigned key,
|
|
|
|
int64_t Default, unsigned base) {
|
2018-05-22 10:53:11 +08:00
|
|
|
auto *a = args.getLastArg(key);
|
|
|
|
if (!a)
|
|
|
|
return Default;
|
|
|
|
|
2019-06-07 14:05:26 +08:00
|
|
|
int64_t v;
|
2020-09-22 10:02:12 +08:00
|
|
|
StringRef s = a->getValue();
|
|
|
|
if (base == 16 && (s.startswith("0x") || s.startswith("0X")))
|
|
|
|
s = s.drop_front(2);
|
|
|
|
if (to_integer(s, v, base))
|
2018-05-22 10:53:11 +08:00
|
|
|
return v;
|
|
|
|
|
|
|
|
StringRef spelling = args.getArgString(a->getIndex());
|
|
|
|
error(spelling + ": number expected, but got '" + a->getValue() + "'");
|
|
|
|
return 0;
|
2017-11-29 03:58:45 +08:00
|
|
|
}
|
|
|
|
|
2020-07-31 05:28:45 +08:00
|
|
|
int64_t lld::args::getInteger(opt::InputArgList &args, unsigned key,
|
|
|
|
int64_t Default) {
|
|
|
|
return ::getInteger(args, key, Default, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t lld::args::getHex(opt::InputArgList &args, unsigned key,
|
|
|
|
int64_t Default) {
|
|
|
|
return ::getInteger(args, key, Default, 16);
|
|
|
|
}
|
|
|
|
|
2017-11-29 03:58:45 +08:00
|
|
|
std::vector<StringRef> lld::args::getStrings(opt::InputArgList &args, int id) {
|
|
|
|
std::vector<StringRef> v;
|
|
|
|
for (auto *arg : args.filtered(id))
|
|
|
|
v.push_back(arg->getValue());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-11-29 04:01:30 +08:00
|
|
|
uint64_t lld::args::getZOptionValue(opt::InputArgList &args, int id,
|
|
|
|
StringRef key, uint64_t Default) {
|
2018-09-27 04:50:39 +08:00
|
|
|
for (auto *arg : args.filtered_reverse(id)) {
|
2017-11-29 03:58:45 +08:00
|
|
|
std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
|
|
|
|
if (kv.first == key) {
|
|
|
|
uint64_t result = Default;
|
|
|
|
if (!to_integer(kv.second, result))
|
|
|
|
error("invalid " + key + ": " + kv.second);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) {
|
|
|
|
SmallVector<StringRef, 0> arr;
|
|
|
|
mb.getBuffer().split(arr, '\n');
|
|
|
|
|
|
|
|
std::vector<StringRef> ret;
|
|
|
|
for (StringRef s : arr) {
|
|
|
|
s = s.trim();
|
|
|
|
if (!s.empty() && s[0] != '#')
|
|
|
|
ret.push_back(s);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-23 07:52:13 +08:00
|
|
|
|
2018-08-27 14:18:10 +08:00
|
|
|
StringRef lld::args::getFilenameWithoutExe(StringRef path) {
|
2021-06-24 16:06:35 +08:00
|
|
|
if (path.endswith_insensitive(".exe"))
|
2018-08-23 07:52:13 +08:00
|
|
|
return sys::path::stem(path);
|
|
|
|
return sys::path::filename(path);
|
|
|
|
}
|