# vim: ft=make ts=4 sw=4 noet

NAME    = gcc
VERSION = 9.5.0
GARTYPE = v2

DESCRIPTION = GNU Compiler Collection 9.x
define BLURB
  GCC 9.x C, C++, Fortran, and Go compiler. Builds into /opt/ozsw using
  the bootstrap compiler at /opt/oz (built by the old shell recipes).
  After installation, set GCC9_CC_HOME = /opt/ozsw in garrc to self-host.
endef

MASTER_SITES  = http://ftp.gnu.org/gnu/gcc/gcc-$(VERSION)/
DISTFILES    += $(DISTNAME).tar.gz

LICENSE = COPYING3

BUILD_DEP_PKGS += OZSWbinutils OZSWzlib

# -----------------------------------------------------------------------
# OZSWgcc9rt - GCC runtime libraries only.
# Any compiled program that uses C++, Fortran, Go, OpenMP, or extended
# C runtime support needs these shared libraries at run time; it does NOT
# need the compiler itself.
# -----------------------------------------------------------------------
PACKAGES                  += OZSWgcc9rt
SPKG_DESC_OZSWgcc9rt      = GCC 9.x runtime libraries (libgcc_s, libstdc++, libgfortran, libgo, ...)
CATALOGNAME_OZSWgcc9rt    = gcc9rt
RUNTIME_DEP_PKGS_OZSWgcc9rt += OZSWzlib
# Match the GCC-generated shared libraries in /opt/ozsw/lib/.
# libcc1 is a compiler plugin (stays with OZSWgcc9), everything else here
# is a true runtime library needed by compiled programs.
PKGFILES_OZSWgcc9rt  = .*/lib/libgcc_s\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libstdc\+\+\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libgfortran\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libgomp\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libquadmath\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libgo\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libitm\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libatomic\.so.*
PKGFILES_OZSWgcc9rt += .*/lib/libssp\.so.*

# -----------------------------------------------------------------------
# OZSWgcc9 - the compiler, headers, static support libs, and tools.
# Depends on the runtime package since the driver links against libgcc_s.
# -----------------------------------------------------------------------
PACKAGES             += OZSWgcc9
SPKG_DESC_OZSWgcc9   = GNU Compiler Collection 9.x (C, C++, Fortran, Go) - compiler
CATALOGNAME_OZSWgcc9 = gcc9
RUNTIME_DEP_PKGS_OZSWgcc9 += OZSWgcc9rt OZSWzlib
# Catch-all: leave PKGFILES_OZSWgcc9 unset so GAR uses the exclude
# mechanism — it excludes everything claimed by OZSWgcc9rt and the
# source package, and lets everything else through.

# -----------------------------------------------------------------------
# Build configuration
# -----------------------------------------------------------------------

# GCC must be built outside the source tree.
OBJDIR = ../gcc-build

# GAR's PARALLELMFLAGS is derived from MFLAGS which on GNU Make 3.81 contains
# --jobserver-fds refs even at the top level; passing those with MAKEFLAGS=
# (cleared) causes unlimited parallelism.  Set an explicit clean value here.
PARALLELMFLAGS = -j4

# Apply Solaris compatibility fixes by prepending to specific libiberty files.
# Solaris patch(1) cannot handle unified diffs reliably, so we use gsed instead.
POSIX_HDR = \#ifndef _XOPEN_SOURCE\n\#define _XOPEN_SOURCE 600\n\#endif\n\#ifndef _POSIX_C_SOURCE\n\#define _POSIX_C_SOURCE 200112L\n\#endif\n\#include <sys/types.h>\n\#include <sys/stat.h>\n\#include <unistd.h>
SUN_PROC_STUB = \#ifdef __sun\n\#define PROC_UNSUPPORTED 1\n\#endif

post-extract:
	@echo "==> Applying Solaris libiberty compatibility fixes"
	@for f in getpwd.c pex-common.c pex-common.h unlink-if-ordinary.c; do \
		gsed -i "1s|^|$(POSIX_HDR)\n|" $(WORKSRC)/libiberty/$$f; \
	done
	@for f in pex-unix.c regex.c; do \
		gsed -i "1s|^|$(SUN_PROC_STUB)\n\n|" $(WORKSRC)/libiberty/$$f; \
	done
	@$(MAKECOOKIE)

# Download GCC prerequisites (GMP, MPFR, MPC, ISL) into the source tree.
# Patch the script to use ftp:// and gtar (required on OpenSolaris).
pre-configure:
	@echo "==> Patching contrib/download_prerequisites for OpenSolaris"
	gsed -i 's|http:|ftp:|g' $(WORKSRC)/contrib/download_prerequisites
	gsed -i 's/tar -x/gtar -x/g' $(WORKSRC)/contrib/download_prerequisites
	cd $(WORKSRC) && sh contrib/download_prerequisites --no-verify
	@$(MAKECOOKIE)

CONFIGURE_ARGS  = --prefix=$(prefix)
CONFIGURE_ARGS += --enable-languages=c,c++,fortran,go
CONFIGURE_ARGS += --with-gnu-as
CONFIGURE_ARGS += --with-as=$(bindir)/as
CONFIGURE_ARGS += --with-gnu-ld
CONFIGURE_ARGS += --with-ld=$(bindir)/ld
CONFIGURE_ARGS += --disable-libsanitizer
CONFIGURE_ARGS += --disable-multilib
CONFIGURE_ARGS += --with-zlib=$(BUILD_PREFIX)
# Propagate RPATH to target libraries (libgo, libgfortran, libquadmath).
# Without this, each sub-package configure resets LDFLAGS to empty and the
# libtool -rpath mechanism in old libtool (2.2.7a) fails to embed RPATH.
CONFIGURE_ARGS += LDFLAGS_FOR_TARGET="-Wl,-R,$(BUILD_PREFIX)/lib"

# GCC's internal static libs (libgcc.a, libgcc_eh.a) live in
# lib/gcc/i386-pc-solaris2.11/9.5.0/ and are required by the compiler
# driver at link time. Override the default static-lib exclusion so
# they are included in OZSWgcc9.
MERGE_EXCLUDE_STATICLIBS =

EXTRA_CC_FLAGS  = -fPIC
EXTRA_CXX_FLAGS = -fPIC

include gar/category.mk
