compiler.cc 2.8 KB

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