comparison gcc/d/d-incpath.cc @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* d-incpath.cc -- Set up combined import paths for the D frontend.
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
3
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21
22 #include "dmd/globals.h"
23
24 #include "cppdefault.h"
25
26 /* Look for directories that start with the standard prefix.
27 "Translate" them, i.e: replace /usr/local/lib/gcc with
28 IPREFIX and search them first. Based on incpath.c. */
29
30 static char *
31 prefixed_path (const char *path, const char *iprefix)
32 {
33 size_t len;
34
35 if (cpp_relocated () && (len = cpp_PREFIX_len) != 0)
36 {
37 if (!strncmp (path, cpp_PREFIX, len))
38 {
39 static const char *relocated_prefix;
40 /* If this path starts with the configure-time prefix,
41 but the compiler has been relocated, replace it
42 with the run-time prefix. */
43 if (!relocated_prefix)
44 {
45 /* Make relative prefix expects the first argument
46 to be a program, not a directory. */
47 char *dummy = concat (gcc_exec_prefix, "dummy", NULL);
48 relocated_prefix
49 = make_relative_prefix (dummy,
50 cpp_EXEC_PREFIX,
51 cpp_PREFIX);
52 free (dummy);
53 }
54
55 return concat (relocated_prefix, path + len, NULL);
56 }
57 }
58
59 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
60 {
61 if (!strncmp (path, cpp_GCC_INCLUDE_DIR, len))
62 return concat (iprefix, path + len, NULL);
63 }
64
65 return xstrdup (path);
66 }
67
68 /* Add PATHS to the global import lookup path. */
69
70 static void
71 add_globalpaths (Strings *paths)
72 {
73 if (paths)
74 {
75 if (!global.path)
76 global.path = new Strings ();
77
78 for (size_t i = 0; i < paths->dim; i++)
79 {
80 const char *path = (*paths)[i];
81 const char *target = lrealpath (path);
82
83 if (target == NULL || !FileName::exists (target))
84 {
85 if (target)
86 free (CONST_CAST (char *, target));
87 continue;
88 }
89
90 global.path->push (target);
91 }
92 }
93 }
94
95 /* Add PATHS to the global file import lookup path. */
96
97 static void
98 add_filepaths (Strings *paths)
99 {
100 if (paths)
101 {
102 if (!global.filePath)
103 global.filePath = new Strings ();
104
105 for (size_t i = 0; i < paths->dim; i++)
106 {
107 const char *path = (*paths)[i];
108 const char *target = lrealpath (path);
109
110 if (!FileName::exists (target))
111 {
112 free (CONST_CAST (char *, target));
113 continue;
114 }
115
116 global.filePath->push (target);
117 }
118 }
119 }
120
121 /* Add all search directories to compiler runtime.
122 if STDINC, also include standard library paths. */
123
124 void
125 add_import_paths (const char *iprefix, const char *imultilib, bool stdinc)
126 {
127 if (stdinc)
128 {
129 for (const default_include *p = cpp_include_defaults; p->fname; p++)
130 {
131 char *path;
132
133 /* Ignore C++ paths. */
134 if (p->cplusplus)
135 continue;
136
137 if (!p->add_sysroot)
138 path = prefixed_path (p->fname, iprefix);
139 else
140 path = xstrdup (p->fname);
141
142 /* Add D-specific suffix. */
143 path = concat (path, "/d", NULL);
144
145 /* Ignore duplicate entries. */
146 bool found = false;
147 for (size_t i = 0; i < global.params.imppath->dim; i++)
148 {
149 if (strcmp (path, (*global.params.imppath)[i]) == 0)
150 {
151 found = true;
152 break;
153 }
154 }
155
156 if (found)
157 {
158 free (path);
159 continue;
160 }
161
162 /* Multilib support. */
163 if (imultilib)
164 {
165 char *target_path = concat (path, "/", imultilib, NULL);
166 global.params.imppath->shift (target_path);
167 }
168
169 global.params.imppath->shift (path);
170 }
171 }
172
173 /* Add import search paths. */
174 if (global.params.imppath)
175 {
176 for (size_t i = 0; i < global.params.imppath->dim; i++)
177 {
178 const char *path = (*global.params.imppath)[i];
179 if (path)
180 add_globalpaths (FileName::splitPath (path));
181 }
182 }
183
184 /* Add string import search paths. */
185 if (global.params.fileImppath)
186 {
187 for (size_t i = 0; i < global.params.fileImppath->dim; i++)
188 {
189 const char *path = (*global.params.fileImppath)[i];
190 if (path)
191 add_filepaths (FileName::splitPath (path));
192 }
193 }
194 }
195