Merged r158704 through r158906 into branch.
[official-gcc.git] / gcc / config / darwin-c.c
blob862a1d1f4c08bd23860ae3638b2c924ba472a912
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Apple Computer Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "c-pragma.h"
29 #include "c-tree.h"
30 #include "incpath.h"
31 #include "c-common.h"
32 #include "toplev.h"
33 #include "flags.h"
34 #include "tm_p.h"
35 #include "cppdefault.h"
36 #include "prefix.h"
37 #include "target.h"
38 #include "target-def.h"
40 /* Pragmas. */
42 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
45 static bool using_frameworks = false;
47 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
48 cpp_dir **dirp);
50 typedef struct align_stack
52 int alignment;
53 struct align_stack * prev;
54 } align_stack;
56 static struct align_stack * field_align_stack = NULL;
58 /* Maintain a small stack of alignments. This is similar to pragma
59 pack's stack, but simpler. */
61 static void
62 push_field_alignment (int bit_alignment)
64 align_stack *entry = XNEW (align_stack);
66 entry->alignment = maximum_field_alignment;
67 entry->prev = field_align_stack;
68 field_align_stack = entry;
70 maximum_field_alignment = bit_alignment;
73 static void
74 pop_field_alignment (void)
76 if (field_align_stack)
78 align_stack *entry = field_align_stack;
80 maximum_field_alignment = entry->alignment;
81 field_align_stack = entry->prev;
82 free (entry);
84 else
85 error ("too many #pragma options align=reset");
88 /* Handlers for Darwin-specific pragmas. */
90 void
91 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
93 /* Do nothing. */
96 /* #pragma options align={mac68k|power|reset} */
98 void
99 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
101 const char *arg;
102 tree t, x;
104 if (pragma_lex (&t) != CPP_NAME)
105 BAD ("malformed '#pragma options', ignoring");
106 arg = IDENTIFIER_POINTER (t);
107 if (strcmp (arg, "align"))
108 BAD ("malformed '#pragma options', ignoring");
109 if (pragma_lex (&t) != CPP_EQ)
110 BAD ("malformed '#pragma options', ignoring");
111 if (pragma_lex (&t) != CPP_NAME)
112 BAD ("malformed '#pragma options', ignoring");
114 if (pragma_lex (&x) != CPP_EOF)
115 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
117 arg = IDENTIFIER_POINTER (t);
118 if (!strcmp (arg, "mac68k"))
119 push_field_alignment (16);
120 else if (!strcmp (arg, "power"))
121 push_field_alignment (0);
122 else if (!strcmp (arg, "reset"))
123 pop_field_alignment ();
124 else
125 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
128 /* #pragma unused ([var {, var}*]) */
130 void
131 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
133 tree decl, x;
134 int tok;
136 if (pragma_lex (&x) != CPP_OPEN_PAREN)
137 BAD ("missing '(' after '#pragma unused', ignoring");
139 while (1)
141 tok = pragma_lex (&decl);
142 if (tok == CPP_NAME && decl)
144 tree local = lookup_name (decl);
145 if (local && (TREE_CODE (local) == PARM_DECL
146 || TREE_CODE (local) == VAR_DECL))
147 TREE_USED (local) = 1;
148 tok = pragma_lex (&x);
149 if (tok != CPP_COMMA)
150 break;
154 if (tok != CPP_CLOSE_PAREN)
155 BAD ("missing ')' after '#pragma unused', ignoring");
157 if (pragma_lex (&x) != CPP_EOF)
158 BAD ("junk at end of '#pragma unused'");
161 /* Parse the ms_struct pragma. */
162 void
163 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
165 const char *arg;
166 tree t;
168 if (pragma_lex (&t) != CPP_NAME)
169 BAD ("malformed '#pragma ms_struct', ignoring");
170 arg = IDENTIFIER_POINTER (t);
172 if (!strcmp (arg, "on"))
173 darwin_ms_struct = true;
174 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
175 darwin_ms_struct = false;
176 else
177 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
179 if (pragma_lex (&t) != CPP_EOF)
180 BAD ("junk at end of '#pragma ms_struct'");
183 static struct frameworks_in_use {
184 size_t len;
185 const char *name;
186 cpp_dir* dir;
187 } *frameworks_in_use;
188 static int num_frameworks = 0;
189 static int max_frameworks = 0;
192 /* Remember which frameworks have been seen, so that we can ensure
193 that all uses of that framework come from the same framework. DIR
194 is the place where the named framework NAME, which is of length
195 LEN, was found. We copy the directory name from NAME, as it will be
196 freed by others. */
198 static void
199 add_framework (const char *name, size_t len, cpp_dir *dir)
201 char *dir_name;
202 int i;
203 for (i = 0; i < num_frameworks; ++i)
205 if (len == frameworks_in_use[i].len
206 && strncmp (name, frameworks_in_use[i].name, len) == 0)
208 return;
211 if (i >= max_frameworks)
213 max_frameworks = i*2;
214 max_frameworks += i == 0;
215 frameworks_in_use = XRESIZEVEC (struct frameworks_in_use,
216 frameworks_in_use, max_frameworks);
218 dir_name = XNEWVEC (char, len + 1);
219 memcpy (dir_name, name, len);
220 dir_name[len] = '\0';
221 frameworks_in_use[num_frameworks].name = dir_name;
222 frameworks_in_use[num_frameworks].len = len;
223 frameworks_in_use[num_frameworks].dir = dir;
224 ++num_frameworks;
227 /* Recall if we have seen the named framework NAME, before, and where
228 we saw it. NAME is LEN bytes long. The return value is the place
229 where it was seen before. */
231 static struct cpp_dir*
232 find_framework (const char *name, size_t len)
234 int i;
235 for (i = 0; i < num_frameworks; ++i)
237 if (len == frameworks_in_use[i].len
238 && strncmp (name, frameworks_in_use[i].name, len) == 0)
240 return frameworks_in_use[i].dir;
243 return 0;
246 /* There are two directories in a framework that contain header files,
247 Headers and PrivateHeaders. We search Headers first as it is more
248 common to upgrade a header from PrivateHeaders to Headers and when
249 that is done, the old one might hang around and be out of data,
250 causing grief. */
252 struct framework_header {const char * dirName; int dirNameLen; };
253 static struct framework_header framework_header_dirs[] = {
254 { "Headers", 7 },
255 { "PrivateHeaders", 14 },
256 { NULL, 0 }
259 /* Returns a pointer to a malloced string that contains the real pathname
260 to the file, given the base name and the name. */
262 static char *
263 framework_construct_pathname (const char *fname, cpp_dir *dir)
265 char *buf;
266 size_t fname_len, frname_len;
267 cpp_dir *fast_dir;
268 char *frname;
269 struct stat st;
270 int i;
272 /* Framework names must have a / in them. */
273 buf = strchr (fname, '/');
274 if (buf)
275 fname_len = buf - fname;
276 else
277 return 0;
279 fast_dir = find_framework (fname, fname_len);
281 /* Framework includes must all come from one framework. */
282 if (fast_dir && dir != fast_dir)
283 return 0;
285 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
286 + strlen(".framework/") + strlen("PrivateHeaders"));
287 strncpy (&frname[0], dir->name, dir->len);
288 frname_len = dir->len;
289 if (frname_len && frname[frname_len-1] != '/')
290 frname[frname_len++] = '/';
291 strncpy (&frname[frname_len], fname, fname_len);
292 frname_len += fname_len;
293 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
294 frname_len += strlen (".framework/");
296 if (fast_dir == 0)
298 frname[frname_len-1] = 0;
299 if (stat (frname, &st) == 0)
301 /* As soon as we find the first instance of the framework,
302 we stop and never use any later instance of that
303 framework. */
304 add_framework (fname, fname_len, dir);
306 else
308 /* If we can't find the parent directory, no point looking
309 further. */
310 free (frname);
311 return 0;
313 frname[frname_len-1] = '/';
316 /* Append framework_header_dirs and header file name */
317 for (i = 0; framework_header_dirs[i].dirName; i++)
319 strncpy (&frname[frname_len],
320 framework_header_dirs[i].dirName,
321 framework_header_dirs[i].dirNameLen);
322 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
323 &fname[fname_len]);
325 if (stat (frname, &st) == 0)
326 return frname;
329 free (frname);
330 return 0;
333 /* Search for FNAME in sub-frameworks. pname is the context that we
334 wish to search in. Return the path the file was found at,
335 otherwise return 0. */
337 static const char*
338 find_subframework_file (const char *fname, const char *pname)
340 char *sfrname;
341 const char *dot_framework = ".framework/";
342 char *bufptr;
343 int sfrname_len, i, fname_len;
344 struct cpp_dir *fast_dir;
345 static struct cpp_dir subframe_dir;
346 struct stat st;
348 bufptr = strchr (fname, '/');
350 /* Subframework files must have / in the name. */
351 if (bufptr == 0)
352 return 0;
354 fname_len = bufptr - fname;
355 fast_dir = find_framework (fname, fname_len);
357 /* Sub framework header filename includes parent framework name and
358 header name in the "CarbonCore/OSUtils.h" form. If it does not
359 include slash it is not a sub framework include. */
360 bufptr = strstr (pname, dot_framework);
362 /* If the parent header is not of any framework, then this header
363 cannot be part of any subframework. */
364 if (!bufptr)
365 return 0;
367 /* Now translate. For example, +- bufptr
368 fname = CarbonCore/OSUtils.h |
369 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
370 into
371 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
373 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
374 strlen ("Frameworks/") + strlen (".framework/")
375 + strlen ("PrivateHeaders"));
377 bufptr += strlen (dot_framework);
379 sfrname_len = bufptr - pname;
381 strncpy (&sfrname[0], pname, sfrname_len);
383 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
384 sfrname_len += strlen("Frameworks/");
386 strncpy (&sfrname[sfrname_len], fname, fname_len);
387 sfrname_len += fname_len;
389 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
390 sfrname_len += strlen (".framework/");
392 /* Append framework_header_dirs and header file name */
393 for (i = 0; framework_header_dirs[i].dirName; i++)
395 strncpy (&sfrname[sfrname_len],
396 framework_header_dirs[i].dirName,
397 framework_header_dirs[i].dirNameLen);
398 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
399 &fname[fname_len]);
401 if (stat (sfrname, &st) == 0)
403 if (fast_dir != &subframe_dir)
405 if (fast_dir)
406 warning (0, "subframework include %s conflicts with framework include",
407 fname);
408 else
409 add_framework (fname, fname_len, &subframe_dir);
412 return sfrname;
415 free (sfrname);
417 return 0;
420 /* Add PATH to the system includes. PATH must be malloc-ed and
421 NUL-terminated. System framework paths are C++ aware. */
423 static void
424 add_system_framework_path (char *path)
426 int cxx_aware = 1;
427 cpp_dir *p;
429 p = XNEW (cpp_dir);
430 p->next = NULL;
431 p->name = path;
432 p->sysp = 1 + !cxx_aware;
433 p->construct = framework_construct_pathname;
434 using_frameworks = 1;
436 add_cpp_dir_path (p, SYSTEM);
439 /* Add PATH to the bracket includes. PATH must be malloc-ed and
440 NUL-terminated. */
442 void
443 add_framework_path (char *path)
445 cpp_dir *p;
447 p = XNEW (cpp_dir);
448 p->next = NULL;
449 p->name = path;
450 p->sysp = 0;
451 p->construct = framework_construct_pathname;
452 using_frameworks = 1;
454 add_cpp_dir_path (p, BRACKET);
457 static const char *framework_defaults [] =
459 "/System/Library/Frameworks",
460 "/Library/Frameworks",
463 /* Register the GNU objective-C runtime include path if STDINC. */
465 void
466 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
467 int stdinc)
469 const char *fname;
470 size_t len;
471 /* We do not do anything if we do not want the standard includes. */
472 if (!stdinc)
473 return;
475 fname = GCC_INCLUDE_DIR "-gnu-runtime";
477 /* Register the GNU OBJC runtime include path if we are compiling OBJC
478 with GNU-runtime. */
480 if (c_dialect_objc () && !flag_next_runtime)
482 char *str;
483 /* See if our directory starts with the standard prefix.
484 "Translate" them, i.e. replace /usr/local/lib/gcc... with
485 IPREFIX and search them first. */
486 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
487 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
489 str = concat (iprefix, fname + len, NULL);
490 /* FIXME: wrap the headers for C++awareness. */
491 add_path (str, SYSTEM, /*c++aware=*/false, false);
494 /* Should this directory start with the sysroot? */
495 if (sysroot)
496 str = concat (sysroot, fname, NULL);
497 else
498 str = update_path (fname, "");
500 add_path (str, SYSTEM, /*c++aware=*/false, false);
505 /* Register all the system framework paths if STDINC is true and setup
506 the missing_header callback for subframework searching if any
507 frameworks had been registered. */
509 void
510 darwin_register_frameworks (const char *sysroot,
511 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
513 if (stdinc)
515 size_t i;
517 /* Setup default search path for frameworks. */
518 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
520 char *str;
521 if (sysroot)
522 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
523 else
524 str = xstrdup (framework_defaults[i]);
525 /* System Framework headers are cxx aware. */
526 add_system_framework_path (str);
530 if (using_frameworks)
531 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
534 /* Search for HEADER in context dependent way. The return value is
535 the malloced name of a header to try and open, if any, or NULL
536 otherwise. This is called after normal header lookup processing
537 fails to find a header. We search each file in the include stack,
538 using FUNC, starting from the most deeply nested include and
539 finishing with the main input file. We stop searching when FUNC
540 returns nonzero. */
542 static const char*
543 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
545 const char *fname = header;
546 struct cpp_buffer *b;
547 const char *n;
549 for (b = cpp_get_buffer (pfile);
550 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
551 b = cpp_get_prev (b))
553 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
554 if (n)
556 /* Logically, the place where we found the subframework is
557 the place where we found the Framework that contains the
558 subframework. This is useful for tracking wether or not
559 we are in a system header. */
560 *dirp = cpp_get_dir (cpp_get_file (b));
561 return n;
565 return 0;
568 /* Return the value of darwin_macosx_version_min suitable for the
569 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
570 so '10.4.2' becomes 1040. The lowest digit is always zero.
571 Print a warning if the version number can't be understood. */
572 static const char *
573 version_as_macro (void)
575 static char result[] = "1000";
577 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
578 goto fail;
579 if (! ISDIGIT (darwin_macosx_version_min[3]))
580 goto fail;
581 result[2] = darwin_macosx_version_min[3];
582 if (darwin_macosx_version_min[4] != '\0'
583 && darwin_macosx_version_min[4] != '.')
584 goto fail;
586 return result;
588 fail:
589 error ("Unknown value %qs of -mmacosx-version-min",
590 darwin_macosx_version_min);
591 return "1000";
594 /* Define additional CPP flags for Darwin. */
596 #define builtin_define(TXT) cpp_define (pfile, TXT)
598 void
599 darwin_cpp_builtins (cpp_reader *pfile)
601 builtin_define ("__MACH__");
602 builtin_define ("__APPLE__");
604 /* __APPLE_CC__ is defined as some old Apple include files expect it
605 to be defined and won't work if it isn't. */
606 builtin_define_with_value ("__APPLE_CC__", "1", false);
608 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
609 version_as_macro(), false);
612 /* Handle C family front-end options. */
614 static bool
615 handle_c_option (size_t code,
616 const char *arg,
617 int value ATTRIBUTE_UNUSED)
619 switch (code)
621 default:
622 /* Unrecognized options that we said we'd handle turn into
623 errors if not listed here. */
624 return false;
626 case OPT_iframework:
627 add_system_framework_path (xstrdup (arg));
628 break;
630 case OPT_fapple_kext:
634 /* We recognized the option. */
635 return true;
638 #undef TARGET_HANDLE_C_OPTION
639 #define TARGET_HANDLE_C_OPTION handle_c_option
641 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;