Skip to content

Commit 2812f69

Browse files
committed
[clang][PP] Add extension to predefine target OS macros
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 a93cacf commit 2812f69

File tree

8 files changed

+214
-0
lines changed

8 files changed

+214
-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/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.hasArg(OPT_fdefine_target_os_macros, OPT_fno_define_target_os_macros,
4471+
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
@@ -1344,6 +1344,15 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
13441344
if (TI.getTriple().isOSBinFormatELF())
13451345
Builder.defineMacro("__ELF__");
13461346

1347+
// Target OS macro definitions.
1348+
if (PPOpts.DefineTargetOSMacros) {
1349+
const llvm::Triple &Triple = TI.getTriple();
1350+
#define TARGET_OS(Name, Predicate) \
1351+
Builder.defineMacro(#Name, Predicate ? "1" : "0");
1352+
#include "clang/Basic/TargetOSMacros.def"
1353+
#undef TARGET_OS
1354+
}
1355+
13471356
// Get other target #defines.
13481357
TI.getTargetDefines(LangOpts, Builder);
13491358
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s --check-prefix=DEFAULT-OPTION
2+
3+
// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
4+
// RUN: | FileCheck %s -DMAC=1 \
5+
// RUN: -DOSX=1 \
6+
// RUN: -DIPHONE=0 \
7+
// RUN: -DIOS=0 \
8+
// RUN: -DTV=0 \
9+
// RUN: -DWATCH=0 \
10+
// RUN: -DDRIVERKIT=0 \
11+
// RUN: -DMACCATALYST=0 \
12+
// RUN: -DEMBEDDED=0 \
13+
// RUN: -DSIMULATOR=0
14+
15+
// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
16+
// RUN: | FileCheck %s -DMAC=1 \
17+
// RUN: -DOSX=0 \
18+
// RUN: -DIPHONE=1 \
19+
// RUN: -DIOS=1 \
20+
// RUN: -DTV=0 \
21+
// RUN: -DWATCH=0 \
22+
// RUN: -DDRIVERKIT=0 \
23+
// RUN: -DMACCATALYST=0 \
24+
// RUN: -DEMBEDDED=1 \
25+
// RUN: -DSIMULATOR=0
26+
27+
// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
28+
// RUN: | FileCheck %s -DMAC=1 \
29+
// RUN: -DOSX=0 \
30+
// RUN: -DIPHONE=1 \
31+
// RUN: -DIOS=1 \
32+
// RUN: -DTV=0 \
33+
// RUN: -DWATCH=0 \
34+
// RUN: -DDRIVERKIT=0 \
35+
// RUN: -DMACCATALYST=1 \
36+
// RUN: -DEMBEDDED=0 \
37+
// RUN: -DSIMULATOR=0
38+
39+
// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
40+
// RUN: | FileCheck %s -DMAC=1 \
41+
// RUN: -DOSX=0 \
42+
// RUN: -DIPHONE=1 \
43+
// RUN: -DIOS=1 \
44+
// RUN: -DTV=0 \
45+
// RUN: -DWATCH=0 \
46+
// RUN: -DDRIVERKIT=0 \
47+
// RUN: -DMACCATALYST=0 \
48+
// RUN: -DEMBEDDED=0 \
49+
// RUN: -DSIMULATOR=1
50+
51+
// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
52+
// RUN: | FileCheck %s -DMAC=1 \
53+
// RUN: -DOSX=0 \
54+
// RUN: -DIPHONE=1 \
55+
// RUN: -DIOS=0 \
56+
// RUN: -DTV=1 \
57+
// RUN: -DWATCH=0 \
58+
// RUN: -DDRIVERKIT=0 \
59+
// RUN: -DMACCATALYST=0 \
60+
// RUN: -DEMBEDDED=1 \
61+
// RUN: -DSIMULATOR=0
62+
63+
// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
64+
// RUN: | FileCheck %s -DMAC=1 \
65+
// RUN: -DOSX=0 \
66+
// RUN: -DIPHONE=1 \
67+
// RUN: -DIOS=0 \
68+
// RUN: -DTV=1 \
69+
// RUN: -DWATCH=0 \
70+
// RUN: -DDRIVERKIT=0 \
71+
// RUN: -DMACCATALYST=0 \
72+
// RUN: -DEMBEDDED=0 \
73+
// RUN: -DSIMULATOR=1
74+
75+
// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
76+
// RUN: | FileCheck %s -DMAC=1 \
77+
// RUN: -DOSX=0 \
78+
// RUN: -DIPHONE=1 \
79+
// RUN: -DIOS=0 \
80+
// RUN: -DTV=0 \
81+
// RUN: -DWATCH=1 \
82+
// RUN: -DDRIVERKIT=0 \
83+
// RUN: -DMACCATALYST=0 \
84+
// RUN: -DEMBEDDED=1 \
85+
// RUN: -DSIMULATOR=0
86+
87+
// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
88+
// RUN: | FileCheck %s -DMAC=1 \
89+
// RUN: -DOSX=0 \
90+
// RUN: -DIPHONE=1 \
91+
// RUN: -DIOS=0 \
92+
// RUN: -DTV=0 \
93+
// RUN: -DWATCH=1 \
94+
// RUN: -DDRIVERKIT=0 \
95+
// RUN: -DMACCATALYST=0 \
96+
// RUN: -DEMBEDDED=0 \
97+
// RUN: -DSIMULATOR=1
98+
99+
// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
100+
// RUN: | FileCheck %s -DMAC=1 \
101+
// RUN: -DOSX=0 \
102+
// RUN: -DIPHONE=0 \
103+
// RUN: -DIOS=0 \
104+
// RUN: -DTV=0 \
105+
// RUN: -DWATCH=0 \
106+
// RUN: -DDRIVERKIT=1 \
107+
// RUN: -DMACCATALYST=0 \
108+
// RUN: -DEMBEDDED=0 \
109+
// RUN: -DSIMULATOR=0
110+
111+
// DEFAULT-OPTION: "-fdefine-target-os-macros"
112+
113+
// CHECK-DAG: #define TARGET_OS_MAC [[MAC]]
114+
// CHECK-DAG: #define TARGET_OS_OSX [[OSX]]
115+
// CHECK-DAG: #define TARGET_OS_IPHONE [[IPHONE]]
116+
// CHECK-DAG: #define TARGET_OS_IOS [[IOS]]
117+
// CHECK-DAG: #define TARGET_OS_TV [[TV]]
118+
// CHECK-DAG: #define TARGET_OS_WATCH [[WATCH]]
119+
// CHECK-DAG: #define TARGET_OS_DRIVERKIT [[DRIVERKIT]]
120+
// CHECK-DAG: #define TARGET_OS_MACCATALYST [[MACCATALYST]]
121+
// CHECK-DAG: #define TARGET_OS_SIMULATOR [[SIMULATOR]]
122+
// Deprecated
123+
// CHECK-DAG: #define TARGET_OS_EMBEDDED [[EMBEDDED]]
124+
// CHECK-DAG: #define TARGET_OS_NANO [[WATCH]]
125+
// CHECK-DAG: #define TARGET_IPHONE_SIMULATOR [[SIMULATOR]]
126+
// CHECK-DAG: #define TARGET_OS_UIKITFORMAC [[MACCATALYST]]
127+
// Non-darwin OSes
128+
// CHECK-DAG: #define TARGET_OS_WIN32 0
129+
// CHECK-DAG: #define TARGET_OS_WINDOWS 0
130+
// CHECK-DAG: #define TARGET_OS_LINUX 0
131+
// CHECK-DAG: #define TARGET_OS_UNIX 0

0 commit comments

Comments
 (0)