]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/d-incpath.cc
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / d / d-incpath.cc
1 /* d-incpath.cc -- Set up combined import paths for the D frontend.
2 Copyright (C) 2006-2021 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 #include "d-frontend.h"
24
25 #include "cppdefault.h"
26
27 /* Look for directories that start with the standard prefix.
28 "Translate" them, i.e: replace /usr/local/lib/gcc with
29 IPREFIX and search them first. Based on incpath.c. */
30
31 static char *
32 prefixed_path (const char *path, const char *iprefix)
33 {
34 if (cpp_relocated () && cpp_PREFIX_len != 0)
35 {
36 if (!filename_ncmp (path, cpp_PREFIX, cpp_PREFIX_len))
37 {
38 static const char *relocated_prefix;
39 /* If this path starts with the configure-time prefix,
40 but the compiler has been relocated, replace it
41 with the run-time prefix. */
42 if (!relocated_prefix)
43 {
44 /* Make relative prefix expects the first argument
45 to be a program, not a directory. */
46 char *dummy = concat (gcc_exec_prefix, "dummy", NULL);
47 relocated_prefix
48 = make_relative_prefix (dummy,
49 cpp_EXEC_PREFIX,
50 cpp_PREFIX);
51 free (dummy);
52 }
53
54 return concat (relocated_prefix, path + cpp_PREFIX_len, NULL);
55 }
56 }
57
58 if (iprefix && cpp_GCC_INCLUDE_DIR_len != 0)
59 {
60 if (!filename_ncmp (path, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len))
61 return concat (iprefix, path + cpp_GCC_INCLUDE_DIR_len, NULL);
62 }
63
64 return xstrdup (path);
65 }
66
67 /* Add PATHS to the global import lookup path. */
68
69 static void
70 add_globalpaths (Strings *paths)
71 {
72 if (paths)
73 {
74 if (!global.path)
75 global.path = d_gc_malloc<Strings> ();
76
77 for (size_t i = 0; i < paths->length; i++)
78 {
79 const char *path = (*paths)[i];
80 const char *target = lrealpath (path);
81
82 if (target == NULL || !FileName::exists (target))
83 {
84 if (target)
85 free (CONST_CAST (char *, target));
86 continue;
87 }
88
89 global.path->push (target);
90 }
91 }
92 }
93
94 /* Add PATHS to the global file import lookup path. */
95
96 static void
97 add_filepaths (Strings *paths)
98 {
99 if (paths)
100 {
101 if (!global.filePath)
102 global.filePath = d_gc_malloc<Strings> ();
103
104 for (size_t i = 0; i < paths->length; i++)
105 {
106 const char *path = (*paths)[i];
107 const char *target = lrealpath (path);
108
109 if (!FileName::exists (target))
110 {
111 free (CONST_CAST (char *, target));
112 continue;
113 }
114
115 global.filePath->push (target);
116 }
117 }
118 }
119
120 /* Add all search directories to compiler runtime.
121 if STDINC, also include standard library paths. */
122
123 void
124 add_import_paths (const char *iprefix, const char *imultilib, bool stdinc)
125 {
126 if (stdinc)
127 {
128 for (const default_include *p = cpp_include_defaults; p->fname; p++)
129 {
130 char *path;
131
132 /* Ignore C++ paths. */
133 if (p->cplusplus)
134 continue;
135
136 if (!p->add_sysroot)
137 path = prefixed_path (p->fname, iprefix);
138 else
139 path = xstrdup (p->fname);
140
141 /* Add D-specific suffix. */
142 path = concat (path, "/d", NULL);
143
144 /* Ignore duplicate entries. */
145 bool found = false;
146 for (size_t i = 0; i < global.params.imppath->length; i++)
147 {
148 if (strcmp (path, (*global.params.imppath)[i]) == 0)
149 {
150 found = true;
151 break;
152 }
153 }
154
155 if (found)
156 {
157 free (path);
158 continue;
159 }
160
161 /* Multilib support. */
162 if (imultilib)
163 {
164 char *target_path = concat (path, "/", imultilib, NULL);
165 global.params.imppath->shift (target_path);
166 }
167
168 global.params.imppath->shift (path);
169 }
170 }
171
172 /* Add import search paths. */
173 if (global.params.imppath)
174 {
175 for (size_t i = 0; i < global.params.imppath->length; i++)
176 {
177 const char *path = (*global.params.imppath)[i];
178 if (path)
179 add_globalpaths (FileName::splitPath (path));
180 }
181 }
182
183 /* Add string import search paths. */
184 if (global.params.fileImppath)
185 {
186 for (size_t i = 0; i < global.params.fileImppath->length; i++)
187 {
188 const char *path = (*global.params.fileImppath)[i];
189 if (path)
190 add_filepaths (FileName::splitPath (path));
191 }
192 }
193 }
194