# Macros for cmake
#
%__cmake cmake
%_cmake__builddir BUILD
%_cmake_skip_rpath -DCMAKE_SKIP_RPATH:BOOL=ON

# XXX: Private copy-paste from %%_smp_mflags.
%_cmake_b_ncpus ${NPROCS:-%__nprocs}

# This has to be idempotent.
# Use cases to support:
# %cmake_build VERBOSE=1 and other keyword-style makeflags (invoke make(1))
# %cmake_build t1 t2 t3 (invoke make(1))
# %cmake_build -t t1 t2 t3 ... (invoke cmake --build)
# %cmake_build --target t1 t2 t3 ... (invoke cmake --build)
%_cmake_build_compat_definition \
    __cmake_build_compat() { \
        __cmake_build_compat_use_cmd=make \
        for i; do \
            case $i in \
            --verbose) \
                echo >&2 "cmake_build: --verbose detected in %%cmake_build invocation." \
                __cmake_build_compat_use_cmd=cmake-build \
                break; \
                ;; \
            -t|--target) \
                echo >&2 "cmake_build: -t or --target detected in %%cmake_build invocation." \
                __cmake_build_compat_use_cmd=cmake-build \
                break; \
                ;; \
            *=*) \
                echo >&2 "cmake_build: Make flag '$i' found on %%cmake_build command line." \
                __cmake_build_compat_use_cmd=make \
                break; \
                ;; \
            --) \
                echo >&2 "cmake_build: encountered '--', refusing to parse further." \
                break; \
                ;; \
            *) \
                ;; \
            esac \
        done \
        \
        case $__cmake_build_compat_use_cmd in \
        cmake-build) \
            echo >&2 "cmake_build: Invoking cmake --build for forward compatibility with Sisyphus." \
            %__cmake --build "%_cmake__builddir" --verbose -j%_cmake_b_ncpus "$@" \
            ;; \
        make) \
            echo >&2 "cmake_build: Direct make(1) arguments found on %%cmake_build command line." \
            echo >&2 "cmake_build: Invoking make to be backward compatible with p9." \
            %make_build -C "%_cmake__builddir" "$@" \
            ;; \
        *) \
            echo >&2 "cmake_build: Warning: State machine has failed; bug in cmake macros." \
            return 1 \
            ;; \
        esac \
    } \

%_cmake_common_definition \
    __cmake_common() { \
    %__cmake \\\
        %{?_cmake_skip_rpath} \\\
        -DCMAKE_SKIP_INSTALL_RPATH:BOOL=yes \\\
        -DCMAKE_C_FLAGS:STRING='%optflags' \\\
        -DCMAKE_CXX_FLAGS:STRING='%optflags' \\\
        -DCMAKE_Fortran_FLAGS:STRING='%optflags' \\\
        -DCMAKE_INSTALL_PREFIX=%prefix \\\
        -DINCLUDE_INSTALL_DIR:PATH=%_includedir \\\
        -DLIB_INSTALL_DIR:PATH=%_libdir \\\
        -DSYSCONF_INSTALL_DIR:PATH=%_sysconfdir \\\
        -DSHARE_INSTALL_PREFIX:PATH=%_datadir \\\
        -DLIB_DESTINATION=%_lib \\\
        -DLIB_SUFFIX="%_libsuff" \\\
        \\\
        "$@" \
    } \

# Some upstreams have broken CMakeLists and expect CMake to be run
# in the build directory.
# Our strategy is to try both.
%cmake \
    %_cmake_common_definition \
    __cmake() { \
        mkdir -p "%_cmake__builddir"; \
        echo >&2 "%%cmake: p9 compat: calling cmake in the build directory." \
        pushd "%_cmake__builddir" \
        ret=0 \
        __cmake_common -S .. -B . "$@" || ret=$? \
        popd \
        if [ $ret -eq 0 ]; then \
            touch %_tmppath/.rpm-macros-cmake-B.-succeeded \
            return $ret \
        fi \
        \
        rm -rf "%_cmake__builddir" \
        mkdir -p "%_cmake__builddir" \
        echo >&2 "%%cmake: forward compat: calling cmake in the source directory." \
        ret=0 \
        __cmake_common -S . -B "%_cmake__builddir" "$@" || ret=$? \
        if [ $ret -eq 0 ]; then \
            touch %_tmppath/.rpm-macros-cmake-S.-succeeded \
            return $ret \
        fi \
        return $ret \
    } \
    __cmake

# Notice: this macro affects _cmake__builddir, effectively preventing further
# out-of-source builds. Maintainers opting to build in-source are not
# interested in those, though.
%cmake_insource \
    %define _cmake__builddir . \
    %_cmake_common_definition \
    __cmake_common -S . -B .

%cmake_build \
    %_cmake_build_compat_definition \
    __cmake_build_compat

%cmake_install %makeinstall_std -C "%_cmake__builddir"

%cmakeinstall_std %makeinstall_std -C "%_cmake__builddir"
