1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <algorithm>
17
18 #include "libpandabase/utils/pandargs.h"
19 #include "libpandabase/utils/logger.h"
20 #include "generated/link_options.h"
21
22 #include "linker.h"
23
24 namespace {
25
PrintHelp(const ark::PandArgParser & paParser)26 int PrintHelp(const ark::PandArgParser &paParser)
27 {
28 const auto a = paParser.GetErrorString();
29 if (!a.empty()) {
30 std::cerr << "Error: " << a << std::endl;
31 }
32
33 std::cerr << "Usage:" << std::endl;
34 std::cerr << "ark_link [OPTIONS] -- FILES..." << std::endl << std::endl;
35 std::cerr << "Supported options:" << std::endl << std::endl;
36 std::cerr << paParser.GetHelpString() << std::endl;
37 return 1;
38 }
39
MangleClass(std::string s)40 std::string MangleClass(std::string s)
41 {
42 s.insert(s.begin(), 'L');
43 s += ";";
44 return s;
45 }
46
47 } // namespace
48
main(int argc,const char * argv[])49 int main(int argc, const char *argv[])
50 {
51 ark::PandArgParser paParser;
52 ark::static_linker::Options options {*argv};
53 options.AddOptions(&paParser);
54 paParser.EnableRemainder();
55
56 if (!paParser.Parse(argc, argv)) {
57 return PrintHelp(paParser);
58 }
59
60 ark::Logger::InitializeStdLogging(
61 ark::Logger::LevelFromString(options.GetLogLevel()),
62 ark::Logger::ComponentMask().set(ark::Logger::Component::STATIC_LINKER).set(ark::Logger::Component::PANDAFILE));
63
64 const auto files = paParser.GetRemainder();
65 if (files.empty()) {
66 std::cerr << "must have at least one file" << std::endl;
67 return PrintHelp(paParser);
68 }
69
70 if (options.GetOutput().empty()) {
71 auto const &fn = files[0];
72 options.SetOutput(fn.substr(0, fn.find_last_of('.')) + ".linked.abc");
73 }
74
75 auto conf = ark::static_linker::DefaultConfig();
76
77 conf.stripDebugInfo = options.IsStripDebugInfo();
78
79 auto classesVecToSet = [](const std::vector<std::string> &v, std::set<std::string> &s) {
80 s.clear();
81 std::transform(v.begin(), v.end(), std::inserter(s, s.begin()), MangleClass);
82 };
83
84 classesVecToSet(options.GetPartialClasses(), conf.partial);
85 classesVecToSet(options.GetRemainsPartialClasses(), conf.remainsPartial);
86
87 auto res = ark::static_linker::Link(conf, options.GetOutput(), files);
88
89 size_t i = 0;
90 for (const auto &s : res.errors) {
91 std::cerr << "# " << ++i << "\n";
92 std::cerr << s << std::endl;
93 }
94
95 if (options.IsShowStats()) {
96 std::cout << "stats:\n" << res.stats << std::endl;
97 }
98
99 const auto wasError = !res.errors.empty();
100 return static_cast<int>(wasError);
101 }
102