xcbuild: refactor

This reworks some of xcbuild logic to make it more compatible with
Apple’s SDK.

- Add a fake version of xcrun & xcode-select
- Cleanup platform generation. Clang does not like having 20 char
  hashes in sysroot so it is much easier to just build the parent
  directory for each runCommand. This is a little awkward but I have
  renamed everything with an added ‘s’ to make the distinction more clear.
- Cleaned up wrapper.nix in some different ways
- Reuse some versioning logic so that we don’t end up with two
  different versions of Xcode or SDK reported.
This commit is contained in:
Matthew Bauer 2018-07-06 18:49:38 -04:00
parent fec309aee4
commit 35b2c0edf2
5 changed files with 199 additions and 142 deletions

View file

@ -1,13 +1,13 @@
{ runCommand, lib, sdk, platformName, writeText }:
{ runCommand, lib, sdks, xcodePlatform, writeText }:
let
inherit (lib.generators) toPlist;
Info = {
CFBundleIdentifier = platformName;
CFBundleIdentifier = "com.apple.platform.${lib.toLower xcodePlatform}";
Type = "Platform";
Name = "macosx";
Name = lib.toLower xcodePlatform;
};
Version = {
@ -285,14 +285,18 @@ let
in
runCommand "MacOSX.platform" {} ''
install -D ${writeText "Info.plist" (toPlist {} Info)} $out/Info.plist
install -D ${writeText "version.plist" (toPlist {} Version)} $out/version.plist
install -D ${writeText "Architectures.xcspec" (toPlist {} Architectures)} $out/Developer/Library/Xcode/Specifications/Architectures.xcspec
install -D ${writeText "PackageTypes.xcspec" (toPlist {} PackageTypes)} $out/Developer/Library/Xcode/Specifications/PackageTypes.xcspec
install -D ${writeText "ProductTypes.xcspec" (toPlist {} ProductTypes)} $out/Developer/Library/Xcode/Specifications/ProductTypes.xcspec
runCommand "Platforms" {} ''
platform=$out/${xcodePlatform}.platform
mkdir -p $out/Developer/SDKs/
cd $out/Developer/SDKs/
cp -r ${sdk} ${sdk.name}
install -D ${writeText "Info.plist" (toPlist {} Info)} $platform/Info.plist
install -D ${writeText "version.plist" (toPlist {} Version)} $platform/version.plist
install -D ${writeText "Architectures.xcspec" (toPlist {} Architectures)} $platform/Developer/Library/Xcode/Specifications/Architectures.xcspec
install -D ${writeText "PackageTypes.xcspec" (toPlist {} PackageTypes)} $platform/Developer/Library/Xcode/Specifications/PackageTypes.xcspec
install -D ${writeText "ProductTypes.xcspec" (toPlist {} ProductTypes)} $platform/Developer/Library/Xcode/Specifications/ProductTypes.xcspec
# per-platform bins go here
mkdir -p $platform/usr/bin
mkdir -p $platform/Developer
ln -s ${sdks} $platform/Developer/SDKs
''

View file

@ -1,11 +1,8 @@
{ runCommand, lib, toolchainName, sdkName, writeText }:
{ runCommand, lib, toolchainName, sdkName, writeText, version, xcodePlatform }:
let
inherit (lib.generators) toPlist;
# TODO: expose MACOSX_DEPLOYMENT_TARGET in nix so we can use it here.
version = "10.10";
SDKSettings = {
CanonicalName = sdkName;
DisplayName = sdkName;
@ -21,9 +18,11 @@ let
};
in
runCommand "MacOSX${version}.sdk" {
runCommand "SDKs" {
inherit version;
} ''
install -D ${writeText "SDKSettings.plist" (toPlist {} SDKSettings)} $out/SDKSettings.plist
install -D ${writeText "SystemVersion.plist" (toPlist {} SystemVersion)} $out/System/Library/CoreServices/SystemVersion.plist
sdk=$out/${sdkName}.sdk
install -D ${writeText "SDKSettings.plist" (toPlist {} SDKSettings)} $sdk/SDKSettings.plist
install -D ${writeText "SystemVersion.plist" (toPlist {} SystemVersion)} $sdk/System/Library/CoreServices/SystemVersion.plist
ln -s $sdk $out/${xcodePlatform}.sdk
''

View file

@ -1,69 +0,0 @@
{ runCommand, toolchainName, fetchurl, makeWrapper, stdenv
, buildPackages, lib, writeText }:
let
inherit (lib) getBin optionalString;
inherit (lib.generators) toPlist;
ToolchainInfo = {
Identifier = toolchainName;
};
# We could pull this out of developer_cmds but it adds an annoying
# loop if we want to bootstrap and this is just a tiny script so I'm
# not going to bother.
mkdep-darwin-src = fetchurl {
url = "https://opensource.apple.com/source/developer_cmds/developer_cmds-63/mkdep/mkdep.sh";
sha256 = "0n4wpqfslfjs5zbys5yri8pfi2awyhlmknsf6laa5jzqbzq9x541";
executable = true;
};
in
runCommand "nixpkgs.xctoolchain" {
nativeBuildInputs = [ makeWrapper ];
} (''
mkdir -p $out
install -D ${writeText "ToolchainInfo.plist" (toPlist {} ToolchainInfo)} $out/ToolchainInfo.plist
mkdir -p $out/usr/include
mkdir -p $out/usr/lib
mkdir -p $out/usr/libexec
mkdir -p $out/usr/share
mkdir -p $out/usr/bin
for bin in ${getBin stdenv.cc}/bin/*; do
ln -s $bin $out/usr/bin
done
for bin in ${getBin stdenv.cc.bintools.bintools}/bin/*; do
if ! [ -e "$out/usr/bin/$(basename $bin)" ]; then
ln -s $bin $out/usr/bin
fi
done
ln -s ${buildPackages.yacc}/bin/yacc $out/usr/bin/yacc
ln -s ${buildPackages.yacc}/bin/bison $out/usr/bin/bison
ln -s ${buildPackages.flex}/bin/flex $out/usr/bin/flex
ln -s ${buildPackages.flex}/bin/flex++ $out/usr/bin/flex++
ln -s $out/bin/flex $out/usr/bin/lex
ln -s ${buildPackages.m4}/bin/m4 $out/usr/bin/m4
ln -s $out/usr/bin/m4 $out/usr/bin/gm4
ln -s ${buildPackages.unifdef}/bin/unifdef $out/usr/bin/unifdef
ln -s ${buildPackages.unifdef}/bin/unifdefall $out/usr/bin/unifdefall
ln -s ${buildPackages.gperf}/bin/gperf $out/usr/bin/gperf
ln -s ${buildPackages.indent}/bin/indent $out/usr/bin/indent
ln -s ${buildPackages.ctags}/bin/ctags $out/usr/bin/ctags
'' + optionalString stdenv.isDarwin ''
for bin in ${getBin buildPackages.darwin.cctools}/bin/*; do
if ! [ -e "$out/usr/bin/$(basename $bin)" ]; then
ln -s $bin $out/usr/bin
fi
done
ln -s ${buildPackages.darwin.bootstrap_cmds}/bin/mig $out/usr/bin
ln -s ${mkdep-darwin-src} $out/usr/bin/mkdep
'')

View file

@ -0,0 +1,71 @@
{ runCommand, toolchainName, fetchurl, makeWrapper, stdenv
, buildPackages, lib, writeText }:
let
inherit (lib) getBin optionalString;
inherit (lib.generators) toPlist;
ToolchainInfo = {
Identifier = toolchainName;
};
# We could pull this out of developer_cmds but it adds an annoying
# loop if we want to bootstrap and this is just a tiny script so I'm
# not going to bother.
mkdep-darwin-src = fetchurl {
url = "https://opensource.apple.com/source/developer_cmds/developer_cmds-63/mkdep/mkdep.sh";
sha256 = "0n4wpqfslfjs5zbys5yri8pfi2awyhlmknsf6laa5jzqbzq9x541";
executable = true;
};
in
runCommand "Toolchains" {
nativeBuildInputs = [ makeWrapper ];
} (''
toolchain=$out/XcodeDefault.xctoolchain
mkdir -p $toolchain
install -D ${writeText "ToolchainInfo.plist" (toPlist {} ToolchainInfo)} $toolchain/ToolchainInfo.plist
mkdir -p $toolchain/usr/include
mkdir -p $toolchain/usr/lib
mkdir -p $toolchain/usr/libexec
mkdir -p $toolchain/usr/share
mkdir -p $toolchain/usr/bin
for bin in ${getBin stdenv.cc}/bin/*; do
ln -s $bin $toolchain/usr/bin
done
for bin in ${getBin stdenv.cc.bintools.bintools}/bin/*; do
if ! [ -e "$toolchain/usr/bin/$(basename $bin)" ]; then
ln -s $bin $toolchain/usr/bin
fi
done
ln -s ${buildPackages.yacc}/bin/yacc $toolchain/usr/bin/yacc
ln -s ${buildPackages.yacc}/bin/bison $toolchain/usr/bin/bison
ln -s ${buildPackages.flex}/bin/flex $toolchain/usr/bin/flex
ln -s ${buildPackages.flex}/bin/flex++ $toolchain/usr/bin/flex++
ln -s $toolchain/bin/flex $toolchain/usr/bin/lex
ln -s ${buildPackages.m4}/bin/m4 $toolchain/usr/bin/m4
ln -s $toolchain/usr/bin/m4 $toolchain/usr/bin/gm4
ln -s ${buildPackages.unifdef}/bin/unifdef $toolchain/usr/bin/unifdef
ln -s ${buildPackages.unifdef}/bin/unifdefall $toolchain/usr/bin/unifdefall
ln -s ${buildPackages.gperf}/bin/gperf $toolchain/usr/bin/gperf
ln -s ${buildPackages.indent}/bin/indent $toolchain/usr/bin/indent
ln -s ${buildPackages.ctags}/bin/ctags $toolchain/usr/bin/ctags
'' + optionalString stdenv.isDarwin ''
for bin in ${getBin buildPackages.darwin.cctools}/bin/*; do
if ! [ -e "$toolchain/usr/bin/$(basename $bin)" ]; then
ln -s $bin $toolchain/usr/bin
fi
done
ln -s ${buildPackages.darwin.bootstrap_cmds}/bin/mig $toolchain/usr/bin
ln -s ${mkdep-darwin-src} $toolchain/usr/bin/mkdep
'')

View file

@ -1,80 +1,132 @@
{ stdenv, buildPackages, makeWrapper, writeText, runCommand
, CoreServices, ImageIO, CoreGraphics }:
{ stdenv, lib, buildPackages, makeWrapper, writeText, runCommand
, CoreServices, ImageIO, CoreGraphics
, targetPlatform
, xcodePlatform ? targetPlatform.xcodePlatform or "MacOSX"
, xcodeVer ? targetPlatform.xcodeVer or "9.4.1"
, sdkVer ? targetPlatform.sdkVer or "10.10" }:
let
inherit (lib) toLower;
toolchainName = "com.apple.dt.toolchain.XcodeDefault";
platformName = "com.apple.platform.macosx";
sdkName = "macosx10.10";
sdkName = "${xcodePlatform}${sdkVer}";
# TODO: expose MACOSX_DEPLOYMENT_TARGET in nix so we can use it here.
sdkBuildVersion = "17E189";
xcodeSelectVersion = "2349";
xcbuild = buildPackages.callPackage ./default.nix {
inherit CoreServices ImageIO CoreGraphics;
};
toolchain = buildPackages.callPackage ./toolchain.nix {
toolchains = buildPackages.callPackage ./toolchains.nix {
inherit toolchainName;
};
sdk = buildPackages.callPackage ./sdk.nix {
inherit toolchainName sdkName;
sdks = buildPackages.callPackage ./sdks.nix {
inherit toolchainName sdkName xcodePlatform;
version = sdkVer;
};
platform = buildPackages.callPackage ./platform.nix {
inherit sdk platformName;
platforms = buildPackages.callPackage ./platforms.nix {
inherit sdks xcodePlatform;
};
xcconfig = writeText "nix.xcconfig" ''
SDKROOT=${sdkName}
'';
in
stdenv.mkDerivation {
name = "xcbuild-wrapper-${xcbuild.version}";
nativeBuildInputs = [ makeWrapper ];
setupHook = ./setup-hook.sh;
phases = [ "installPhase" "fixupPhase" ];
installPhase = ''
mkdir -p $out/bin
for file in ${xcbuild}/bin/*; do
ln -s $file $out/bin
done
mkdir -p $out/usr
ln -s $out/bin $out/usr/bin
mkdir -p $out/Library/Xcode
ln -s ${xcbuild}/Library/Xcode/Specifications $out/Library/Xcode/Specifications
mkdir -p $out/Platforms
ln -s ${platform} $out/Platforms/nixpkgs.platform
mkdir -p $out/Toolchains
ln -s ${toolchain} $out/Toolchains/nixpkgs.xctoolchain
wrapProgram $out/bin/xcodebuild \
--add-flags "-xcconfig ${xcconfig}" \
--add-flags "DERIVED_DATA_DIR=." \
--set DEVELOPER_DIR "$out" \
--set SDKROOT ${sdkName}
wrapProgram $out/bin/xcrun \
--set DEVELOPER_DIR "$out" \
--set SDKROOT ${sdkName}
wrapProgram $out/bin/xcode-select \
--set DEVELOPER_DIR "$out" \
--set SDKROOT ${sdkName}
xcode-select = writeText "xcode-select" ''
#!/usr/bin/env sh
while [ $# -gt 0 ]; do
case "$1" in
-h | --help) ;; # noop
-s | --switch) shift;; # noop
-r | --reset) ;; # noop
-v | --version) echo xcode-select version ${xcodeSelectVersion} ;;
-p | --print-path) echo @DEVELOPER_DIR@ ;;
--install) ;; # noop
esac
shift
done
'';
xcrun = writeText "xcrun" ''
#!/usr/bin/env sh
while [ $# -gt 0 ]; do
case "$1" in
--sdk | -sdk) shift ;;
--find | -find)
shift
command -v $1 ;;
--log | -log) ;; # noop
--verbose | -verbose) ;; # noop
--no-cache | -no-cache) ;; # noop
--kill-cache | -kill-cache) ;; # noop
--show-sdk-path | -show-sdk-path)
echo ${sdks}/${sdkName}.sdk ;;
--show-sdk-platform-path | -show-sdk-platform-path)
echo ${platforms}/${xcodePlatform}.platform ;;
--show-sdk-version | -show-sdk-version)
echo ${sdkVer} ;;
--show-sdk-build-version | -show-sdk-build-version)
echo ${sdkBuildVersion} ;;
*) break ;;
esac
shift
done
if ! [[ -z "$@" ]]; then
exec "$@"
fi
'';
in
runCommand "xcodebuild-${xcbuild.version}" {
nativeBuildInputs = [ makeWrapper ];
inherit (xcbuild) meta;
passthru = {
raw = xcbuild;
};
# ensure that the toolchain goes in PATH
propagatedBuildInputs = [ "${toolchains}/XcodeDefault.xctoolchain/usr" ];
passthru = { inherit xcbuild; };
preferLocalBuild = true;
}
} ''
mkdir -p $out/bin
mkdir -p $out/usr
ln -s $out/bin $out/usr/bin
mkdir -p $out/Library/Xcode
ln -s ${xcbuild}/Library/Xcode/Specifications $out/Library/Xcode/Specifications
ln -s ${platforms} $out/Platforms
ln -s ${toolchains} $out/Toolchains
makeWrapper ${xcbuild}/bin/xcodebuild $out/bin/xcodebuild \
--add-flags "-xcconfig ${xcconfig}" \
--add-flags "DERIVED_DATA_DIR=." \
--set DEVELOPER_DIR "$out" \
--set SDKROOT ${sdkName} \
--run '[ "$1" = "-version" ] && (echo Xcode ${xcodeVer}; echo Build version ${sdkBuildVersion}) && exit 0'
substitute ${xcode-select} $out/bin/xcode-select \
--subst-var-by DEVELOPER_DIR $out
chmod +x $out/bin/xcode-select
substitute ${xcrun} $out/bin/xcrun
chmod +x $out/bin/xcrun
for bin in PlistBuddy actool builtin-copy builtin-copyPlist \
builtin-copyStrings builtin-copyTiff \
builtin-embeddedBinaryValidationUtility \
builtin-infoPlistUtility builtin-lsRegisterURL \
builtin-productPackagingUtility builtin-validationUtility \
lsbom plutil; do
ln -s ${xcbuild}/bin/$bin $out/bin/$bin
done
fixupPhase
''