xref: /src/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1344a3780SDimitry Andric //===- Target.cpp -----------------------------------------------*- C++ -*-===//
21d5ae102SDimitry Andric //
31d5ae102SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41d5ae102SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
51d5ae102SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61d5ae102SDimitry Andric //
71d5ae102SDimitry Andric //===----------------------------------------------------------------------===//
81d5ae102SDimitry Andric 
9344a3780SDimitry Andric #include "llvm/TextAPI/Target.h"
101d5ae102SDimitry Andric #include "llvm/ADT/StringSwitch.h"
116f8fc217SDimitry Andric #include "llvm/ADT/Twine.h"
121d5ae102SDimitry Andric #include "llvm/Support/raw_ostream.h"
131d5ae102SDimitry Andric 
141d5ae102SDimitry Andric namespace llvm {
151d5ae102SDimitry Andric namespace MachO {
161d5ae102SDimitry Andric 
create(StringRef TargetValue)171d5ae102SDimitry Andric Expected<Target> Target::create(StringRef TargetValue) {
181d5ae102SDimitry Andric   auto Result = TargetValue.split('-');
191d5ae102SDimitry Andric   auto ArchitectureStr = Result.first;
201d5ae102SDimitry Andric   auto Architecture = getArchitectureFromName(ArchitectureStr);
211d5ae102SDimitry Andric   auto PlatformStr = Result.second;
226f8fc217SDimitry Andric   PlatformType Platform;
236f8fc217SDimitry Andric   Platform = StringSwitch<PlatformType>(PlatformStr)
24b1c73532SDimitry Andric #define PLATFORM(platform, id, name, build_name, target, tapi_target,          \
25b1c73532SDimitry Andric                  marketing)                                                    \
26b1c73532SDimitry Andric   .Case(#tapi_target, PLATFORM_##platform)
27b1c73532SDimitry Andric #include "llvm/BinaryFormat/MachO.def"
286f8fc217SDimitry Andric                  .Default(PLATFORM_UNKNOWN);
291d5ae102SDimitry Andric 
306f8fc217SDimitry Andric   if (Platform == PLATFORM_UNKNOWN) {
31312c0ed1SDimitry Andric     if (PlatformStr.starts_with("<") && PlatformStr.ends_with(">")) {
321d5ae102SDimitry Andric       PlatformStr = PlatformStr.drop_front().drop_back();
331d5ae102SDimitry Andric       unsigned long long RawValue;
341d5ae102SDimitry Andric       if (!PlatformStr.getAsInteger(10, RawValue))
356f8fc217SDimitry Andric         Platform = (PlatformType)RawValue;
361d5ae102SDimitry Andric     }
371d5ae102SDimitry Andric   }
381d5ae102SDimitry Andric 
391d5ae102SDimitry Andric   return Target{Architecture, Platform};
401d5ae102SDimitry Andric }
411d5ae102SDimitry Andric 
operator std::string() const421d5ae102SDimitry Andric Target::operator std::string() const {
437fa27ce4SDimitry Andric   auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
447fa27ce4SDimitry Andric 
457fa27ce4SDimitry Andric   return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
467fa27ce4SDimitry Andric           Version + ")")
471d5ae102SDimitry Andric       .str();
481d5ae102SDimitry Andric }
491d5ae102SDimitry Andric 
operator <<(raw_ostream & OS,const Target & Target)501d5ae102SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
511d5ae102SDimitry Andric   OS << std::string(Target);
521d5ae102SDimitry Andric   return OS;
531d5ae102SDimitry Andric }
541d5ae102SDimitry Andric 
mapToPlatformVersionSet(ArrayRef<Target> Targets)557fa27ce4SDimitry Andric PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
567fa27ce4SDimitry Andric   PlatformVersionSet Result;
577fa27ce4SDimitry Andric   for (const auto &Target : Targets)
587fa27ce4SDimitry Andric     Result.insert({Target.Platform, Target.MinDeployment});
597fa27ce4SDimitry Andric   return Result;
607fa27ce4SDimitry Andric }
617fa27ce4SDimitry Andric 
mapToPlatformSet(ArrayRef<Target> Targets)621d5ae102SDimitry Andric PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
631d5ae102SDimitry Andric   PlatformSet Result;
641d5ae102SDimitry Andric   for (const auto &Target : Targets)
651d5ae102SDimitry Andric     Result.insert(Target.Platform);
661d5ae102SDimitry Andric   return Result;
671d5ae102SDimitry Andric }
681d5ae102SDimitry Andric 
mapToArchitectureSet(ArrayRef<Target> Targets)691d5ae102SDimitry Andric ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
701d5ae102SDimitry Andric   ArchitectureSet Result;
711d5ae102SDimitry Andric   for (const auto &Target : Targets)
721d5ae102SDimitry Andric     Result.set(Target.Arch);
731d5ae102SDimitry Andric   return Result;
741d5ae102SDimitry Andric }
751d5ae102SDimitry Andric 
getTargetTripleName(const Target & Targ)76344a3780SDimitry Andric std::string getTargetTripleName(const Target &Targ) {
777fa27ce4SDimitry Andric   auto Version =
787fa27ce4SDimitry Andric       Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
797fa27ce4SDimitry Andric 
80344a3780SDimitry Andric   return (getArchitectureName(Targ.Arch) + "-apple-" +
817fa27ce4SDimitry Andric           getOSAndEnvironmentName(Targ.Platform, Version))
82344a3780SDimitry Andric       .str();
83344a3780SDimitry Andric }
84344a3780SDimitry Andric 
851d5ae102SDimitry Andric } // end namespace MachO.
861d5ae102SDimitry Andric } // end namespace llvm.
87