#!/bin/bash
# shellcheck disable=SC2155
if ! type "include" > /dev/null 2>&1; then
  include() {
    . "$(cd "$(dirname "$1")" && pwd)/${2}";
  };
fi
include "${BASH_SOURCE[0]}" ../pragma_once.bash
if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi
include "${BASH_SOURCE[0]}" ../base/export.bash
false && source ../base/export.bash


make_mingw(){
  # 通常是sysroot/usr
  local install_prefix="${1}"
  local ver="${2}"
  local CPU="${3}"
  local MINGWTRIPLET="${CPU}-w64-mingw32"
  local MINGWW64COMMON
  if [[ ${CPU} == "x86_64" ]]; then
    if [[ "$multilibs" == "yes" ]]; then
      MINGWW64COMMON="--enable-lib32 --enable-lib64"
    else
      MINGWW64COMMON="--disable-lib32 --enable-lib64"
    fi
  elif [[ ${CPU} == "aarch64" ]]; then
    MINGWW64COMMON="--disable-lib32 --disable-lib64 --disable-libarm32 --enable-libarm64"
  elif [[ ${CPU} == "arm" ]]; then
    MINGWW64COMMON="--disable-lib32 --disable-lib64 --enable-libarm32 --disable-libarm64"
  elif [[ ${CPU} =~ i[3-6]86 ]]; then
    MINGWW64COMMON="--enable-lib32 --disable-lib64 --with-default-msvcrt=msvcrt"
  else
    MINGWW64COMMON="--disable-lib32 --disable-lib64 --disable-libarm32 --disable-libarm64 --enable-lib$CPU"
  fi
  MINGWW64COMMON="$MINGWW64COMMON --host=${MINGWTRIPLET} --disable-nls --disable-werror "

  export src_dir
  ensure_repo_version src_dir  mingw-w64  "${ver}"





#  configure_gnu_ex "${src_dir}/mingw-w64-headers" "--prefix=${install_prefix}/${MINGWTRIPLET}/usr ${MINGWW64COMMON} "
#  exec_record "make -j$(nproc)"
#  exec_record "make install-strip -j$(nproc)"
#
#  configure_gnu_ex "${src_dir}/mingw-w64-crt" "--prefix=${install_prefix}/${MINGWTRIPLET}/usr ${MINGWW64COMMON}"
#  exec_record "make -j$(nproc)"
#  exec_record "make install-strip -j$(nproc)"

  #ln -sf "${install_prefix}/${MINGWTRIPLET}/lib" ./usr/lib

  configure_gnu_ex "${src_dir}/mingw-w64-libraries/winpthreads"  "--prefix=${install_prefix} ${MINGWW64COMMON}"
  log_info "CC ==== $CC"
  exec_record_fast_fail "../configure --help"
  exec_record "make -j$(nproc)"
  exec_record "make install-strip -j$(nproc)"

}



#  c库类型   构建c库使用的编译器  选择自己的 stage1 gcc 或者外部给选项
#make_mingw "/ae/bash/libc" "v13.0.0" "x86_64"