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)
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/>. */
24 #include "coretypes.h"
29 #include "c-family/c-common.h"
30 #include "c-family/c-pragma.h"
34 #include "cppdefault.h"
37 #include "target-def.h"
41 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
42 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
44 static bool using_frameworks
= false;
46 static const char *find_subframework_header (cpp_reader
*pfile
, const char *header
,
49 typedef struct align_stack
52 struct align_stack
* prev
;
55 static struct align_stack
* field_align_stack
= NULL
;
57 /* Maintain a small stack of alignments. This is similar to pragma
58 pack's stack, but simpler. */
61 push_field_alignment (int bit_alignment
)
63 align_stack
*entry
= XNEW (align_stack
);
65 entry
->alignment
= maximum_field_alignment
;
66 entry
->prev
= field_align_stack
;
67 field_align_stack
= entry
;
69 maximum_field_alignment
= bit_alignment
;
73 pop_field_alignment (void)
75 if (field_align_stack
)
77 align_stack
*entry
= field_align_stack
;
79 maximum_field_alignment
= entry
->alignment
;
80 field_align_stack
= entry
->prev
;
84 error ("too many #pragma options align=reset");
87 /* Handlers for Darwin-specific pragmas. */
90 darwin_pragma_ignore (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
95 /* #pragma options align={mac68k|power|reset} */
98 darwin_pragma_options (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
103 if (pragma_lex (&t
) != CPP_NAME
)
104 BAD ("malformed '#pragma options', ignoring");
105 arg
= IDENTIFIER_POINTER (t
);
106 if (strcmp (arg
, "align"))
107 BAD ("malformed '#pragma options', ignoring");
108 if (pragma_lex (&t
) != CPP_EQ
)
109 BAD ("malformed '#pragma options', ignoring");
110 if (pragma_lex (&t
) != CPP_NAME
)
111 BAD ("malformed '#pragma options', ignoring");
113 if (pragma_lex (&x
) != CPP_EOF
)
114 warning (OPT_Wpragmas
, "junk at end of '#pragma options'");
116 arg
= IDENTIFIER_POINTER (t
);
117 if (!strcmp (arg
, "mac68k"))
118 push_field_alignment (16);
119 else if (!strcmp (arg
, "power"))
120 push_field_alignment (0);
121 else if (!strcmp (arg
, "reset"))
122 pop_field_alignment ();
124 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
127 /* #pragma unused ([var {, var}*]) */
130 darwin_pragma_unused (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
135 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
136 BAD ("missing '(' after '#pragma unused', ignoring");
140 tok
= pragma_lex (&decl
);
141 if (tok
== CPP_NAME
&& decl
)
143 tree local
= lookup_name (decl
);
144 if (local
&& (TREE_CODE (local
) == PARM_DECL
145 || TREE_CODE (local
) == VAR_DECL
))
147 TREE_USED (local
) = 1;
148 DECL_READ_P (local
) = 1;
150 tok
= pragma_lex (&x
);
151 if (tok
!= CPP_COMMA
)
156 if (tok
!= CPP_CLOSE_PAREN
)
157 BAD ("missing ')' after '#pragma unused', ignoring");
159 if (pragma_lex (&x
) != CPP_EOF
)
160 BAD ("junk at end of '#pragma unused'");
163 /* Parse the ms_struct pragma. */
165 darwin_pragma_ms_struct (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
170 if (pragma_lex (&t
) != CPP_NAME
)
171 BAD ("malformed '#pragma ms_struct', ignoring");
172 arg
= IDENTIFIER_POINTER (t
);
174 if (!strcmp (arg
, "on"))
175 darwin_ms_struct
= true;
176 else if (!strcmp (arg
, "off") || !strcmp (arg
, "reset"))
177 darwin_ms_struct
= false;
179 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
181 if (pragma_lex (&t
) != CPP_EOF
)
182 BAD ("junk at end of '#pragma ms_struct'");
185 static struct frameworks_in_use
{
189 } *frameworks_in_use
;
190 static int num_frameworks
= 0;
191 static int max_frameworks
= 0;
194 /* Remember which frameworks have been seen, so that we can ensure
195 that all uses of that framework come from the same framework. DIR
196 is the place where the named framework NAME, which is of length
197 LEN, was found. We copy the directory name from NAME, as it will be
201 add_framework (const char *name
, size_t len
, cpp_dir
*dir
)
205 for (i
= 0; i
< num_frameworks
; ++i
)
207 if (len
== frameworks_in_use
[i
].len
208 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
213 if (i
>= max_frameworks
)
215 max_frameworks
= i
*2;
216 max_frameworks
+= i
== 0;
217 frameworks_in_use
= XRESIZEVEC (struct frameworks_in_use
,
218 frameworks_in_use
, max_frameworks
);
220 dir_name
= XNEWVEC (char, len
+ 1);
221 memcpy (dir_name
, name
, len
);
222 dir_name
[len
] = '\0';
223 frameworks_in_use
[num_frameworks
].name
= dir_name
;
224 frameworks_in_use
[num_frameworks
].len
= len
;
225 frameworks_in_use
[num_frameworks
].dir
= dir
;
229 /* Recall if we have seen the named framework NAME, before, and where
230 we saw it. NAME is LEN bytes long. The return value is the place
231 where it was seen before. */
233 static struct cpp_dir
*
234 find_framework (const char *name
, size_t len
)
237 for (i
= 0; i
< num_frameworks
; ++i
)
239 if (len
== frameworks_in_use
[i
].len
240 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
242 return frameworks_in_use
[i
].dir
;
248 /* There are two directories in a framework that contain header files,
249 Headers and PrivateHeaders. We search Headers first as it is more
250 common to upgrade a header from PrivateHeaders to Headers and when
251 that is done, the old one might hang around and be out of data,
254 struct framework_header
{const char * dirName
; int dirNameLen
; };
255 static struct framework_header framework_header_dirs
[] = {
257 { "PrivateHeaders", 14 },
261 /* Returns a pointer to a malloced string that contains the real pathname
262 to the file, given the base name and the name. */
265 framework_construct_pathname (const char *fname
, cpp_dir
*dir
)
268 size_t fname_len
, frname_len
;
274 /* Framework names must have a / in them. */
275 buf
= strchr (fname
, '/');
277 fname_len
= buf
- fname
;
281 fast_dir
= find_framework (fname
, fname_len
);
283 /* Framework includes must all come from one framework. */
284 if (fast_dir
&& dir
!= fast_dir
)
287 frname
= XNEWVEC (char, strlen (fname
) + dir
->len
+ 2
288 + strlen(".framework/") + strlen("PrivateHeaders"));
289 strncpy (&frname
[0], dir
->name
, dir
->len
);
290 frname_len
= dir
->len
;
291 if (frname_len
&& frname
[frname_len
-1] != '/')
292 frname
[frname_len
++] = '/';
293 strncpy (&frname
[frname_len
], fname
, fname_len
);
294 frname_len
+= fname_len
;
295 strncpy (&frname
[frname_len
], ".framework/", strlen (".framework/"));
296 frname_len
+= strlen (".framework/");
300 frname
[frname_len
-1] = 0;
301 if (stat (frname
, &st
) == 0)
303 /* As soon as we find the first instance of the framework,
304 we stop and never use any later instance of that
306 add_framework (fname
, fname_len
, dir
);
310 /* If we can't find the parent directory, no point looking
315 frname
[frname_len
-1] = '/';
318 /* Append framework_header_dirs and header file name */
319 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
321 strncpy (&frname
[frname_len
],
322 framework_header_dirs
[i
].dirName
,
323 framework_header_dirs
[i
].dirNameLen
);
324 strcpy (&frname
[frname_len
+ framework_header_dirs
[i
].dirNameLen
],
327 if (stat (frname
, &st
) == 0)
335 /* Search for FNAME in sub-frameworks. pname is the context that we
336 wish to search in. Return the path the file was found at,
337 otherwise return 0. */
340 find_subframework_file (const char *fname
, const char *pname
)
343 const char *dot_framework
= ".framework/";
345 int sfrname_len
, i
, fname_len
;
346 struct cpp_dir
*fast_dir
;
347 static struct cpp_dir subframe_dir
;
350 bufptr
= strchr (fname
, '/');
352 /* Subframework files must have / in the name. */
356 fname_len
= bufptr
- fname
;
357 fast_dir
= find_framework (fname
, fname_len
);
359 /* Sub framework header filename includes parent framework name and
360 header name in the "CarbonCore/OSUtils.h" form. If it does not
361 include slash it is not a sub framework include. */
362 bufptr
= strstr (pname
, dot_framework
);
364 /* If the parent header is not of any framework, then this header
365 cannot be part of any subframework. */
369 /* Now translate. For example, +- bufptr
370 fname = CarbonCore/OSUtils.h |
371 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
373 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
375 sfrname
= XNEWVEC (char, strlen (pname
) + strlen (fname
) + 2 +
376 strlen ("Frameworks/") + strlen (".framework/")
377 + strlen ("PrivateHeaders"));
379 bufptr
+= strlen (dot_framework
);
381 sfrname_len
= bufptr
- pname
;
383 strncpy (&sfrname
[0], pname
, sfrname_len
);
385 strncpy (&sfrname
[sfrname_len
], "Frameworks/", strlen ("Frameworks/"));
386 sfrname_len
+= strlen("Frameworks/");
388 strncpy (&sfrname
[sfrname_len
], fname
, fname_len
);
389 sfrname_len
+= fname_len
;
391 strncpy (&sfrname
[sfrname_len
], ".framework/", strlen (".framework/"));
392 sfrname_len
+= strlen (".framework/");
394 /* Append framework_header_dirs and header file name */
395 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
397 strncpy (&sfrname
[sfrname_len
],
398 framework_header_dirs
[i
].dirName
,
399 framework_header_dirs
[i
].dirNameLen
);
400 strcpy (&sfrname
[sfrname_len
+ framework_header_dirs
[i
].dirNameLen
],
403 if (stat (sfrname
, &st
) == 0)
405 if (fast_dir
!= &subframe_dir
)
408 warning (0, "subframework include %s conflicts with framework include",
411 add_framework (fname
, fname_len
, &subframe_dir
);
422 /* Add PATH to the system includes. PATH must be malloc-ed and
423 NUL-terminated. System framework paths are C++ aware. */
426 add_system_framework_path (char *path
)
434 p
->sysp
= 1 + !cxx_aware
;
435 p
->construct
= framework_construct_pathname
;
436 using_frameworks
= 1;
438 add_cpp_dir_path (p
, SYSTEM
);
441 /* Add PATH to the bracket includes. PATH must be malloc-ed and
445 add_framework_path (char *path
)
453 p
->construct
= framework_construct_pathname
;
454 using_frameworks
= 1;
456 add_cpp_dir_path (p
, BRACKET
);
459 static const char *framework_defaults
[] =
461 "/System/Library/Frameworks",
462 "/Library/Frameworks",
465 /* Register the GNU objective-C runtime include path if STDINC. */
468 darwin_register_objc_includes (const char *sysroot
, const char *iprefix
,
473 /* We do not do anything if we do not want the standard includes. */
477 fname
= GCC_INCLUDE_DIR
"-gnu-runtime";
479 /* Register the GNU OBJC runtime include path if we are compiling OBJC
482 if (c_dialect_objc () && !flag_next_runtime
)
485 /* See if our directory starts with the standard prefix.
486 "Translate" them, i.e. replace /usr/local/lib/gcc... with
487 IPREFIX and search them first. */
488 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0 && !sysroot
489 && !strncmp (fname
, cpp_GCC_INCLUDE_DIR
, len
))
491 str
= concat (iprefix
, fname
+ len
, NULL
);
492 /* FIXME: wrap the headers for C++awareness. */
493 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
496 /* Should this directory start with the sysroot? */
498 str
= concat (sysroot
, fname
, NULL
);
500 str
= update_path (fname
, "");
502 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
507 /* Register all the system framework paths if STDINC is true and setup
508 the missing_header callback for subframework searching if any
509 frameworks had been registered. */
512 darwin_register_frameworks (const char *sysroot
,
513 const char *iprefix ATTRIBUTE_UNUSED
, int stdinc
)
519 /* Setup default search path for frameworks. */
520 for (i
=0; i
<sizeof (framework_defaults
)/sizeof(const char *); ++i
)
524 str
= concat (sysroot
, xstrdup (framework_defaults
[i
]), NULL
);
526 str
= xstrdup (framework_defaults
[i
]);
527 /* System Framework headers are cxx aware. */
528 add_system_framework_path (str
);
532 if (using_frameworks
)
533 cpp_get_callbacks (parse_in
)->missing_header
= find_subframework_header
;
536 /* Search for HEADER in context dependent way. The return value is
537 the malloced name of a header to try and open, if any, or NULL
538 otherwise. This is called after normal header lookup processing
539 fails to find a header. We search each file in the include stack,
540 using FUNC, starting from the most deeply nested include and
541 finishing with the main input file. We stop searching when FUNC
545 find_subframework_header (cpp_reader
*pfile
, const char *header
, cpp_dir
**dirp
)
547 const char *fname
= header
;
548 struct cpp_buffer
*b
;
551 for (b
= cpp_get_buffer (pfile
);
552 b
&& cpp_get_file (b
) && cpp_get_path (cpp_get_file (b
));
553 b
= cpp_get_prev (b
))
555 n
= find_subframework_file (fname
, cpp_get_path (cpp_get_file (b
)));
558 /* Logically, the place where we found the subframework is
559 the place where we found the Framework that contains the
560 subframework. This is useful for tracking wether or not
561 we are in a system header. */
562 *dirp
= cpp_get_dir (cpp_get_file (b
));
570 /* Return the value of darwin_macosx_version_min suitable for the
571 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
572 so '10.4.2' becomes 1040. The lowest digit is always zero.
573 Print a warning if the version number can't be understood. */
575 version_as_macro (void)
577 static char result
[] = "1000";
579 if (strncmp (darwin_macosx_version_min
, "10.", 3) != 0)
581 if (! ISDIGIT (darwin_macosx_version_min
[3]))
583 result
[2] = darwin_macosx_version_min
[3];
584 if (darwin_macosx_version_min
[4] != '\0'
585 && darwin_macosx_version_min
[4] != '.')
591 error ("Unknown value %qs of -mmacosx-version-min",
592 darwin_macosx_version_min
);
596 /* Define additional CPP flags for Darwin. */
598 #define builtin_define(TXT) cpp_define (pfile, TXT)
601 darwin_cpp_builtins (cpp_reader
*pfile
)
603 builtin_define ("__MACH__");
604 builtin_define ("__APPLE__");
606 /* __APPLE_CC__ is defined as some old Apple include files expect it
607 to be defined and won't work if it isn't. */
608 builtin_define_with_value ("__APPLE_CC__", "1", false);
610 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
611 version_as_macro(), false);
614 /* Handle C family front-end options. */
617 handle_c_option (size_t code
,
619 int value ATTRIBUTE_UNUSED
)
624 /* Unrecognized options that we said we'd handle turn into
625 errors if not listed here. */
629 add_system_framework_path (xstrdup (arg
));
632 case OPT_fapple_kext
:
636 /* We recognized the option. */
640 #undef TARGET_HANDLE_C_OPTION
641 #define TARGET_HANDLE_C_OPTION handle_c_option
643 struct gcc_targetcm targetcm
= TARGETCM_INITIALIZER
;