1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
5 This file is part of GCC.
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)
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.
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. */
24 #include "coretypes.h"
30 #include "c-incpath.h"
35 #include "cppdefault.h"
40 #define BAD(gmsgid) do { warning (0, gmsgid); return; } while (0)
42 static bool using_frameworks = false;
44 /* Maintain a small stack of alignments. This is similar to pragma
45 pack's stack, but simpler. */
47 static void push_field_alignment (int);
48 static void pop_field_alignment (void);
49 static const char *find_subframework_file (const char *, const char *);
50 static void add_system_framework_path (char *);
51 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
54 typedef struct align_stack
57 struct align_stack * prev;
60 static struct align_stack * field_align_stack = NULL;
63 push_field_alignment (int bit_alignment)
65 align_stack *entry = XNEW (align_stack);
67 entry->alignment = maximum_field_alignment;
68 entry->prev = field_align_stack;
69 field_align_stack = entry;
71 maximum_field_alignment = bit_alignment;
75 pop_field_alignment (void)
77 if (field_align_stack)
79 align_stack *entry = field_align_stack;
81 maximum_field_alignment = entry->alignment;
82 field_align_stack = entry->prev;
86 error ("too many #pragma options align=reset");
89 /* Handlers for Darwin-specific pragmas. */
92 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
97 /* #pragma options align={mac68k|power|reset} */
100 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
105 if (pragma_lex (&t) != CPP_NAME)
106 BAD ("malformed '#pragma options', ignoring");
107 arg = IDENTIFIER_POINTER (t);
108 if (strcmp (arg, "align"))
109 BAD ("malformed '#pragma options', ignoring");
110 if (pragma_lex (&t) != CPP_EQ)
111 BAD ("malformed '#pragma options', ignoring");
112 if (pragma_lex (&t) != CPP_NAME)
113 BAD ("malformed '#pragma options', ignoring");
115 if (pragma_lex (&x) != CPP_EOF)
116 warning (0, "junk at end of '#pragma options'");
118 arg = IDENTIFIER_POINTER (t);
119 if (!strcmp (arg, "mac68k"))
120 push_field_alignment (16);
121 else if (!strcmp (arg, "power"))
122 push_field_alignment (0);
123 else if (!strcmp (arg, "reset"))
124 pop_field_alignment ();
126 warning (0, "malformed '#pragma options align={mac68k|power|reset}', ignoring");
129 /* #pragma unused ([var {, var}*]) */
132 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
137 if (pragma_lex (&x) != CPP_OPEN_PAREN)
138 BAD ("missing '(' after '#pragma unused', ignoring");
142 tok = pragma_lex (&decl);
143 if (tok == CPP_NAME && decl)
145 tree local = lookup_name (decl);
146 if (local && (TREE_CODE (local) == PARM_DECL
147 || TREE_CODE (local) == VAR_DECL))
148 TREE_USED (local) = 1;
149 tok = pragma_lex (&x);
150 if (tok != CPP_COMMA)
155 if (tok != CPP_CLOSE_PAREN)
156 BAD ("missing ')' after '#pragma unused', ignoring");
158 if (pragma_lex (&x) != CPP_EOF)
159 warning (0, "junk at end of '#pragma unused'");
162 /* Parse the ms_struct pragma. */
164 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
169 if (pragma_lex (&t) != CPP_NAME)
170 BAD ("malformed '#pragma ms_struct', ignoring");
171 arg = IDENTIFIER_POINTER (t);
173 if (!strcmp (arg, "on"))
174 darwin_ms_struct = true;
175 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
176 darwin_ms_struct = false;
178 warning (OPT_Wpragmas, "malformed '#pragma ms_struct {on|off|reset}', ignoring");
180 if (pragma_lex (&t) != CPP_EOF)
181 warning (OPT_Wpragmas, "junk at end of '#pragma ms_struct'");
188 } *frameworks_in_use;
189 static int num_frameworks = 0;
190 static int max_frameworks = 0;
193 /* Remember which frameworks have been seen, so that we can ensure
194 that all uses of that framework come from the same framework. DIR
195 is the place where the named framework NAME, which is of length
196 LEN, was found. We copy the directory name from NAME, as it will be
200 add_framework (const char *name, size_t len, cpp_dir *dir)
204 for (i = 0; i < num_frameworks; ++i)
206 if (len == frameworks_in_use[i].len
207 && strncmp (name, frameworks_in_use[i].name, len) == 0)
212 if (i >= max_frameworks)
214 max_frameworks = i*2;
215 max_frameworks += i == 0;
216 frameworks_in_use = xrealloc (frameworks_in_use,
217 max_frameworks*sizeof(*frameworks_in_use));
219 dir_name = XNEWVEC (char, len + 1);
220 memcpy (dir_name, name, len);
221 dir_name[len] = '\0';
222 frameworks_in_use[num_frameworks].name = dir_name;
223 frameworks_in_use[num_frameworks].len = len;
224 frameworks_in_use[num_frameworks].dir = dir;
228 /* Recall if we have seen the named framework NAME, before, and where
229 we saw it. NAME is LEN bytes long. The return value is the place
230 where it was seen before. */
232 static struct cpp_dir*
233 find_framework (const char *name, size_t len)
236 for (i = 0; i < num_frameworks; ++i)
238 if (len == frameworks_in_use[i].len
239 && strncmp (name, frameworks_in_use[i].name, len) == 0)
241 return frameworks_in_use[i].dir;
247 /* There are two directories in a framework that contain header files,
248 Headers and PrivateHeaders. We search Headers first as it is more
249 common to upgrade a header from PrivateHeaders to Headers and when
250 that is done, the old one might hang around and be out of data,
253 struct framework_header {const char * dirName; int dirNameLen; };
254 static struct framework_header framework_header_dirs[] = {
256 { "PrivateHeaders", 14 },
260 /* Returns a pointer to a malloced string that contains the real pathname
261 to the file, given the base name and the name. */
264 framework_construct_pathname (const char *fname, cpp_dir *dir)
267 size_t fname_len, frname_len;
273 /* Framework names must have a / in them. */
274 buf = strchr (fname, '/');
276 fname_len = buf - fname;
280 fast_dir = find_framework (fname, fname_len);
282 /* Framework includes must all come from one framework. */
283 if (fast_dir && dir != fast_dir)
286 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
287 + strlen(".framework/") + strlen("PrivateHeaders"));
288 strncpy (&frname[0], dir->name, dir->len);
289 frname_len = dir->len;
290 if (frname_len && frname[frname_len-1] != '/')
291 frname[frname_len++] = '/';
292 strncpy (&frname[frname_len], fname, fname_len);
293 frname_len += fname_len;
294 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
295 frname_len += strlen (".framework/");
299 frname[frname_len-1] = 0;
300 if (stat (frname, &st) == 0)
302 /* As soon as we find the first instance of the framework,
303 we stop and never use any later instance of that
305 add_framework (fname, fname_len, dir);
309 /* If we can't find the parent directory, no point looking
314 frname[frname_len-1] = '/';
317 /* Append framework_header_dirs and header file name */
318 for (i = 0; framework_header_dirs[i].dirName; i++)
320 strncpy (&frname[frname_len],
321 framework_header_dirs[i].dirName,
322 framework_header_dirs[i].dirNameLen);
323 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
326 if (stat (frname, &st) == 0)
334 /* Search for FNAME in sub-frameworks. pname is the context that we
335 wish to search in. Return the path the file was found at,
336 otherwise return 0. */
339 find_subframework_file (const char *fname, const char *pname)
342 const char *dot_framework = ".framework/";
344 int sfrname_len, i, fname_len;
345 struct cpp_dir *fast_dir;
346 static struct cpp_dir subframe_dir;
349 bufptr = strchr (fname, '/');
351 /* Subframework files must have / in the name. */
355 fname_len = bufptr - fname;
356 fast_dir = find_framework (fname, fname_len);
358 /* Sub framework header filename includes parent framework name and
359 header name in the "CarbonCore/OSUtils.h" form. If it does not
360 include slash it is not a sub framework include. */
361 bufptr = strstr (pname, dot_framework);
363 /* If the parent header is not of any framework, then this header
364 cannot be part of any subframework. */
368 /* Now translate. For example, +- bufptr
369 fname = CarbonCore/OSUtils.h |
370 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
372 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
374 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
375 strlen ("Frameworks/") + strlen (".framework/")
376 + strlen ("PrivateHeaders"));
378 bufptr += strlen (dot_framework);
380 sfrname_len = bufptr - pname;
382 strncpy (&sfrname[0], pname, sfrname_len);
384 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
385 sfrname_len += strlen("Frameworks/");
387 strncpy (&sfrname[sfrname_len], fname, fname_len);
388 sfrname_len += fname_len;
390 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
391 sfrname_len += strlen (".framework/");
393 /* Append framework_header_dirs and header file name */
394 for (i = 0; framework_header_dirs[i].dirName; i++)
396 strncpy (&sfrname[sfrname_len],
397 framework_header_dirs[i].dirName,
398 framework_header_dirs[i].dirNameLen);
399 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
402 if (stat (sfrname, &st) == 0)
404 if (fast_dir != &subframe_dir)
407 warning (0, "subframework include %s conflicts with framework include",
410 add_framework (fname, fname_len, &subframe_dir);
421 /* Add PATH to the system includes. PATH must be malloc-ed and
422 NUL-terminated. System framework paths are C++ aware. */
425 add_system_framework_path (char *path)
433 p->sysp = 1 + !cxx_aware;
434 p->construct = framework_construct_pathname;
435 using_frameworks = 1;
437 add_cpp_dir_path (p, SYSTEM);
440 /* Add PATH to the bracket includes. PATH must be malloc-ed and
444 add_framework_path (char *path)
452 p->construct = framework_construct_pathname;
453 using_frameworks = 1;
455 add_cpp_dir_path (p, BRACKET);
458 static const char *framework_defaults [] =
460 "/System/Library/Frameworks",
461 "/Library/Frameworks",
464 /* Register the GNU objective-C runtime include path if STDINC. */
467 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
472 /* We do not do anything if we do not want the standard includes. */
476 fname = GCC_INCLUDE_DIR "-gnu-runtime";
478 /* Register the GNU OBJC runtime include path if we are compiling OBJC
481 if (c_dialect_objc () && !flag_next_runtime)
484 /* See if our directory starts with the standard prefix.
485 "Translate" them, i.e. replace /usr/local/lib/gcc... with
486 IPREFIX and search them first. */
487 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
488 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
490 str = concat (iprefix, fname + len, NULL);
491 /* FIXME: wrap the headers for C++awareness. */
492 add_path (str, SYSTEM, /*c++aware=*/false, false);
495 /* Should this directory start with the sysroot? */
497 str = concat (sysroot, fname, NULL);
499 str = update_path (fname, "");
501 add_path (str, SYSTEM, /*c++aware=*/false, false);
506 /* Register all the system framework paths if STDINC is true and setup
507 the missing_header callback for subframework searching if any
508 frameworks had been registered. */
511 darwin_register_frameworks (const char *sysroot,
512 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
518 /* Setup default search path for frameworks. */
519 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
523 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
525 str = xstrdup (framework_defaults[i]);
526 /* System Framework headers are cxx aware. */
527 add_system_framework_path (str);
531 if (using_frameworks)
532 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
535 /* Search for HEADER in context dependent way. The return value is
536 the malloced name of a header to try and open, if any, or NULL
537 otherwise. This is called after normal header lookup processing
538 fails to find a header. We search each file in the include stack,
539 using FUNC, starting from the most deeply nested include and
540 finishing with the main input file. We stop searching when FUNC
544 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
546 const char *fname = header;
547 struct cpp_buffer *b;
550 for (b = cpp_get_buffer (pfile);
551 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
552 b = cpp_get_prev (b))
554 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
557 /* Logically, the place where we found the subframework is
558 the place where we found the Framework that contains the
559 subframework. This is useful for tracking wether or not
560 we are in a system header. */
561 *dirp = cpp_get_dir (cpp_get_file (b));
569 /* Return the value of darwin_macosx_version_min suitable for the
570 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
571 so '10.4.2' becomes 1042.
572 Print a warning if the version number is not known. */
574 version_as_macro (void)
576 static char result[] = "1000";
578 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
580 if (! ISDIGIT (darwin_macosx_version_min[3]))
582 result[2] = darwin_macosx_version_min[3];
583 if (darwin_macosx_version_min[4] != '\0')
585 if (darwin_macosx_version_min[4] != '.')
587 if (! ISDIGIT (darwin_macosx_version_min[5]))
589 if (darwin_macosx_version_min[6] != '\0')
591 result[3] = darwin_macosx_version_min[5];
599 error ("Unknown value %qs of -mmacosx-version-min",
600 darwin_macosx_version_min);
604 /* Define additional CPP flags for Darwin. */
606 #define builtin_define(TXT) cpp_define (pfile, TXT)
609 darwin_cpp_builtins (cpp_reader *pfile)
611 builtin_define ("__MACH__");
612 builtin_define ("__APPLE__");
614 /* __APPLE_CC__ is defined as some old Apple include files expect it
615 to be defined and won't work if it isn't. */
616 builtin_define_with_value ("__APPLE_CC__", "1", false);
618 if (darwin_macosx_version_min)
619 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
620 version_as_macro(), false);