1 //===- CommandLine.cpp ----------------------------------------------------===//
2 //
3 // The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include <mcld/Support/CommandLine.h>
10 #include <llvm/ADT/StringRef.h>
11 #include <llvm/ADT/Twine.h>
12 #include <llvm/Support/ErrorHandling.h>
13
14 using namespace llvm;
15 using namespace llvm::cl;
16
17 using namespace mcld;
18
19 //===----------------------------------------------------------------------===//
20 // parser<mcld::sys::fs::Path>
21 //===----------------------------------------------------------------------===//
parse(llvm::cl::Option & O,llvm::StringRef ArgName,llvm::StringRef Arg,mcld::sys::fs::Path & Val)22 bool parser<mcld::sys::fs::Path>::parse(llvm::cl::Option &O,
23 llvm::StringRef ArgName,
24 llvm::StringRef Arg,
25 mcld::sys::fs::Path &Val)
26 {
27 Val.assign<llvm::StringRef::const_iterator>(Arg.begin(), Arg.end());
28 return false;
29 }
30
31 static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff
32
printOptionDiff(const llvm::cl::Option & O,const mcld::sys::fs::Path & V,parser<mcld::sys::fs::Path>::OptVal Default,size_t GlobalWidth) const33 void parser<mcld::sys::fs::Path>::printOptionDiff(const llvm::cl::Option &O,
34 const mcld::sys::fs::Path &V,
35 parser<mcld::sys::fs::Path>::OptVal Default,
36 size_t GlobalWidth) const
37 {
38 printOptionName(O, GlobalWidth);
39 outs() << "= " << V;
40 size_t VSize = V.native().size();
41 size_t NumSpaces = MaxOptWidth > VSize ? MaxOptWidth - VSize : 0;
42 outs().indent(NumSpaces) << " (default: ";
43 if (Default.hasValue())
44 outs() << Default.getValue().c_str();
45 else
46 outs() << "*no default*";
47 outs() << ")\n";
48 }
49
anchor()50 void parser<mcld::sys::fs::Path>::anchor()
51 {
52 // do nothing
53 }
54
55 //===----------------------------------------------------------------------===//
56 // parser<mcld::MCLDDirectory>
57 //===----------------------------------------------------------------------===//
parse(llvm::cl::Option & O,llvm::StringRef ArgName,llvm::StringRef Arg,mcld::MCLDDirectory & Val)58 bool parser<mcld::MCLDDirectory>::parse(llvm::cl::Option &O,
59 llvm::StringRef ArgName,
60 llvm::StringRef Arg,
61 mcld::MCLDDirectory &Val)
62 {
63 Val.assign(Arg);
64 return false;
65 }
66
printOptionDiff(const llvm::cl::Option & O,const mcld::MCLDDirectory & V,parser<mcld::MCLDDirectory>::OptVal Default,size_t GlobalWidth) const67 void parser<mcld::MCLDDirectory>::printOptionDiff(const llvm::cl::Option &O,
68 const mcld::MCLDDirectory &V,
69 parser<mcld::MCLDDirectory>::OptVal Default,
70 size_t GlobalWidth) const
71 {
72 printOptionName(O, GlobalWidth);
73 outs() << "= " << V.name();
74 size_t VSize = V.name().size();
75 size_t NumSpaces = MaxOptWidth > VSize ? MaxOptWidth - VSize : 0;
76 outs().indent(NumSpaces) << " (default: ";
77 if (Default.hasValue())
78 outs() << Default.getValue().name();
79 else
80 outs() << "*no default*";
81 outs() << ")\n";
82 }
83
anchor()84 void parser<mcld::MCLDDirectory>::anchor()
85 {
86 // do nothing
87 }
88
89 //===----------------------------------------------------------------------===//
90 // parser<mcld::ZOption>
91 //===----------------------------------------------------------------------===//
parse(llvm::cl::Option & O,llvm::StringRef ArgName,llvm::StringRef Arg,mcld::ZOption & Val)92 bool parser<mcld::ZOption>::parse(llvm::cl::Option &O,
93 llvm::StringRef ArgName,
94 llvm::StringRef Arg,
95 mcld::ZOption &Val)
96 {
97 if (0 == Arg.compare("combreloc"))
98 Val.setKind(ZOption::CombReloc);
99 else if (0 == Arg.compare("nocombreloc"))
100 Val.setKind(ZOption::NoCombReloc);
101 else if (0 == Arg.compare("defs"))
102 Val.setKind(ZOption::Defs);
103 else if (0 == Arg.compare("execstack"))
104 Val.setKind(ZOption::ExecStack);
105 else if (0 == Arg.compare("noexecstack"))
106 Val.setKind(ZOption::NoExecStack);
107 else if (0 == Arg.compare("initfirst"))
108 Val.setKind(ZOption::InitFirst);
109 else if (0 == Arg.compare("interpose"))
110 Val.setKind(ZOption::InterPose);
111 else if (0 == Arg.compare("loadfltr"))
112 Val.setKind(ZOption::LoadFltr);
113 else if (0 == Arg.compare("muldefs"))
114 Val.setKind(ZOption::MulDefs);
115 else if (0 == Arg.compare("nocopyreloc"))
116 Val.setKind(ZOption::NoCopyReloc);
117 else if (0 == Arg.compare("nodefaultlib"))
118 Val.setKind(ZOption::NoDefaultLib);
119 else if (0 == Arg.compare("nodelete"))
120 Val.setKind(ZOption::NoDelete);
121 else if (0 == Arg.compare("nodlopen"))
122 Val.setKind(ZOption::NoDLOpen);
123 else if (0 == Arg.compare("nodump"))
124 Val.setKind(ZOption::NoDump);
125 else if (0 == Arg.compare("relro"))
126 Val.setKind(ZOption::Relro);
127 else if (0 == Arg.compare("norelro"))
128 Val.setKind(ZOption::NoRelro);
129 else if (0 == Arg.compare("lazy"))
130 Val.setKind(ZOption::Lazy);
131 else if (0 == Arg.compare("now"))
132 Val.setKind(ZOption::Now);
133 else if (0 == Arg.compare("origin"))
134 Val.setKind(ZOption::Origin);
135 else if (Arg.startswith("common-page-size=")) {
136 Val.setKind(ZOption::CommPageSize);
137 long long unsigned size = 0;
138 Arg.drop_front(17).getAsInteger(0, size);
139 Val.setPageSize(static_cast<uint64_t>(size));
140 } else if (Arg.startswith("max-page-size=")) {
141 Val.setKind(ZOption::MaxPageSize);
142 long long unsigned size = 0;
143 Arg.drop_front(14).getAsInteger(0, size);
144 Val.setPageSize(static_cast<uint64_t>(size));
145 }
146
147 if (ZOption::Unknown == Val.kind())
148 llvm::report_fatal_error(llvm::Twine("unknown -z option: `") +
149 Arg +
150 llvm::Twine("'\n"));
151 return false;
152 }
153
printOptionDiff(const llvm::cl::Option & O,const mcld::ZOption & V,parser<mcld::ZOption>::OptVal Default,size_t GlobalWidth) const154 void parser<mcld::ZOption>::printOptionDiff(const llvm::cl::Option &O,
155 const mcld::ZOption &V,
156 parser<mcld::ZOption>::OptVal Default,
157 size_t GlobalWidth) const
158 {
159 // TODO
160 }
161
anchor()162 void parser<mcld::ZOption>::anchor()
163 {
164 // do nothing
165 }
166
167