about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/compilers/swift/swiftpm/default.nix
blob: 4a7a4ab63cced711520a4076a50eed6c2849e87f (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
{ lib
, stdenv
, callPackage
, cmake
, ninja
, git
, swift
, swiftpm2nix
, Foundation
, XCTest
, pkg-config
, sqlite
, ncurses
, substituteAll
, runCommandLocal
, makeWrapper
, DarwinTools # sw_vers
, cctools # vtool
, xcbuild
, CryptoKit
, LocalAuthentication
}:

let

  inherit (swift) swiftOs swiftModuleSubdir swiftStaticModuleSubdir;
  sharedLibraryExt = stdenv.hostPlatform.extensions.sharedLibrary;

  sources = callPackage ../sources.nix { };
  generated = swiftpm2nix.helpers ./generated;
  cmakeGlue = callPackage ./cmake-glue.nix { };

  # Common attributes for the bootstrap swiftpm and the final swiftpm.
  commonAttrs = {
    inherit (sources) version;
    src = sources.swift-package-manager;
    nativeBuildInputs = [ makeWrapper ];
    # Required at run-time for the host platform to build package manifests.
    propagatedBuildInputs = [ Foundation ];
    patches = [
      ./patches/cmake-disable-rpath.patch
      ./patches/cmake-fix-quoting.patch
      ./patches/disable-index-store.patch
      ./patches/disable-sandbox.patch
      ./patches/disable-xctest.patch
      ./patches/fix-clang-cxx.patch
      ./patches/nix-pkgconfig-vars.patch
      (substituteAll {
        src = ./patches/fix-stdlib-path.patch;
        inherit (builtins) storeDir;
        swiftLib = swift.swift.lib;
      })
    ];
    postPatch = ''
      # The location of xcrun is hardcoded. We need PATH lookup instead.
      find Sources -name '*.swift' | xargs sed -i -e 's|/usr/bin/xcrun|xcrun|g'

      # Patch the location where swiftpm looks for its API modules.
      substituteInPlace Sources/PackageModel/UserToolchain.swift \
        --replace \
          'librariesPath = applicationPath.parentDirectory' \
          "librariesPath = AbsolutePath(\"$out\")"

      # Fix case-sensitivity issues.
      # Upstream PR: https://github.com/apple/swift-package-manager/pull/6500
      substituteInPlace Sources/CMakeLists.txt \
        --replace \
          'packageCollectionsSigning' \
          'PackageCollectionsSigning'
      substituteInPlace Sources/PackageCollectionsSigning/CMakeLists.txt \
        --replace \
          'SubjectPublickeyInfo' \
          'SubjectPublicKeyInfo'
      substituteInPlace Sources/PackageCollections/CMakeLists.txt \
        --replace \
          'FilepackageCollectionsSourcesStorage' \
          'FilePackageCollectionsSourcesStorage'
    '';
  };

  # Tools invoked by swiftpm at run-time.
  runtimeDeps = [ git ]
    ++ lib.optionals stdenv.isDarwin [
      xcbuild.xcrun
      # These tools are part of cctools, but adding that as a build input puts
      # an unwrapped linker in PATH, and breaks builds. This small derivation
      # exposes just the tools we need:
      # - vtool is used to determine a minimum deployment target.
      # - libtool is used to build static libraries.
      (runCommandLocal "swiftpm-cctools" { } ''
        mkdir -p $out/bin
        ln -s ${cctools}/bin/vtool $out/bin/vtool
        ln -s ${cctools}/bin/libtool $out/bin/libtool
      '')
    ];

  # Common attributes for the bootstrap derivations.
  mkBootstrapDerivation = attrs: stdenv.mkDerivation (attrs // {
    nativeBuildInputs = (attrs.nativeBuildInputs or [ ])
      ++ [ cmake ninja swift ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];

    buildInputs = (attrs.buildInputs or [ ])
      ++ [ Foundation ];

    postPatch = (attrs.postPatch or "")
      + lib.optionalString stdenv.isDarwin ''
        # On Darwin only, Swift uses arm64 as cpu arch.
        if [ -e cmake/modules/SwiftSupport.cmake ]; then
          substituteInPlace cmake/modules/SwiftSupport.cmake \
            --replace '"aarch64" PARENT_SCOPE' '"arm64" PARENT_SCOPE'
        fi
      '';

    preConfigure = (attrs.preConfigure or "")
      + ''
        # Builds often don't set a target, and our default minimum macOS deployment
        # target on x86_64-darwin is too low. Harmless on non-Darwin.
        export MACOSX_DEPLOYMENT_TARGET=10.15.4
      '';

    postInstall = (attrs.postInstall or "")
      + lib.optionalString stdenv.isDarwin ''
        # The install name of libraries is incorrectly set to lib/ (via our
        # CMake setup hook) instead of lib/swift/. This'd be easily fixed by
        # fixDarwinDylibNames, but some builds create libraries that reference
        # eachother, and we also have to fix those references.
        dylibs="$(find $out/lib/swift* -name '*.dylib')"
        changes=""
        for dylib in $dylibs; do
          changes+=" -change $(otool -D $dylib | tail -n 1) $dylib"
        done
        for dylib in $dylibs; do
          install_name_tool -id $dylib $changes $dylib
        done
      '';

    cmakeFlags = (attrs.cmakeFlags or [ ])
      ++ [
        # Some builds link to libraries within the same build. Make sure these
        # create references to $out. None of our builds run their own products,
        # so we don't have to account for that scenario.
        "-DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON"
      ];
  });

  # On Darwin, we only want ncurses in the linker search path, because headers
  # are part of libsystem. Adding its headers to the search path causes strange
  # mixing and errors.
  # TODO: Find a better way to prevent this conflict.
  ncursesInput = if stdenv.isDarwin then ncurses.out else ncurses;

  # Derivations for bootstrapping dependencies using CMake.
  # This is based on the `swiftpm/Utilities/bootstrap` script.
  #
  # Some of the installation steps here are a bit hacky, because it seems like
  # these packages were not really meant to be installed using CMake. The
  # regular swiftpm bootstrap simply refers to the source and build
  # directories. The advantage of separate builds is that we can more easily
  # link libs together using existing Nixpkgs infra.
  #
  # In the end, we don't expose these derivations, and they only exist during
  # the bootstrap phase. The final swiftpm derivation does not depend on them.

  swift-system = mkBootstrapDerivation {
    name = "swift-system";
    src = generated.sources.swift-system;

    postInstall = cmakeGlue.SwiftSystem
      + lib.optionalString (!stdenv.isDarwin) ''
        # The cmake rules apparently only use the Darwin install convention.
        # Fix up the installation so the module can be found on non-Darwin.
        mkdir -p $out/${swiftStaticModuleSubdir}
        mv $out/lib/swift_static/${swiftOs}/*.swiftmodule $out/${swiftStaticModuleSubdir}/
      '';
  };

  swift-collections = mkBootstrapDerivation {
    name = "swift-collections";
    src = generated.sources.swift-collections;

    postPatch = ''
      # Only builds static libs on Linux, but this installation difference is a
      # hassle. Because this installation is temporary for the bootstrap, may
      # as well build static libs everywhere.
      sed -i -e '/BUILD_SHARED_LIBS/d' CMakeLists.txt
    '';

    postInstall = cmakeGlue.SwiftCollections
      + lib.optionalString (!stdenv.isDarwin) ''
        # The cmake rules apparently only use the Darwin install convention.
        # Fix up the installation so the module can be found on non-Darwin.
        mkdir -p $out/${swiftStaticModuleSubdir}
        mv $out/lib/swift_static/${swiftOs}/*.swiftmodule $out/${swiftStaticModuleSubdir}/
      '';
  };

  swift-tools-support-core = mkBootstrapDerivation {
    name = "swift-tools-support-core";
    src = generated.sources.swift-tools-support-core;

    patches = [
      ./patches/force-unwrap-file-handles.patch
    ];

    buildInputs = [
      swift-system
      sqlite
    ];

    postInstall = cmakeGlue.TSC + ''
      # Swift modules are not installed.
      mkdir -p $out/${swiftModuleSubdir}
      cp swift/*.swift{module,doc} $out/${swiftModuleSubdir}/

      # Static libs are not installed.
      cp lib/*.a $out/lib/

      # Headers are not installed.
      mkdir -p $out/include
      cp -r ../Sources/TSCclibc/include $out/include/TSC
    '';
  };

  swift-argument-parser = mkBootstrapDerivation {
    name = "swift-argument-parser";
    src = generated.sources.swift-argument-parser;

    buildInputs = [ ncursesInput sqlite ];

    cmakeFlags = [
      "-DBUILD_TESTING=NO"
      "-DBUILD_EXAMPLES=NO"
    ];

    postInstall = cmakeGlue.ArgumentParser
      + lib.optionalString stdenv.isLinux ''
        # Fix rpath so ArgumentParserToolInfo can be found.
        patchelf --add-rpath "$out/lib/swift/${swiftOs}" \
          $out/lib/swift/${swiftOs}/libArgumentParser.so
      '';
  };

  Yams = mkBootstrapDerivation {
    name = "Yams";
    src = generated.sources.Yams;

    # Conflicts with BUILD file on case-insensitive filesystems.
    cmakeBuildDir = "_build";

    postInstall = cmakeGlue.Yams;
  };

  llbuild = mkBootstrapDerivation {
    name = "llbuild";
    src = generated.sources.swift-llbuild;

    nativeBuildInputs = lib.optional stdenv.isDarwin xcbuild;
    buildInputs = [ ncursesInput sqlite ];

    patches = [
      ./patches/llbuild-cmake-disable-rpath.patch
    ];

    postPatch = ''
      # Substitute ncurses for curses.
      find . -name CMakeLists.txt | xargs sed -i -e 's/curses/ncurses/'

      # Use absolute install names instead of rpath.
      substituteInPlace \
        products/libllbuild/CMakeLists.txt \
        products/llbuildSwift/CMakeLists.txt \
        --replace '@rpath' "$out/lib"

      # This subdirectory is enabled for Darwin only, but requires ObjC XCTest
      # (and only Swift XCTest is open source).
      substituteInPlace perftests/CMakeLists.txt \
        --replace 'add_subdirectory(Xcode/' '#add_subdirectory(Xcode/'
    '';

    cmakeFlags = [
      "-DLLBUILD_SUPPORT_BINDINGS=Swift"
    ];

    postInstall = cmakeGlue.LLBuild + ''
      # Install module map.
      cp ../products/libllbuild/include/module.modulemap $out/include

      # Swift modules are not installed.
      mkdir -p $out/${swiftModuleSubdir}
      cp products/llbuildSwift/*.swift{module,doc} $out/${swiftModuleSubdir}/
    '';
  };

  swift-driver = mkBootstrapDerivation {
    name = "swift-driver";
    src = generated.sources.swift-driver;

    buildInputs = [
      Yams
      llbuild
      swift-system
      swift-argument-parser
      swift-tools-support-core
    ];

    postPatch = ''
      # Tries to link against CYaml, but that's private.
      substituteInPlace Sources/SwiftDriver/CMakeLists.txt \
        --replace CYaml ""
    '';

    postInstall = cmakeGlue.SwiftDriver + ''
      # Swift modules are not installed.
      mkdir -p $out/${swiftModuleSubdir}
      cp swift/*.swift{module,doc} $out/${swiftModuleSubdir}/
    '';
  };

  swift-crypto = mkBootstrapDerivation {
    name = "swift-crypto";
    src = generated.sources.swift-crypto;

    postPatch = ''
      # Fix use of hardcoded tool paths on Darwin.
      substituteInPlace CMakeLists.txt \
        --replace /usr/bin/ar $NIX_CC/bin/ar
      substituteInPlace CMakeLists.txt \
        --replace /usr/bin/ranlib $NIX_CC/bin/ranlib
    '';

    postInstall = cmakeGlue.SwiftCrypto + ''
      # Static libs are not installed.
      cp lib/*.a $out/lib/

      # Headers are not installed.
      cp -r ../Sources/CCryptoBoringSSL/include $out/include
    '';
  };

  # Build a bootrapping swiftpm using CMake.
  swiftpm-bootstrap = mkBootstrapDerivation (commonAttrs // {
    pname = "swiftpm-bootstrap";

    buildInputs = [
      llbuild
      sqlite
      swift-argument-parser
      swift-collections
      swift-crypto
      swift-driver
      swift-system
      swift-tools-support-core
    ];

    cmakeFlags = [
      "-DUSE_CMAKE_INSTALL=ON"
    ];

    postInstall = ''
      for program in $out/bin/swift-*; do
        wrapProgram $program --prefix PATH : ${lib.makeBinPath runtimeDeps}
      done
    '';
  });

# Build the final swiftpm with the bootstrapping swiftpm.
in stdenv.mkDerivation (commonAttrs // {
  pname = "swiftpm";

  nativeBuildInputs = commonAttrs.nativeBuildInputs ++ [
    pkg-config
    swift
    swiftpm-bootstrap
  ];
  buildInputs = [
    ncursesInput
    sqlite
    XCTest
  ]
    ++ lib.optionals stdenv.isDarwin [
      CryptoKit
      LocalAuthentication
    ];

  configurePhase = generated.configure + ''
    # Functionality provided by Xcode XCTest, but not available in
    # swift-corelibs-xctest.
    swiftpmMakeMutable swift-tools-support-core
    substituteInPlace .build/checkouts/swift-tools-support-core/Sources/TSCTestSupport/XCTestCasePerf.swift \
      --replace 'canImport(Darwin)' 'false'
    patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch}

    # Prevent a warning about SDK directories we don't have.
    swiftpmMakeMutable swift-driver
    patch -p1 -d .build/checkouts/swift-driver -i ${substituteAll {
      src = ../swift-driver/patches/prevent-sdk-dirs-warnings.patch;
      inherit (builtins) storeDir;
    }}
  '';

  buildPhase = ''
    # Required to link with swift-corelibs-xctest on Darwin.
    export SWIFTTSC_MACOS_DEPLOYMENT_TARGET=10.12

    TERM=dumb swift-build -c release
  '';

  # TODO: Tests depend on indexstore-db being provided by an existing Swift
  # toolchain. (ie. looks for `../lib/libIndexStore.so` relative to swiftc.
  #doCheck = true;
  #checkPhase = ''
  #  TERM=dumb swift-test -c release
  #'';

  # The following is dervied from Utilities/bootstrap, see install_swiftpm.
  installPhase = ''
    binPath="$(swift-build --show-bin-path -c release)"

    mkdir -p $out/bin $out/lib/swift

    cp $binPath/swift-package-manager $out/bin/swift-package
    wrapProgram $out/bin/swift-package \
      --prefix PATH : ${lib.makeBinPath runtimeDeps}
    for tool in swift-build swift-test swift-run swift-package-collection swift-experimental-destination; do
      ln -s $out/bin/swift-package $out/bin/$tool
    done

    installSwiftpmModule() {
      mkdir -p $out/lib/swift/pm/$2
      cp $binPath/lib$1${sharedLibraryExt} $out/lib/swift/pm/$2/

      if [[ -f $binPath/$1.swiftinterface ]]; then
        cp $binPath/$1.swiftinterface $out/lib/swift/pm/$2/
      else
        cp -r $binPath/$1.swiftmodule $out/lib/swift/pm/$2/
      fi
      cp $binPath/$1.swiftdoc $out/lib/swift/pm/$2/
    }
    installSwiftpmModule PackageDescription ManifestAPI
    installSwiftpmModule PackagePlugin PluginAPI
  '';

  setupHook = ./setup-hook.sh;

  meta = {
    description = "The Package Manager for the Swift Programming Language";
    homepage = "https://github.com/apple/swift-package-manager";
    platforms = with lib.platforms; linux ++ darwin;
    license = lib.licenses.asl20;
    maintainers = with lib.maintainers; [ dtzWill trepetti dduan trundle stephank ];
  };
})