Skip to content

Commit 6e1f191

Browse files
authored
[clang][PP] Add extension to predefine target OS macros (#74676)
Add an extension feature `define-target-os-macros` that enables clang to provide definitions of common TARGET_OS_* conditional macros. The extension is enabled in the Darwin toolchain driver.
1 parent c502a81 commit 6e1f191

File tree

9 files changed

+327
-0
lines changed

9 files changed

+327
-0
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ FEATURE(blocks, LangOpts.Blocks)
8989
FEATURE(c_thread_safety_attributes, true)
9090
FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
9191
FEATURE(cxx_rtti, LangOpts.RTTI &&LangOpts.RTTIData)
92+
EXTENSION(define_target_os_macros,
93+
PP.getPreprocessorOpts().DefineTargetOSMacros)
9294
FEATURE(enumerator_attributes, true)
9395
FEATURE(nullability, true)
9496
FEATURE(nullability_on_arrays, true)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===--- TargetOSMacros.def - Target OS macros ------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file specifies the predefined TARGET_OS_* conditional macros.
10+
// A target macro `Name` should be defined if `Predicate` evaluates to true.
11+
// The macro expects `const llvm::Triple &Triple` and the class `llvm::Triple`
12+
// to be available for the predicate.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef TARGET_OS
17+
#define TARGET_OS(Name, Predicate)
18+
#endif
19+
20+
// Windows targets.
21+
TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
22+
TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
23+
24+
// Linux target.
25+
TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
26+
27+
// Unix target.
28+
TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
29+
Triple.isOSFreeBSD() ||
30+
Triple.isOSOpenBSD() ||
31+
Triple.isOSSolaris())
32+
33+
// Apple (Mac) targets.
34+
TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
35+
TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
36+
TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
37+
Triple.isWatchOS())
38+
// Triple::isiOS() also includes tvOS
39+
TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
40+
TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
41+
TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
42+
TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
43+
TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
44+
TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
45+
46+
// Deprecated Apple target conditionals.
47+
TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
48+
|| Triple.isWatchOS()) \
49+
&& !Triple.isMacCatalystEnvironment() \
50+
&& !Triple.isSimulatorEnvironment())
51+
TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
52+
TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
53+
TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
54+
55+
#undef TARGET_OS

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,9 @@ def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Gr
18181818
Visibility<[ClangOption, CC1Option]>,
18191819
HelpText<"Treat each comma separated argument in <arg> as a documentation comment block command">,
18201820
MetaVarName<"<arg>">, MarshallingInfoStringVector<LangOpts<"CommentOpts.BlockCommandNames">>;
1821+
defm define_target_os_macros : OptInCC1FFlag<"define-target-os-macros",
1822+
"Enable", "Disable", " predefined target OS macros",
1823+
[ClangOption, CC1Option]>;
18211824
def fparse_all_comments : Flag<["-"], "fparse-all-comments">, Group<f_clang_Group>,
18221825
Visibility<[ClangOption, CC1Option]>,
18231826
MarshallingInfoFlag<LangOpts<"CommentOpts.ParseAllComments">>;

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class PreprocessorOptions {
7676
/// predefines.
7777
bool UsePredefines = true;
7878

79+
/// Indicates whether to predefine target OS macros.
80+
bool DefineTargetOSMacros = false;
81+
7982
/// Whether we should maintain a detailed record of all macro
8083
/// definitions and expansions.
8184
bool DetailedRecord = false;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12941294
CmdArgs.push_back("-source-date-epoch");
12951295
CmdArgs.push_back(Args.MakeArgString(Epoch));
12961296
}
1297+
1298+
Args.addOptInFlag(CmdArgs, options::OPT_fdefine_target_os_macros,
1299+
options::OPT_fno_define_target_os_macros);
12971300
}
12981301

12991302
// FIXME: Move to target hook.

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,6 +2916,10 @@ void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
29162916
// to fix the same problem with C++ headers, and is generally fragile.
29172917
if (!sdkSupportsBuiltinModules(TargetPlatform, SDKInfo))
29182918
CC1Args.push_back("-fbuiltin-headers-in-system-modules");
2919+
2920+
if (!DriverArgs.hasArgNoClaim(options::OPT_fdefine_target_os_macros,
2921+
options::OPT_fno_define_target_os_macros))
2922+
CC1Args.push_back("-fdefine-target-os-macros");
29192923
}
29202924

29212925
void Darwin::addClangCC1ASTargetOptions(

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4365,6 +4365,9 @@ static void GeneratePreprocessorArgs(const PreprocessorOptions &Opts,
43654365
if (Opts.SourceDateEpoch)
43664366
GenerateArg(Consumer, OPT_source_date_epoch, Twine(*Opts.SourceDateEpoch));
43674367

4368+
if (Opts.DefineTargetOSMacros)
4369+
GenerateArg(Consumer, OPT_fdefine_target_os_macros);
4370+
43684371
// Don't handle LexEditorPlaceholders. It is implied by the action that is
43694372
// generated elsewhere.
43704373
}
@@ -4463,6 +4466,10 @@ static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
44634466
if (isStrictlyPreprocessorAction(Action))
44644467
Opts.LexEditorPlaceholders = false;
44654468

4469+
Opts.DefineTargetOSMacros =
4470+
Args.hasFlag(OPT_fdefine_target_os_macros,
4471+
OPT_fno_define_target_os_macros, Opts.DefineTargetOSMacros);
4472+
44664473
return Diags.getNumErrors() == NumErrorsBefore;
44674474
}
44684475

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,15 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
13511351
if (TI.getTriple().isOSBinFormatELF())
13521352
Builder.defineMacro("__ELF__");
13531353

1354+
// Target OS macro definitions.
1355+
if (PPOpts.DefineTargetOSMacros) {
1356+
const llvm::Triple &Triple = TI.getTriple();
1357+
#define TARGET_OS(Name, Predicate) \
1358+
Builder.defineMacro(#Name, (Predicate) ? "1" : "0");
1359+
#include "clang/Basic/TargetOSMacros.def"
1360+
#undef TARGET_OS
1361+
}
1362+
13541363
// Get other target #defines.
13551364
TI.getTargetDefines(LangOpts, Builder);
13561365
}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s --check-prefix=DARWIN-DEFAULT
2+
// DARWIN-DEFAULT: "-fdefine-target-os-macros"
3+
4+
// RUN: %clang -### --target=arm-none-linux-gnu %s 2>&1 | FileCheck %s --check-prefix=NON-DARWIN-DEFAULT
5+
// RUN: %clang -### --target=x86_64-pc-win32 %s 2>&1 | FileCheck %s --check-prefix=NON-DARWIN-DEFAULT
6+
// NON-DARWIN-DEFAULT-NOT: "-fdefine-target-os-macros"
7+
8+
// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
9+
// RUN: | FileCheck %s -DMAC=1 \
10+
// RUN: -DOSX=1 \
11+
// RUN: -DIPHONE=0 \
12+
// RUN: -DIOS=0 \
13+
// RUN: -DTV=0 \
14+
// RUN: -DWATCH=0 \
15+
// RUN: -DDRIVERKIT=0 \
16+
// RUN: -DMACCATALYST=0 \
17+
// RUN: -DEMBEDDED=0 \
18+
// RUN: -DSIMULATOR=0 \
19+
// RUN: -DWINDOWS=0 \
20+
// RUN: -DLINUX=0 \
21+
// RUN: -DUNIX=0
22+
23+
// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
24+
// RUN: | FileCheck %s -DMAC=1 \
25+
// RUN: -DOSX=0 \
26+
// RUN: -DIPHONE=1 \
27+
// RUN: -DIOS=1 \
28+
// RUN: -DTV=0 \
29+
// RUN: -DWATCH=0 \
30+
// RUN: -DDRIVERKIT=0 \
31+
// RUN: -DMACCATALYST=0 \
32+
// RUN: -DEMBEDDED=1 \
33+
// RUN: -DSIMULATOR=0 \
34+
// RUN: -DWINDOWS=0 \
35+
// RUN: -DLINUX=0 \
36+
// RUN: -DUNIX=0
37+
38+
// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
39+
// RUN: | FileCheck %s -DMAC=1 \
40+
// RUN: -DOSX=0 \
41+
// RUN: -DIPHONE=1 \
42+
// RUN: -DIOS=1 \
43+
// RUN: -DTV=0 \
44+
// RUN: -DWATCH=0 \
45+
// RUN: -DDRIVERKIT=0 \
46+
// RUN: -DMACCATALYST=1 \
47+
// RUN: -DEMBEDDED=0 \
48+
// RUN: -DSIMULATOR=0 \
49+
// RUN: -DWINDOWS=0 \
50+
// RUN: -DLINUX=0 \
51+
// RUN: -DUNIX=0
52+
53+
// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
54+
// RUN: | FileCheck %s -DMAC=1 \
55+
// RUN: -DOSX=0 \
56+
// RUN: -DIPHONE=1 \
57+
// RUN: -DIOS=1 \
58+
// RUN: -DTV=0 \
59+
// RUN: -DWATCH=0 \
60+
// RUN: -DDRIVERKIT=0 \
61+
// RUN: -DMACCATALYST=0 \
62+
// RUN: -DEMBEDDED=0 \
63+
// RUN: -DSIMULATOR=1 \
64+
// RUN: -DWINDOWS=0 \
65+
// RUN: -DLINUX=0 \
66+
// RUN: -DUNIX=0
67+
68+
// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
69+
// RUN: | FileCheck %s -DMAC=1 \
70+
// RUN: -DOSX=0 \
71+
// RUN: -DIPHONE=1 \
72+
// RUN: -DIOS=0 \
73+
// RUN: -DTV=1 \
74+
// RUN: -DWATCH=0 \
75+
// RUN: -DDRIVERKIT=0 \
76+
// RUN: -DMACCATALYST=0 \
77+
// RUN: -DEMBEDDED=1 \
78+
// RUN: -DSIMULATOR=0 \
79+
// RUN: -DWINDOWS=0 \
80+
// RUN: -DLINUX=0 \
81+
// RUN: -DUNIX=0
82+
83+
// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
84+
// RUN: | FileCheck %s -DMAC=1 \
85+
// RUN: -DOSX=0 \
86+
// RUN: -DIPHONE=1 \
87+
// RUN: -DIOS=0 \
88+
// RUN: -DTV=1 \
89+
// RUN: -DWATCH=0 \
90+
// RUN: -DDRIVERKIT=0 \
91+
// RUN: -DMACCATALYST=0 \
92+
// RUN: -DEMBEDDED=0 \
93+
// RUN: -DSIMULATOR=1 \
94+
// RUN: -DWINDOWS=0 \
95+
// RUN: -DLINUX=0 \
96+
// RUN: -DUNIX=0
97+
98+
// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
99+
// RUN: | FileCheck %s -DMAC=1 \
100+
// RUN: -DOSX=0 \
101+
// RUN: -DIPHONE=1 \
102+
// RUN: -DIOS=0 \
103+
// RUN: -DTV=0 \
104+
// RUN: -DWATCH=1 \
105+
// RUN: -DDRIVERKIT=0 \
106+
// RUN: -DMACCATALYST=0 \
107+
// RUN: -DEMBEDDED=1 \
108+
// RUN: -DSIMULATOR=0 \
109+
// RUN: -DWINDOWS=0 \
110+
// RUN: -DLINUX=0 \
111+
// RUN: -DUNIX=0
112+
113+
// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
114+
// RUN: | FileCheck %s -DMAC=1 \
115+
// RUN: -DOSX=0 \
116+
// RUN: -DIPHONE=1 \
117+
// RUN: -DIOS=0 \
118+
// RUN: -DTV=0 \
119+
// RUN: -DWATCH=1 \
120+
// RUN: -DDRIVERKIT=0 \
121+
// RUN: -DMACCATALYST=0 \
122+
// RUN: -DEMBEDDED=0 \
123+
// RUN: -DSIMULATOR=1 \
124+
// RUN: -DWINDOWS=0 \
125+
// RUN: -DLINUX=0 \
126+
// RUN: -DUNIX=0
127+
128+
// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
129+
// RUN: | FileCheck %s -DMAC=1 \
130+
// RUN: -DOSX=0 \
131+
// RUN: -DIPHONE=0 \
132+
// RUN: -DIOS=0 \
133+
// RUN: -DTV=0 \
134+
// RUN: -DWATCH=0 \
135+
// RUN: -DDRIVERKIT=1 \
136+
// RUN: -DMACCATALYST=0 \
137+
// RUN: -DEMBEDDED=0 \
138+
// RUN: -DSIMULATOR=0 \
139+
// RUN: -DWINDOWS=0 \
140+
// RUN: -DLINUX=0 \
141+
// RUN: -DUNIX=0
142+
143+
// RUN: %clang -dM -E --target=x86_64-pc-linux-gnu \
144+
// RUN: -fdefine-target-os-macros %s 2>&1 \
145+
// RUN: | FileCheck %s -DMAC=0 \
146+
// RUN: -DOSX=0 \
147+
// RUN: -DIPHONE=0 \
148+
// RUN: -DIOS=0 \
149+
// RUN: -DTV=0 \
150+
// RUN: -DWATCH=0 \
151+
// RUN: -DDRIVERKIT=0 \
152+
// RUN: -DMACCATALYST=0 \
153+
// RUN: -DEMBEDDED=0 \
154+
// RUN: -DSIMULATOR=0 \
155+
// RUN: -DWINDOWS=0 \
156+
// RUN: -DLINUX=1 \
157+
// RUN: -DUNIX=0
158+
159+
// RUN: %clang -dM -E --target=x86_64-pc-win32 \
160+
// RUN: -fdefine-target-os-macros %s 2>&1 \
161+
// RUN: | FileCheck %s -DMAC=0 \
162+
// RUN: -DOSX=0 \
163+
// RUN: -DIPHONE=0 \
164+
// RUN: -DIOS=0 \
165+
// RUN: -DTV=0 \
166+
// RUN: -DWATCH=0 \
167+
// RUN: -DDRIVERKIT=0 \
168+
// RUN: -DMACCATALYST=0 \
169+
// RUN: -DEMBEDDED=0 \
170+
// RUN: -DSIMULATOR=0 \
171+
// RUN: -DWINDOWS=1 \
172+
// RUN: -DLINUX=0 \
173+
// RUN: -DUNIX=0
174+
175+
// RUN: %clang -dM -E --target=x86_64-pc-windows-gnu \
176+
// RUN: -fdefine-target-os-macros %s 2>&1 \
177+
// RUN: | FileCheck %s -DMAC=0 \
178+
// RUN: -DOSX=0 \
179+
// RUN: -DIPHONE=0 \
180+
// RUN: -DIOS=0 \
181+
// RUN: -DTV=0 \
182+
// RUN: -DWATCH=0 \
183+
// RUN: -DDRIVERKIT=0 \
184+
// RUN: -DMACCATALYST=0 \
185+
// RUN: -DEMBEDDED=0 \
186+
// RUN: -DSIMULATOR=0 \
187+
// RUN: -DWINDOWS=1 \
188+
// RUN: -DLINUX=0 \
189+
// RUN: -DUNIX=0
190+
191+
// RUN: %clang -dM -E --target=sparc-none-solaris \
192+
// RUN: -fdefine-target-os-macros %s 2>&1 \
193+
// RUN: | FileCheck %s -DMAC=0 \
194+
// RUN: -DOSX=0 \
195+
// RUN: -DIPHONE=0 \
196+
// RUN: -DIOS=0 \
197+
// RUN: -DTV=0 \
198+
// RUN: -DWATCH=0 \
199+
// RUN: -DDRIVERKIT=0 \
200+
// RUN: -DMACCATALYST=0 \
201+
// RUN: -DEMBEDDED=0 \
202+
// RUN: -DSIMULATOR=0 \
203+
// RUN: -DWINDOWS=0 \
204+
// RUN: -DLINUX=0 \
205+
// RUN: -DUNIX=1
206+
207+
// RUN: %clang -dM -E --target=arm64-apple-macos \
208+
// RUN: -fno-define-target-os-macros %s 2>&1 \
209+
// RUN: | FileCheck %s --check-prefix=NEG
210+
211+
// RUN: %clang -dM -E --target=arm64-apple-macos \
212+
// RUN: -fdefine-target-os-macros \
213+
// RUN: -fno-define-target-os-macros %s 2>&1 \
214+
// RUN: | FileCheck %s --check-prefix=NEG
215+
216+
// RUN: %clang -dM -E --target=x86_64-pc-windows \
217+
// RUN: -fdefine-target-os-macros \
218+
// RUN: -fno-define-target-os-macros %s 2>&1 \
219+
// RUN: | FileCheck %s --check-prefix=NEG
220+
221+
// NEG-NOT: #define TARGET_OS_
222+
223+
// CHECK-DAG: #define TARGET_OS_MAC [[MAC]]
224+
// CHECK-DAG: #define TARGET_OS_OSX [[OSX]]
225+
// CHECK-DAG: #define TARGET_OS_IPHONE [[IPHONE]]
226+
// CHECK-DAG: #define TARGET_OS_IOS [[IOS]]
227+
// CHECK-DAG: #define TARGET_OS_TV [[TV]]
228+
// CHECK-DAG: #define TARGET_OS_WATCH [[WATCH]]
229+
// CHECK-DAG: #define TARGET_OS_DRIVERKIT [[DRIVERKIT]]
230+
// CHECK-DAG: #define TARGET_OS_MACCATALYST [[MACCATALYST]]
231+
// CHECK-DAG: #define TARGET_OS_SIMULATOR [[SIMULATOR]]
232+
// Deprecated
233+
// CHECK-DAG: #define TARGET_OS_EMBEDDED [[EMBEDDED]]
234+
// CHECK-DAG: #define TARGET_OS_NANO [[WATCH]]
235+
// CHECK-DAG: #define TARGET_IPHONE_SIMULATOR [[SIMULATOR]]
236+
// CHECK-DAG: #define TARGET_OS_UIKITFORMAC [[MACCATALYST]]
237+
// Non-darwin OSes
238+
// CHECK-DAG: #define TARGET_OS_WIN32 [[WINDOWS]]
239+
// CHECK-DAG: #define TARGET_OS_WINDOWS [[WINDOWS]]
240+
// CHECK-DAG: #define TARGET_OS_LINUX [[LINUX]]
241+
// CHECK-DAG: #define TARGET_OS_UNIX [[UNIX]]

0 commit comments

Comments
 (0)