Repository: dotnet/runtime
File: eng/native/build-commons.sh (Shell)

Code before:
    #
    # These flags MUST be exported before gen-buildsys.sh runs or cmake will ignore them
    #
    export CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
    export CXXFLAGS="${CXXFLAGS} ${EXTRA_CXXFLAGS}"
    export LDFLAGS="${LDFLAGS} ${EXTRA_LDFLAGS}"

    if [[ "$targetOS" == osx || "$targetOS" == maccatalyst ]]; then
        if [[ "$hostArch" == x64 ]]; then
            cmakeArgs="-DCMAKE_OSX_ARCHITECTURES=\"x86_64\" $cmakeArgs"
        elif [[ "$hostArch" == arm64 ]]; then
            cmakeArgs="-DCMAKE_OSX_ARCHITECTURES=\"arm64\" $cmakeArgs"
        else
            echo "Error: Unknown OSX architecture $hostArch."
            exit 1
        fi
    fi

    if [[ "$targetOS" == maccatalyst ]]; then
        cmakeArgs="-C $__RepoRootDir/eng/native/tryrun_ios_tvos.cmake $cmakeArgs"

        # CMAKE_OSX_DEPLOYMENT_TARGET is set to the macOS equivalent (14.0) of the
        # Catalyst min version (17.0) so CMake's compiler test emits a valid
        # -mmacosx-version-min flag. The effective Catalyst min version is enforced
        # via the -target triple in eng/native/configurecompiler.cmake, not this value.
        cmakeArgs="-DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_SYSROOT=macosx -DCMAKE_SYSTEM_VARIANT=maccatalyst -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 $cmakeArgs"
    fi

    if [[ "$targetOS" == android || "$targetOS" == linux-bionic ]]; then
        # Keep in sync with $(AndroidApiLevelMin) in Directory.Build.props in the repository rooot
        local ANDROID_API_LEVEL=21
        if [[ -z "$ANDROID_NDK_ROOT" ]]; then
            echo "Error: You need to set the ANDROID_NDK_ROOT environment variable pointing to the Android NDK root."
            exit 1
        fi

        cmakeArgs="-DANDROID_BUILD=1 -C $__RepoRootDir/eng/native/tryrun.cmake $cmakeArgs"
        cmakeArgs="-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-${ANDROID_API_LEVEL} -DANDROID_NATIVE_API_LEVEL=${ANDROID_API_LEVEL} $cmakeArgs"

        # Don't try to set CC/CXX in init-compiler.sh - it's handled in android.toolchain.cmake already
        __Compiler="default"

        if [[ "$hostArch" == x64 ]]; then
            cmakeArgs="-DANDROID_ABI=x86_64 $cmakeArgs"
        elif [[ "$hostArch" == x86 ]]; then
            cmakeArgs="-DANDROID_ABI=x86 $cmakeArgs"
        elif [[ "$hostArch" == arm64 ]]; then
            cmakeArgs="-DANDROID_ABI=arm64-v8a $cmakeArgs"
        elif [[ "$hostArch" == arm ]]; then
            cmakeArgs="-DANDROID_ABI=armeabi-v7a $cmakeArgs"
        else

Review comment (suggestion):
I wonder if we can just remove CMAKE_OSX_DEPLOYMENT_TARGET, according to the comment in https://github.com/dotnet/runtime/blob/92741bebcae5b82cd537979eef962674627e7f89/eng/native/configurecompiler.cmake#L711-L715 it should result in some default value (which gets overridden by the -target)

Code after:
    #
    # These flags MUST be exported before gen-buildsys.sh runs or cmake will ignore them
    #
    export CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
    export CXXFLAGS="${CXXFLAGS} ${EXTRA_CXXFLAGS}"
    export LDFLAGS="${LDFLAGS} ${EXTRA_LDFLAGS}"

    if [[ "$targetOS" == osx || "$targetOS" == maccatalyst ]]; then
        if [[ "$hostArch" == x64 ]]; then
            cmakeArgs="-DCMAKE_OSX_ARCHITECTURES=\"x86_64\" $cmakeArgs"
        elif [[ "$hostArch" == arm64 ]]; then
            cmakeArgs="-DCMAKE_OSX_ARCHITECTURES=\"arm64\" $cmakeArgs"
        else
            echo "Error: Unknown OSX architecture $hostArch."
            exit 1
        fi
    fi

    if [[ "$targetOS" == maccatalyst ]]; then
        cmakeArgs="-C $__RepoRootDir/eng/native/tryrun_ios_tvos.cmake $cmakeArgs"

        # Intentionally do not set CMAKE_OSX_DEPLOYMENT_TARGET for maccatalyst here:
        # - CMake interprets CMAKE_OSX_DEPLOYMENT_TARGET as a macOS minimum version
        #   instead of MacCatalyst, causing newer clang to reject it as invalid.
        # - The effective Catalyst minimum version is enforced via the
        #   -target *-apple-ios<version>-macabi flag in eng/native/configurecompiler.cmake
        cmakeArgs="-DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_SYSROOT=macosx -DCMAKE_SYSTEM_VARIANT=maccatalyst $cmakeArgs"
    fi

    if [[ "$targetOS" == android || "$targetOS" == linux-bionic ]]; then
        # Keep in sync with $(AndroidApiLevelMin) in Directory.Build.props in the repository rooot
        local ANDROID_API_LEVEL=21
        if [[ -z "$ANDROID_NDK_ROOT" ]]; then
            echo "Error: You need to set the ANDROID_NDK_ROOT environment variable pointing to the Android NDK root."
            exit 1
        fi

        cmakeArgs="-DANDROID_BUILD=1 -C $__RepoRootDir/eng/native/tryrun.cmake $cmakeArgs"
        cmakeArgs="-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-${ANDROID_API_LEVEL} -DANDROID_NATIVE_API_LEVEL=${ANDROID_API_LEVEL} $cmakeArgs"

        # Don't try to set CC/CXX in init-compiler.sh - it's handled in android.toolchain.cmake already
        __Compiler="default"

        if [[ "$hostArch" == x64 ]]; then
            cmakeArgs="-DANDROID_ABI=x86_64 $cmakeArgs"
        elif [[ "$hostArch" == x86 ]]; then
            cmakeArgs="-DANDROID_ABI=x86 $cmakeArgs"
        elif [[ "$hostArch" == arm64 ]]; then
            cmakeArgs="-DANDROID_ABI=arm64-v8a $cmakeArgs"
        elif [[ "$hostArch" == arm ]]; then
            cmakeArgs="-DANDROID_ABI=armeabi-v7a $cmakeArgs"