about summary refs log tree commit diff
path: root/pkgs/development/compilers/swift/default.nix
blob: 76db695b08c30289b42ef2a75b41c1c67282d842 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
{ lib
, pkgs
, newScope
, darwin
, llvmPackages
, llvmPackages_15
, overrideCC
}:

let
  swiftLlvmPackages = llvmPackages_15;

  self = rec {

    callPackage = newScope self;

    # Current versions of Swift on Darwin require macOS SDK 10.15 at least.
    # Re-export this so we can rely on the minimum Swift SDK elsewhere.
    apple_sdk = pkgs.darwin.apple_sdk_11_0;

    # Swift builds its own Clang for internal use. We wrap that clang with a
    # cc-wrapper derived from the clang configured below. Because cc-wrapper
    # applies a specific resource-root, the two versions are best matched, or
    # we'll often run into compilation errors.
    #
    # The following selects the correct Clang version, matching the version
    # used in Swift, and applies the same libc overrides as `apple_sdk.stdenv`.
    clang = if pkgs.stdenv.hostPlatform.isDarwin
      then
        swiftLlvmPackages.clang.override rec {
          libc = apple_sdk.Libsystem;
          bintools = pkgs.bintools.override { inherit libc; };
          # Ensure that Swift’s internal clang uses the same libc++ and libc++abi as the
          # default Darwin stdenv. Using the default libc++ avoids issues (such as crashes)
          # that can happen when a Swift application dynamically links different versions
          # of libc++ and libc++abi than libraries it links are using.
          inherit (llvmPackages) libcxx;
        }
      else
        swiftLlvmPackages.clang;

    # Overrides that create a useful environment for swift packages, allowing
    # packaging with `swiftPackages.callPackage`. These are similar to
    # `apple_sdk_11_0.callPackage`, with our clang on top.
    inherit (clang) bintools;
    stdenv = overrideCC pkgs.stdenv clang;
    darwin = pkgs.darwin.overrideScope (_: prev: {
      inherit apple_sdk;
      inherit (apple_sdk) Libsystem LibsystemCross libcharset libunwind objc4 configd IOKit Security;
      CF = apple_sdk.CoreFoundation // { __attrsFailEvaluation = true; };
      __attrsFailEvaluation = true;
    });
    xcodebuild = pkgs.xcbuild.override {
      inherit (apple_sdk.frameworks) CoreServices CoreGraphics ImageIO;
      inherit stdenv;
      sdkVer = "10.15";
    };
    xcbuild = xcodebuild;

    swift-unwrapped = callPackage ./compiler {
      inherit (darwin) DarwinTools sigtool;
      inherit (apple_sdk) MacOSX-SDK CLTools_Executables;
      inherit (apple_sdk.frameworks) CoreServices Foundation Combine;
    };

    swiftNoSwiftDriver = callPackage ./wrapper {
      swift = swift-unwrapped;
      useSwiftDriver = false;
    };

    Dispatch = if stdenv.hostPlatform.isDarwin
      then null # part of libsystem
      else callPackage ./libdispatch { swift = swiftNoSwiftDriver; };

    Foundation = if stdenv.hostPlatform.isDarwin
      then apple_sdk.frameworks.Foundation
      else callPackage ./foundation { swift = swiftNoSwiftDriver; };

    # TODO: Apple distributes a binary XCTest with Xcode, but it is not part of
    # CLTools (or SUS), so would have to figure out how to fetch it. The binary
    # version has several extra features, like a test runner and ObjC support.
    XCTest = callPackage ./xctest {
      inherit (darwin) DarwinTools;
      swift = swiftNoSwiftDriver;
    };

    swiftpm = callPackage ./swiftpm {
      inherit (darwin) DarwinTools;
      inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
      swift = swiftNoSwiftDriver;
    };

    swift-driver = callPackage ./swift-driver {
      swift = swiftNoSwiftDriver;
    };

    swift = callPackage ./wrapper {
      swift = swift-unwrapped;
    };

    sourcekit-lsp = callPackage ./sourcekit-lsp {
      inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
    };

    swift-docc = callPackage ./swift-docc {
      inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
    };

    swift-format = callPackage ./swift-format { };

    swiftpm2nix = callPackage ./swiftpm2nix { };

  };

in self