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 /* True if we're setting __attribute__ ((ms_struct)). */
45 static bool darwin_ms_struct = false;
47 /* Maintain a small stack of alignments. This is similar to pragma
48 pack's stack, but simpler. */
50 static void push_field_alignment (int);
51 static void pop_field_alignment (void);
52 static const char *find_subframework_file (const char *, const char *);
53 static void add_system_framework_path (char *);
54 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
57 typedef struct align_stack
60 struct align_stack * prev;
63 static struct align_stack * field_align_stack = NULL;
66 push_field_alignment (int bit_alignment)
68 align_stack *entry = XNEW (align_stack);
70 entry->alignment = maximum_field_alignment;
71 entry->prev = field_align_stack;
72 field_align_stack = entry;
74 maximum_field_alignment = bit_alignment;
78 pop_field_alignment (void)
80 if (field_align_stack)
82 align_stack *entry = field_align_stack;
84 maximum_field_alignment = entry->alignment;
85 field_align_stack = entry->prev;
89 error ("too many #pragma options align=reset");
92 /* Handlers for Darwin-specific pragmas. */
95 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
100 /* #pragma options align={mac68k|power|reset} */
103 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
108 if (pragma_lex (&t) != CPP_NAME)
109 BAD ("malformed '#pragma options', ignoring");
110 arg = IDENTIFIER_POINTER (t);
111 if (strcmp (arg, "align"))
112 BAD ("malformed '#pragma options', ignoring");
113 if (pragma_lex (&t) != CPP_EQ)
114 BAD ("malformed '#pragma options', ignoring");
115 if (pragma_lex (&t) != CPP_NAME)
116 BAD ("malformed '#pragma options', ignoring");
118 if (pragma_lex (&x) != CPP_EOF)
119 warning (0, "junk at end of '#pragma options'");
121 arg = IDENTIFIER_POINTER (t);
122 if (!strcmp (arg, "mac68k"))
123 push_field_alignment (16);
124 else if (!strcmp (arg, "power"))
125 push_field_alignment (0);
126 else if (!strcmp (arg, "reset"))
127 pop_field_alignment ();
129 warning (0, "malformed '#pragma options align={mac68k|power|reset}', ignoring");
132 /* #pragma unused ([var {, var}*]) */
135 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
140 if (pragma_lex (&x) != CPP_OPEN_PAREN)
141 BAD ("missing '(' after '#pragma unused', ignoring");
145 tok = pragma_lex (&decl);
146 if (tok == CPP_NAME && decl)
148 tree local = lookup_name (decl);
149 if (local && (TREE_CODE (local) == PARM_DECL
150 || TREE_CODE (local) == VAR_DECL))
151 TREE_USED (local) = 1;
152 tok = pragma_lex (&x);
153 if (tok != CPP_COMMA)
158 if (tok != CPP_CLOSE_PAREN)
159 BAD ("missing ')' after '#pragma unused', ignoring");
161 if (pragma_lex (&x) != CPP_EOF)
162 warning (0, "junk at end of '#pragma unused'");
165 /* Parse the ms_struct pragma. */
167 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
172 if (pragma_lex (&t) != CPP_NAME)
173 BAD ("malformed '#pragma ms_struct', ignoring");
174 arg = IDENTIFIER_POINTER (t);
176 if (!strcmp (arg, "on"))
177 darwin_ms_struct = true;
178 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
179 darwin_ms_struct = false;
181 warning (OPT_Wpragmas, "malformed '#pragma ms_struct {on|off|reset}', ignoring");
183 if (pragma_lex (&t) != CPP_EOF)
184 warning (OPT_Wpragmas, "junk at end of '#pragma ms_struct'");
187 /* Set the darwin specific attributes on TYPE. */
189 darwin_set_default_type_attributes (tree type)
192 && TREE_CODE (type) == RECORD_TYPE)
193 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
195 TYPE_ATTRIBUTES (type));
202 } *frameworks_in_use;
203 static int num_frameworks = 0;
204 static int max_frameworks = 0;
207 /* Remember which frameworks have been seen, so that we can ensure
208 that all uses of that framework come from the same framework. DIR
209 is the place where the named framework NAME, which is of length
210 LEN, was found. We copy the directory name from NAME, as it will be
214 add_framework (const char *name, size_t len, cpp_dir *dir)
218 for (i = 0; i < num_frameworks; ++i)
220 if (len == frameworks_in_use[i].len
221 && strncmp (name, frameworks_in_use[i].name, len) == 0)
226 if (i >= max_frameworks)
228 max_frameworks = i*2;
229 max_frameworks += i == 0;
230 frameworks_in_use = xrealloc (frameworks_in_use,
231 max_frameworks*sizeof(*frameworks_in_use));
233 dir_name = XNEWVEC (char, len + 1);
234 memcpy (dir_name, name, len);
235 dir_name[len] = '\0';
236 frameworks_in_use[num_frameworks].name = dir_name;
237 frameworks_in_use[num_frameworks].len = len;
238 frameworks_in_use[num_frameworks].dir = dir;
242 /* Recall if we have seen the named framework NAME, before, and where
243 we saw it. NAME is LEN bytes long. The return value is the place
244 where it was seen before. */
246 static struct cpp_dir*
247 find_framework (const char *name, size_t len)
250 for (i = 0; i < num_frameworks; ++i)
252 if (len == frameworks_in_use[i].len
253 && strncmp (name, frameworks_in_use[i].name, len) == 0)
255 return frameworks_in_use[i].dir;
261 /* There are two directories in a framework that contain header files,
262 Headers and PrivateHeaders. We search Headers first as it is more
263 common to upgrade a header from PrivateHeaders to Headers and when
264 that is done, the old one might hang around and be out of data,
267 struct framework_header {const char * dirName; int dirNameLen; };
268 static struct framework_header framework_header_dirs[] = {
270 { "PrivateHeaders", 14 },
274 /* Returns a pointer to a malloced string that contains the real pathname
275 to the file, given the base name and the name. */
278 framework_construct_pathname (const char *fname, cpp_dir *dir)
281 size_t fname_len, frname_len;
287 /* Framework names must have a / in them. */
288 buf = strchr (fname, '/');
290 fname_len = buf - fname;
294 fast_dir = find_framework (fname, fname_len);
296 /* Framework includes must all come from one framework. */
297 if (fast_dir && dir != fast_dir)
300 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
301 + strlen(".framework/") + strlen("PrivateHeaders"));
302 strncpy (&frname[0], dir->name, dir->len);
303 frname_len = dir->len;
304 if (frname_len && frname[frname_len-1] != '/')
305 frname[frname_len++] = '/';
306 strncpy (&frname[frname_len], fname, fname_len);
307 frname_len += fname_len;
308 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
309 frname_len += strlen (".framework/");
313 frname[frname_len-1] = 0;
314 if (stat (frname, &st) == 0)
316 /* As soon as we find the first instance of the framework,
317 we stop and never use any later instance of that
319 add_framework (fname, fname_len, dir);
323 /* If we can't find the parent directory, no point looking
328 frname[frname_len-1] = '/';
331 /* Append framework_header_dirs and header file name */
332 for (i = 0; framework_header_dirs[i].dirName; i++)
334 strncpy (&frname[frname_len],
335 framework_header_dirs[i].dirName,
336 framework_header_dirs[i].dirNameLen);
337 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
340 if (stat (frname, &st) == 0)
348 /* Search for FNAME in sub-frameworks. pname is the context that we
349 wish to search in. Return the path the file was found at,
350 otherwise return 0. */
353 find_subframework_file (const char *fname, const char *pname)
356 const char *dot_framework = ".framework/";
358 int sfrname_len, i, fname_len;
359 struct cpp_dir *fast_dir;
360 static struct cpp_dir subframe_dir;
363 bufptr = strchr (fname, '/');
365 /* Subframework files must have / in the name. */
369 fname_len = bufptr - fname;
370 fast_dir = find_framework (fname, fname_len);
372 /* Sub framework header filename includes parent framework name and
373 header name in the "CarbonCore/OSUtils.h" form. If it does not
374 include slash it is not a sub framework include. */
375 bufptr = strstr (pname, dot_framework);
377 /* If the parent header is not of any framework, then this header
378 cannot be part of any subframework. */
382 /* Now translate. For example, +- bufptr
383 fname = CarbonCore/OSUtils.h |
384 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
386 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
388 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
389 strlen ("Frameworks/") + strlen (".framework/")
390 + strlen ("PrivateHeaders"));
392 bufptr += strlen (dot_framework);
394 sfrname_len = bufptr - pname;
396 strncpy (&sfrname[0], pname, sfrname_len);
398 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
399 sfrname_len += strlen("Frameworks/");
401 strncpy (&sfrname[sfrname_len], fname, fname_len);
402 sfrname_len += fname_len;
404 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
405 sfrname_len += strlen (".framework/");
407 /* Append framework_header_dirs and header file name */
408 for (i = 0; framework_header_dirs[i].dirName; i++)
410 strncpy (&sfrname[sfrname_len],
411 framework_header_dirs[i].dirName,
412 framework_header_dirs[i].dirNameLen);
413 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
416 if (stat (sfrname, &st) == 0)
418 if (fast_dir != &subframe_dir)
421 warning (0, "subframework include %s conflicts with framework include",
424 add_framework (fname, fname_len, &subframe_dir);
435 /* Add PATH to the system includes. PATH must be malloc-ed and
436 NUL-terminated. System framework paths are C++ aware. */
439 add_system_framework_path (char *path)
447 p->sysp = 1 + !cxx_aware;
448 p->construct = framework_construct_pathname;
449 using_frameworks = 1;
451 add_cpp_dir_path (p, SYSTEM);
454 /* Add PATH to the bracket includes. PATH must be malloc-ed and
458 add_framework_path (char *path)
466 p->construct = framework_construct_pathname;
467 using_frameworks = 1;
469 add_cpp_dir_path (p, BRACKET);
472 static const char *framework_defaults [] =
474 "/System/Library/Frameworks",
475 "/Library/Frameworks",
478 /* Register the GNU objective-C runtime include path if STDINC. */
481 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
486 /* We do not do anything if we do not want the standard includes. */
490 fname = GCC_INCLUDE_DIR "-gnu-runtime";
492 /* Register the GNU OBJC runtime include path if we are compiling OBJC
495 if (c_dialect_objc () && !flag_next_runtime)
498 /* See if our directory starts with the standard prefix.
499 "Translate" them, i.e. replace /usr/local/lib/gcc... with
500 IPREFIX and search them first. */
501 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
502 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
504 str = concat (iprefix, fname + len, NULL);
505 /* FIXME: wrap the headers for C++awareness. */
506 add_path (str, SYSTEM, /*c++aware=*/false, false);
509 /* Should this directory start with the sysroot? */
511 str = concat (sysroot, fname, NULL);
513 str = update_path (fname, "");
515 add_path (str, SYSTEM, /*c++aware=*/false, false);
520 /* Register all the system framework paths if STDINC is true and setup
521 the missing_header callback for subframework searching if any
522 frameworks had been registered. */
525 darwin_register_frameworks (const char *sysroot,
526 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
532 /* Setup default search path for frameworks. */
533 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
537 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
539 str = xstrdup (framework_defaults[i]);
540 /* System Framework headers are cxx aware. */
541 add_system_framework_path (str);
545 if (using_frameworks)
546 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
549 /* Search for HEADER in context dependent way. The return value is
550 the malloced name of a header to try and open, if any, or NULL
551 otherwise. This is called after normal header lookup processing
552 fails to find a header. We search each file in the include stack,
553 using FUNC, starting from the most deeply nested include and
554 finishing with the main input file. We stop searching when FUNC
558 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
560 const char *fname = header;
561 struct cpp_buffer *b;
564 for (b = cpp_get_buffer (pfile);
565 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
566 b = cpp_get_prev (b))
568 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
571 /* Logically, the place where we found the subframework is
572 the place where we found the Framework that contains the
573 subframework. This is useful for tracking wether or not
574 we are in a system header. */
575 *dirp = cpp_get_dir (cpp_get_file (b));
583 /* Return the value of darwin_macosx_version_min suitable for the
584 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
585 so '10.4.2' becomes 1042.
586 Print a warning if the version number is not known. */
588 version_as_macro (void)
590 static char result[] = "1000";
592 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
594 if (! ISDIGIT (darwin_macosx_version_min[3]))
596 result[2] = darwin_macosx_version_min[3];
597 if (darwin_macosx_version_min[4] != '\0')
599 if (darwin_macosx_version_min[4] != '.')
601 if (! ISDIGIT (darwin_macosx_version_min[5]))
603 if (darwin_macosx_version_min[6] != '\0')
605 result[3] = darwin_macosx_version_min[5];
613 error ("Unknown value %qs of -mmacosx-version-min",
614 darwin_macosx_version_min);
618 /* Define additional CPP flags for Darwin. */
620 #define builtin_define(TXT) cpp_define (pfile, TXT)
623 darwin_cpp_builtins (cpp_reader *pfile)
625 builtin_define ("__MACH__");
626 builtin_define ("__APPLE__");
628 /* __APPLE_CC__ is defined as some old Apple include files expect it
629 to be defined and won't work if it isn't. */
630 builtin_define_with_value ("__APPLE_CC__", "1", false);
632 if (darwin_macosx_version_min)
633 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
634 version_as_macro(), false);