driver-native.c revision 1.10
1/* Subroutines for the gcc driver.
2   Copyright (C) 2008-2019 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3.  If not see
18<http://www.gnu.org/licenses/>.  */
19
20#define IN_TARGET_CODE 1
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26
27/* This will be called by the spec parser in gcc.c when it sees
28   a %:local_cpu_detect(args) construct.  Currently it will be called
29   with either "arch" or "tune" as argument depending on if -march=native
30   or -mtune=native is to be substituted.
31
32   It returns a string containing new command line parameters to be
33   put at the place of the above two options, depending on what CPU
34   this is executed.  E.g. "-march=loongson2f" on a Loongson 2F for
35   -march=native.  If the routine can't detect a known processor,
36   the -march or -mtune option is discarded.
37
38   ARGC and ARGV are set depending on the actual arguments given
39   in the spec.  */
40const char *
41host_detect_local_cpu (int argc, const char **argv)
42{
43  const char *cpu = NULL;
44  char buf[128];
45  FILE *f;
46  bool arch;
47
48  if (argc < 1)
49    return NULL;
50
51  arch = strcmp (argv[0], "arch") == 0;
52  if (!arch && strcmp (argv[0], "tune"))
53    return NULL;
54
55  f = fopen ("/proc/cpuinfo", "r");
56  if (f == NULL)
57    return NULL;
58
59  while (fgets (buf, sizeof (buf), f) != NULL)
60    if (strncmp (buf, "cpu model", sizeof ("cpu model") - 1) == 0)
61      {
62	if (strstr (buf, "Godson2 V0.2") != NULL
63	    || strstr (buf, "Loongson-2 V0.2") != NULL
64	    || strstr (buf, "Loongson-2E") != NULL)
65	  cpu = "loongson2e";
66	else if (strstr (buf, "Godson2 V0.3") != NULL
67		 || strstr (buf, "Loongson-2 V0.3") != NULL
68		 || strstr (buf, "Loongson-2F") != NULL)
69	  cpu = "loongson2f";
70	else if (strstr (buf, "Godson3 V0.5") != NULL
71		 || strstr (buf, "Loongson-3 V0.5") != NULL
72		 || strstr (buf, "Loongson-3A") != NULL)
73	  cpu = "loongson3a";
74	else if (strstr (buf, "SiByte SB1") != NULL)
75	  cpu = "sb1";
76	else if (strstr (buf, "R5000") != NULL)
77	  cpu = "r5000";
78	else if (strstr (buf, "Octeon II") != NULL)
79	  cpu = "octeon2";
80	else if (strstr (buf, "Octeon") != NULL)
81	  cpu = "octeon";
82	break;
83      }
84
85  fclose (f);
86
87  if (cpu == NULL)
88    return NULL;
89
90  return concat ("-m", argv[0], "=", cpu, NULL);
91}
92