# -*- makefile -*-
# definitions used in more than one Makefile / rules file

SHELL		= /bin/bash -e	# brace expansion used in rules file
export SHELL

PWD		:= $(shell pwd)
srcdir		= $(PWD)/src
builddir	= $(PWD)/build
builddir_hppa64	= $(PWD)/build-hppa64
stampdir	= stamps
distribution	:= $(shell lsb_release -is)

lib64		= lib64

# architecture dependent variables
DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

DEB_HOST_ARCH		:= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_CPU	:= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_SYSTEM	:= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH	:= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# fix up the LIBRARY_PATH to allow building with multiarch
export LIBRARY_PATH = /usr/lib/$(DEB_HOST_MULTIARCH)

# allow debian/target to be used instead of GCC_TARGET - this was requested
# by toolchain-source maintainer
ifndef GCC_TARGET
DEBIAN_TARGET_FILE := $(strip $(shell cat debian/target 2>/dev/null))
ifneq ($(DEBIAN_TARGET_FILE),)
GCC_TARGET := $(DEBIAN_TARGET_FILE)
else
GCC_TARGET := $(DEB_HOST_ARCH)
endif
endif

DEB_TARGET_ARCH		:= $(shell dpkg-architecture -f \
			-a$(GCC_TARGET) -qDEB_HOST_ARCH 2>/dev/null)
DEB_TARGET_ARCH_OS	:= $(shell dpkg-architecture -f \
			-a$(GCC_TARGET) -qDEB_HOST_ARCH_OS 2>/dev/null)
DEB_TARGET_ARCH_CPU	:= $(shell dpkg-architecture -f \
			-a$(GCC_TARGET) -qDEB_HOST_ARCH_CPU 2>/dev/null)

DEB_TARGET_GNU_CPU	:= $(shell dpkg-architecture -f \
			-a$(DEB_TARGET_ARCH) -qDEB_HOST_GNU_CPU 2>/dev/null)
DEB_TARGET_GNU_SYSTEM	:= $(shell dpkg-architecture -f \
			-a$(DEB_TARGET_ARCH) -qDEB_HOST_GNU_SYSTEM 2>/dev/null)
DEB_TARGET_GNU_TYPE	:= $(shell dpkg-architecture -f \
			-a$(DEB_TARGET_ARCH) -qDEB_HOST_GNU_TYPE 2>/dev/null)

# ---------------------------------------------------------------------------
# which binary packages are built?

# cross compiler support. If GCC_TARGET is set, then it's the architecture
# we build for.

ifeq ($(DEB_TARGET_ARCH),)
$(error GCC_TARGET value "$(GCC_TARGET)" is not a valid Debian architecture)
endif
  
ifneq ($(DEB_HOST_ARCH),$(DEB_TARGET_ARCH))
  DEB_CROSS = yes
  # TP: Target Prefix. Used primarily as a prefix for cross tool
  #     names (e.g. powerpc-linux-gcc).
  # TS: Target Suffix. Used primarily at the end of cross compiler
  #     package names (e.g. gcc-powerpc).
  # LS: Library Suffix. Used primarily at the end of cross compiler
  #     library package names (e.g. libgcc-powerpc-cross).
  DEB_TARGET_ALIAS ?= $(DEB_TARGET_GNU_TYPE)
  TP = $(DEB_TARGET_GNU_TYPE)-
  TS = -$(DEB_TARGET_ALIAS)
  LS = -$(DEB_TARGET_ARCH)-cross
endif

ifeq ($(DEB_CROSS),yes)
  TARGET_ALIAS := $(DEB_TARGET_ALIAS)
else
  TARGET_ALIAS := $(DEB_TARGET_GNU_TYPE)

  # old target for the Hurd in sarge
  ifeq ($(TARGET_ALIAS),i386-gnu)
    TARGET_ALIAS := i586-gnu
  endif

  # for compatibility with older dpkg versions.
  TARGET_ALIAS := $(subst i386,i486,$(TARGET_ALIAS))

  # configure as linux-gnu, not linux
  #ifeq ($(findstring linux,$(TARGET_ALIAS))/$(findstring linux-gnu,$(TARGET_ALIAS)),linux/)
  #  TARGET_ALIAS := $(TARGET_ALIAS)-gnu
  #endif

  # configure as linux, not linux-gnu
  #TARGET_ALIAS := $(subst linux-gnu,linux,$(TARGET_ALIAS))
endif

printarch:
	@echo DEB_TARGET_ARCH: $(DEB_TARGET_ARCH)
	@echo DEB_TARGET_GNU_SYSTEM: $(DEB_TARGET_GNU_SYSTEM)
	@echo DEB_TARGET_GNU_TYPE: $(DEB_TARGET_GNU_TYPE)
	@echo TARGET_ALIAS: $(TARGET_ALIAS)
	@echo TP: $(TP)
	@echo TS: $(TS)

# The name of the source package
PKGSOURCE := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2;exit 0}')

gcc_tarball		:= $(firstword $(wildcard gcc-*.tar.*))
gcc_srcdir		:= $(shell echo $(gcc_tarball) | sed 's/\.tar.*//;s/-dfsg//')

gpc_tarball		:= gpc-20060215.tar.bz2
gpc_srcdir		:= gpc-20060215

#testsuite_srcdir	:= testsuite-3.3-20030118
#testsuite_tarball	:= $(testsuite_srcdir).tar.bz2


versioned_packages := yes

ifdef DEB_CROSS
  cross_bin_arch := -$(TARGET_ALIAS)
  cross_lib_arch := -$(DEB_TARGET_ARCH)-cross
endif

# Don't include docs with GFDL invariant sections --------------------
# As we are only building libstdc++5 we can turn this on unconditionally.
# (Debian bug #586011)
GFDL_INVARIANT_FREE := yes

# --------------------
# Configuration of components

# common things --------------------
# build common packages, where package names don't differ in different
# gcc versions (fastjar, fixincludes, libgcj-common, protoize) ...
with_common_pkgs := yes

# ... and some libraries, which do not change (libgcc1, libffi2, libg2c,
# libobjc1).
with_common_libs := yes

with_dev := no

# multiarch --------------------
with_multiarch := no

# C --------------------
enabled_languages := c
# Build all packages needed for C development
ifeq ($(with_base_only),yes)
  with_cdev := no
else
  ifeq ($(with_dev),yes)
    with_cdev := yes
  else
    with_cdev := no
  endif
endif

# C++ --------------------
ifeq ($(with_base_only),yes)
  with_cxx := no
else
  with_cxx := yes
endif
no_cxx_archs := avr
ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(no_cxx_archs)))
  with_cxx := disabled for architecture $(DEB_TARGET_ARCH)
endif
ifeq (c++, $(findstring c++,$(WITHOUT_LANG)))
  with_cxx := disabled by environment
endif
# Build all packages needed for C++ development
ifeq ($(with_cxx)-$(with_dev),yes-yes)
  with_cxxdev := yes
else
  with_cxxdev := no
endif

ifeq ($(with_cxx),yes)
  enabled_languages += c++
endif

ifeq ($(with_cxx),yes)
  with_libcxx := yes
else
  with_libcxx := no
endif

# debugging versions of libstdc++
ifeq ($(with_cxxdev),yes)
  with_debug := yes
else
  with_debug := no
endif
#debug_no_archs := powerpc
#ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(debug_no_archs)))
#    with_debug := disabled for architecure $(DEB_TARGET_ARCH)
#endif

# Java --------------------
# java converted for V3 C++ ABI for some archs
ifeq ($(with_base_only),yes)
  with_java := no
else
  with_java := yes
endif

java_no_archs := hurd-i386 mips mipsel kfreebsd-i386
ifneq ($(DEB_TARGET_ARCH),i386)
  ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(java_no_archs)))
    with_java := disabled for architecure $(DEB_TARGET_ARCH)
  endif
endif
ifdef DEB_CROSS
  with_java := disabled for cross compiler package
endif
ifeq (java, $(findstring java,$(WITHOUT_LANG)))
  with_java := disabled by environment
endif
ifneq ($(with_cxx),yes)
  with_java := disabled, because C++ disabled: $(with_cxx)
endif

with_java := disabled for the gcc-3.3 build

# Build all packages needed for Java development (gcj, libgcj-dev)
ifeq ($(with_java)-$(with_dev),yes-yes)
  with_javadev := yes
else
  with_javadev := no
endif
ifeq ($(with_java),yes)
  with_libgcj := yes
else
  with_libgcj := no
endif

ifeq ($(with_java),yes)
  enabled_languages += java
endif

# fastjar -------------------
ifeq ($(with_common_pkgs),yes)
  ifdef DEB_CROSS
    with_fastjar := disabled for cross compiler package
  else
  with_fastjar := yes
  endif
else
  with_fastjar := no
endif

with_fastjar := built from gcc-3.4 source

# libffi -------------------
ifeq ($(with_common_libs),yes)
  with_libffi := yes
  no_ffi_archs := hurd-i386 kfreebsd-i386
  ifneq ($(with_java),yes)
    ifneq ($(DEB_TARGET_ARCH),i386)
    ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(no_ffi_archs)))
      with_libffi := disabled for architecure $(DEB_TARGET_ARCH)
    endif
    endif
  endif
  ifdef DEB_CROSS
    with_libffi := disabled for cross compiler package
  endif
endif

with_libffi := disabled for the gcc-3.3 build

# Fortran --------------------
ifeq ($(with_base_only),yes)
  with_fortran := no
else
  with_fortran := yes
endif

ifdef DEB_CROSS
  with_fortran := disabled for cross compiler package
endif
ifeq (f77, $(findstring f77,$(WITHOUT_LANG)))
  with_fortran := disabled by environment
endif

with_fortran := disabled for the gcc-3.3 build

# Build all packages needed for Fortran development
ifeq ($(with_fortran)-$(with_dev),yes-yes)
  with_fdev := yes
else
  with_fdev := no
endif

ifeq ($(with_common_libs)-$(with_fortran),yes-yes)
  with_libg2c := yes
else
  with_libg2c := no
  # ABI changes, keep the package for mipsel
  #ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),mipsel))
  #  with_libg2c := yes
  #endif
endif
with_libg2c := built from the gcc-3.4 sources

ifeq ($(with_fortran),yes)
  enabled_languages += f77
endif

# protoize --------------------
ifeq ($(with_common_pkgs),yes)
  with_proto := yes
  ifdef DEB_CROSS
    with_proto := disabled for cross compiler package
  endif
else
  with_proto := no
endif
#ifeq ($(with_proto),yes)
#  enabled_languages += proto
#endif
with_proto := built from gcc-4.0 sources

# fixincludes --------------------
ifeq ($(with_common_pkgs),yes)
  with_fixincl := yes
  ifdef DEB_CROSS
    with_fixincl := disabled for cross compiler package
  endif
else
  with_fixincl := no
endif
with_fixincl := built from gcc-4.0 sources

# Pascal --------------------
with_pascal := yes
ifneq ($(with_dev),yes)
  with_pascal := no
endif

ifneq ($(DEB_TARGET_ARCH),i386)
  ifeq ($(DEB_TARGET_ARCH),$(findstring $(DEB_TARGET_ARCH),$(pascal_no_archs)))
    with_pascal := disabled for architecture $(DEB_TARGET_ARCH)
  endif
endif
with_gpidump := yes
#ifeq ($(DEB_TARGET_ARCH),$(findstring $(DEB_TARGET_ARCH),s390 powerpc))
#  with_gpidump := disabled for architecture $(DEB_TARGET_ARCH)
#endif
pascal_version := 20030830
ifdef DEB_CROSS
  with_pascal := disabled for cross compiler package
endif
ifeq (pascal, $(findstring pascal,$(WITHOUT_LANG)))
  with_pascal := disabled by environment
endif

with_pascal := disabled for the gcc-3.3 build

ifeq ($(with_pascal),yes)
  enabled_languages += pascal
endif

# ObjC --------------------
ifeq ($(with_base_only),yes)
  with_objc := no
else
  with_objc := yes
endif
# the ObjC runtime with garbage collection enabled needs the Boehm GC
with_objc_gc := yes

# disable ObjC garbage collection library (needs libgc)
libgc_no_archs := avr
ifneq ($(DEB_TARGET_ARCH),i386)
  ifeq ($(DEB_TARGET_ARCH),$(findstring $(DEB_TARGET_ARCH),$(libgc_no_archs)))
    with_objc_gc := disabled for architecture $(DEB_TARGET_ARCH)
  endif
endif
ifdef DEB_CROSS
  with_objc := disabled for cross compiler package
endif
ifeq (objc, $(findstring objc,$(WITHOUT_LANG)))
  with_objc := disabled by environment
endif

with_objc := disabled for the gcc-3.3 build

ifneq ($(with_objc),yes)
  with_objc_gc := $(with_objc)
endif

# Build all packages needed for Objective-C development
ifeq ($(with_objc)-$(with_dev),yes-yes)
  with_objcdev := yes
else
  with_objcdev := no
endif
ifeq ($(with_common_libs)-$(with_objc),yes-yes)
  with_libobjc := yes
else
  with_libobjc := no
endif
with_libobjc := built from the gcc-4.0 sources

ifeq ($(with_objc),yes)
  enabled_languages += objc
endif

# Ada --------------------
with_ada := yes
ifneq ($(with_dev),yes)
  with_ada := no
endif

with_libgnat := yes
ada_no_archs := arm armeb hurd-i386 m68k ppc64 kfreebsd-i386
ifneq ($(DEB_TARGET_ARCH),i386)
  ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(ada_no_archs)))
    with_ada := disabled for architecure $(DEB_TARGET_ARCH)
  endif
endif
ifeq (ada, $(findstring ada,$(WITHOUT_LANG)))
  with_ada := disabled by environment
endif
with_libgnat := no
ifdef DEB_CROSS
  with_ada := disabled for cross compiler package
endif

with_ada := disabled for the gcc-3.3 build

# needed for 3.4 on alpha
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),xxxxx))
  with_ada_bootstrap_workaround := yes
endif

ifeq ($(with_ada),yes)
  enabled_languages += ada
else
  with_libgnat := $(with_ada)
  with_libgnat := disabled
endif

# treelang --------------------
ifeq ($(with_base_only),yes)
  with_treelang := no
else
  ifeq ($(with_dev),yes)
    with_treelang := yes
  else
    with_treelang := no
  endif
endif
tl_no_archs := powerpc
ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(tl_no_archs)))
    with_treelang := disabled for architecure $(DEB_TARGET_ARCH)
endif
ifdef DEB_CROSS
  with_treelang := disabled for cross compiler package
endif
ifeq (treelang, $(findstring treelang,$(WITHOUT_LANG)))
  with_treelang := disabled by environment
endif

with_treelang := disabled for the gcc-3.3 build

ifeq ($(with_treelang),yes)
  enabled_languages += treelang
endif

# Shared libgcc --------------------
with_shared_libgcc := yes

#ifeq ($(with_common_libs),yes)
#  with_libgcc := yes
#else
ifdef DEB_CROSS
  with_libgcc := yes
else
  libgcc_archs := hppa m68k
  ifeq ($(DEB_TARGET_ARCH),i386)
    with_libgcc := no
    with_shared_libgcc := no
  else
    ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(libgcc_archs)))
      with_libgcc := yes
    else
      with_libgcc := no
      with_shared_libgcc := no
    endif
  endif
  with_libgcc := no
  with_shared_libgcc := no
endif
#endif

# run testsuite --------------------
with_check := yes
# If you don't want to run the gcc testsuite, set `with_check' to `no'
#with_check := disabled by hand
ifeq ($(with_base_only),yes)
  with_check := no
endif
ifdef DEB_CROSS
  with_check := disabled for cross compiler package
endif
check_no_archs := hurd-i386
ifneq ($(DEB_TARGET_ARCH),i386)
  ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),$(check_no_archs)))
    with_check :=disabled for $(DEB_TARGET_ARCH)
  endif
endif
ifneq ($(WITHOUT_CHECK),)
  with_check := disabled by environment
endif

# powerpc nof libraries --------------------
with_libnof := no

biarch := no

with_lib64gcc	:= no
with_lib64cxx	:= no
with_lib64objc	:= no
with_lib64ffi	:= no
with_lib64gcj	:= no
with_lib64g2c	:= no

# i386/x86_64 build
ifeq ($(DEB_TARGET_ARCH),XXXi386-XXX)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
  export TARGET32_MACHINE = i486-linux-gnu
  export TARGET64_MACHINE = x86_64-linux-gnu
endif

# hppa64 build --------------------
with_hppa64 := no
ifeq ($(DEB_TARGET_ARCH),hppa)
  ifneq ($(DEB_CROSS),yes)
    with_hppa64 := yes
  endif
endif
with_hppa64 := not built anymore from the gcc-3.3 source

# sparc64 build --------------------
ifeq ($(DEB_TARGET_ARCH),sparc)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
  export TARGET32_MACHINE = sparc-linux-gnu
  export TARGET64_MACHINE = sparc64-linux-gnu
endif

# s390x build --------------------
ifeq ($(DEB_TARGET_ARCH),s390)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
  export TARGET32_MACHINE = s390-linux-gnu
  export TARGET64_MACHINE = s390x-linux-gnu
endif

# GNU locales
force_gnu_locales := yes

unpack_stamp		:= $(stampdir)/01-unpack-stamp
patch_stamp		:= $(stampdir)/02-patch-stamp
control_stamp		:= $(stampdir)/03-control-stamp
configure_stamp		:= $(stampdir)/04-configure-stamp
build_stamp		:= $(stampdir)/05-build-stamp
build_html_stamp	:= $(stampdir)/05-build-html-stamp
build_doxygen_stamp	:= $(stampdir)/05-build-doxygen-stamp
check_stamp		:= $(stampdir)/06-check-stamp
check32_stamp		:= $(stampdir)/06-check-3.2-stamp
install_stamp		:= $(stampdir)/07-install-stamp
binary_stamp		:= $(stampdir)/08-binary-stamp

configure_dummy_stamp	:= $(stampdir)/04-configure-dummy-stamp
build_dummy_stamp	:= $(stampdir)/05-build-dummy-stamp
install_dummy_stamp	:= $(stampdir)/07-install-dummy-stamp

configure_hppa64_stamp	:= $(stampdir)/04-configure-hppa64-stamp
build_hppa64_stamp	:= $(stampdir)/05-build-hppa64-stamp
install_hppa64_stamp	:= $(stampdir)/07-install-hppa64-stamp

ifeq ($(with_base_only),yes)
  control_dependencies = $(patch_stamp)
  configure_dependencies = $(configure_dummy_stamp)
  build_dependencies = $(build_dummy_stamp)
  install_dependencies = $(install_dummy_stamp)
else
  control_dependencies = $(patch_stamp)
  configure_dependencies = $(configure_stamp)
  build_dependencies = $(build_stamp)
  ifeq ($(with_check),yes)
      build_dependencies += check
  endif
  install_dependencies = $(install_stamp)
endif

ifeq ($(with_hppa64),yes)
  build_dependencies += $(build_hppa64_stamp)
  install_dependencies += $(install_hppa64_stamp)
endif

stamp-dir:
	mkdir -p $(stampdir)
