name: 'macOS: Cross-compile iOS libraries, copy to install dir & extend ldc2.conf'
inputs:
  arch:
    required: true
  ios_deployment_target:
    required: false
    default: '12.0'
runs:
  using: composite
  steps:
    - shell: bash
      run: |
        set -eux
        cd ..

        arch='${{ inputs.arch }}'
        deployment_target='${{ inputs.ios_deployment_target }}'
        triple="$arch-apple-ios$deployment_target"
        if [[ "$arch" == arm64 ]]; then
            sysroot='/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk'
        else
            sysroot='/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk'
        fi

        # use bootstrap-ldc, which is guaranteed to be native
        bootstrap-ldc/bin/ldc-build-runtime --ninja \
          --buildDir=build-libs-ios \
          --dFlags="-mtriple=$triple" \
          --ldcSrcDir="$PWD/ldc" \
          CMAKE_SYSTEM_NAME=iOS \
          CMAKE_OSX_SYSROOT="$sysroot" \
          CMAKE_OSX_ARCHITECTURES="$arch" \
          CMAKE_OSX_DEPLOYMENT_TARGET="$deployment_target" \
          BUILD_LTO_LIBS=ON

        mkdir "installed/lib-ios-$arch"
        cp -a build-libs-ios/lib/*.{a,dylib,o} "installed/lib-ios-$arch/"

        section="
        \"$arch-apple-ios\":
        {
            switches = [
                \"-defaultlib=phobos2-ldc,druntime-ldc\",
                \"-Xcc=-target\",
                \"-Xcc=$triple\",
                \"-Xcc=-miphoneos-version-min=$deployment_target\",
                \"-Xcc=-isysroot\",
                \"-Xcc=$sysroot\",
            ];
            lib-dirs = [
                \"%%ldcbinarypath%%/../lib-ios-$arch\",
            ];
            rpath = \"%%ldcbinarypath%%/../lib-ios-$arch\";
        };"
        echo "$section" >> installed/etc/ldc2.conf
        cat installed/etc/ldc2.conf
