[llvm-ifs] Improving detection of PlatformKind from triple for TBD generation.
authorPuyan Lotfi <puyan@puyan.org>
Fri, 6 Sep 2019 19:59:59 +0000 (19:59 +0000)
committerPuyan Lotfi <puyan@puyan.org>
Fri, 6 Sep 2019 19:59:59 +0000 (19:59 +0000)
It was pointed out that I had hard-coded PlatformKind. This is rectifying that.

Differential Revision: https://reviews.llvm.org/D67255

llvm-svn: 371248

llvm/test/tools/llvm-ifs/ios-tbd.ifs [new file with mode: 0644]
llvm/test/tools/llvm-ifs/macos-tbd.ifs [new file with mode: 0644]
llvm/test/tools/llvm-ifs/tvos-tbd.ifs [new file with mode: 0644]
llvm/test/tools/llvm-ifs/watchos-tbd.ifs [new file with mode: 0644]
llvm/tools/llvm-ifs/llvm-ifs.cpp

diff --git a/llvm/test/tools/llvm-ifs/ios-tbd.ifs b/llvm/test/tools/llvm-ifs/ios-tbd.ifs
new file mode 100644 (file)
index 0000000..13671b0
--- /dev/null
@@ -0,0 +1,22 @@
+# RUN: llvm-ifs --action write-bin -o - %s | FileCheck %s
+
+# CHECK: --- !tapi-tbd-v3
+# CHECK-NEXT: archs:           [ arm64 ]
+# CHECK-NEXT: platform:        ios
+# CHECK-NEXT: flags:           [ flat_namespace, not_app_extension_safe ]
+# CHECK-NEXT: install-name:    ''
+# CHECK-NEXT: current-version: 0
+# CHECK-NEXT: compatibility-version: 0
+# CHECK-NEXT: objc-constraint: none
+# CHECK-NEXT: exports:
+# CHECK-NEXT:   - archs:           [ arm64 ]
+# CHECK-NEXT:     symbols:         [ __Z3fooi ]
+# CHECK-NEXT: ...
+
+--- !experimental-ifs-v1
+IfsVersion: 1.0
+Triple: arm64-apple-ios
+ObjectFileFormat: TBD
+Symbols:
+  __Z3fooi: { Type: Func }
+...
diff --git a/llvm/test/tools/llvm-ifs/macos-tbd.ifs b/llvm/test/tools/llvm-ifs/macos-tbd.ifs
new file mode 100644 (file)
index 0000000..bd84806
--- /dev/null
@@ -0,0 +1,22 @@
+# RUN: llvm-ifs --action write-bin -o - %s | FileCheck %s
+
+# CHECK: --- !tapi-tbd-v3
+# CHECK-NEXT: archs:           [ arm64 ]
+# CHECK-NEXT: platform:        macosx
+# CHECK-NEXT: flags:           [ flat_namespace, not_app_extension_safe ]
+# CHECK-NEXT: install-name:    ''
+# CHECK-NEXT: current-version: 0
+# CHECK-NEXT: compatibility-version: 0
+# CHECK-NEXT: objc-constraint: none
+# CHECK-NEXT: exports:
+# CHECK-NEXT:   - archs:           [ arm64 ]
+# CHECK-NEXT:     symbols:         [ __Z3fooi ]
+# CHECK-NEXT: ...
+
+--- !experimental-ifs-v1
+IfsVersion: 1.0
+Triple: arm64-apple-macosx
+ObjectFileFormat: TBD
+Symbols:
+  __Z3fooi: { Type: Func }
+...
diff --git a/llvm/test/tools/llvm-ifs/tvos-tbd.ifs b/llvm/test/tools/llvm-ifs/tvos-tbd.ifs
new file mode 100644 (file)
index 0000000..08c8478
--- /dev/null
@@ -0,0 +1,22 @@
+# RUN: llvm-ifs --action write-bin -o - %s | FileCheck %s
+
+# CHECK: --- !tapi-tbd-v3
+# CHECK-NEXT: archs:           [ arm64 ]
+# CHECK-NEXT: platform:        tvos
+# CHECK-NEXT: flags:           [ flat_namespace, not_app_extension_safe ]
+# CHECK-NEXT: install-name:    ''
+# CHECK-NEXT: current-version: 0
+# CHECK-NEXT: compatibility-version: 0
+# CHECK-NEXT: objc-constraint: none
+# CHECK-NEXT: exports:
+# CHECK-NEXT:   - archs:           [ arm64 ]
+# CHECK-NEXT:     symbols:         [ __Z3fooi ]
+# CHECK-NEXT: ...
+
+--- !experimental-ifs-v1
+IfsVersion: 1.0
+Triple: arm64-apple-tvos
+ObjectFileFormat: TBD
+Symbols:
+  __Z3fooi: { Type: Func }
+...
diff --git a/llvm/test/tools/llvm-ifs/watchos-tbd.ifs b/llvm/test/tools/llvm-ifs/watchos-tbd.ifs
new file mode 100644 (file)
index 0000000..74a9d96
--- /dev/null
@@ -0,0 +1,22 @@
+# RUN: llvm-ifs --action write-bin -o - %s | FileCheck %s
+
+# CHECK: --- !tapi-tbd-v3
+# CHECK-NEXT: archs:           [ arm64 ]
+# CHECK-NEXT: platform:        watchos
+# CHECK-NEXT: flags:           [ flat_namespace, not_app_extension_safe ]
+# CHECK-NEXT: install-name:    ''
+# CHECK-NEXT: current-version: 0
+# CHECK-NEXT: compatibility-version: 0
+# CHECK-NEXT: objc-constraint: none
+# CHECK-NEXT: exports:
+# CHECK-NEXT:   - archs:           [ arm64 ]
+# CHECK-NEXT:     symbols:         [ __Z3fooi ]
+# CHECK-NEXT: ...
+
+--- !experimental-ifs-v1
+IfsVersion: 1.0
+Triple: arm64-apple-watchos
+ObjectFileFormat: TBD
+Symbols:
+  __Z3fooi: { Type: Func }
+...
index 3377985..1746357 100644 (file)
@@ -154,8 +154,8 @@ template <> struct CustomMappingTraits<std::set<IFSSymbol>> {
       IO.mapRequired(Sym.Name.c_str(), const_cast<IFSSymbol &>(Sym));
   }
 };
-} // End yaml namespace
-} // End llvm namespace
+} // namespace yaml
+} // namespace llvm
 
 // A cumulative representation of ELF stubs.
 // Both textual and binary stubs will read into and write from this object.
@@ -196,8 +196,8 @@ template <> struct MappingTraits<IFSStub> {
     IO.mapRequired("Symbols", Stub.Symbols);
   }
 };
-} // End yaml namespace
-} // End llvm namespace
+} // namespace yaml
+} // namespace llvm
 
 static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) {
   // Read in file.
@@ -224,7 +224,7 @@ int writeTbdStub(const llvm::Triple &T, const std::set<IFSSymbol> &Symbols,
       [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::Architecture> {
     switch (T.getArch()) {
     default:
-      return createStringError(errc::not_supported, "Invalid Architecture.");
+      return createStringError(errc::not_supported, "Invalid Architecture.\n");
     case llvm::Triple::ArchType::x86:
       return AK_i386;
     case llvm::Triple::ArchType::x86_64:
@@ -236,15 +236,39 @@ int writeTbdStub(const llvm::Triple &T, const std::set<IFSSymbol> &Symbols,
     }
   }(T);
 
+  auto PlatformKindOrError =
+      [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::PlatformKind> {
+    if (T.isMacOSX())
+      return llvm::MachO::PlatformKind::macOS;
+    if (T.isTvOS())
+      return llvm::MachO::PlatformKind::tvOS;
+    if (T.isWatchOS())
+      return llvm::MachO::PlatformKind::watchOS;
+    // Note: put isiOS last because tvOS and watchOS are also iOS according
+    // to the Triple.
+    if (T.isiOS())
+      return llvm::MachO::PlatformKind::iOS;
+
+    // TODO: Add an option for ForceTriple, but keep ForceFormat for now.
+    if (ForceFormat == "TBD")
+      return llvm::MachO::PlatformKind::macOS;
+
+    return createStringError(errc::not_supported, "Invalid Platform.\n");
+  }(T);
+
   if (!ArchOrError)
     return -1;
 
+  if (!PlatformKindOrError)
+    return -1;
+
   Architecture Arch = ArchOrError.get();
+  PlatformKind Plat = PlatformKindOrError.get();
 
   InterfaceFile File;
-  File.setFileType(FileType::TBD_V3);
+  File.setFileType(FileType::TBD_V3); // Only supporting v3 for now.
   File.setArchitectures(Arch);
-  File.setPlatform(PlatformKind::macOS);
+  File.setPlatform(Plat);
 
   for (const auto &Symbol : Symbols) {
     auto Name = Symbol.Name;