]>
Commit | Line | Data |
---|---|---|
900e3ae5 | 1 | /* Subroutines for the gcc driver. |
aeee4812 | 2 | Copyright (C) 2008-2023 Free Software Foundation, Inc. |
900e3ae5 DJ |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License 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 | ||
8fcc61f8 RS |
20 | #define IN_TARGET_CODE 1 |
21 | ||
900e3ae5 DJ |
22 | #include "config.h" |
23 | #include "system.h" | |
432b5555 DD |
24 | #include "coretypes.h" |
25 | #include "tm.h" | |
66c48be2 YS |
26 | #ifdef HAVE_SYS_AUXV_H |
27 | #include <sys/auxv.h> | |
28 | #endif | |
900e3ae5 | 29 | |
e53b6e56 | 30 | /* This will be called by the spec parser in gcc.cc when it sees |
900e3ae5 DJ |
31 | a %:local_cpu_detect(args) construct. Currently it will be called |
32 | with either "arch" or "tune" as argument depending on if -march=native | |
33 | or -mtune=native is to be substituted. | |
34 | ||
35 | It returns a string containing new command line parameters to be | |
36 | put at the place of the above two options, depending on what CPU | |
37 | this is executed. E.g. "-march=loongson2f" on a Loongson 2F for | |
38 | -march=native. If the routine can't detect a known processor, | |
39 | the -march or -mtune option is discarded. | |
40 | ||
41 | ARGC and ARGV are set depending on the actual arguments given | |
42 | in the spec. */ | |
43 | const char * | |
44 | host_detect_local_cpu (int argc, const char **argv) | |
45 | { | |
46 | const char *cpu = NULL; | |
66c48be2 | 47 | char *ret = NULL; |
900e3ae5 DJ |
48 | char buf[128]; |
49 | FILE *f; | |
50 | bool arch; | |
51 | ||
52 | if (argc < 1) | |
53 | return NULL; | |
54 | ||
55 | arch = strcmp (argv[0], "arch") == 0; | |
56 | if (!arch && strcmp (argv[0], "tune")) | |
57 | return NULL; | |
58 | ||
59 | f = fopen ("/proc/cpuinfo", "r"); | |
60 | if (f == NULL) | |
66c48be2 | 61 | goto fallback_cpu; |
900e3ae5 DJ |
62 | |
63 | while (fgets (buf, sizeof (buf), f) != NULL) | |
c0129e2d | 64 | if (startswith (buf, "cpu model")) |
900e3ae5 DJ |
65 | { |
66 | if (strstr (buf, "Godson2 V0.2") != NULL | |
87ed883e HC |
67 | || strstr (buf, "Loongson-2 V0.2") != NULL |
68 | || strstr (buf, "Loongson-2E") != NULL) | |
900e3ae5 DJ |
69 | cpu = "loongson2e"; |
70 | else if (strstr (buf, "Godson2 V0.3") != NULL | |
87ed883e HC |
71 | || strstr (buf, "Loongson-2 V0.3") != NULL |
72 | || strstr (buf, "Loongson-2F") != NULL) | |
900e3ae5 | 73 | cpu = "loongson2f"; |
87ed883e HC |
74 | else if (strstr (buf, "Godson3 V0.5") != NULL |
75 | || strstr (buf, "Loongson-3 V0.5") != NULL | |
76 | || strstr (buf, "Loongson-3A") != NULL) | |
77 | cpu = "loongson3a"; | |
a5a12a83 DD |
78 | else if (strstr (buf, "SiByte SB1") != NULL) |
79 | cpu = "sb1"; | |
80 | else if (strstr (buf, "R5000") != NULL) | |
81 | cpu = "r5000"; | |
38a53a0e AP |
82 | else if (strstr (buf, "Octeon II") != NULL) |
83 | cpu = "octeon2"; | |
d97e6aca AN |
84 | else if (strstr (buf, "Octeon") != NULL) |
85 | cpu = "octeon"; | |
900e3ae5 DJ |
86 | break; |
87 | } | |
88 | ||
89 | fclose (f); | |
90 | ||
66c48be2 YS |
91 | fallback_cpu: |
92 | #if defined (__mips_nan2008) | |
93 | ret = reconcat (ret, " -mnan=2008 ", NULL); | |
94 | #endif | |
95 | ||
96 | #ifdef HAVE_GETAUXVAL | |
900e3ae5 | 97 | if (cpu == NULL) |
66c48be2 YS |
98 | cpu = (const char *) getauxval (AT_BASE_PLATFORM); |
99 | #endif | |
100 | ||
101 | #if defined (_MIPS_ARCH) | |
102 | if (cpu == NULL) | |
103 | cpu = _MIPS_ARCH; | |
104 | #endif | |
105 | ||
106 | if (cpu) | |
107 | ret = reconcat (ret, ret, "-m", argv[0], "=", cpu, NULL); | |
900e3ae5 | 108 | |
66c48be2 | 109 | return ret; |
900e3ae5 | 110 | } |