RISC-V: Make stack_save_restore tests more robust
[official-gcc.git] / gcc / d / d-incpath.cc
blobab73ce1a1aab9629ef53bcfcc25561561cdb2b20
1 /* d-incpath.cc -- Set up combined import paths for the D frontend.
2 Copyright (C) 2006-2023 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"
23 #include "d-frontend.h"
25 #include "cppdefault.h"
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.cc. */
31 static char *
32 prefixed_path (const char *path, const char *iprefix)
34 if (cpp_relocated () && cpp_PREFIX_len != 0)
36 if (!filename_ncmp (path, cpp_PREFIX, cpp_PREFIX_len))
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)
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);
54 return concat (relocated_prefix, path + cpp_PREFIX_len, NULL);
58 if (iprefix && cpp_GCC_INCLUDE_DIR_len != 0)
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);
64 return xstrdup (path);
67 /* Add PATHS to the global import lookup path. */
69 static void
70 add_globalpaths (Strings *paths)
72 if (paths)
74 if (!global.path)
75 global.path = d_gc_malloc<Strings> ();
77 for (size_t i = 0; i < paths->length; i++)
79 const char *path = (*paths)[i];
80 const char *target = lrealpath (path);
82 if (target == NULL || !FileName::exists (target))
84 if (target)
85 free (CONST_CAST (char *, target));
86 continue;
89 global.path->push (target);
94 /* Add PATHS to the global file import lookup path. */
96 static void
97 add_filepaths (Strings *paths)
99 if (paths)
101 if (!global.filePath)
102 global.filePath = d_gc_malloc<Strings> ();
104 for (size_t i = 0; i < paths->length; i++)
106 const char *path = (*paths)[i];
107 const char *target = lrealpath (path);
109 if (!FileName::exists (target))
111 free (CONST_CAST (char *, target));
112 continue;
115 global.filePath->push (target);
120 /* Add all search directories to compiler runtime.
121 if STDINC, also include standard library paths. */
123 void
124 add_import_paths (const char *iprefix, const char *imultilib, bool stdinc)
126 if (stdinc)
128 for (const default_include *p = cpp_include_defaults; p->fname; p++)
130 char *path;
132 /* Ignore C++ paths. */
133 if (p->cplusplus)
134 continue;
136 if (!p->add_sysroot)
137 path = prefixed_path (p->fname, iprefix);
138 else
139 path = xstrdup (p->fname);
141 /* Add D-specific suffix. */
142 path = concat (path, "/d", NULL);
144 /* Ignore duplicate entries. */
145 bool found = false;
146 for (size_t i = 0; i < global.params.imppath->length; i++)
148 if (strcmp (path, (*global.params.imppath)[i]) == 0)
150 found = true;
151 break;
155 if (found)
157 free (path);
158 continue;
161 /* Multilib support. */
162 if (imultilib)
164 char *target_path = concat (path, "/", imultilib, NULL);
165 global.params.imppath->shift (target_path);
168 global.params.imppath->shift (path);
172 /* Add import search paths. */
173 if (global.params.imppath)
175 for (size_t i = 0; i < global.params.imppath->length; i++)
177 const char *path = (*global.params.imppath)[i];
178 if (path)
179 add_globalpaths (FileName::splitPath (path));
183 /* Add string import search paths. */
184 if (global.params.fileImppath)
186 for (size_t i = 0; i < global.params.fileImppath->length; i++)
188 const char *path = (*global.params.fileImppath)[i];
189 if (path)
190 add_filepaths (FileName::splitPath (path));