driver-native.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Subroutines for the gcc driver.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  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. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License 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 "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. /* This will be called by the spec parser in gcc.c when it sees
  20. a %:local_cpu_detect(args) construct. Currently it will be called
  21. with either "arch" or "tune" as argument depending on if -march=native
  22. or -mtune=native is to be substituted.
  23. It returns a string containing new command line parameters to be
  24. put at the place of the above two options, depending on what CPU
  25. this is executed. E.g. "-march=loongson2f" on a Loongson 2F for
  26. -march=native. If the routine can't detect a known processor,
  27. the -march or -mtune option is discarded.
  28. ARGC and ARGV are set depending on the actual arguments given
  29. in the spec. */
  30. const char *
  31. host_detect_local_cpu (int argc, const char **argv)
  32. {
  33. const char *cpu = NULL;
  34. char buf[128];
  35. FILE *f;
  36. bool arch;
  37. if (argc < 1)
  38. return NULL;
  39. arch = strcmp (argv[0], "arch") == 0;
  40. if (!arch && strcmp (argv[0], "tune"))
  41. return NULL;
  42. f = fopen ("/proc/cpuinfo", "r");
  43. if (f == NULL)
  44. return NULL;
  45. while (fgets (buf, sizeof (buf), f) != NULL)
  46. if (strncmp (buf, "cpu model", sizeof ("cpu model") - 1) == 0)
  47. {
  48. if (strstr (buf, "Godson2 V0.2") != NULL
  49. || strstr (buf, "Loongson-2 V0.2") != NULL
  50. || strstr (buf, "Loongson-2E") != NULL)
  51. cpu = "loongson2e";
  52. else if (strstr (buf, "Godson2 V0.3") != NULL
  53. || strstr (buf, "Loongson-2 V0.3") != NULL
  54. || strstr (buf, "Loongson-2F") != NULL)
  55. cpu = "loongson2f";
  56. else if (strstr (buf, "Godson3 V0.5") != NULL
  57. || strstr (buf, "Loongson-3 V0.5") != NULL
  58. || strstr (buf, "Loongson-3A") != NULL)
  59. cpu = "loongson3a";
  60. else if (strstr (buf, "SiByte SB1") != NULL)
  61. cpu = "sb1";
  62. else if (strstr (buf, "R5000") != NULL)
  63. cpu = "r5000";
  64. else if (strstr (buf, "Octeon II") != NULL)
  65. cpu = "octeon2";
  66. else if (strstr (buf, "Octeon") != NULL)
  67. cpu = "octeon";
  68. break;
  69. }
  70. fclose (f);
  71. if (cpu == NULL)
  72. return NULL;
  73. return concat ("-m", argv[0], "=", cpu, NULL);
  74. }