• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- SearchPathOptions.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/SearchPathOptions.h>
10 #include <mcld/LinkerConfig.h>
11 #include <mcld/LinkerScript.h>
12 #include <mcld/Support/raw_ostream.h>
13 
14 namespace {
15 
16 llvm::cl::opt<mcld::sys::fs::Path,
17               false,
18               llvm::cl::parser<mcld::sys::fs::Path> > ArgSysRoot("sysroot",
19   llvm::cl::desc("Use directory as the location of the sysroot"),
20   llvm::cl::value_desc("directory"),
21   llvm::cl::ValueRequired);
22 
23 llvm::cl::list<std::string,
24                bool,
25                llvm::cl::SearchDirParser> ArgSearchDirList("L",
26   llvm::cl::ZeroOrMore,
27   llvm::cl::desc("Add [searchdir] to the list of search paths"),
28   llvm::cl::value_desc("searchdir"),
29   llvm::cl::Prefix);
30 
31 llvm::cl::alias ArgSearchDirListAlias("library-path",
32   llvm::cl::desc("alias for -L"),
33   llvm::cl::aliasopt(ArgSearchDirList));
34 
35 llvm::cl::opt<bool> ArgNoStdlib("nostdlib",
36   llvm::cl::desc("Only search lib dirs explicitly specified on cmdline"),
37   llvm::cl::init(false));
38 
39 llvm::cl::list<std::string,
40                bool,
41                llvm::cl::SearchDirParser> ArgRuntimePath("rpath",
42   llvm::cl::ZeroOrMore,
43   llvm::cl::desc("Add a directory to the runtime library search path"),
44   llvm::cl::value_desc("dir"));
45 
46 llvm::cl::alias ArgRuntimePathAlias("R",
47   llvm::cl::desc("alias for --rpath"),
48   llvm::cl::aliasopt(ArgRuntimePath), llvm::cl::Prefix);
49 
50 // Not supported yet {
51 llvm::cl::list<std::string,
52                bool,
53                llvm::cl::SearchDirParser> ArgRuntimePathLink("rpath-link",
54   llvm::cl::ZeroOrMore,
55   llvm::cl::desc("Add a directory to the link time library search path"),
56   llvm::cl::value_desc("dir"));
57 
58 llvm::cl::list<std::string> ArgY("Y",
59   llvm::cl::desc("Add path to the default library search path"),
60   llvm::cl::value_desc("default-search-path"));
61 // } Not supported yet
62 
63 } // anonymous namespace
64 
65 using namespace mcld;
66 
67 //===----------------------------------------------------------------------===//
68 // SearchPathOptions
69 //===----------------------------------------------------------------------===//
SearchPathOptions()70 SearchPathOptions::SearchPathOptions()
71   : m_SysRoot(ArgSysRoot),
72     m_SearchDirList(ArgSearchDirList),
73     m_NoStdlib(ArgNoStdlib),
74     m_RuntimePath(ArgRuntimePath),
75     m_RuntimePathLink(ArgRuntimePathLink),
76     m_Y(ArgY) {
77 }
78 
parse(LinkerConfig & pConfig,LinkerScript & pScript)79 bool SearchPathOptions::parse(LinkerConfig& pConfig, LinkerScript& pScript)
80 {
81   // set --sysroot
82   if (!m_SysRoot.empty()) {
83     if (exists(m_SysRoot) && is_directory(m_SysRoot))
84       pScript.setSysroot(m_SysRoot);
85   }
86 
87   // set -L[path]
88   llvm::cl::list<std::string>::iterator sd;
89   llvm::cl::list<std::string>::iterator sdEnd = m_SearchDirList.end();
90   for (sd = m_SearchDirList.begin(); sd != sdEnd; ++sd) {
91     if (!pScript.directories().insert(*sd)) {
92       // FIXME: need a warning function
93       errs() << "WARNING: can not open search directory `-L"
94              << *sd
95              << "'.\n";
96     }
97   }
98 
99   // set -no-stdlib
100   pConfig.options().setNoStdlib(m_NoStdlib);
101 
102   // set --rpath [path]
103   llvm::cl::list<std::string>::iterator rp;
104   llvm::cl::list<std::string>::iterator rpEnd = m_RuntimePath.end();
105   for (rp = m_RuntimePath.begin(); rp != rpEnd; ++rp) {
106     pConfig.options().getRpathList().push_back(*rp);
107   }
108 
109   return true;
110 }
111 
112