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"
34 #include "cppdefault.h"
39 #define BAD(gmsgid) do { warning (0, gmsgid); return; } while (0)
41 static bool using_frameworks = false;
43 /* Maintain a small stack of alignments. This is similar to pragma
44 pack's stack, but simpler. */
46 static void push_field_alignment (int);
47 static void pop_field_alignment (void);
48 static const char *find_subframework_file (const char *, const char *);
49 static void add_system_framework_path (char *);
50 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
53 typedef struct align_stack
56 struct align_stack * prev;
59 static struct align_stack * field_align_stack = NULL;
62 push_field_alignment (int bit_alignment)
64 align_stack *entry = (align_stack *) xmalloc (sizeof (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;
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;
85 error ("too many #pragma options align=reset");
88 /* Handlers for Darwin-specific pragmas. */
91 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
96 /* #pragma options align={mac68k|power|reset} */
99 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
104 if (c_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 (c_lex (&t) != CPP_EQ)
110 BAD ("malformed '#pragma options', ignoring");
111 if (c_lex (&t) != CPP_NAME)
112 BAD ("malformed '#pragma options', ignoring");
114 if (c_lex (&x) != CPP_EOF)
115 warning (0, "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 ();
125 warning (0, "malformed '#pragma options align={mac68k|power|reset}', ignoring");
128 /* #pragma unused ([var {, var}*]) */
131 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
136 if (c_lex (&x) != CPP_OPEN_PAREN)
137 BAD ("missing '(' after '#pragma unused', ignoring");
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;
149 if (tok != CPP_COMMA)
154 if (tok != CPP_CLOSE_PAREN)
155 BAD ("missing ')' after '#pragma unused', ignoring");
157 if (c_lex (&x) != CPP_EOF)
158 warning (0, "junk at end of '#pragma unused'");
165 } *frameworks_in_use;
166 static int num_frameworks = 0;
167 static int max_frameworks = 0;
170 /* Remember which frameworks have been seen, so that we can ensure
171 that all uses of that framework come from the same framework. DIR
172 is the place where the named framework NAME, which is of length
173 LEN, was found. We copy the directory name from NAME, as it will be
177 add_framework (const char *name, size_t len, cpp_dir *dir)
181 for (i = 0; i < num_frameworks; ++i)
183 if (len == frameworks_in_use[i].len
184 && strncmp (name, frameworks_in_use[i].name, len) == 0)
189 if (i >= max_frameworks)
191 max_frameworks = i*2;
192 max_frameworks += i == 0;
193 frameworks_in_use = xrealloc (frameworks_in_use,
194 max_frameworks*sizeof(*frameworks_in_use));
196 dir_name = xmalloc (len + 1);
197 memcpy (dir_name, name, len);
198 dir_name[len] = '\0';
199 frameworks_in_use[num_frameworks].name = dir_name;
200 frameworks_in_use[num_frameworks].len = len;
201 frameworks_in_use[num_frameworks].dir = dir;
205 /* Recall if we have seen the named framework NAME, before, and where
206 we saw it. NAME is LEN bytes long. The return value is the place
207 where it was seen before. */
209 static struct cpp_dir*
210 find_framework (const char *name, size_t len)
213 for (i = 0; i < num_frameworks; ++i)
215 if (len == frameworks_in_use[i].len
216 && strncmp (name, frameworks_in_use[i].name, len) == 0)
218 return frameworks_in_use[i].dir;
224 /* There are two directories in a framework that contain header files,
225 Headers and PrivateHeaders. We search Headers first as it is more
226 common to upgrade a header from PrivateHeaders to Headers and when
227 that is done, the old one might hang around and be out of data,
230 struct framework_header {const char * dirName; int dirNameLen; };
231 static struct framework_header framework_header_dirs[] = {
233 { "PrivateHeaders", 14 },
237 /* Returns a pointer to a malloced string that contains the real pathname
238 to the file, given the base name and the name. */
241 framework_construct_pathname (const char *fname, cpp_dir *dir)
244 size_t fname_len, frname_len;
250 /* Framework names must have a / in them. */
251 buf = strchr (fname, '/');
253 fname_len = buf - fname;
257 fast_dir = find_framework (fname, fname_len);
259 /* Framework includes must all come from one framework. */
260 if (fast_dir && dir != fast_dir)
263 frname = xmalloc (strlen (fname) + dir->len + 2
264 + strlen(".framework/") + strlen("PrivateHeaders"));
265 strncpy (&frname[0], dir->name, dir->len);
266 frname_len = dir->len;
267 if (frname_len && frname[frname_len-1] != '/')
268 frname[frname_len++] = '/';
269 strncpy (&frname[frname_len], fname, fname_len);
270 frname_len += fname_len;
271 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
272 frname_len += strlen (".framework/");
276 frname[frname_len-1] = 0;
277 if (stat (frname, &st) == 0)
279 /* As soon as we find the first instance of the framework,
280 we stop and never use any later instance of that
282 add_framework (fname, fname_len, dir);
286 /* If we can't find the parent directory, no point looking
291 frname[frname_len-1] = '/';
294 /* Append framework_header_dirs and header file name */
295 for (i = 0; framework_header_dirs[i].dirName; i++)
297 strncpy (&frname[frname_len],
298 framework_header_dirs[i].dirName,
299 framework_header_dirs[i].dirNameLen);
300 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
303 if (stat (frname, &st) == 0)
311 /* Search for FNAME in sub-frameworks. pname is the context that we
312 wish to search in. Return the path the file was found at,
313 otherwise return 0. */
316 find_subframework_file (const char *fname, const char *pname)
319 const char *dot_framework = ".framework/";
321 int sfrname_len, i, fname_len;
322 struct cpp_dir *fast_dir;
323 static struct cpp_dir subframe_dir;
326 bufptr = strchr (fname, '/');
328 /* Subframework files must have / in the name. */
332 fname_len = bufptr - fname;
333 fast_dir = find_framework (fname, fname_len);
335 /* Sub framework header filename includes parent framework name and
336 header name in the "CarbonCore/OSUtils.h" form. If it does not
337 include slash it is not a sub framework include. */
338 bufptr = strstr (pname, dot_framework);
340 /* If the parent header is not of any framework, then this header
341 cannot be part of any subframework. */
345 /* Now translate. For example, +- bufptr
346 fname = CarbonCore/OSUtils.h |
347 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
349 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
351 sfrname = (char *) xmalloc (strlen (pname) + strlen (fname) + 2 +
352 strlen ("Frameworks/") + strlen (".framework/")
353 + strlen ("PrivateHeaders"));
355 bufptr += strlen (dot_framework);
357 sfrname_len = bufptr - pname;
359 strncpy (&sfrname[0], pname, sfrname_len);
361 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
362 sfrname_len += strlen("Frameworks/");
364 strncpy (&sfrname[sfrname_len], fname, fname_len);
365 sfrname_len += fname_len;
367 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
368 sfrname_len += strlen (".framework/");
370 /* Append framework_header_dirs and header file name */
371 for (i = 0; framework_header_dirs[i].dirName; i++)
373 strncpy (&sfrname[sfrname_len],
374 framework_header_dirs[i].dirName,
375 framework_header_dirs[i].dirNameLen);
376 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
379 if (stat (sfrname, &st) == 0)
381 if (fast_dir != &subframe_dir)
384 warning (0, "subframework include %s conflicts with framework include",
387 add_framework (fname, fname_len, &subframe_dir);
398 /* Add PATH to the system includes. PATH must be malloc-ed and
399 NUL-terminated. System framework paths are C++ aware. */
402 add_system_framework_path (char *path)
407 p = xmalloc (sizeof (cpp_dir));
410 p->sysp = 1 + !cxx_aware;
411 p->construct = framework_construct_pathname;
412 using_frameworks = 1;
414 add_cpp_dir_path (p, SYSTEM);
417 /* Add PATH to the bracket includes. PATH must be malloc-ed and
421 add_framework_path (char *path)
425 p = xmalloc (sizeof (cpp_dir));
429 p->construct = framework_construct_pathname;
430 using_frameworks = 1;
432 add_cpp_dir_path (p, BRACKET);
435 static const char *framework_defaults [] =
437 "/System/Library/Frameworks",
438 "/Library/Frameworks",
441 /* Register the GNU objective-C runtime include path if STDINC. */
444 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
449 /* We do not do anything if we do not want the standard includes. */
453 fname = GCC_INCLUDE_DIR "-gnu-runtime";
455 /* Register the GNU OBJC runtime include path if we are compiling OBJC
458 if (c_dialect_objc () && !flag_next_runtime)
461 /* See if our directory starts with the standard prefix.
462 "Translate" them, i.e. replace /usr/local/lib/gcc... with
463 IPREFIX and search them first. */
464 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
465 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
467 str = concat (iprefix, fname + len, NULL);
468 /* FIXME: wrap the headers for C++awareness. */
469 add_path (str, SYSTEM, /*c++aware=*/false, false);
472 /* Should this directory start with the sysroot? */
474 str = concat (sysroot, fname, NULL);
476 str = update_path (fname, "");
478 add_path (str, SYSTEM, /*c++aware=*/false, false);
483 /* Register all the system framework paths if STDINC is true and setup
484 the missing_header callback for subframework searching if any
485 frameworks had been registered. */
488 darwin_register_frameworks (const char *sysroot,
489 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
495 /* Setup default search path for frameworks. */
496 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
500 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
502 str = xstrdup (framework_defaults[i]);
503 /* System Framework headers are cxx aware. */
504 add_system_framework_path (str);
508 if (using_frameworks)
509 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
512 /* Search for HEADER in context dependent way. The return value is
513 the malloced name of a header to try and open, if any, or NULL
514 otherwise. This is called after normal header lookup processing
515 fails to find a header. We search each file in the include stack,
516 using FUNC, starting from the most deeply nested include and
517 finishing with the main input file. We stop searching when FUNC
521 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
523 const char *fname = header;
524 struct cpp_buffer *b;
527 for (b = cpp_get_buffer (pfile);
528 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
529 b = cpp_get_prev (b))
531 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
534 /* Logically, the place where we found the subframework is
535 the place where we found the Framework that contains the
536 subframework. This is useful for tracking wether or not
537 we are in a system header. */
538 *dirp = cpp_get_dir (cpp_get_file (b));
546 /* Return the value of darwin_macosx_version_min suitable for the
547 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
548 so '10.4.2' becomes 1042.
549 Print a warning if the version number is not known. */
551 version_as_macro (void)
553 static char result[] = "1000";
555 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
557 if (! ISDIGIT (darwin_macosx_version_min[3]))
559 result[2] = darwin_macosx_version_min[3];
560 if (darwin_macosx_version_min[4] != '\0')
562 if (darwin_macosx_version_min[4] != '.')
564 if (! ISDIGIT (darwin_macosx_version_min[5]))
566 if (darwin_macosx_version_min[6] != '\0')
568 result[3] = darwin_macosx_version_min[5];
576 error ("Unknown value %qs of -mmacosx-version-min",
577 darwin_macosx_version_min);
581 /* Define additional CPP flags for Darwin. */
583 #define builtin_define(TXT) cpp_define (pfile, TXT)
586 darwin_cpp_builtins (cpp_reader *pfile)
588 builtin_define ("__MACH__");
589 builtin_define ("__APPLE__");
591 /* __APPLE_CC__ is defined as some old Apple include files expect it
592 to be defined and won't work if it isn't. */
593 builtin_define_with_value ("__APPLE_CC__", "1", false);
595 if (darwin_macosx_version_min)
596 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
597 version_as_macro(), false);