Fix VS2022 MSMs detection

After commits 42da199753515584e615cdcdd8601d2ffc27245b (more VS2022 integration,
Oct 14 10:58:25 2021 +0200) and 9ea17a6331ca9491e7012dca837d3815cd4309ac (Adapt
to latest VS 2022 MSM versions, Apr 08 09:15:19 2022 +0200), the new 143 version
of MSMs gets detected and passed to SCP, but not used there, so MSIs lack CRT.

Additionally, it may happen that a redist is configured that does not match the
actual CRT used in the build, because it picked a latest available version, and
then an error happens when building MSI, because it expects MSMs in wrong source
directory.

This simplifies the checks, makes sure to look for the correct MSM version, and
adds an error message in SCP in case when the passed version is not handled, to
detect the incomplete support early.

Change-Id: I47d45472309c584314730e7f5f46a9596fc8d21d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142587
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
diff --git a/configure.ac b/configure.ac
index a3f00fd..f693764 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6703,35 +6703,23 @@ find_winsdk()
find_msms()
{
    # Return value: $msmdir
    local version="$1"

    AC_MSG_CHECKING([for MSVC merge modules directory])
    local my_msm_files
    AC_MSG_CHECKING([for MSVC $version merge modules directory])
    local my_msm_file="Microsoft_VC${version}_CRT_x86.msm"
    local my_msm_dir

    dnl Order my_msm_files in increasing order. Then check the directories returned
    dnl by ls in an inner loop; assuming they are also ordered in increasing order,
    dnl the result will be the highest MSM version found in the highest directory.

    case "$VCVER" in
        16.0 | 17.0 | 17.4)
        my_msm_files="Microsoft_VC141_CRT_x86.msm Microsoft_VC142_CRT_x86.msm Microsoft_VC143_CRT_x86.msm ${my_msm_files}"
        ;;
    esac
    for f in $my_msm_files; do
        echo "$as_me:$LINENO: searching for $f" >&5
    done
    echo "$as_me:$LINENO: searching for $my_msm_file" >&5

    msmdir=
    case "$VCVER" in
    16.0 | 17.0 | 17.4)
        for f in ${my_msm_files}; do
            for l in `ls -1 $VC_PRODUCT_DIR/redist/MSVC/`; do
                my_msm_dir="$VC_PRODUCT_DIR/redist/MSVC/$l/MergeModules/"
                echo "$as_me:$LINENO: looking for $my_msm_dir${f}])" >&5
                if test -e "$my_msm_dir${f}"; then
                    msmdir=$my_msm_dir
                fi
            done
        for l in `ls -1 $VC_PRODUCT_DIR/redist/MSVC/`; do
            my_msm_dir="$VC_PRODUCT_DIR/redist/MSVC/$l/MergeModules/"
            echo "$as_me:$LINENO: looking for $my_msm_dir${my_msm_file}])" >&5
            if test -e "$my_msm_dir${my_msm_file}"; then
                msmdir=$my_msm_dir
            fi
        done
        ;;
    esac
@@ -6744,7 +6732,7 @@ find_msms()
            AC_MSG_FAILURE([not found])
        else
            AC_MSG_WARN([not found (check config.log)])
            add_warning "MSM none of ${my_msm_files} found"
            add_warning "MSM ${my_msm_file} not found"
        fi
    fi
}
@@ -8565,25 +8553,25 @@ AC_ARG_WITH(ucrt-dir,
UCRT_REDISTDIR="$with_ucrt_dir"
if test $_os = "WINNT"; then
    find_msvc_x64_dlls
    MSVC_DLL_PATH=`win_short_path_for_make "$msvcdllpath"`
    MSVC_DLLS="$msvcdlls"
    if echo "$msvcdllpath" | grep -q "VC143.CRT$"; then
        with_redist=143
    elif echo "$msvcdllpath" | grep -q "VC142.CRT$"; then
        with_redist=142
    elif echo "$msvcdllpath" | grep -q "VC141.CRT$"; then
        with_redist=141
    fi
    for i in $PKGFORMAT; do
        if test "$i" = msi; then
            find_msms
            find_msms "$with_redist"
            if test -n "$msmdir"; then
                MSM_PATH=`win_short_path_for_make "$msmdir"`
                SCPDEFS="$SCPDEFS -DWITH_VC_REDIST=$with_redist"
            fi
            break
        fi
    done
    MSVC_DLL_PATH=`win_short_path_for_make "$msvcdllpath"`
    MSVC_DLLS="$msvcdlls"
    test -n "$msmdir" && MSM_PATH=`win_short_path_for_make "$msmdir"`
    # MSVC 15.3 changed it to VC141
    if echo "$msvcdllpath" | grep -q "VC143.CRT$"; then
        SCPDEFS="$SCPDEFS -DWITH_VC143_REDIST"
    elif echo "$msvcdllpath" | grep -q "VC142.CRT$"; then
        SCPDEFS="$SCPDEFS -DWITH_VC142_REDIST"
    elif echo "$msvcdllpath" | grep -q "VC141.CRT$"; then
        SCPDEFS="$SCPDEFS -DWITH_VC141_REDIST"
    else
        SCPDEFS="$SCPDEFS -DWITH_VC${VCVER}_REDIST"
    fi

    if test "$UCRT_REDISTDIR" = "no"; then
        dnl explicitly disabled
diff --git a/scp2/source/ooo/vc_redist.scp b/scp2/source/ooo/vc_redist.scp
index 75ccf2b..3fe7af2 100644
--- a/scp2/source/ooo/vc_redist.scp
+++ b/scp2/source/ooo/vc_redist.scp
@@ -18,16 +18,27 @@

#include "macros.inc"

#if defined(WITH_VC142_REDIST)
#if defined(WITH_VC_REDIST)
#if WITH_VC_REDIST == 143
    #define WINDOWS_X64_MERGEMODULE gid_MergeModule_Microsoft_VC143_CRT_x64
    #define WINDOWS_X64_MERGEMODULE_FILE "Microsoft_VC143_CRT_x64.msm"
    #define WINDOWS_X86_MERGEMODULE gid_MergeModule_Microsoft_VC143_CRT_x86
    #define WINDOWS_X86_MERGEMODULE_FILE "Microsoft_VC143_CRT_x86.msm"
#elif WITH_VC_REDIST == 142
    #define WINDOWS_X64_MERGEMODULE gid_MergeModule_Microsoft_VC142_CRT_x64
    #define WINDOWS_X64_MERGEMODULE_FILE "Microsoft_VC142_CRT_x64.msm"
    #define WINDOWS_X86_MERGEMODULE gid_MergeModule_Microsoft_VC142_CRT_x86
    #define WINDOWS_X86_MERGEMODULE_FILE "Microsoft_VC142_CRT_x86.msm"
#elif defined(WITH_VC141_REDIST)
#elif WITH_VC_REDIST == 141
    #define WINDOWS_X64_MERGEMODULE gid_MergeModule_Microsoft_VC141_CRT_x64
    #define WINDOWS_X64_MERGEMODULE_FILE "Microsoft_VC141_CRT_x64.msm"
    #define WINDOWS_X86_MERGEMODULE gid_MergeModule_Microsoft_VC141_CRT_x86
    #define WINDOWS_X86_MERGEMODULE_FILE "Microsoft_VC141_CRT_x86.msm"
#else
    // The next string will result in "malformed par file" error, terminating build as expected.
    // Possibly a new version of CRT was enabled in configure.ac, but not handled here yet?
    error: unknown VC redist version WITH_VC_REDIST. Check configure.ac
#endif
#endif

#if defined(WINDOWS_X86_MERGEMODULE)