• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 panda::PandArgParser & paParser)26 int PrintHelp(const panda::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     panda::PandArgParser paParser;
52     panda::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     panda::Logger::InitializeStdLogging(panda::Logger::LevelFromString(options.GetLogLevel()),
61                                         panda::Logger::ComponentMask()
62                                             .set(panda::Logger::Component::STATIC_LINKER)
63                                             .set(panda::Logger::Component::PANDAFILE));
64 
65     const auto files = paParser.GetRemainder();
66 
67     if (files.empty()) {
68         std::cerr << "must have at least one file" << std::endl;
69         return PrintHelp(paParser);
70     }
71 
72     if (options.GetOutput().empty()) {
73         auto const &fn = files[0];
74         options.SetOutput(fn.substr(0, fn.find_last_of('.')) + ".linked.abc");
75     }
76 
77     auto conf = panda::static_linker::DefaultConfig();
78 
79     conf.stripDebugInfo = options.IsStripDebugInfo();
80 
81     auto classesVecToSet = [](const std::vector<std::string> &v, std::set<std::string> &s) {
82         s.clear();
83         std::transform(v.begin(), v.end(), std::inserter(s, s.begin()), MangleClass);
84     };
85 
86     classesVecToSet(options.GetParitalClasses(), conf.partial);
87     classesVecToSet(options.GetRemainsPartialClasses(), conf.remainsPartial);
88 
89     auto res = panda::static_linker::Link(conf, options.GetOutput(), files);
90 
91     size_t i = 0;
92     for (const auto &s : res.errors) {
93         std::cerr << "# " << ++i << "\n";
94         std::cerr << s << std::endl;
95     }
96 
97     if (options.IsShowStats()) {
98         std::cout << "stats:\n" << res.stats << std::endl;
99     }
100 
101     const auto wasError = !res.errors.empty();
102     return static_cast<int>(wasError);
103 }
104