# OS/2 specific rpm configuration file.

#------------------------------------------------------------------------------
# Macros we override from default macros

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

#------------------------------------------------------------------------------
# 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:
  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
  path = rpm.expand("%{?**}")
  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:
  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
  path = rpm.expand("%{?**}")
  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:
  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.

# C compiler flags.  This is traditionally called CFLAGS in makefiles.
# Historically also available as %%{optflags}, and %%build sets the
# environment variable RPM_OPT_FLAGS to this value.
%build_cflags %{optflags} %{?_distro_extra_cflags}

# C++ compiler flags.  This is traditionally called CXXFLAGS in makefiles.
%build_cxxflags %{optflags} %{?_distro_extra_cxxflags}

# Fortran compiler flags.  Makefiles use both FFLAGS and FCFLAGS as
# the corresponding variable names.
%build_fflags %{optflags} -I%{_fmoddir} %{?_distro_extra_fflags}

# Link editor flags.  This is usually called LDFLAGS in makefiles.
# (Some makefiles use LFLAGS instead.)  The default value assumes that
# the flags, while intended for ld, are still passed through the gcc
# compiler driver.  At the beginning of %%build, the environment
# variable RPM_LD_FLAGS to this value.
%build_ldflags -Zomf -Zhigh-mem -lcx %{?_distro_extra_ldflags}

# Expands to shell code to set the compiler/linker environment
# variables CFLAGS, CXXFLAGS, FFLAGS, LDFLAGS if they
# have not been set already.  RPM_OPT_FLAGS and RPM_LD_FLAGS have already
# been set implicitly at the start of the %%build section.
# LT_SYS_LIBRARY_PATH is used by libtool script.
%set_build_flags \
  CFLAGS="${CFLAGS:-%{build_cflags}}" ; export CFLAGS ; \
  CXXFLAGS="${CXXFLAGS:-%{build_cxxflags}}" ; export CXXFLAGS ; \
  FFLAGS="${FFLAGS:-%{build_fflags}}" ; export FFLAGS ; \
  FCFLAGS="${FCFLAGS:-%{build_fflags}}" ; export FCFLAGS ; \
  LDFLAGS="${LDFLAGS:-%{build_ldflags}}" ; export LDFLAGS ; \
  CC="${CC:-%{__cc}}" ; export CC ; \
  CXX="${CXX:-%{__cxx}}" ; export CXX

# 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}
