#!/bin/sh
#
# Copyright (C) 2026 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
#
# Computes per-build constants used by postprocess-header-rule, and writes them
# to a sourceable shell file. By computing these once per build (instead of on
# every per-header invocation) we avoid:
#
#   - Internal builds: 20-30ms of perl + JSON parsing of branch_config.json,
#     per invocation, by sourcing postprocess-framework-headers-definitions.
#   - All builds: per-invocation initialization of OSX_VERSION/IOS_VERSION/
#     XROS_VERSION (which are constant within a single build).
#
# postprocess-header-rule sources the cache file produced here at startup
# instead of redoing this work on each *.h build-rule invocation.

set -e

CACHE_OUT="${SCRIPT_OUTPUT_FILE_0}"
mkdir -p "$(dirname "${CACHE_OUT}")"

# Try to source the Internal definitions file (which sets MAC_TBA_VERSION etc.
# and may override OSX_VERSION / WK_FRAMEWORK_HEADER_POSTPROCESSING_DISABLED).
# In a pure OpenSource build the file isn't present and we just skip this.
HAVE_ADDITIONS=NO
for search_path in "${BUILT_PRODUCTS_DIR}" "${SDKROOT}"; do
    candidate="${search_path}${WK_LIBRARY_HEADERS_FOLDER_PATH}/WebKitAdditions/Scripts/postprocess-framework-headers-definitions"
    if [ -f "${candidate}" ]; then
        # shellcheck disable=SC1090
        . "${candidate}"
        HAVE_ADDITIONS=YES
        break
    fi
done

# Mirror postprocess-header-rule's per-platform default initialization for
# OSX_VERSION/IOS_VERSION/XROS_VERSION when the Internal file didn't set them.
case "${WK_PLATFORM_NAME}" in
    macosx)
        : "${OSX_VERSION:=${MACOSX_DEPLOYMENT_TARGET}}"
        : "${IOS_VERSION:=NA}"
        : "${XROS_VERSION:=NA}"
        ;;
    maccatalyst)
        : "${IOS_VERSION:=${LLVM_TARGET_TRIPLE_OS_VERSION#ios}}"
        : "${XROS_VERSION:=NA}"
        : "${OSX_VERSION:=NA}"
        ;;
    iphoneos|iphonesimulator)
        : "${IOS_VERSION:=${IPHONEOS_DEPLOYMENT_TARGET}}"
        : "${XROS_VERSION:=NA}"
        : "${OSX_VERSION:=NA}"
        ;;
    xros|xrsimulator)
        : "${XROS_VERSION:=${XROS_DEPLOYMENT_TARGET}}"
        : "${IOS_VERSION:=NA}"
        : "${OSX_VERSION:=NA}"
        ;;
esac

# Serialize OTHER_SED_OPTIONS (an array) for re-import on the consumer side.
# Use bash's `declare -p` for safe round-tripping. If we're being run by /bin/sh
# without bash array support, fall back to writing nothing (consumer treats
# unset as empty).
SERIALIZED_OTHER_SED_OPTIONS=""
if [ -n "${BASH_VERSION:-}" ]; then
    SERIALIZED_OTHER_SED_OPTIONS="$(declare -p OTHER_SED_OPTIONS 2>/dev/null || true)"
fi

cat > "${CACHE_OUT}" <<EOF
# Auto-generated by generate-header-postprocess-config.sh.
# Sourced by postprocess-header-rule to skip per-invocation recomputation
# of values that are constant within one build.
#
# OSX_VERSION / IOS_VERSION / XROS_VERSION are intentionally written with
# whatever value the per-platform case above set (or left unset). On
# platforms not handled by that case (e.g. appletvos, watchos), all three
# stay empty so postprocess-header-rule's [[ -n … ]] gate falls through
# to the strip-annotations branch — matching the legacy per-invocation
# behaviour.
WK_HEADER_POSTPROCESS_CACHE_LOADED=1
WK_HEADER_POSTPROCESS_HAS_ADDITIONS=${HAVE_ADDITIONS}
OSX_VERSION=${OSX_VERSION:-}
IOS_VERSION=${IOS_VERSION:-}
XROS_VERSION=${XROS_VERSION:-}
WATCHOS_VERSION=${WATCHOS_VERSION:-}
TVOS_VERSION=${TVOS_VERSION:-}
WK_FRAMEWORK_HEADER_POSTPROCESSING_DISABLED=${WK_FRAMEWORK_HEADER_POSTPROCESSING_DISABLED:-NO}
${SERIALIZED_OTHER_SED_OPTIONS}
EOF
