File: Paths.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (105 lines) | stat: -rw-r--r-- 3,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// RUN: %empty-directory(%t)

// RUN: mkdir -p %t/swift-root/libexec/swift %t/swift-root/bin
// RUN: touch %t/swift-root/libexec/swift/Foo
// RUN: touch %t/swift-root/libexec/swift/Foo.exe
// RUN: touch %t/swift-root/bin/Foo.exe

// RUN: %target-clang %s -std=c++11 -I %swift_src_root/include -I %swift_obj_root/include -I %swift_src_root/stdlib/public/SwiftShims -I %clang-include-dir -isysroot %sdk -L%swift-lib-dir/swift/%target-sdk-name/%target-arch -L%llvm_obj_root/lib/swift/%target-sdk-name/%target-arch -lswiftCore -o %t/paths-test
// RUN: %target-codesign %t/paths-test
// RUN: %target-run %t/paths-test | %FileCheck %s
// RUN: env %env-SWIFT_ROOT=%t/swift-root %target-run %t/paths-test | %FileCheck %s --check-prefix CHECK-FR
// REQUIRES: executable_test

// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

// This can't be done in unittests, because that statically links the runtime
// so we get the wrong paths.  We explicitly want to test that we get the
// path we expect (that is, the path to the runtime, and paths relative to
// that).

#include "swift/Runtime/Paths.h"

#include <sys/types.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if defined(_WIN32) && !defined(__CYGWIN)
#define stat _stat
#define S_IFDIR _S_IFDIR
#endif

static bool
exists(const char *path) {
  struct stat st;

  return stat(path, &st) == 0;
}

static bool
isfile(const char *path) {
  struct stat st;

  return stat(path, &st) == 0 && !(st.st_mode & S_IFDIR);
}

static bool
isdir(const char *path) {
  struct stat st;

  return stat(path, &st) == 0 && (st.st_mode & S_IFDIR);
}

static bool
containsLibSwift(const char *path) {
  const char *posix = "/lib/swift/";
  const char *windows = "\\lib\\swift\\";

  return strstr(path, posix) || strstr(path, windows);
}

int main(void) {
  const char *runtimePath = swift_getRuntimeLibraryPath();

  // Runtime path must point to libswiftCore and must be a file.

  // CHECK: runtime path: {{.*[\\/](lib)?}}swiftCore.{{so|dylib|dll}}
  // CHECK-NEXT: runtime is a file: yes

  // CHECK-FR: runtime path: {{.*[\\/](lib)?}}swiftCore.{{so|dylib|dll}}
  // CHECK-FR-NEXT: runtime is a file: yes
  printf("runtime path: %s\n", runtimePath ? runtimePath: "<NULL>");
  printf("runtime is a file: %s\n", isfile(runtimePath) ? "yes" : "no");

  const char *rootPath = swift_getRootPath();

  // Root path must end in a separator and must be a directory

  // CHECK: root path: {{.*[\\/]$}}
  // CHECK-NEXT: root is a directory: yes

  // CHECK-FR: root path: {{.*[\\/]$}}
  // CHECK-FR-NEXT: root is a directory: yes
  printf("root path: %s\n", rootPath ? rootPath : "<NULL>");
  printf("root is a directory: %s\n", isdir(rootPath) ? "yes" : "no");

  // CHECK: root path contains /lib/swift/: no
  // CHECK-FR: root path contains /lib/swift/: no
  printf("root path contains /lib/swift/: %s\n",
         containsLibSwift(rootPath) ? "yes" : "no");

  const char *auxPath = swift_copyAuxiliaryExecutablePath("Foo");

  // CHECK: aux path: <NULL>
  // CHECK-FR: aux path: {{.*[\\/]libexec[\\/]swift[\\/]Foo(\.exe)?}}

  printf("aux path: %s\n", auxPath ? auxPath : "<NULL>");

  free((void *)auxPath);

  return 0;
}