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)
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"
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. */
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
);
47 = make_relative_prefix (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. */
69 add_globalpaths (Strings
*paths
)
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
))
84 free (CONST_CAST (char *, target
));
88 global
.path
->push (target
);
93 /* Add PATHS to the global file import lookup path. */
96 add_filepaths (Strings
*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
));
114 global
.filePath
->push (target
);
119 /* Add all search directories to compiler runtime.
120 if STDINC, also include standard library paths. */
123 add_import_paths (const char *iprefix
, const char *imultilib
, bool stdinc
)
127 for (const default_include
*p
= cpp_include_defaults
; p
->fname
; p
++)
131 /* Ignore C++ paths. */
136 path
= prefixed_path (p
->fname
, iprefix
);
138 path
= xstrdup (p
->fname
);
140 /* Add D-specific suffix. */
141 path
= concat (path
, "/d", NULL
);
143 /* Ignore duplicate entries. */
145 for (size_t i
= 0; i
< global
.params
.imppath
->length
; i
++)
147 if (strcmp (path
, (*global
.params
.imppath
)[i
]) == 0)
160 /* Multilib support. */
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
];
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
];
189 add_filepaths (FileName::splitPath (path
));