]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcc1/findcomp.cc
Add SET_DECL_MODE
[thirdparty/gcc.git] / libcc1 / findcomp.cc
CommitLineData
ab103e33 1/* Find the correct compiler.
6f8b1fff 2 Copyright (C) 2014-2016 Free Software Foundation, Inc.
ab103e33 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include <config.h>
21#include <string>
22#include <dirent.h>
23#include <stdlib.h>
24
25#include "libiberty.h"
26#include "xregex.h"
27#include "findcomp.hh"
28
29class scanner
30{
31public:
32
33 scanner (const std::string &dir)
34 {
35 m_dir = opendir (dir.c_str ());
36 }
37
38 ~scanner ()
39 {
40 if (m_dir != NULL)
41 closedir (m_dir);
42 }
43
44 const char *next ()
45 {
46 if (m_dir == NULL)
47 return NULL;
48
49 struct dirent *entry = readdir (m_dir);
50 if (entry == NULL)
51 return NULL;
52
53 return entry->d_name;
54 }
55
56private:
57
58 DIR *m_dir;
59};
60
61static bool
62search_dir (const regex_t &regexp, const std::string &dir, std::string *result)
63{
64 scanner scan (dir);
65 const char *filename;
66
67 while ((filename = scan.next ()) != NULL)
68 {
69 if (regexec (&regexp, filename, 0, NULL, 0) == 0)
70 {
71 *result = filename;
72 return true;
73 }
74 }
75
76 return false;
77}
78
79class tokenizer
80{
81public:
82
83 tokenizer (const char *str)
84 : m_str (str),
85 m_pos (0)
86 {
87 }
88
89 bool done () const
90 {
91 return m_pos == std::string::npos;
92 }
93
94 std::string next ()
95 {
96 std::string::size_type last_pos = m_pos;
97 std::string::size_type colon = m_str.find(':', last_pos);
98
99 std::string result;
100 if (colon == std::string::npos)
101 {
102 m_pos = colon;
103 result = m_str.substr(last_pos, colon);
104 }
105 else
106 {
107 m_pos = colon + 1;
108 result = m_str.substr(last_pos, colon - last_pos);
109 }
110 if (result == "")
111 result = ".";
112
113 return result;
114 }
115
116private:
117
118 std::string m_str;
119 std::string::size_type m_pos;
120};
121
122bool
123find_compiler (const regex_t &regexp, std::string *result)
124{
125 const char *cpath = getenv ("PATH");
126
127 if (cpath == NULL)
128 return false;
129
130 tokenizer dirs (cpath);
131 while (!dirs.done ())
132 {
133 std::string dir = dirs.next ();
134 if (search_dir (regexp, dir, result))
135 return true;
136 }
137
138 return false;
139}