<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 3, 2017, at 10:14, Saleem Abdulrasool via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">On Fri, Dec 1, 2017 at 2:46 PM, Bob Wilson<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:bob.wilson@apple.com" target="_blank" class="">bob.wilson@apple.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word; line-break: after-white-space;" class=""><br class=""><div class=""><span class="gmail-"><blockquote type="cite" class=""><div class="">On Dec 1, 2017, at 8:09 AM, Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org" target="_blank" class="">compnerd@compnerd.org</a>> wrote:</div><br class="gmail-m_1631216024595456930Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class="">On Thu, Nov 30, 2017 at 5:15 PM, Bob Wilson via cfe-dev<span class="gmail-m_1631216024595456930Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.<wbr class="">org</a>></span><span class="gmail-m_1631216024595456930Apple-converted-space"> </span>wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">I wondering if anyone besides Apple would be interested in having predefined macros to identify the target OS and environment.<br class=""><br class="">Background: Over the last few years, Apple has added several new platforms (tvOS and watchOS) as well as simulator variants for those, and we’ve accumulated a fair bit of complexity by adding Clang support for those platforms with a copy-and-paste approach. The -mmacosx-version-min option worked well when there was only one Apple platform, but it’s not so great now that we have a lot of -m*-version-min options. We’re trying to move toward the more standard approach of using the -target option to specify the target triple, including the OS version. Akira added support for that earlier (Clang r307982) and we’ve started moving toward identifying simulator targets via the environment field of the triple (LLVM r316380). Related to that, we also need a way to identify via predefined macros what target we’re building for. We recently added the __APPLE_EMBEDDED_SIMULATOR__ macro to distinguish the simulator targets (although it is currently not working when you use the -target option alone), but we don’t have anything to distinguish iOS vs. tvOS vs. watchOS.<br class=""><br class="">It occurred to me that we can do this in a very general way by exposing the OS and Environment fields of the target triple directly in predefined macros. (We could do the same for the Arch and Vendor fields if anyone has a use for those.)<br class=""></blockquote><div class=""><br class=""></div><div class="">+1 on this.</div><div class=""><br class=""></div><div class="">Arch is exposed, although possibly with a different spelling (e.g. `__powerpc__`). The vendor is also usually exposed in most toolchains (e.g. `__APPLE__`, or `__INTEL__` from ICC), but is not done in a generalized manner, and is something which I had intended to do as a cleanup. If you can beat me to it, great! The OS is already exposed (e.g. `__linux__`), but again suffers the problem of not being done in a generalized manner.</div></div></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">We could add these new macros only for Darwin targets, but since they are not at all Darwin-specific, what do you all think of adding them for all targets?<br class=""></blockquote><div class=""><br class=""></div><div class="">I think all the targets is pretty reasonable. This actually came up a while ago, but we never went down this path for libc++. Right now, libc++ is able to build with the itanium ABI and the Microsoft ABI on Windows. Differentiating which ABI we are using is currently not possible, but access to the environment would solve the problem.</div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">To be specific, here’s the change that I’m suggesting. (I’ll put it up for a proper review with a testcase if there is positive feedback for doing it this way.)<br class=""></blockquote><div class=""><br class=""></div><div class="">Most of the triple is made accessible, the environment is the one piece which is not, and I think it would be nice to have a uniform way of inspecting this information.</div><div class=""><br class=""></div><div class="">I believe that we could keep the existing architecture macros, uniformly expose vendor as `__<upper case vendor>__`. The OS could be exposed uniformly as `__<lower case os>__`. The environment, since there is no real precedent, we can bike shed (fuchsia seems to be a popular color these days :-p), but no strong opinions on that. I believe if we expose the environment, all 9 pieces of the triple should be visible.</div></div></div></div></div></blockquote><div class=""><br class=""></div></span>This is different than what I was proposing here, but you made me realize the fatal flaw in my proposal: there isn’t a good way to do string comparisons in preprocessor conditionals. My thought was that we could write things like:</div><div class=""><br class=""></div><div class="">#if __TARGET_ENVIRONMENT__ == simulator</div><div class=""><br class=""></div><div class="">for code that is specific to the iOS simulator (for example), but of course that doesn’t work.</div></div></blockquote><div class=""><br class=""></div><div class="">Yeah, its unfortunate that that pattern isn't really feasible.</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class="">Alternatively, we could adopt a convention of prefixing the macro names with the triple component name, e.g.:</div><div class=""><br class=""></div><div class="">__TARGET_ENVIRONMENT_<wbr class="">SIMULATOR__</div><div class="">__TARGET_OS_TVOS__</div><div class="">etc.</div><div class=""><br class=""></div><div class="">instead of the raw values:</div><div class=""><br class=""></div><div class="">__SIMULATOR__</div><div class="">__TVOS__</div><div class=""><br class=""></div><div class="">It would make sense to me to add the __TARGET-prefixed versions for all targets, but if we do the raw values I would be inclined to do them selectively for platforms that I understand and can test. I slightly prefer the prefixed versions — they give context to show that the macros are about the environment or OS. On the other hand, I suppose they would be a departure from the existing convention with macros like __APPLE__ and __ELF__.</div></div></blockquote><div class=""><br class=""></div><div class="">I actually prefer the un-prefixed versions. I can appreciate the additional context, but, it would be rather grating from the perspective of combining the old and new versions. I could be convinced for the prefixed environment macro as there isn't as strong of a precedent there (there is only android which has an environment mapping macro that I can think of).</div></div></div></div></div></blockquote><div><br class=""></div><div>I'd worry that the non-prefixed versions would stomp on macros used by headers also provided elsewhere in the implementation (e.g., system headers).</div><div><br class=""></div><div>I'm not sure I understand the concern with the prefixed versions. For x86_64-apple-ios-simulator, I imagine we'd have:</div><div>```</div><div>__X86_64__ // or whatever the current one is</div><div>__APPLE__</div><div>__TARGET_ARCH_X86_64__</div><div>__TARGET_VENDOR_APPLE__</div><div>__TARGET_OS_IOS__</div><div>__TARGET_ENVIRONMENT_SIMULATOR__</div><div>```</div><div><br class=""></div><div>I imagine legacy header code would use the old, un-prefixed macros, and new header code would use the prefixed macros. It seems unlikely that much code would have logic depending on a mix of them.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class="">Opinions?</div></div></blockquote><div class=""><br class=""></div><div class="">One other option is to provide a function-like macros to accomplish this. Similar to `__has_feature`, we could expose:</div><div class=""><br class=""></div><div class="">- `__is_auxiliary_os(os)`</div><div class="">- `__is_target_os(os)`</div><div class="">- `__is_auxiliary_environment(environment)`</div><div class="">- `__is_target_environment(environment)`</div><div class="">- `__is_auxiliary_vendor(vendor)`</div><div class="">- `__is_target_vendor(vendor)`</div><div class=""><br class=""></div><div class="">This would be cleaner overall I feel, but a much more radical departure for these cases.</div></div></div></div></div></blockquote><div><br class=""></div><div>FWIW, I like this idea quite a bit.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word; line-break: after-white-space;" class=""><div class=""><div class="gmail-h5"><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">[arch][subarch][endian]-[<wbr class="">vendor]-[os]-[environment][<wbr class="">abi][object format][version]</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">diff --git lib/Frontend/InitPreprocessor.<wbr class="">cpp lib/Frontend/InitPreprocessor.<wbr class="">cpp<br class="">index 67c1faddc3..c1dfb39c37 100644<br class="">--- lib/Frontend/InitPreprocessor.<wbr class="">cpp<br class="">+++ lib/Frontend/InitPreprocessor.<wbr class="">cpp<br class="">@@ -713,6 +713,14 @@ static void InitializePredefinedMacros(con<wbr class="">st TargetInfo &TI,<br class=""><br class=""> // Initialize target-specific preprocessor defines.<br class=""><br class="">+ // Identify the OS and Environment fields of the target triple.<br class="">+ Builder.defineMacro("__TARGET_<wbr class="">OS__",<br class="">+ TI.getTriple().getOSTypeName(T<wbr class="">I.getTriple().getOS()));<br class="">+ if (TI.getTriple().hasEnvironment<wbr class="">()) {<br class="">+ Builder.defineMacro("__TARGET_<wbr class="">ENVIRONMENT__",<br class="">+ TI.getTriple().getEnvironmentT<wbr class="">ypeName(TI.getTriple().<wbr class="">getEnvironment()));<br class="">+ }<br class="">+<br class=""> // __BYTE_ORDER__ was added in GCC 4.6. It's analogous<br class=""> // to the macro __BYTE_ORDER (no trailing underscores)<br class=""> // from glibc's <endian.h> header.<br class="">______________________________<wbr class="">_________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/<wbr class="">mailman/listinfo/cfe-dev</a><br class=""></blockquote></div><br class="">--<span class="gmail-m_1631216024595456930Apple-converted-space"> </span><br class=""><div class="gmail-m_1631216024595456930gmail_signature">Saleem Abdulrasool<br class="">compnerd (at) compnerd (dot) org</div></div></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>--<span class="Apple-converted-space"> </span><br class=""><div class="gmail_signature">Saleem Abdulrasool<br class="">compnerd (at) compnerd (dot) org</div></div></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">cfe-dev mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:cfe-dev@lists.llvm.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">cfe-dev@lists.llvm.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></div></blockquote></div><br class=""></body></html>