#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
# Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
# Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
# Copyright 2016 RackTop Systems.
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
# Copyright 2020 Joyent, Inc.
# Copyright 2024 Bill Sommerfeld <sommerfeld@hamachi.org>
# Copyright 2026 Zygaena Project - Hammerhead Build System
#
# Hammerhead Build Environment for Zygaena (OpenIndiana cross-build host)
#
# For building on an OpenIndiana VM with OI-packaged GCC 14 and tools.
# For building on a native Hammerhead/Zygaena host, use hammerhead.sh instead.
#
# - This file is sourced by "hh-env" (bldenv) and "hh-build" and
#   should not be executed directly.
# - This script is only interpreted by ksh93 and explicitly allows the
#   use of ksh93 language extensions.


# -----------------------------------------------------------------------------
# Parameters you are likely to want to change
# -----------------------------------------------------------------------------

# Hammerhead NIGHTLY_OPTIONS:
#   -F    Do NOT do a non-DEBUG build (DEBUG only)
#   -n    Do not bringover from parent (use local sources)
#   -C    Run 'make check' (cstyle/hdrchk)
#   -D    Do a DEBUG build
#   -A    Check for ABI discrepancies
#   -m    Send mail on completion
#   -r    Check ELF runpaths
#   -t    Build and use workspace tools
#
# Note: -p (package creation) removed - Hammerhead uses Coral packaging
export NIGHTLY_OPTIONS='-FnCDAmrt'

# Some scripts optionally send mail messages to MAILTO.
#export MAILTO=

# CODEMGR_WS - where is your workspace at
# Hammerhead repo structure: git root contains hammerhead/ subdirectory with source
# If CODEMGR_WS not set, detect from git root + /hammerhead
if [[ -z "$CODEMGR_WS" ]]; then
    _gitroot=$(git rev-parse --show-toplevel 2>/dev/null)
    if [[ -d "$_gitroot/base/usr/src" ]]; then
        # Nested structure: repo/hammerhead/usr/src
        export CODEMGR_WS="$_gitroot/base"
    elif [[ -d "$_gitroot/usr/src" ]]; then
        # Flat structure: repo/usr/src
        export CODEMGR_WS="$_gitroot"
    else
        echo "Error: Cannot find usr/src directory" >&2
        return 1
    fi
    unset _gitroot
else
    export CODEMGR_WS
fi

# -----------------------------------------------------------------------------
# Compiler Configuration
# -----------------------------------------------------------------------------
#
# Hammerhead uses GCC 14 exclusively - no shadow compilation
#
# Each entry has the form <name>,<path to binary>,<style> where name is a
# free-form name (possibly used in the makefiles to guard options), path is
# the path to the executable.  style is the 'style' of command line taken by
# the compiler, currently either gnu (or gcc) or sun (or cc).
#
export GNUC_ROOT=/usr/gcc/14
export PRIMARY_CC=gcc14,$GNUC_ROOT/bin/gcc,gnu
export PRIMARY_CCC=gcc14,$GNUC_ROOT/bin/g++,gnu

# OI's patched GCC 14 supports -msave-args for mdb stack debugging.
# Stock upstream GCC does not. Set here so OI builds get this feature.
export SAVEARGS='-msave-args'

# Shadow compilation disabled - single compiler only
export SHADOW_CCS=
export SHADOW_CCCS=

# Smatch disabled (simplifies build)
#export ENABLE_SMATCH=1

# Hammerhead: CUPS not in base OS — SMB printing disabled
# export ENABLE_SMB_PRINTING=

# -----------------------------------------------------------------------------
# Perl/Python Configuration
# -----------------------------------------------------------------------------

# If your distro uses certain versions of Perl, make sure either Makefile.master
# contains your new defaults OR your .env file sets them.
#export PERL_VERSION=5.28
#export PERL_VARIANT=-thread-multi
#export PERL_PKGVERS=

# Hammerhead: 64-bit only - disable 32-bit perl modules
export BUILDPERL32='#'
#export BUILDPERL64='#'

# If your distro uses certain versions of Python, make sure either
# Makefile.master contains your new defaults OR your .env file sets them.
#export PYTHON3_VERSION=3.9
#export PYTHON3_PKGVERS=-39
#export PYTHON3_SUFFIX=

# -----------------------------------------------------------------------------
# Console/Display Configuration
# -----------------------------------------------------------------------------

# Set console color scheme either by build type:
#
#export RELEASE_CONSOLE_COLOR="-DDEFAULT_ANSI_FOREGROUND=ANSI_COLOR_BLACK \
#	-DDEFAULT_ANSI_BACKGROUND=ANSI_COLOR_WHITE"
#
#export DEBUG_CONSOLE_COLOR="-DDEFAULT_ANSI_FOREGROUND=ANSI_COLOR_RED \
#	-DDEFAULT_ANSI_BACKGROUND=ANSI_COLOR_WHITE"
#
# or just one for any build type:
#
#export DEFAULT_CONSOLE_COLOR="-DDEFAULT_ANSI_FOREGROUND=ANSI_COLOR_BLACK \
#	-DDEFAULT_ANSI_BACKGROUND=ANSI_COLOR_WHITE"

# Skip Java 11 builds on distributions that don't support it
#export BLD_JAVA_11=

# POST_NIGHTLY can be any command to be run at the end of the build.
# See hh-build for interactions between environment variables and this command.
#POST_NIGHTLY=

# Populates /etc/versions/build on each build run
export BUILDVERSION_EXEC="git describe --all --long --dirty"

# -----------------------------------------------------------------------------
# Source Control / Bringover Configuration
# -----------------------------------------------------------------------------
#
# When the 'n' option is not present in NIGHTLY_OPTIONS, hh-build updates
# the build workspace from the sources identified by the following variables.
# Defaults are set in hh-build if the environment file leaves them unset.
#

# SCM in use.  Defaults to "git"
#export BRINGOVER_SCM="git"

# URL or pathname to source workspace.
#export BRINGOVER_WS="${CLONE_WS}"

# Branch within BRINGOVER_WS; must be specified if BRINGOVER_WS is a URL
#export BRINGOVER_BRANCH=""

# Short name to use within CODEMGR_WS for BRINGOVER_WS (git-specific)
#export BRINGOVER_REMOTE=nightly_bringover_ws

# Command-line options to add to the clone operation
#export CLONE_OPTIONS=""

# -----------------------------------------------------------------------------
# Build System Configuration
# -----------------------------------------------------------------------------

# Maximum number of parallel make jobs, set to NCPUS.
# GNU Make's job server keeps all CPUs busy without needing extra padding.
function maxjobs
{
	nameref maxjobs=$1
	integer ncpu
	integer -r min_mem_per_job=512 # minimum amount of memory for a job

	ncpu=$(builtin getconf ; getconf 'NPROCESSORS_ONLN')
	(( maxjobs=ncpu ))

	# Throttle number of parallel jobs to a value which guarantees that
	# all jobs have enough memory. This was added to avoid excessive
	# paging/swapping in cases of virtual machine installations which
	# have lots of CPUs but not enough memory assigned.
	if [[ $(/usr/sbin/prtconf 2>'/dev/null') == ~(E)Memory\ size:\ ([[:digit:]]+)\ Megabytes ]] ; then
		integer max_jobs_per_memory # parallel jobs which fit into physical memory
		integer physical_memory # physical memory installed

		# The array ".sh.match" contains the contents of capturing
		# brackets in the last regex, .sh.match[1] will contain
		# the value matched by ([[:digit:]]+), i.e. the amount of
		# memory installed
		physical_memory="10#${.sh.match[1]}"

		((
			max_jobs_per_memory=round(physical_memory/min_mem_per_job) ,
			maxjobs=fmax(2, fmin(maxjobs, max_jobs_per_memory))
		))
	fi

	return 0
}

# Hammerhead: parallelism control for GNU Make builds
# Reduced to -j4 for stability. Full parallelism (-j NCPU) exposes race
# conditions in kernel ctfmerge, _msg preprocessing, and locale data
# generation. Revisit after adding proper ordering constraints.
GMAKE_MAX_JOBS=4
export GMAKE_MAX_JOBS

# path to onbld tool binaries
ONBLD_BIN='/opt/onbld/bin'

# PARENT_WS is used to determine the parent of this workspace. This is
# for the options that deal with the parent workspace (such as where the
# proto area will go).
export PARENT_WS="${PARENT_WS:-}"

# CLONE_WS is the workspace hh-build should do a bringover from.
# The bringover, if any, is done as STAFFER.
export CLONE_WS="${CLONE_WS:-}"

# Set STAFFER to your own login as gatekeeper or developer
# The point is to use group "staff" and avoid referencing the parent
# workspace as root.
export STAFFER="$LOGNAME"
export MAILTO="${MAILTO:-$STAFFER}"

# If you wish the mail messages to be From: an arbitrary address, export
# MAILFROM.
#export MAILFROM="user@example.com"

# The project (see project(5)) under which to run this build.  If not
# specified, the build is simply run in a new task in the current project.
export BUILD_PROJECT=''

# -----------------------------------------------------------------------------
# Architecture Configuration - Hammerhead is amd64-only
# -----------------------------------------------------------------------------
#
# MACH=i386 represents the x86 CPU family (used for Makefile directory selection)
# MACH64=amd64 is the actual 64-bit architecture we build
# BUILD32 is set to $(POUND_SIGN) in Makefile.master to disable 32-bit builds
#
export MACH=i386
export MACH64=amd64

# -----------------------------------------------------------------------------
# Path Configuration
# -----------------------------------------------------------------------------

export ATLOG="$CODEMGR_WS/log"
export LOGFILE="$ATLOG/build.log"

# Proto area - use MACH64 (amd64) since Hammerhead is 64-bit only
export ROOT="$CODEMGR_WS/proto/root_${MACH64}"
export SRC="$CODEMGR_WS/usr/src"
export MULTI_PROTO="no"

# REF_PROTO_LIST - for comparing the list of stuff in your proto area
# with. Generally this should be left alone, since you want to see differences
# from your parent (the gate).
export REF_PROTO_LIST="$PARENT_WS/usr/src/proto_list_${MACH64}"

# Proto area in parent for optionally depositing a copy of headers and
# libraries corresponding to the protolibs target
export PARENT_ROOT="$PARENT_WS/proto/root_${MACH64}"
export PARENT_TOOLS_ROOT="$PARENT_WS/usr/src/tools/proto/root_$MACH-nd"

# -----------------------------------------------------------------------------
# Version Configuration
# -----------------------------------------------------------------------------

# Build version (YYYYMMDD-NN format, auto-generated by hh-build/bldenv).
# NOT exported as VERSION to avoid polluting sub-builds (e.g. zsh autotools).
#export HAMMERHEAD_VERSION="20260301-01"

# The RELEASE and RELEASE_DATE variables are set in Makefile.master;
# there might be special reasons to override them here, but that
# should not be the case in general
# export RELEASE='5.11'
# export RELEASE_DATE='October 2007'

# -----------------------------------------------------------------------------
# Package Configuration (IPS - disabled for Hammerhead)
# -----------------------------------------------------------------------------
#
# Hammerhead uses Coral packaging instead of IPS.
# These variables are kept for compatibility but -p is not in NIGHTLY_OPTIONS.
#
# PKGARCHIVE determines where the repository will be created.
# PKGPUBLISHER_REDIST controls the publisher setting for the repository.
#
export PKGARCHIVE="${CODEMGR_WS}/packages/${MACH64}/build"
# export PKGPUBLISHER_REDIST='on-redist'

# Package manifest format version.
export PKGFMT_OUTPUT='v2'

# -----------------------------------------------------------------------------
# Make Configuration
# -----------------------------------------------------------------------------

# We want make to do as much as it can, just in case there's more than
# one problem.
# We also must set e in MAKEFLAGS as the makefiles depend on importing
# the environment variables set here.
export MAKEFLAGS='ke'

# Hammerhead: Use GNU Make instead of dmake
export MAKE=gmake

# -----------------------------------------------------------------------------
# Build Tools Configuration
# -----------------------------------------------------------------------------

# Build tools - don't change these unless you know what you're doing.  These
# variables allows you to get the compilers and onbld files locally.
# Set BUILD_TOOLS to pull everything from one location.
# Alternately, you can set ONBLD_TOOLS to where you keep the contents of
# SUNWonbld.
export BUILD_TOOLS='/opt'
#export ONBLD_TOOLS='/opt/onbld'

# Set this flag to 'n' to disable the use of 'checkpaths'.  The default,
# if the 'N' option is not specified, is to run this test.
#CHECK_PATHS='y'

# Smatch integration (disabled by default in Hammerhead)
if [[ "$ENABLE_SMATCH" == "1" ]]; then
	SMATCHBIN=$CODEMGR_WS/usr/src/tools/proto/root_$MACH-nd/opt/onbld/bin/$MACH/smatch
	export SHADOW_CCS="$SHADOW_CCS smatch,$SMATCHBIN,smatch"
fi
