Unverified Commit 15d5ce36 authored by Siddarth Kumar's avatar Siddarth Kumar
Browse files

xcodeenv: accept version & perform runtime checks

- xcodewrapper nix derivation has been updated to now accept a list of acceptable versions.
- allowHigher is now removed
- this matches closely to what we use for building react-native with nix at status-mobile repo
ref -> https://github.com/status-im/status-mobile/blob/develop/nix/pkgs/xcodeenv/compose-xcodewrapper.nix
- The key change done here is that now xcodewrapper checks Xcode versions at runtime instead of build time. This helps us to show warning messages when underlying environment does not have the Xcode version we want to support.
parent 07535c6e
Loading
Loading
Loading
Loading
+29 −21
Original line number Diff line number Diff line
{ stdenv, lib }:
{ version ? "11.1"
, allowHigher ? false
, xcodeBaseDir ? "/Applications/Xcode.app" }:
{ lib,
  stdenv,
  writeShellScriptBin }:
{ versions ? [ ] , xcodeBaseDir ? "/Applications/Xcode.app" }:

assert stdenv.isDarwin;
let
  xcodebuildPath = "${xcodeBaseDir}/Contents/Developer/usr/bin/xcodebuild";

  xcodebuildWrapper = writeShellScriptBin "xcodebuild" ''
    currentVer="$(${xcodebuildPath} -version | awk 'NR==1{print $2}')"
    wrapperVers=(${lib.concatStringsSep " " versions})

    for ver in "''${wrapperVers[@]}"; do
      if [[ "$currentVer" == "$ver" ]]; then
        # here exec replaces the shell without creating a new process
        # https://www.gnu.org/software/bash/manual/bash.html#index-exec
        exec "${xcodebuildPath}" "$@"
      fi
    done

    echo "The installed Xcode version ($currentVer) does not match any of the allowed versions: ${lib.concatStringsSep ", " versions}"
    echo "Please update your local Xcode installation to match one of the allowed versions"
    exit 1
  '';
in
stdenv.mkDerivation {
  pname = "xcode-wrapper${lib.optionalString allowHigher "-plus"}";
  inherit version;
  name = "xcode-wrapper-impure";
  # Fails in sandbox. Use `--option sandbox relaxed` or `--option sandbox false`.
  __noChroot = true;
  buildCommand = ''
    mkdir -p $out/bin
    cd $out/bin
    ln -s /usr/bin/xcode-select
    ${if versions == [ ] then ''
    ln -s "${xcodebuildPath}"
    '' else ''
    ln -s "${xcodebuildWrapper}/bin/xcode-select"
    ''}
    ln -s /usr/bin/security
    ln -s /usr/bin/codesign
    ln -s /usr/bin/xcrun
@@ -22,23 +44,9 @@ stdenv.mkDerivation {
    ln -s /usr/bin/lipo
    ln -s /usr/bin/file
    ln -s /usr/bin/rev
    ln -s "${xcodeBaseDir}/Contents/Developer/usr/bin/xcodebuild"
    ln -s "${xcodeBaseDir}/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator"

    cd ..
    ln -s "${xcodeBaseDir}/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs"

    # Check if we have the xcodebuild version that we want
    currVer=$($out/bin/xcodebuild -version | head -n1)
    ${if allowHigher then ''
    if [ -z "$(printf '%s\n' "${version}" "$currVer" | sort -V | head -n1)""" != "${version}" ]
    '' else ''
    if [ -z "$(echo $currVer | grep -x 'Xcode ${version}')" ]
    ''}
    then
        echo "We require xcodebuild version${if allowHigher then " or higher" else ""}: ${version}"
        echo "Instead what was found: $currVer"
        exit 1
    fi
  '';
}