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)
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/>. */
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. */
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
);
48 = make_relative_prefix (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. */
70 add_globalpaths (Strings
*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
))
82 free (CONST_CAST (char *, target
));
86 global
.path
.push (target
);
91 /* Add PATHS to the global file import lookup path. */
94 add_filepaths (Strings
*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
));
109 global
.filePath
.push (target
);
114 /* Add all search directories to compiler runtime.
115 if STDINC, also include standard library paths. */
118 add_import_paths (const char *iprefix
, const char *imultilib
, bool stdinc
)
122 for (const default_include
*p
= cpp_include_defaults
; p
->fname
; p
++)
126 /* Ignore C++ paths. */
131 path
= prefixed_path (p
->fname
, iprefix
);
133 path
= xstrdup (p
->fname
);
135 /* Add D-specific suffix. */
136 path
= concat (path
, "/d", NULL
);
138 /* Ignore duplicate entries. */
140 for (size_t i
= 0; i
< global
.params
.imppath
.length
; i
++)
142 if (strcmp (path
, global
.params
.imppath
[i
]) == 0)
155 /* Multilib support. */
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
];
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
];
179 add_filepaths (FileName::splitPath (path
));