findcomp.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Find the correct compiler.
  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 <config.h>
  16. #include <string>
  17. #include <dirent.h>
  18. #include <stdlib.h>
  19. #include "libiberty.h"
  20. #include "xregex.h"
  21. #include "findcomp.hh"
  22. #include "system.h"
  23. class scanner
  24. {
  25. public:
  26. scanner (const std::string &dir)
  27. {
  28. m_dir = opendir (dir.c_str ());
  29. }
  30. ~scanner ()
  31. {
  32. if (m_dir != NULL)
  33. closedir (m_dir);
  34. }
  35. const char *next ()
  36. {
  37. if (m_dir == NULL)
  38. return NULL;
  39. struct dirent *entry = readdir (m_dir);
  40. if (entry == NULL)
  41. return NULL;
  42. return entry->d_name;
  43. }
  44. private:
  45. DIR *m_dir;
  46. };
  47. static bool
  48. search_dir (const regex_t &regexp, const std::string &dir, std::string *result)
  49. {
  50. scanner scan (dir);
  51. const char *filename;
  52. while ((filename = scan.next ()) != NULL)
  53. {
  54. if (regexec (&regexp, filename, 0, NULL, 0) == 0)
  55. {
  56. *result = dir + DIR_SEPARATOR + filename;
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. class tokenizer
  63. {
  64. public:
  65. tokenizer (const char *str)
  66. : m_str (str),
  67. m_pos (0)
  68. {
  69. }
  70. bool done () const
  71. {
  72. return m_pos == std::string::npos;
  73. }
  74. std::string next ()
  75. {
  76. std::string::size_type last_pos = m_pos;
  77. std::string::size_type colon = m_str.find(':', last_pos);
  78. std::string result;
  79. if (colon == std::string::npos)
  80. {
  81. m_pos = colon;
  82. result = m_str.substr(last_pos, colon);
  83. }
  84. else
  85. {
  86. m_pos = colon + 1;
  87. result = m_str.substr(last_pos, colon - last_pos);
  88. }
  89. if (result == "")
  90. result = ".";
  91. return result;
  92. }
  93. private:
  94. std::string m_str;
  95. std::string::size_type m_pos;
  96. };
  97. bool
  98. find_compiler (const regex_t &regexp, std::string *result)
  99. {
  100. const char *cpath = getenv ("PATH");
  101. if (cpath == NULL)
  102. return false;
  103. tokenizer dirs (cpath);
  104. while (!dirs.done ())
  105. {
  106. std::string dir = dirs.next ();
  107. if (search_dir (regexp, dir, result))
  108. return true;
  109. }
  110. return false;
  111. }