| 1 |
/* d-incpath.cc -- Set up combined import paths for the D frontend. |
| 2 |
Copyright (C) 2006-2025 Free Software Foundation, Inc. |
| 3 |
|
| 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. |
| 8 |
|
| 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. |
| 13 |
|
| 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/>. */ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include "system.h" |
| 20 |
#include "coretypes.h" |
| 21 |
|
| 22 |
#include "dmd/globals.h" |
| 23 |
#include "d-frontend.h" |
| 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.cc. */ |
| 30 |
|
| 31 |
static char * |
| 32 |
prefixed_path (const char *path, const char *iprefix) |
| 33 |
{ |
| 34 |
if (cpp_relocated () && cpp_PREFIX_len != 0) |
| 35 |
{ |
| 36 |
if (!filename_ncmp (path, cpp_PREFIX, cpp_PREFIX_len)) |
| 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 |
|
| 54 |
return concat (relocated_prefix, path + cpp_PREFIX_len, NULL); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if (iprefix && cpp_GCC_INCLUDE_DIR_len != 0) |
| 59 |
{ |
| 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); |
| 62 |
} |
| 63 |
|
| 64 |
return xstrdup (path); |
| 65 |
} |
| 66 |
|
| 67 |
/* Add PATHS to the global import lookup path. */ |
| 68 |
|
| 69 |
static void |
| 70 |
add_globalpaths (Strings &paths) |
| 71 |
{ |
| 72 |
for (size_t i = 0; i < paths.length; i++) |
| 73 |
{ |
| 74 |
const char *path = paths[i]; |
| 75 |
const char *target = lrealpath (path); |
| 76 |
|
| 77 |
if (target == NULL || !FileName::exists (target)) |
| 78 |
{ |
| 79 |
if (target) |
| 80 |
free (CONST_CAST (char *, target)); |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
global.path.push (target); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* Add PATHS to the global file import lookup path. */ |
| 89 |
|
| 90 |
static void |
| 91 |
add_filepaths (Strings &paths) |
| 92 |
{ |
| 93 |
for (size_t i = 0; i < paths.length; i++) |
| 94 |
{ |
| 95 |
const char *path = paths[i]; |
| 96 |
const char *target = lrealpath (path); |
| 97 |
|
| 98 |
if (!FileName::exists (target)) |
| 99 |
{ |
| 100 |
free (CONST_CAST (char *, target)); |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
global.filePath.push (target); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* Add all search directories to compiler runtime. |
| 109 |
if STDINC, also include standard library paths. */ |
| 110 |
|
| 111 |
void |
| 112 |
add_import_paths (const char *iprefix, const char *imultilib, bool stdinc) |
| 113 |
{ |
| 114 |
if (stdinc) |
| 115 |
{ |
| 116 |
for (const default_include *p = cpp_include_defaults; p->fname; p++) |
| 117 |
{ |
| 118 |
char *path; |
| 119 |
|
| 120 |
/* Ignore C++ paths. */ |
| 121 |
if (p->cplusplus) |
| 122 |
continue; |
| 123 |
|
| 124 |
if (!p->add_sysroot) |
| 125 |
path = prefixed_path (p->fname, iprefix); |
| 126 |
else |
| 127 |
path = xstrdup (p->fname); |
| 128 |
|
| 129 |
/* Add D-specific suffix. */ |
| 130 |
path = concat (path, "/d", NULL); |
| 131 |
|
| 132 |
/* Ignore duplicate entries. */ |
| 133 |
bool found = false; |
| 134 |
for (size_t i = 0; i < global.params.imppath.length; i++) |
| 135 |
{ |
| 136 |
if (strcmp (path, global.params.imppath[i].path) == 0) |
| 137 |
{ |
| 138 |
found = true; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (found) |
| 144 |
{ |
| 145 |
free (path); |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
/* Multilib support. */ |
| 150 |
if (imultilib) |
| 151 |
{ |
| 152 |
char *target_path = concat (path, "/", imultilib, NULL); |
| 153 |
global.params.imppath.shift (target_path); |
| 154 |
} |
| 155 |
|
| 156 |
global.params.imppath.shift (path); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/* Add import search paths. */ |
| 161 |
for (size_t i = 0; i < global.params.imppath.length; i++) |
| 162 |
{ |
| 163 |
const char *path = global.params.imppath[i].path; |
| 164 |
if (path) |
| 165 |
{ |
| 166 |
Strings array; |
| 167 |
FileName::appendSplitPath (path, array); |
| 168 |
add_globalpaths (array); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* Add string import search paths. */ |
| 173 |
for (size_t i = 0; i < global.params.fileImppath.length; i++) |
| 174 |
{ |
| 175 |
const char *path = global.params.fileImppath[i]; |
| 176 |
if (path) |
| 177 |
{ |
| 178 |
Strings array; |
| 179 |
FileName::appendSplitPath (path, array); |
| 180 |
add_filepaths (array); |
| 181 |
} |
| 182 |
} |
| 183 |
} |