OSDN Git Service

1123aa37c438fbc2bd0b2b08ecef3cd516768be6
[pf3gnuchains/gcc-fork.git] / gcc / config / darwin-driver.c
1 /* Additional functions for the GCC driver on Darwin native.
2    Copyright (C) 2006, 2007, 2008, 2010 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #ifndef CROSS_DIRECTORY_STRUCTURE
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "gcc.h"
27 #include "opts.h"
28 #include <sys/sysctl.h>
29 #include "xregex.h"
30
31 /* When running on a Darwin system and using that system's headers and
32    libraries, default the -mmacosx-version-min flag to be the version
33    of the system on which the compiler is running.  */
34
35 void
36 darwin_default_min_version (unsigned int *decoded_options_count,
37                             struct cl_decoded_option **decoded_options)
38 {
39   const unsigned int argc = *decoded_options_count;
40   struct cl_decoded_option *const argv = *decoded_options;
41   unsigned int i;
42   char osversion[32];
43   size_t osversion_len = sizeof (osversion) - 1;
44   static int osversion_name[2] = { CTL_KERN, KERN_OSRELEASE };
45   char * version_p;
46   char * version_pend;
47   int major_vers;
48   char minor_vers[6];
49   static char new_flag[sizeof ("10.0.0") + 6];
50
51   /* If the command-line is empty, just return.  */
52   if (argc <= 1)
53     return;
54   
55   /* Don't do this if the user specified -mmacosx-version-min= or
56      -mno-macosx-version-min.  */
57   for (i = 1; i < argc; i++)
58     if (argv[i].opt_index == OPT_mmacosx_version_min_)
59       return;
60
61   /* Retrieve the deployment target from the environment and insert
62      it as a flag.  */
63   {
64     const char * macosx_deployment_target;
65     macosx_deployment_target = getenv ("MACOSX_DEPLOYMENT_TARGET");
66     if (macosx_deployment_target
67         /* Apparently, an empty string for MACOSX_DEPLOYMENT_TARGET means
68            "use the default".  Or, possibly "use 10.1".  We choose
69            to ignore the environment variable, as if it was never set.  */
70         && macosx_deployment_target[0])
71       {
72         ++*decoded_options_count;
73         *decoded_options = XNEWVEC (struct cl_decoded_option,
74                                     *decoded_options_count);
75         (*decoded_options)[0] = argv[0];
76         generate_option (OPT_mmacosx_version_min_, macosx_deployment_target,
77                          1, CL_DRIVER, &(*decoded_options)[1]);
78         memcpy (*decoded_options + 2, argv + 1,
79                 (argc - 1) * sizeof (struct cl_decoded_option));
80         return;
81       }
82   }
83
84   /* Determine the version of the running OS.  If we can't, warn user,
85      and do nothing.  */
86   if (sysctl (osversion_name, ARRAY_SIZE (osversion_name), osversion,
87               &osversion_len, NULL, 0) == -1)
88     {
89       warning (0, "sysctl for kern.osversion failed: %m");
90       return;
91     }
92
93   /* Try to parse the first two parts of the OS version number.  Warn
94      user and return if it doesn't make sense.  */
95   if (! ISDIGIT (osversion[0]))
96     goto parse_failed;
97   major_vers = osversion[0] - '0';
98   version_p = osversion + 1;
99   if (ISDIGIT (*version_p))
100     major_vers = major_vers * 10 + (*version_p++ - '0');
101   if (major_vers > 4 + 9)
102     goto parse_failed;
103   if (*version_p++ != '.')
104     goto parse_failed;
105   version_pend = strchr(version_p, '.');
106   if (!version_pend)
107     goto parse_failed;
108   if (! ISDIGIT (*version_p))
109     goto parse_failed;
110   strncpy(minor_vers, version_p, version_pend - version_p);
111   minor_vers[version_pend - version_p] = '\0';
112   
113   /* The major kernel version number is 4 plus the second OS version
114      component.  */
115   if (major_vers - 4 <= 4)
116     /* On 10.4 and earlier, the old linker is used which does not
117        support three-component system versions.  */
118     sprintf (new_flag, "10.%d", major_vers - 4);
119   else
120     sprintf (new_flag, "10.%d.%s", major_vers - 4,
121              minor_vers);
122
123   /* Add the new flag.  */
124   ++*decoded_options_count;
125   *decoded_options = XNEWVEC (struct cl_decoded_option,
126                               *decoded_options_count);
127   (*decoded_options)[0] = argv[0];
128   generate_option (OPT_mmacosx_version_min_, new_flag,
129                    1, CL_DRIVER, &(*decoded_options)[1]);
130   memcpy (*decoded_options + 2, argv + 1,
131           (argc - 1) * sizeof (struct cl_decoded_option));
132   return;
133   
134  parse_failed:
135   warning (0, "couldn't understand kern.osversion %q.*s",
136            (int) osversion_len, osversion);
137   return;
138 }
139
140 #endif /* CROSS_DIRECTORY_STRUCTURE */