nixpkgs/pkgs/development/compilers/ios-cross-compile/9.2_builder.sh
Maximilian Bosch 8bc5104a6e
treewide: refactor .attrs.sh detection
When specifying the `builder` attribute in `stdenv.mkDerivation`, this
will be effectively transformed into

    builtins.derivation {
      builder = stdenv.shell;
      args = [ "-e" builder ];
    }

This also means that `default-builder.sh` is never sourced and as a
result it's not guaranteed that `$NIX_ATTRS_SH_FILE` is set to a correct
location[1].

Also, we need to source `.attrs.sh` to source `$stdenv`. So, the
following is done now:

* If `$NIX_ATTRS_SH_FILE` points to a correct location, then use it.
  Directly using `.attrs.sh` is problematic for `nix-shell(1)` usage
  (see previous commit for more context), so prefer the environment
  variable if possible.

* Otherwise, if `.attrs.sh` exists, then use it. See [1] for when this
  can happen.

* If neither applies, it can be assumed that `__structuredAttrs` is
  turned off and thus nothing needs to be done.

[1] It's possible that it doesn't exist at all - in case of Nix 2.3 or
    it can point to a wrong location on older Nix versions with a bug in
    `__structuredAttrs`.
2023-10-04 18:36:57 +02:00

155 lines
3.5 KiB
Bash

# -*- shell-script -*-
if [ -e "$NIX_ATTRS_SH_FILE" ]; then . "$NIX_ATTRS_SH_FILE"; elif [ -f .attrs.sh ]; then . .attrs.sh; fi
source $stdenv/setup
function extract
{
printf "extracting $(basename $1) ...\n"
local tarflags="xf"
case "$1" in
*.tar.xz)
xz -dc $1 | tar "$tarflags" - ;;
*)
printf "Make sure you give a iPhoneOS9.2.sdk.tar.xz file \n" ;;
esac
}
function verify_arch {
case "$1" in
# Our good arches.
armv7|arm64) ;;
*)
local
acc="armv7 | arm64"
error_message=$(
printf '%s is not an acceptable arch. Try one of %s' "$1" "$acc"
)
printf "$error_message\n"
exit
esac
}
function verify_sdk_version {
sdk_version=$(basename "$1" | grep -P -o "[0-9].[0-9]+")
case "$sdk_version" in
# Make sure the SDK is correct.
[5-9].[0-9]) ;;
*)
printf 'No iPhone SDK version in file name\n'
esac
}
function do_build {
if [ $# -lt 2 ]; then
printf "usage: $0 iPhoneOS.sdk.tar* <target cpu>\n" 1>&2
printf "i.e. $0 /path/to/iPhoneOS.sdk.tar.xz armv7\n" 1>&2
exit 1
fi
mkdir -p $out
chmod -R 755 "$cctools_port"
pushd "$cctools_port"/usage_examples/ios_toolchain &> /dev/null
export LC_ALL=C
local
triple='%s-apple-darwin11'
target_dir="$PWD/target"
sdk_dir="$target_dir/SDK"
platform="$(uname -s)"
# Will be mutated by verify_sdk_version
sdk_version=
mkdir -p "$target_dir"
mkdir -p "$target_dir/bin"
mkdir -p "$sdk_dir"
verify_arch "$2"
verify_sdk_version "$1"
triple="$(printf "$triple" "$2")"
pushd "$sdk_dir" &>/dev/null
extract "$1"
local sys_lib=$(
find $sdk_dir -name libSystem.dylib -o -name libSystem.tbd | head -n1
)
if [ -z "$sys_lib" ]; then
printf "SDK should contain libSystem{.dylib,.tbd}\n" 1>&2
exit 1
fi
local sys_root=$(readlink -f "$(dirname $sys_lib)/../..")
local sdk_unboxed=$(basename $sys_root)
mv -f "$sys_root"/* "$sdk_dir" || true
popd &>/dev/null
printf "\nbuilding wrapper\n"
printf "int main(){return 0;}" | clang -xc -O2 -o "$target_dir"/bin/dsymutil -
clang -O2 -std=c99 $alt_wrapper \
-DTARGET_CPU=$(printf '"%s"' "$2") \
-DNIX_APPLE_HDRS=$(
printf '"%s"' "-I$out/$sdk/usr/include"
) \
-DNIX_APPLE_FRAMEWORKS=$(
printf '"%s"' "$out/$sdk/System/Library/Frameworks"
) \
-DNIX_APPLE_PRIV_FRAMEWORKS=$(
printf '"%s"' "$out/$sdk/System/Library/PrivateFrameworks"
) \
-DOS_VER_MIN=$(printf '"%s"' "7.1") \
-o "$target_dir/bin/$triple-clang"
pushd "$target_dir"/bin &>/dev/null
cp "$triple"-clang "$triple"-clang++
popd &>/dev/null
printf "\nbuilding ldid\n"
mkdir -p tmp
pushd tmp &>/dev/null
pushd "$ldid" &>/dev/null
chmod -R 755 "$ldid"
make INSTALLPREFIX="$target_dir" -j4 install
popd &>/dev/null
popd &>/dev/null
printf "\nbuilding cctools / ld64\n"
pushd ../../cctools &>/dev/null
git clean -fdx . &>/dev/null || true
./autogen.sh
./configure --target="$triple" --prefix="$target_dir"
make -j4
make install &>/dev/null
popd &>/dev/null
local me=`whoami`
for d in bin libexec SDK; do
chown -R $me:$me target/$d
cp -R target/$d $out
done
# Crucial piece
rm -rf $out/$sdk
mv $out/SDK $out/$sdk
}
do_build $src armv7