| 1 | /* Compiler handling for plugin |
| 2 | Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GCC. |
| 5 | |
| 6 | GCC is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free |
| 8 | Software Foundation; either version 3, or (at your option) any later |
| 9 | version. |
| 10 | |
| 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GCC; see the file COPYING3. If not see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <cc1plugin-config.h> |
| 21 | #include <string> |
| 22 | #include <sstream> |
| 23 | #include "libiberty.h" |
| 24 | #include "compiler.hh" |
| 25 | #include "xregex.h" |
| 26 | #include "findcomp.hh" |
| 27 | #include "intl.h" |
| 28 | |
| 29 | // Construct an appropriate regexp to match the compiler name. |
| 30 | static std::string |
| 31 | make_regexp (const std::string &triplet_regexp, const char *compiler) |
| 32 | { |
| 33 | std::stringstream buf; |
| 34 | |
| 35 | buf << "^" << triplet_regexp << "-" ; |
| 36 | |
| 37 | // Quote the compiler name in case it has something funny in it. |
| 38 | for (const char *p = compiler; *p; ++p) |
| 39 | { |
| 40 | switch (*p) |
| 41 | { |
| 42 | case '.': |
| 43 | case '^': |
| 44 | case '$': |
| 45 | case '*': |
| 46 | case '+': |
| 47 | case '?': |
| 48 | case '(': |
| 49 | case ')': |
| 50 | case '[': |
| 51 | case '{': |
| 52 | case '\\': |
| 53 | case '|': |
| 54 | buf << '\\'; |
| 55 | break; |
| 56 | } |
| 57 | buf << *p; |
| 58 | } |
| 59 | buf << "$" ; |
| 60 | |
| 61 | return buf.str (); |
| 62 | } |
| 63 | |
| 64 | char * |
| 65 | cc1_plugin::compiler::find (const char *, std::string &) const |
| 66 | { |
| 67 | return xstrdup (_("Compiler has not been specified" )); |
| 68 | } |
| 69 | |
| 70 | char * |
| 71 | cc1_plugin::compiler_triplet_regexp::find (const char *base, |
| 72 | std::string &compiler) const |
| 73 | { |
| 74 | std::string rx = make_regexp (triplet_regexp: triplet_regexp_, compiler: base); |
| 75 | if (verbose) |
| 76 | fprintf (stderr, _("searching for compiler matching regex %s\n" ), |
| 77 | rx.c_str()); |
| 78 | regex_t triplet; |
| 79 | int code = regcomp (preg: &triplet, pattern: rx.c_str (), REG_EXTENDED | REG_NOSUB); |
| 80 | if (code != 0) |
| 81 | { |
| 82 | size_t len = regerror (errcode: code, preg: &triplet, NULL, errbuf_size: 0); |
| 83 | char err[len]; |
| 84 | |
| 85 | regerror (errcode: code, preg: &triplet, errbuf: err, errbuf_size: len); |
| 86 | |
| 87 | return concat ("Could not compile regexp \"" , |
| 88 | rx.c_str (), |
| 89 | "\": " , |
| 90 | err, |
| 91 | (char *) NULL); |
| 92 | } |
| 93 | |
| 94 | if (!find_compiler (regexp: triplet, result: &compiler)) |
| 95 | { |
| 96 | regfree (preg: &triplet); |
| 97 | return concat ("Could not find a compiler matching \"" , |
| 98 | rx.c_str (), |
| 99 | "\"" , |
| 100 | (char *) NULL); |
| 101 | } |
| 102 | regfree (preg: &triplet); |
| 103 | if (verbose) |
| 104 | fprintf (stderr, _("found compiler %s\n" ), compiler.c_str()); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | char * |
| 109 | cc1_plugin::compiler_driver_filename::find (const char *, |
| 110 | std::string &compiler) const |
| 111 | { |
| 112 | // Simulate fnotice by fprintf. |
| 113 | if (verbose) |
| 114 | fprintf (stderr, _("using explicit compiler filename %s\n" ), |
| 115 | driver_filename_.c_str()); |
| 116 | compiler = driver_filename_; |
| 117 | return NULL; |
| 118 | } |
| 119 | |