]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/d/d-incpath.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / d / d-incpath.cc
CommitLineData
b4c522fa 1/* d-incpath.cc -- Set up combined import paths for the D frontend.
7adcbafe 2 Copyright (C) 2006-2022 Free Software Foundation, Inc.
b4c522fa
IB
3
4GCC is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 3, or (at your option)
7any later version.
8
9GCC is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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"
5fee5ec3 23#include "d-frontend.h"
b4c522fa
IB
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
31static char *
32prefixed_path (const char *path, const char *iprefix)
33{
5c66a1a3 34 if (cpp_relocated () && cpp_PREFIX_len != 0)
b4c522fa 35 {
5c66a1a3 36 if (!filename_ncmp (path, cpp_PREFIX, cpp_PREFIX_len))
b4c522fa
IB
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
5c66a1a3 54 return concat (relocated_prefix, path + cpp_PREFIX_len, NULL);
b4c522fa
IB
55 }
56 }
57
5c66a1a3 58 if (iprefix && cpp_GCC_INCLUDE_DIR_len != 0)
b4c522fa 59 {
5c66a1a3
IB
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);
b4c522fa
IB
62 }
63
64 return xstrdup (path);
65}
66
67/* Add PATHS to the global import lookup path. */
68
69static void
70add_globalpaths (Strings *paths)
71{
72 if (paths)
73 {
74 if (!global.path)
5fee5ec3 75 global.path = d_gc_malloc<Strings> ();
b4c522fa 76
2cbc99d1 77 for (size_t i = 0; i < paths->length; i++)
b4c522fa
IB
78 {
79 const char *path = (*paths)[i];
bed5ed71 80 const char *target = lrealpath (path);
b4c522fa
IB
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
96static void
97add_filepaths (Strings *paths)
98{
99 if (paths)
100 {
101 if (!global.filePath)
5fee5ec3 102 global.filePath = d_gc_malloc<Strings> ();
b4c522fa 103
2cbc99d1 104 for (size_t i = 0; i < paths->length; i++)
b4c522fa
IB
105 {
106 const char *path = (*paths)[i];
bed5ed71 107 const char *target = lrealpath (path);
b4c522fa
IB
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
123void
124add_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;
2cbc99d1 146 for (size_t i = 0; i < global.params.imppath->length; i++)
b4c522fa
IB
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 {
2cbc99d1 175 for (size_t i = 0; i < global.params.imppath->length; i++)
b4c522fa
IB
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 {
2cbc99d1 186 for (size_t i = 0; i < global.params.fileImppath->length; i++)
b4c522fa
IB
187 {
188 const char *path = (*global.params.fileImppath)[i];
189 if (path)
190 add_filepaths (FileName::splitPath (path));
191 }
192 }
193}
194