Daily bump.
[official-gcc.git] / gcc / d / d-incpath.cc
blob32ab0b71efad27fe51b2121459498d97ff91dc3d
1 /* d-incpath.cc -- Set up combined import paths for the D frontend.
2 Copyright (C) 2006-2024 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 for (size_t i = 0; i < paths->length; i++)
76 const char *path = (*paths)[i];
77 const char *target = lrealpath (path);
79 if (target == NULL || !FileName::exists (target))
81 if (target)
82 free (CONST_CAST (char *, target));
83 continue;
86 global.path.push (target);
91 /* Add PATHS to the global file import lookup path. */
93 static void
94 add_filepaths (Strings *paths)
96 if (paths)
98 for (size_t i = 0; i < paths->length; i++)
100 const char *path = (*paths)[i];
101 const char *target = lrealpath (path);
103 if (!FileName::exists (target))
105 free (CONST_CAST (char *, target));
106 continue;
109 global.filePath.push (target);
114 /* Add all search directories to compiler runtime.
115 if STDINC, also include standard library paths. */
117 void
118 add_import_paths (const char *iprefix, const char *imultilib, bool stdinc)
120 if (stdinc)
122 for (const default_include *p = cpp_include_defaults; p->fname; p++)
124 char *path;
126 /* Ignore C++ paths. */
127 if (p->cplusplus)
128 continue;
130 if (!p->add_sysroot)
131 path = prefixed_path (p->fname, iprefix);
132 else
133 path = xstrdup (p->fname);
135 /* Add D-specific suffix. */
136 path = concat (path, "/d", NULL);
138 /* Ignore duplicate entries. */
139 bool found = false;
140 for (size_t i = 0; i < global.params.imppath.length; i++)
142 if (strcmp (path, global.params.imppath[i]) == 0)
144 found = true;
145 break;
149 if (found)
151 free (path);
152 continue;
155 /* Multilib support. */
156 if (imultilib)
158 char *target_path = concat (path, "/", imultilib, NULL);
159 global.params.imppath.shift (target_path);
162 global.params.imppath.shift (path);
166 /* Add import search paths. */
167 for (size_t i = 0; i < global.params.imppath.length; i++)
169 const char *path = global.params.imppath[i];
170 if (path)
171 add_globalpaths (FileName::splitPath (path));
174 /* Add string import search paths. */
175 for (size_t i = 0; i < global.params.fileImppath.length; i++)
177 const char *path = global.params.fileImppath[i];
178 if (path)
179 add_filepaths (FileName::splitPath (path));