# OS/2 specific rpm configuration file.

#------------------------------------------------------------------------------
# Overrides for default macros

%_pkgdocdir %{_docdir}/%{name}
%_docdir_fmt %%{NAME}

# Order: TMPDIR, TMP (where /tmp is rewritten to), TEMP, /var/tmp
%_tmppath %os2_fwdslashes %{lua:print(os.getenv("TMPDIR") or os.getenv("TMP") or os.getenv("TEMP") or rpm.expand("%{_var}/tmp"))}

#------------------------------------------------------------------------------
# Extensions for default macros

# Need -mstackrealign -fno-common to get proper SSE2 alignment and disable AVX on pentium4
# until https://github.com/bitwiseworks/gcc-os2/issues/11 is fixed.
# Note that -mstackrealign is only needed for GCC < 8 and -fno-common only for GCC < 10
# as in those versions they are default.
# TODO: If they are still needed for a newer GCC RPM, they should go to macros.gcc
# belonging to the GCC package, as _vendor_optflags_gcc.
# -ffile-prefix-map redacts build paths in debug info files also reducing their size.
%_vendor_optflags_pentium4 -mno-avx
%_vendor_optflags -ffile-prefix-map=%{_builddir}=. %{expand:%%{?_vendor_optflags_%{_target_cpu}}} -mstackrealign -fno-common %{?_vendor_optflags_gcc}

# Always add LIBCx DLL
%_vendor_build_ldflags -Zomf -Zhigh-mem -lcx

# We define standard C and C++ compilers below
%_vendor_set_build_flags \
  CC="${CC:-%{__cc}}" ; export CC \
  CXX="${CXX:-%{__cxx}}" ; export CXX

#------------------------------------------------------------------------------
# Support for the alternate install tree

# Root directory for alternate package installs (e.g. versioned packages)
%_alt_prefix %{_prefix}/alt

# Sets up alternate install paths for a package, arguments:
# 1 - package name (must include the pinned version info to make it unique)
# NOTE: Saves all known _prefix-based paths with the _sys_ prefix for reference
# NOTE: Must be used only ONCE per .spec
# NOTE: Defines _alt_prefix_activate macro with the activation script path
%setup_alt_prefix()\
%global _sys_prefix %{_prefix}\
%global _sys_exec_prefix %{_exec_prefix}\
%global _sys_bindir %{_bindir}\
%global _sys_sbindir %{_sbindir}\
%global _sys_libexecdir %{_libexecdir}\
%global _sys_datadir %{_datadir}\
%global _sys_sharedstatedir %{_sharedstatedir}\
%global _sys_libdir %{_libdir}\
%global _sys_includedir %{_includedir}\
%global _sys_infodir %{_infodir}\
%global _sys_mandir %{_mandir}\
%global _sys_datarootdir %{_datarootdir}\
%global _prefix %{_alt_prefix}/%1\
%global _alt_prefix_activate %{_prefix}/activate\
%{nil}

# Generates and installs the _alt_prefix_activate script, arguments:
# 1 - comma-separated list of standard path vars to set (e.g. "PATH,INFOPATH")
# 2 - optional arbitrary text to append to the activation script
%install_alt_prefix_activate()\
%{!?_alt_prefix_activate:%{error:%0: Missing setup_alt_prefix specification}}\
%{!?1:%{error:%0: Missing path-variable list}}\
%{__cat} >%{?buildroot}%{_alt_prefix_activate} <<'EOF'\
%{lua:
  local name = rpm.expand("%0")
  local sep = rpm.expand("%{_pathsep}")
  local paths = {
    PATH = rpm.expand("%{_bindir}"),
    INFOPATH = rpm.expand("%{_infodir}"),
    MANPATH = rpm.expand("%{_mandir}"),
    CPATH = rpm.expand("%{_includedir}"),
    C_INCLUDE_PATH = rpm.expand("%{_includedir}"),
    CPLUS_INCLUDE_PATH = rpm.expand("%{_includedir}"),
    LIBRARY_PATH = rpm.expand("%{_libdir}"),
    BEGINLIBPATH = rpm.expand("%{_libdir}"),
    PKG_CONFIG_PATH = rpm.expand("%{_libdir}/pkgconfig%{_pathsep}%{_datadir}/pkgconfig"),
    ACLOCAL_PATH = rpm.expand("%{_datadir}/aclocal"),
    CMAKE_PREFIX_PATH = rpm.expand("%{_prefix}"),
  }
  print(rpm.expand("#!%{___build_shell}\\n"))
  local list = rpm.expand("%1")
  for item in string.gmatch(list, "[^,]+") do
    item = string.match(item, "^%s*(.-)%s*$")
    local path = paths[item]
    assert(path, name .. ": unknown path variable " .. item)
    print(string.format('export %s="%s${%s:+%s$%s}"\\n', item, path, item, sep, item))
    if (item == "BEGINLIBPATH") then
      print("export LIBPATHSTRICT=T\\n")
    end
  end
}\
%{?2}\
EOF\
%{__chmod} 755 %{?buildroot}%{_alt_prefix_activate}\
%{nil}

#------------------------------------------------------------------------------
# Macros needed at rpm runtime (package installation and such)

# Path to OS/2 specific rpm configuration directory containing helper scripts
%_rpmconfigdir_os2 %{_rpmconfigdir}/%{_vendor}

# System-wide path for OS/2 language files accessed via DPATH env. variable.
%os2_langdir %{_datadir}/os2/lang

# System-wide path for OS/2 INF files accessible via BOOKSHELF env. variable.
%os2_bookdir %{_datadir}/os2/book

# System-wide path for OS/2 HLP files accessible via HELP env. variable.
%os2_helpdir %{_datadir}/os2/help

# Converts back slashes to forward slashes.
%os2_fwdslashes() %{lua: print((string.gsub(rpm.expand("%{?**}"), "\\\\", "/")))}

# Converts forward lashes to back slashes.
%os2_backslashes() %{lua: print((string.gsub(rpm.expand("%{?**}"), "/", "\\\\")))}

# Converts a Unix path to a DOS path (backward slashes) replacing /@unixroot
# with %UNIXROOT% if it starts the path. Note that if %UNIXROOT% per se contains
# forward slashes, they are not converted (if you need this, use
# os2_expand_unixroot and os2_backslashes). Several paths separated by ';' may
# be given to handle them at once (useful for PATH-like env.vars).
%os2_dos_path() %{lua:
  local function conv_path(path)
    if (string.sub(path, 1, 10) == "/@unixroot" and
        (string.len(path) == 10 or string.sub(path, 11, 11) == '/')) then
      path = "%UNIXROOT%" .. string.sub(path, 11)
    end
    path = string.gsub(path, "/", "\\\\")
    return path
  end
  local path = rpm.expand("%{?**}")
  local seen_first = false
  for p in string.gmatch(path, "[^;]*") do
    print((seen_first and ';' or '') .. conv_path(p))
    seen_first = seen_first or true
  end
}

# Expands /@unixroot to the current value of UNIXROOT if it starts the path
# and converts all slashes to forward ones. If you need no fowrard slashes and
# using of %UNIXROOT% instead of the real value is fine, use os2_dos_path as
# it doesn't reqire -e (see below). Several paths separated by ';' may be
# given to handle them at once (useful for PATH-like env.vars).
# NOTE: Don't use in install-time scriptlets unless with -e and as %%{...}.
%os2_expand_unixroot() %{lua:
  local function conv_path(path)
    if (string.sub(path, 1, 10) == "/@unixroot" and
        (string.len(path) == 10 or string.sub(path, 11, 11) == '/')) then
      path = os.getenv("UNIXROOT") .. string.sub(path, 11)
    end
    path = string.gsub(path, "\\\\", "/")
    return path
  end
  local path = rpm.expand("%{?**}")
  local seen_first = false
  for p in string.gmatch(path, "[^;]*") do
    print((seen_first and ';' or '') .. conv_path(p))
    seen_first = seen_first or true
  end
}

# Expands DOS-like environment variables (%VAR%) to their current values.
# NOTE: Don't use in install-time scriptlets unless with -e and as %%{...}.
%os2_expand_dos_vars() %{lua:
  local str = rpm.expand("%{?**}")
  str = string.gsub(str, "%%(%a+)%%", os.getenv)
  print(str)
}

# Current UNIXROOT value in DOS format (backward slashes)
# NOTE: Don't use in install-time scriptlets unless with -e and as %%{...}.
%os2_unixroot_path() %{os2_backslashes %{os2_expand_unixroot /@unixroot}}

# OS/2 boot drive.
# NOTE: Don't use in install-time scriptlets unless with -e and as %%{...}.
%os2_boot_drive %(%{_rpmconfigdir_os2}/getbootdrive.exe)

#------------------------------------------------------------------------------
# Macros needed at package creation time

# Path to script that installs legacy runtime libraries.
%__legacy_runtime_install_post \
    %{_rpmconfigdir_os2}/find-legacy-runtime.sh install "%{_target_cpu}" "%{_builddir}/%{?buildsubdir}"\
%{nil}

# Template for legacy runtime sub-packages.
%legacy_runtime_packages \
%global __legacy_runtime_packages 1\
%{expand:%(%{_rpmconfigdir_os2}/find-legacy-runtime.sh package "%{name}" "%{_sourcedir}" "%{_target_cpu}" "%{buildroot}")}\
%{nil}

# List of debug files generated by brp-strip-os2 with %exclude prepended (to be
# used with -f option in %files to automatically exclude them from packages).
%debug_package_exclude_files "%{_builddir}/%{?buildsubdir}/exclude-debugfiles.list"

# Only run what we really need in post-install on OS/2.
%__os_install_post \
    %{_rpmconfigdir}/brp-compress "./@unixroot"\
    %{_rpmconfigdir_os2}/brp-strip-os2 "%{_builddir}/%{?buildsubdir}" %{?_strip_no_compress:--no-compress} %{!?__debug_package:--no-debuginfo} %{?_strip_opts}\
    %{?__legacy_runtime_packages:%{__legacy_runtime_install_post}}\
    %{?__debug_package:%{__sed} -e 's/^/%exclude /' "%{_builddir}/%{?buildsubdir}/debugfiles.list" > %{debug_package_exclude_files}}\
%{nil}

# Disable using find-debuginfo.sh, brp-strip-os2 does the job.
%__debug_install_post %{nil}

# GCC toolchain
%__cc_gcc gcc
%__cxx_gcc g++
%__cpp_gcc gcc -E

# Default to the GCC toolchain
%toolchain gcc

%__cc %{expand:%%{__cc_%{toolchain}}}
%__cxx %{expand:%%{__cxx_%{toolchain}}}
%__cpp %{expand:%%{__cpp_%{toolchain}}}

# Compiler macros to use for invoking compilers in spec files for packages that
# want to use the default compiler and don't care which compiler that is.
%build_cc %{__cc}
%build_cxx %{__cxx}
%build_cpp %{__cpp}

#==============================================================================
# ---- compiler flags.

# Automatically use set_build_flags macro for build, check, and
# install phases.
# Use "%undefine _auto_set_build_flags" to disable"
%_auto_set_build_flags 1
%__spec_build_pre %{___build_pre} \
  %{?_auto_set_build_flags:%{set_build_flags}} \
  %{?_generate_package_note_file}

%__spec_check_pre %{___build_pre} \
  %{?_auto_set_build_flags:%{set_build_flags}} \
  %{?_generate_package_note_file}

# Deprecated names.  For backwards compatibility only.
%__global_cflags %{build_cflags}
%__global_cxxflags %{build_cxxflags}
%__global_fflags %{build_fflags}
%__global_fcflags %{build_fflags}
%__global_ldflags %{build_ldflags}
