Daily bump.
[official-gcc.git] / gcc / d / d-incpath.cc
blobd86392903e3abcb5bd79bfea0dc88f6fbaa79946
1 /* d-incpath.cc -- Set up combined import paths for the D frontend.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
22 #include "dmd/globals.h"
24 #include "cppdefault.h"
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. */
30 static char *
31 prefixed_path (const char *path, const char *iprefix)
33 if (cpp_relocated () && cpp_PREFIX_len != 0)
35 if (!filename_ncmp (path, cpp_PREFIX, cpp_PREFIX_len))
37 static const char *relocated_prefix;
38 /* If this path starts with the configure-time prefix,
39 but the compiler has been relocated, replace it
40 with the run-time prefix. */
41 if (!relocated_prefix)
43 /* Make relative prefix expects the first argument
44 to be a program, not a directory. */
45 char *dummy = concat (gcc_exec_prefix, "dummy", NULL);
46 relocated_prefix
47 = make_relative_prefix (dummy,
48 cpp_EXEC_PREFIX,
49 cpp_PREFIX);
50 free (dummy);
53 return concat (relocated_prefix, path + cpp_PREFIX_len, NULL);
57 if (iprefix && cpp_GCC_INCLUDE_DIR_len != 0)
59 if (!filename_ncmp (path, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len))
60 return concat (iprefix, path + cpp_GCC_INCLUDE_DIR_len, NULL);
63 return xstrdup (path);
66 /* Add PATHS to the global import lookup path. */
68 static void
69 add_globalpaths (Strings *paths)
71 if (paths)
73 if (!global.path)
74 global.path = new Strings ();
76 for (size_t i = 0; i < paths->length; i++)
78 const char *path = (*paths)[i];
79 const char *target = lrealpath (path);
81 if (target == NULL || !FileName::exists (target))
83 if (target)
84 free (CONST_CAST (char *, target));
85 continue;
88 global.path->push (target);
93 /* Add PATHS to the global file import lookup path. */
95 static void
96 add_filepaths (Strings *paths)
98 if (paths)
100 if (!global.filePath)
101 global.filePath = new Strings ();
103 for (size_t i = 0; i < paths->length; i++)
105 const char *path = (*paths)[i];
106 const char *target = lrealpath (path);
108 if (!FileName::exists (target))
110 free (CONST_CAST (char *, target));
111 continue;
114 global.filePath->push (target);
119 /* Add all search directories to compiler runtime.
120 if STDINC, also include standard library paths. */
122 void
123 add_import_paths (const char *iprefix, const char *imultilib, bool stdinc)
125 if (stdinc)
127 for (const default_include *p = cpp_include_defaults; p->fname; p++)
129 char *path;
131 /* Ignore C++ paths. */
132 if (p->cplusplus)
133 continue;
135 if (!p->add_sysroot)
136 path = prefixed_path (p->fname, iprefix);
137 else
138 path = xstrdup (p->fname);
140 /* Add D-specific suffix. */
141 path = concat (path, "/d", NULL);
143 /* Ignore duplicate entries. */
144 bool found = false;
145 for (size_t i = 0; i < global.params.imppath->length; i++)
147 if (strcmp (path, (*global.params.imppath)[i]) == 0)
149 found = true;
150 break;
154 if (found)
156 free (path);
157 continue;
160 /* Multilib support. */
161 if (imultilib)
163 char *target_path = concat (path, "/", imultilib, NULL);
164 global.params.imppath->shift (target_path);
167 global.params.imppath->shift (path);
171 /* Add import search paths. */
172 if (global.params.imppath)
174 for (size_t i = 0; i < global.params.imppath->length; i++)
176 const char *path = (*global.params.imppath)[i];
177 if (path)
178 add_globalpaths (FileName::splitPath (path));
182 /* Add string import search paths. */
183 if (global.params.fileImppath)
185 for (size_t i = 0; i < global.params.fileImppath->length; i++)
187 const char *path = (*global.params.fileImppath)[i];
188 if (path)
189 add_filepaths (FileName::splitPath (path));