OSDN Git Service

* config/i386/i386.c (ix86_init_mmx_sse_builtins)
[pf3gnuchains/gcc-fork.git] / gcc / config / darwin-driver.c
1 /* Additional functions for the GCC driver on Darwin native.
2    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Apple Computer Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 #ifndef CROSS_DIRECTORY_STRUCTURE
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "gcc.h"
28 #include <sys/sysctl.h>
29 #include "xregex.h"
30
31 #ifndef SWITCH_TAKES_ARG
32 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
33 #endif
34
35 #ifndef WORD_SWITCH_TAKES_ARG
36 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
37 #endif
38
39 /* When running on a Darwin system and using that system's headers and
40    libraries, default the -mmacosx-version-min flag to be the version
41    of the system on which the compiler is running.  */
42
43 void
44 darwin_default_min_version (int * argc_p, char *** argv_p)
45 {
46   const int argc = *argc_p;
47   char ** const argv = *argv_p;
48   int i;
49   char osversion[32];
50   size_t osversion_len = sizeof (osversion) - 1;
51   static int osversion_name[2] = { CTL_KERN, KERN_OSRELEASE };
52   char * version_p;
53   char * version_pend;
54   int major_vers;
55   char minor_vers[6];
56   static char new_flag[sizeof ("-mmacosx-version-min=10.0.0") + 6];
57
58   /* If the command-line is empty, just return.  */
59   if (argc <= 1)
60     return;
61   /* Don't do this if the user has specified -b or -V at the start
62      of the command-line.  */
63   if (argv[1][0] == '-'
64       && (argv[1][1] == 'V' ||
65           ((argv[1][1] == 'b') && (NULL != strchr(argv[1] + 2,'-')))))
66     return;
67   
68   /* Don't do this if the user specified -mmacosx-version-min= or
69      -mno-macosx-version-min.  */
70   for (i = 1; i < argc; i++)
71     if (argv[i][0] == '-')
72       {
73         const char * const p = argv[i];
74         if (strncmp (p, "-mno-macosx-version-min", 23) == 0
75             || strncmp (p, "-mmacosx-version-min", 20) == 0)
76           return;
77         
78         /* It doesn't count if it's an argument to a different switch.  */
79         if (p[0] == '-'
80             && ((SWITCH_TAKES_ARG (p[1]) > (p[2] != 0))
81                 || WORD_SWITCH_TAKES_ARG (p + 1)))
82           i++;
83       }
84
85   /* Retrieve the deployment target from the environment and insert
86      it as a flag.  */
87   {
88     const char * macosx_deployment_target;
89     macosx_deployment_target = getenv ("MACOSX_DEPLOYMENT_TARGET");
90     if (macosx_deployment_target
91         /* Apparently, an empty string for MACOSX_DEPLOYMENT_TARGET means
92            "use the default".  Or, possibly "use 10.1".  We choose
93            to ignore the environment variable, as if it was never set.  */
94         && macosx_deployment_target[0])
95       {
96         ++*argc_p;
97         *argv_p = xmalloc (sizeof (char *) * *argc_p);
98         (*argv_p)[0] = argv[0];
99         (*argv_p)[1] = concat ("-mmacosx-version-min=",
100                                macosx_deployment_target, NULL);
101         memcpy (*argv_p + 2, argv + 1, (argc - 1) * sizeof (char *));
102         return;
103       }
104   }
105
106   /* Determine the version of the running OS.  If we can't, warn user,
107      and do nothing.  */
108   if (sysctl (osversion_name, ARRAY_SIZE (osversion_name), osversion,
109               &osversion_len, NULL, 0) == -1)
110     {
111       fprintf (stderr, "sysctl for kern.osversion failed: %s\n",
112                xstrerror (errno));
113       return;
114     }
115
116   /* Try to parse the first two parts of the OS version number.  Warn
117      user and return if it doesn't make sense.  */
118   if (! ISDIGIT (osversion[0]))
119     goto parse_failed;
120   major_vers = osversion[0] - '0';
121   version_p = osversion + 1;
122   if (ISDIGIT (*version_p))
123     major_vers = major_vers * 10 + (*version_p++ - '0');
124   if (major_vers > 4 + 9)
125     goto parse_failed;
126   if (*version_p++ != '.')
127     goto parse_failed;
128   version_pend = strchr(version_p, '.');
129   if (!version_pend)
130     goto parse_failed;
131   if (! ISDIGIT (*version_p))
132     goto parse_failed;
133   strncpy(minor_vers, version_p, version_pend - version_p);
134   minor_vers[version_pend - version_p] = '\0';
135   
136   /* The major kernel version number is 4 plus the second OS version
137      component.  */
138   if (major_vers - 4 <= 4)
139     /* On 10.4 and earlier, the old linker is used which does not
140        support three-component system versions.  */
141     sprintf (new_flag, "-mmacosx-version-min=10.%d", major_vers - 4);
142   else
143     sprintf (new_flag, "-mmacosx-version-min=10.%d.%s", major_vers - 4,
144              minor_vers);
145
146   /* Add the new flag.  */
147   ++*argc_p;
148   *argv_p = xmalloc (sizeof (char *) * *argc_p);
149   (*argv_p)[0] = argv[0];
150   (*argv_p)[1] = new_flag;
151   memcpy (*argv_p + 2, argv + 1, (argc - 1) * sizeof (char *));
152   return;
153   
154  parse_failed:
155   fprintf (stderr, "couldn't understand kern.osversion `%.*s'\n",
156            (int) osversion_len, osversion);
157   return;
158 }
159
160 #endif /* CROSS_DIRECTORY_STRUCTURE */