# vim: ft=make ts=4 sw=4 noet
#
# gar.conf.mk - OZSW fork configuration for OpenSolaris 2009.06 (snv_111b)
# Forked from OpenCSW mgar/gar/v2
#

# Use bash for all recipe commands.  The default /bin/sh on OpenSolaris is
# ksh93 which has a bug where a { echo; sed | sed | sed; } > file brace group
# produces only the echo line (gnulib header generation hits this).
SHELL = /usr/bin/bash

# Pick up user information
-include $(HOME)/.garrc
-include /etc/opt/ozsw/garrc
-include /opt/ozsw/etc/garrc

THISHOST := $(shell /usr/bin/uname -n)

# Build platform - OpenSolaris x86 only
PACKAGING_PLATFORMS ?= opensolaris-i386

GAR_PLATFORM ?= $(firstword $(foreach P,$(PACKAGING_PLATFORMS),$(if $(filter $(THISHOST),$(PACKAGING_HOST_$P)),$P)))

MODULATION ?= global
FILEDIR ?= files
WORKROOTDIR ?= $(if $(GAR_PLATFORM),work/$(GAR_PLATFORM),work)
WORKDIR ?= $(WORKROOTDIR)/build-$(MODULATION)
WORKDIR_FIRSTMOD ?= $(WORKROOTDIR)/build-$(firstword $(MODULATIONS))
WORKDIR_GLOBAL ?= $(WORKROOTDIR)/build-global
DOWNLOADDIR ?= $(WORKROOTDIR)/download
PARTIALDIR ?= $(DOWNLOADDIR)/partial
COOKIEROOTDIR ?= $(WORKROOTDIR)/cookies
COOKIEDIR ?= $(COOKIEROOTDIR)/$(MODULATION)
EXTRACTDIR ?= $(WORKDIR)
WORKSRC ?= $(WORKDIR)/$(DISTNAME)
WORKSRC_FIRSTMOD ?= $(WORKDIR_FIRSTMOD)/$(DISTNAME)
OBJDIR ?= .
INSTALLISADIR ?= $(WORKROOTDIR)/install-$(MODULATION)
PKGDIR ?= $(WORKROOTDIR)/package
SCRATCHDIR ?= tmp
CHECKSUM_FILE ?= checksums
MANIFEST_FILE ?= manifest
LOGDIR ?= log

ELISP_DIRS ?= $(datadir)/emacs/site-lisp $(EXTRA_ELISP_DIRS)

GIT_PROXY_SCRIPT ?= $(abspath $(GARBIN))/gitproxy
GIT_DEFAULT_TRACK = +refs/heads/master:refs/remotes/origin/master
GIT_COMMIT_OPTS ?= -v

# For parallel builds
PARALLELMODULATIONS ?=
TTY ?= /usr/bin/tty
SSH ?= ssh

# Outbound proxies
http_proxy ?=
ftp_proxy  ?=
export http_proxy ftp_proxy

SKIPDEPEND ?= 1

GARCHIVEDIR ?= /export/home/ai/src/ozsw-distfiles
GARCHIVEPATH ?= $(GARCHIVEDIR)

# Architecture - OpenSolaris is i386 only (snv_111b)
GARCHLIST ?= i386
GARCH    := $(if $(GARCH),$(GARCH),$(shell /usr/bin/uname -p))
GAROSREL := $(if $(GAROSREL),$(GAROSREL),$(shell /usr/bin/uname -r))

# Compiler - GCC9 is the only supported compiler
# GCC9_CC_HOME points to the bootstrap compiler (built by old shell recipes).
# Once GCC is built under GAR it will live under BUILD_PREFIX.
GARCOMPILERS = GCC9 GCC12
GARCOMPILER ?= GCC9

ifeq (,$(filter $(GARCOMPILER),$(GARCOMPILERS)))
  $(error The compiler '$(GARCOMPILER)' is unknown. Please select one of $(GARCOMPILERS))
endif

# Build flavor (OPT/DBG)
GARFLAVOR ?= OPT

# OZSW install prefix
BUILD_PREFIX       ?= /opt/ozsw

prefix             ?= $(BUILD_PREFIX)
exec_prefix        ?= $(prefix)
bindir_install     ?= $(exec_prefix)/bin
bindir             ?= $(abspath $(bindir_install)/$(MM_BINDIR))
sbindir_install    ?= $(exec_prefix)/sbin
sbindir            ?= $(abspath $(sbindir_install)/$(MM_BINDIR))
libexecdir_install ?= $(exec_prefix)/libexec
libexecdir         ?= $(abspath $(libexecdir_install)/$(MM_BINDIR))
datadir            ?= $(prefix)/share
sysconfdir         ?= /etc$(prefix)
sharedstatedir     ?= $(prefix)/share
localstatedir      ?= /var$(prefix)
libdir_install     ?= $(exec_prefix)/lib
libdir             ?= $(abspath $(libdir_install)/$(MM_LIBDIR))
infodir            ?= $(sharedstatedir)/info
includedir         ?= $(prefix)/include
mandir             ?= $(sharedstatedir)/man
docdir             ?= $(sharedstatedir)/doc
sourcedir          ?= $(BUILD_PREFIX)/src

RELOCATE_PREFIX    ?= $(prefix)

binpath_install    ?= $(BUILD_PREFIX)/bin
binpath            ?= $(abspath $(binpath_install)/$(MM_BINDIR))
sbinpath_install   ?= $(BUILD_PREFIX)/sbin
sbinpath           ?= $(abspath $(sbinpath_install)/$(MM_BINDIR))
libpath_install    ?= $(BUILD_PREFIX)/lib
libpath            ?= $(abspath $(libpath_install)/$(MM_LIBDIR))
includepath_install ?= $(BUILD_PREFIX)/include
includepath        ?= $(BUILD_PREFIX)/include

# DESTDIR is used at INSTALL TIME ONLY to determine what the filesystem root should be.
DESTROOT ?= $(HOME)
PKGROOT ?= $(abspath $(WORKROOTDIR)/pkgroot)
DESTDIR  ?= $(abspath $(INSTALLISADIR))
DESTIMG ?= $(LOGNAME)-$(shell hostname)

# Common package dependency
COMMON_PKG_DEPENDS ?=

# Core packages required for GAR to function correctly
DEF_BASE_PKGS += OZSWbzip2
DEF_BASE_PKGS += OZSWcoreutils
DEF_BASE_PKGS += OZSWgawk
DEF_BASE_PKGS += OZSWgmake
DEF_BASE_PKGS += OZSWgsed
DEF_BASE_PKGS += OZSWgtar
DEF_BASE_PKGS += OZSWxz

# During bootstrap, do not block builds on uninstalled base packages.
# Once OZSWgmake, OZSWgtar etc. are installed, set this in garrc:
#   PREREQUISITE_BASE_PKGS = $(DEF_BASE_PKGS)
PREREQUISITE_BASE_PKGS ?=

# Package format - always SVR4 for OZSW
# (OpenSolaris 5.11 defaults to IPS but we explicitly use SVR4)
PKGFORMAT ?= svr4
PKGFORMAT-5.11 ?= svr4

# Supported ISAs for OpenSolaris i386 kernel
ISALIST_i386   = pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386
ISALIST_amd64  = amd64 $(ISALIST_i386)
ISALIST = $(ISALIST_amd64)

# Default ISA per architecture
ISA_DEFAULT_i386  ?= i386
ISA_DEFAULT64_i386 ?= amd64

# Kernel ISA detection
KERNELISA := $(shell /usr/bin/isainfo -k)

# Memory model directories (lib subdir for 64-bit)
MEMORYMODEL_amd64 = 64
MEMORYMODEL_i386  =
$(foreach I,$(ISALIST_i386),$(eval MEMORYMODEL_$(I) ?= ))

# Multi-memory: libdir and bindir subdirs
MM_LIBDIR  ?= $(MEMORYMODEL_$(ISA))
MM_BINDIR  ?= $(MEMORYMODEL_$(ISA))

# ISA default (what we build as the primary ISA)
ISA ?= $(ISA_DEFAULT_$(GARCH))

# Compiler configuration - GCC 9.x
# Bootstrap compiler lives at /opt/oz (built by shell recipes).
# After GCC is bootstrapped under GAR, GCC9_CC_HOME will be BUILD_PREFIX.
GCC9_CC_HOME ?= /opt/oz
GCC9_VERSION ?= 9.5

GCC9_CC  ?= $(GCC9_CC_HOME)/bin/gcc
GCC9_CXX ?= $(GCC9_CC_HOME)/bin/g++
GCC9_F77 ?= $(GCC9_CC_HOME)/bin/gfortran
GCC9_FC  ?= $(GCC9_CC_HOME)/bin/gfortran

# Architecture flags for GCC9 on i386/amd64
ARCHFLAGS_GCC9_amd64          = -m64
ARCHFLAGS_GCC9_pentium_pro+mmx = -march=pentium-pro -mmmx
ARCHFLAGS_GCC9_pentium_pro     = -march=pentium-pro
ARCHFLAGS_GCC9_pentium+mmx     = -march=pentium -mmmx
ARCHFLAGS_GCC9_pentium         = -march=pentium
ARCHFLAGS_GCC9_i486            = -march=i486
ARCHFLAGS_GCC9_i386            =

MEMORYMODEL_pentium_pro+mmx = $(MEMORYMODEL_i386)
MEMORYMODEL_pentium_pro     = $(MEMORYMODEL_i386)
MEMORYMODEL_pentium+mmx     = $(MEMORYMODEL_i386)
MEMORYMODEL_pentium         = $(MEMORYMODEL_i386)
MEMORYMODEL_i486            = $(MEMORYMODEL_i386)

# Build optimisation flags
FLAVOR_FLAGS_OPT = -O2
FLAVOR_FLAGS_DBG = -g
FLAVOR_FLAGS ?= $(FLAVOR_FLAGS_$(GARFLAVOR))

# Compiler flag variables
GCC9_CC_FLAGS  ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC9_CC_FLAGS) $(EXTRA_CC_FLAGS)
GCC9_CXX_FLAGS ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC9_CXX_FLAGS) $(EXTRA_CXX_FLAGS)
GCC9_LD_FLAGS  ?= -L$(BUILD_PREFIX)/lib -Wl,-R,$(BUILD_PREFIX)/lib $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC9_LD_FLAGS) $(EXTRA_LD_FLAGS)
GCC9_FFLAGS    ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC9_FFLAGS) $(EXTRA_FFLAGS)
GCC9_FCFLAGS   ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC9_FCFLAGS) $(EXTRA_FCFLAGS)

# Compiler configuration - GCC 12.x
GCC12_CC_HOME ?= /opt/ozsw
GCC12_VERSION ?= 12.5

GCC12_CC  ?= $(GCC12_CC_HOME)/bin/gcc
GCC12_CXX ?= $(GCC12_CC_HOME)/bin/g++
GCC12_F77 ?= $(GCC12_CC_HOME)/bin/gfortran
GCC12_FC  ?= $(GCC12_CC_HOME)/bin/gfortran

GCC12_CC_FLAGS  ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC12_CC_FLAGS) $(EXTRA_CC_FLAGS)
GCC12_CXX_FLAGS ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC12_CXX_FLAGS) $(EXTRA_CXX_FLAGS)
GCC12_LD_FLAGS  ?= -L$(BUILD_PREFIX)/lib -Wl,-R,$(BUILD_PREFIX)/lib $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC12_LD_FLAGS) $(EXTRA_LD_FLAGS)
GCC12_FFLAGS    ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC12_FFLAGS) $(EXTRA_FFLAGS)
GCC12_FCFLAGS   ?= $(FLAVOR_FLAGS) $(ARCHFLAGS_$(GARCOMPILER)_$(ISA)) $(EXTRA_GCC12_FCFLAGS) $(EXTRA_FCFLAGS)

GCC12_CC_VERSION = $(shell $(GCC12_CC) -dumpversion 2>/dev/null)


# Active compiler variables (used by gar.lib.mk)
CC      := $($(GARCOMPILER)_CC)
CXX     := $($(GARCOMPILER)_CXX)
F77     ?= $($(GARCOMPILER)_F77)
FC      ?= $($(GARCOMPILER)_FC)
CC_FLAGS  ?= $($(GARCOMPILER)_CC_FLAGS)
CXX_FLAGS ?= $($(GARCOMPILER)_CXX_FLAGS)
LD_FLAGS  ?= $($(GARCOMPILER)_LD_FLAGS)
FFLAGS    ?= $($(GARCOMPILER)_FFLAGS)
FCFLAGS   ?= $($(GARCOMPILER)_FCFLAGS)

# Compiler version info
GCC9_CC_VERSION  = $(shell $(GCC9_CC) -dumpversion 2>/dev/null)

# PATH used during builds - include bootstrap compiler and OZSW bin
BUILD_PATH ?= $(BUILD_PREFIX)/bin:$($(GARCOMPILER)_CC_HOME)/bin:/usr/bin:/bin:/usr/sfw/bin

export CC CXX F77 FC
export CFLAGS=$(CC_FLAGS)
export CXXFLAGS=$(CXX_FLAGS)
export FFLAGS
export FCFLAGS
export LDFLAGS=$(LD_FLAGS)

# GNU tool locations (may come from OZSW or OS)
TAR   ?= $(firstword $(wildcard $(BUILD_PREFIX)/bin/gtar /usr/sfw/bin/gtar) gtar)
MAKE  ?= $(firstword $(wildcard $(BUILD_PREFIX)/bin/gmake /usr/bin/gmake) gmake)
PATCH ?= $(firstword $(wildcard $(BUILD_PREFIX)/bin/gpatch /usr/bin/gpatch /usr/sfw/bin/gpatch) gpatch)

# Wget/curl for fetching
# Use ozsw-fetch (curl wrapper) since system wget is too old for TLS 1.2+
WGET ?= $(GARDIR)/bin/ozsw-fetch
CURL  ?= $(firstword $(wildcard $(BUILD_PREFIX)/bin/curl /usr/bin/curl) curl)

# Default fetch command
FETCHCMD ?= $(if $(wildcard $(BUILD_PREFIX)/bin/wget $(firstword $(wildcard /usr/sfw/bin/wget))),$(WGET),$(CURL) -L -O)

# Checksum tool
CHECKSUM_CMD ?= $(firstword $(wildcard $(BUILD_PREFIX)/bin/sha256sum /usr/bin/sha256sum) sha256sum)

# SVR4 packaging tools (standard on Solaris)
PKGMK    ?= /usr/bin/pkgmk
PKGPROTO ?= /usr/bin/pkgproto
PKGTRANS ?= /usr/bin/pkgtrans
PKGADD   ?= /usr/sbin/pkgadd

# Output spool directory for built packages
PKG_SPOOL ?= /export/home/ai/src/ozone/ozone/out/Spool

# SVN (kept for compat, may not be used)
SVN ?= svn


# Set PATH so builds always find bootstrap compiler and OZSW tools.
# This propagates into CONFIGURE_ENV/BUILD_ENV via STANDARD_EXPORTS.
# /opt/oz/bin is kept as a bootstrap fallback for tools (ginstall, curl, etc.)
# that aren't yet built under OZSW.  Remove once coreutils and curl are built.
PATH = $(BUILD_PREFIX)/bin:$($(GARCOMPILER)_CC_HOME)/bin:$(GARBIN):/opt/oz/bin:/usr/bin:/bin:/usr/sfw/bin:/usr/ccs/bin
export PATH

# Default number of parallel make jobs
PARALLELMFLAGS ?= -j$(shell /bin/nproc 2>/dev/null || /usr/bin/getconf _NPROCESSORS_ONLN 2>/dev/null || echo 2)

# Do not build source packages by default
NOSOURCEPACKAGE ?= 1

# Preprocessor: prefer OZSW headers over system /usr/include (which may have stale 3rd-party headers)
CPPFLAGS ?= -I$(BUILD_PREFIX)/include

# Set EXTENSIONS = 1 in a package Makefile to enable Solaris extension macros.
# These unlock XPG6/POSIX:2001 interfaces (wprintf, mbstowcs, pthreads, etc.)
# that are hidden behind feature-test macros on Solaris by default.
ifneq ($(EXTENSIONS),)
CPPFLAGS += -D__XOPEN_OR_POSIX -D_XPG6 -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS
endif

# pkg-config: only look in OZSW tree, not system /usr/lib/pkgconfig
PKG_CONFIG_PATH ?= $(BUILD_PREFIX)/lib/pkgconfig:$(BUILD_PREFIX)/share/pkgconfig
export PKG_CONFIG_PATH

# ========================= ENVIRONMENT EXPORTS =========================
# These control what variables are passed into configure/build/install envs.
# Builds run under "env -i" so only explicitly exported vars are visible.

STANDARD_EXPORTS ?= HOME PATH
LANG_EXPORTS     ?= LC_ALL

CONFIGURE_ENV_LC_ALL ?= C
BUILD_ENV_LC_ALL     ?= C
INSTALL_ENV_LC_ALL   ?= C

DIRECTORY_EXPORTS  = prefix exec_prefix bindir sbindir libexecdir
DIRECTORY_EXPORTS += datadir sysconfdir sharedstatedir localstatedir libdir
DIRECTORY_EXPORTS += infodir includedir mandir docdir sourcedir

# Key compiler vars; CPPFLAGS is included so -I$(BUILD_PREFIX)/include is passed
COMPILER_EXPORTS  = CPPFLAGS CFLAGS CXXFLAGS LDFLAGS
COMPILER_EXPORTS += FFLAGS FCFLAGS F77 FC CC CXX

# Solaris /usr/bin/sed truncates output with many -e flags; gnulib-based packages
# use SED during configure to substitute their generated headers.  Always point
# configure at gsed so the generated Makefiles use it instead of system sed.
SED ?= /usr/bin/gsed
TOOL_EXPORTS ?= SED

GARPKG_EXPORTS += GARCH GAROSREL

# Propagate bash SHELL as a command-line override to package submakes so it
# takes precedence over the "SHELL = /bin/sh" that autoconf-generated
# Makefiles set unconditionally.
BUILD_OVERRIDE_VARS   += SHELL
BUILD_OVERRIDE_VAR_SHELL = /usr/bin/bash
INSTALL_OVERRIDE_VARS += SHELL
INSTALL_OVERRIDE_VAR_SHELL = /usr/bin/bash

COMMON_EXPORTS ?= $(STANDARD_EXPORTS) $(LANG_EXPORTS) $(DIRECTORY_EXPORTS) $(COMPILER_EXPORTS) $(TOOL_EXPORTS) $(GARPKG_EXPORTS) $(EXTRA_COMMON_EXPORTS)

CONFIGURE_EXPORTS ?= $(COMMON_EXPORTS) $(EXTRA_CONFIGURE_EXPORTS) PKG_CONFIG_PATH DESTDIR
BUILD_EXPORTS     ?= $(COMMON_EXPORTS) $(EXTRA_BUILD_EXPORTS)
INSTALL_EXPORTS   ?= $(COMMON_EXPORTS) $(EXTRA_INSTALL_EXPORTS) DESTDIR

CONFIGURE_ENV ?= $(foreach TTT,$(filter-out $(NO_CONFIGURE_EXPORTS),$(CONFIGURE_EXPORTS)),$(TTT)="$(or $(CONFIGURE_ENV_$(TTT)),$($(TTT)))") $(EXTRA_CONFIGURE_ENV)
BUILD_ENV     ?= $(foreach TTT,$(filter-out $(NO_BUILD_EXPORTS),$(BUILD_EXPORTS)),$(TTT)="$(or $(BUILD_ENV_$(TTT)),$($(TTT)))") $(EXTRA_BUILD_ENV)
INSTALL_ENV   ?= $(foreach TTT,$(filter-out $(NO_INSTALL_EXPORTS),$(INSTALL_EXPORTS)),$(TTT)="$(or $(INSTALL_ENV_$(TTT)),$($(TTT)))") $(EXTRA_INSTALL_ENV)

# Standard scripts (may be overridden per package)
CONFIGURE_SCRIPTS ?= $(WORKSRC)/configure
BUILD_SCRIPTS     ?= $(WORKSRC)/Makefile
TEST_SCRIPTS       =
INSTALL_SCRIPTS   ?= $(WORKSRC)/Makefile

# GitHub URL support: define host so findstring/case patterns work correctly
GITHUB_HOST ?= github.com
GITHUB_REFERENCE ?=

# ========================= ISA CONFIGURATION =========================
# OpenSolaris i386 only (snv_111b). No SPARC, no 64-bit by default.

ISALIST_i386  = pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386
ISALIST_amd64 = amd64 $(ISALIST_i386)
ISALIST = $(ISALIST_amd64)

# GCC9 compiler flags per ISA
ARCHFLAGS_GCC9_pentium_pro+mmx ?= -m32 -march=pentium-mmx
ARCHFLAGS_GCC9_pentium_pro     ?= -m32 -march=pentiumpro
ARCHFLAGS_GCC9_pentium+mmx     ?= -m32 -march=pentium-mmx
ARCHFLAGS_GCC9_pentium         ?= -m32 -march=pentium
ARCHFLAGS_GCC9_i486            ?= -m32 -march=i486
ARCHFLAGS_GCC9_i386            ?= -m32 -march=i386
ARCHFLAGS_GCC9_amd64           ?= -m64 -march=x86-64

MEMORYMODEL_pentium_pro+mmx = 32
MEMORYMODEL_pentium_pro     = 32
MEMORYMODEL_pentium+mmx     = 32
MEMORYMODEL_pentium         = 32
MEMORYMODEL_i486            = 32
MEMORYMODEL_i386            = 32
MEMORYMODEL_amd64           = 64

MEMORYMODEL = $(MEMORYMODEL_$(ISA))

MEMORYMODEL_LIBDIR_32 = .
MEMORYMODEL_LIBDIR_64 = 64
MEMORYMODEL_BINDIR_32 = $(ISABINDIR_$(ISA_DEFAULT_$(GARCH)))
MEMORYMODEL_BINDIR_64 = $(ISABINDIR_$(ISA_DEFAULT64_$(GARCH)))

MM_LIBDIR = $(MEMORYMODEL_LIBDIR_$(MEMORYMODEL))
MM_BINDIR = $(MEMORYMODEL_BINDIR_$(MEMORYMODEL))

# Build optimization flags
OPT_FLAGS_GCC ?= -O2 -pipe -fno-omit-frame-pointer
OPT_FLAGS_GCC9 ?= $(OPT_FLAGS_GCC)
OPT_FLAGS_GCC9_i386 ?= $(OPT_FLAGS_GCC9)
DBG_FLAGS_GCC9 ?= -g
DBG_FLAGS_GCC9_i386 ?= $(DBG_FLAGS_GCC9)

FLAVOR_FLAGS ?= $($(GARFLAVOR)_ISAFLAGS_$(GARCOMPILER)_$(ISA)) $($(GARFLAVOR)_FLAGS_$(GARCOMPILER)_$(GARCH))
FLAVOR_FLAGS += $(EXTRA_$(GARFLAVOR)_FLAGS_$(GARCOMPILER)_$(GARCH)) $(EXTRA_$(GARFLAVOR)_FLAGS_$(GARCOMPILER))

# ISA defaults per OS release (GAROSREL = 5.11 on OpenSolaris)
ISA_DEFAULT_i386-5.11   ?= i386
ISA_DEFAULT64_i386-5.11 ?= amd64
ISA_DEFAULT_i386    ?= $(ISA_DEFAULT_i386-$(GAROSREL))
ISA_DEFAULT64_i386  ?= $(ISA_DEFAULT64_i386-$(GAROSREL))

ISA_DEFAULT  ?= $(ISA_DEFAULT_$(GARCH))
ISA_DEFAULT64 ?= $(ISA_DEFAULT64_$(GARCH))

# Populate per-compiler ISALIST from ARCHFLAGS (exclude ERROR entries)
$(foreach C,$(GARCOMPILERS),$(eval ISALIST_$(C) ?= $(foreach I,$(ISALIST),$(if $(filter-out ERROR,$(ARCHFLAGS_$C_$I)),$I))))

# Library and binary subdirectories per ISA
ISALIBDIR_$(ISA_DEFAULT_i386) ?= .
$(foreach I,$(ISALIST),$(eval ISALIBDIR_$(I) ?= $I))
ISALIBDIR ?= $(ISALIBDIR_$(ISA))

$(foreach I,$(ISALIST),$(eval ISABINDIR_$(I) ?= $(ISALIBDIR_$(I))))
ISABINDIR ?= $(ISABINDIR_$(ISA))

# Compute what ISAs to build (32-bit only by default; set BUILD64=1 for 64-bit)
remove-duplicates = $(if $1,$(firstword $1) $(call remove-duplicates,$(filter-out $(firstword $1),$1)))
REQUESTED_ISAS ?= $(strip $(call remove-duplicates,\
    $(foreach A,$(GARCHLIST),\
        $(if $(BUILD64_ONLY),,$(ISA_DEFAULT_$A)) \
        $(if $(or $(BUILD64),$(BUILD64_LIBS_ONLY)),$(ISA_DEFAULT64_$A)) \
        $(EXTRA_BUILD_ISAS_$A)) \
    $(EXTRA_BUILD_ISAS)))

KERNELISA := $(shell isainfo -k 2>/dev/null || echo i386)

NEEDED_ISAS ?= $(strip \
    $(filter     $(ISALIST_$(KERNELISA)),$(filter $(ISALIST_$(ISA_DEFAULT64_$(GARCH))),$(REQUESTED_ISAS))) \
    $(filter-out $(ISALIST_$(KERNELISA)),$(filter $(ISALIST_$(ISA_DEFAULT64_$(GARCH))),$(REQUESTED_ISAS))))
BUILD_ISAS ?= $(filter $(ISALIST_$(KERNELISA)),$(NEEDED_ISAS))

# ISA sanity check - verify we can build the requested ISA on this kernel
verify-isa:
ifeq (,$(filter $(ISA),$(ISALIST_$(KERNELISA))))
	$(error The ISA '$(ISA)' cannot be built on kernel arch '$(KERNELISA)')
endif
	@$(MAKECOOKIE)

# Disable git snapshotting (no git identity on build host)
NOGITPATCH ?= 1

# Disable checkpkg (requires Python 2.6+, system has 2.4)
ENABLE_CHECK ?= 0
