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

SHELL		= /bin/bash -e	# brace expansion used in rules file
PWD		:= $(shell pwd)
srcdir		= $(PWD)/src
builddir	= $(PWD)/build
builddir_hppa64	= $(PWD)/build-hppa64
stampdir	= stamps

lib64		= lib64

# enable newlib support
#with_newlib:=yes
#WITHOUT_LANG=c++ # Dissbale c++ support

# version of default gcc compiler (e.g. 3.3 or 3.4)
BUILD_CC_VERSION:= $(shell gcc -dumpversion | sed 's/\([0-9]*\.[0-9]*\)\(.*\)/\1/')

# 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_TARGET_ARCH		:= $(shell dpkg-architecture -f \
			-a$(GCC_TARGET) -qDEB_HOST_ARCH 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),)
  ifeq ($(GCC_TARGET),none)
    DEB_TARGET_ARCH:=$(shell echo $(DEB_TARGET_ALIAS) | sed -e 's/\-.*//')
    DEB_TARGET_GNU_CPU:=$(DEB_TARGET_ARCH)
    DEB_TARGET_GNU_SYSTEM:=$(shell echo $(DEB_TARGET_ALIAS) | grep -- - | sed -e 's/[^-]*\-//')
    DEB_TARGET_GNU_TYPE:=$(DEB_TARGET_ALIAS)
  else
    $(error GCC_TARGET value "$(GCC_TARGET)" is not a valid Debian architecture)
  endif
endif
  
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_TARGET_GNU_TYPE))
  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).
  TP = $(DEB_TARGET_GNU_TYPE)-
  DEB_TARGET_ALIAS ?= $(DEB_TARGET_GNU_TYPE)
  TS = -$(DEB_TARGET_ALIAS)
endif

ifeq ($(DEB_CROSS),yes)
  TARGET_ALIAS := $(DEB_TARGET_ALIAS)
else
  TARGET_ALIAS := $(DEB_TARGET_GNU_TYPE)
  # overwrite ix86 CPU defaults
  ifeq ($(TARGET_ALIAS),i386-linux)
    TARGET_ALIAS := i486-linux
  endif
  ifeq ($(TARGET_ALIAS),i386-gnu)
    TARGET_ALIAS := i586-gnu
  endif
  ifeq ($(TARGET_ALIAS),i386-kfreebsd-gnu)
    TARGET_ALIAS := i486-kfreebsd-gnu
  endif
  ifeq ($(TARGET_ALIAS),i386-knetbsd-gnu)
    TARGET_ALIAS := i486-knetbsd-gnu
  endif
endif

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

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

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 --------------------
GFDL_INVARIANT_FREE := no

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

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

# ... and some libraries, which do not change (libffi2, libg2c, libobjc1).
with_common_libs := built from gcc-3.3

# Build common_pkgs and common_libs for amd64/gcc-3.4
ifeq ($(DEB_TARGET_GNU_CPU),x86_64)
  ifneq ($(BUILD_CC_VERSION),3.3)
    with_common_pkgs := yes
    with_common_libs := yes
  endif
endif

#no_common_libs := s390
#ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(no_common_libs)))
#  with_common_libs := no
#endif

with_dev := yes

#no_dummy_cpus := ia64 i386 hppa s390 sparc
#with_base_only := yes
#ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(no_dummy_cpus)))
#  with_base_only := no
#  with_common_libs := yes
#  with_common_pkgs := yes
#else
#  with_common_libs := no
#  with_common_pkgs := no
#  with_dev := no
#endif

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

# "old" arm ABI (with 3.5, we get a new one) --------------------
ifeq ($(DEB_TARGET_GNU_CPU),arm)
  with_oldarmabi := yes
endif

# 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_cpus := avr
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(no_cxx_cpus)))
  with_cxx := disabled for architecture $(DEB_TARGET_GNU_CPU)
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_common_pkgs)-$(with_cxx),yes-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_cpus :=
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(debug_no_cpus)))
    with_debug := disabled for architecure $(DEB_TARGET_GNU_CPU)
endif


# __cxa_atexit -------------
# The __cxa_atexit API does not exist in some libc version (NetBSD)
with_cxa_atexit := yes

no_cxa_systems := netbsd-elf-gnu
ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(no_cxa_systems)))
  with_cxa_atexit := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif

ifeq ($(with_newlib),yes)
    with_cxa_atexit := disabled for newlib
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_cpus := mips mipsel m32r
java_no_systems := hurd-i386 kfreebsd-gnu knetbsd-gnu netbsd-elf-gnu
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(java_no_cpus)))
  with_java := disabled for architecture $(DEB_TARGET_GNU_CPU)
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(java_no_systems)))
  with_java := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif
ifeq ($(DEB_TARGET_ARCH),hurd-i386)
  with_java := disabled for architecure $(DEB_TARGET_ARCH)
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

# 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)-$(with_java),yes-yes)
#ifeq ($(with_java),yes)
#  ifdef DEB_CROSS
#    with_fastjar := disabled for cross compiler package
#  else
#    with_fastjar := yes
#  endif
#else
#  with_fastjar := no
#endif

ifdef DEB_CROSS
  with_fastjar := disabled for cross compiler package
else
  with_fastjar := yes
endif

# libffi -------------------
# build with another soname.
#ifeq ($(with_common_libs),yes)
  with_libffi := yes
  no_ffi_cpus :=
  no_ffi_systems := hurd-i386 kfreebsd-gnu knetbsd-gnu netbsd-elf-gnu
  ifneq ($(with_java),yes)
    ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(no_ffi_cpus)))
      with_libffi := disabled for architecure $(DEB_TARGET_GNU_CPU)
    endif
    ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(no_ffi_systems)))
      with_libffi := disabled for $(DEB_TARGET_GNU_SYSTEM)
    endif
    ifeq ($(DEB_TARGET_ARCH),hurd-i386)
      with_libffi := disabled for architecure $(DEB_TARGET_ARCH)
    endif
  endif
  ifdef DEB_CROSS
    with_libffi := disabled for cross compiler package
  endif
#endif

# 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

# 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)
  ifeq ($(DEB_TARGET_GNU_CPU),$(findstring $(DEB_TARGET_GNU_CPU),mipsel))
    with_libg2c := disable on mipsel for gcc-3.4
  else
    with_libg2c := yes
  endif
else
  with_libg2c := no
endif

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

# 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

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

pascal_no_systems := netbsd-elf-gnu
ifeq ($(DEB_TARGET_GNU_SYSTEM),$(findstring $(DEB_TARGET_GNU_SYSTEM),$(pascal_no_systems)))
  with_pascal := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif
with_gpidump := yes
ifeq ($(DEB_TARGET_GNU_CPU),$(findstring $(DEB_TARGET_GNU_CPU),mips mipsel))
  with_gpidump := disabled for architecture $(DEB_TARGET_GNU_CPU)
endif
pascal_version := 20030830
ifeq (pascal, $(findstring pascal,$(WITHOUT_LANG)))
  with_pascal := disabled by environment
endif
ifdef DEB_CROSS
  with_pascal := disabled for cross compiler package
endif
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_cpus := avr
libgc_no_systems := kfreebsd-gnu knetbsd-gnu
ifeq ($(DEB_TARGET_GNU_CPU),$(findstring $(DEB_TARGET_GNU_CPU),$(libgc_no_cpus)))
  with_objc_gc := disabled for architecture $(DEB_TARGET_GNU_CPU)
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM),$(findstring $(DEB_TARGET_GNU_SYSTEM),$(libgc_no_systems)))
  with_objc_gc := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif
with_objc_gc := disabled for gcc-3.4
ifdef DEB_CROSS
  with_objc := disabled for cross compiler package
endif
ifeq (objc, $(findstring objc,$(WITHOUT_LANG)))
  with_objc := disabled by environment
endif

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

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_cpus := arm m68k m32r
ada_no_systems := hurd-i386 kfreebsd-gnu knetbsd-gnu netbsd-elf-gnu
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(ada_no_cpus)))
  with_ada := disabled for architecure $(DEB_TARGET_GNU_CPU)
endif
ifeq ($(DEB_TARGET_ARCH),hurd-i386)
  with_ada := disabled for architecure $(DEB_TARGET_ARCH)
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(ada_no_systems)))
  with_ada := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif
ifeq (ada, $(findstring ada,$(WITHOUT_LANG)))
  with_ada := disabled by environment
endif
ifdef DEB_CROSS
  with_ada := disabled for cross compiler package
endif

# needed for 3.4 on alpha, but not for release tarballs
ifeq ($(DEB_HOST_GNU_CPU),$(findstring $(DEB_HOST_GNU_CPU),alpha))
  with_ada_bootstrap_workaround := yes
endif

ifeq ($(with_ada),yes)
  enabled_languages += ada
  libada_no_cpus := alpha mips mipsel powerpc s390 sparc m32r
  ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(libada_no_cpus)))
    with_libgnat := disabled for architecure $(DEB_TARGET_GNU_CPU)
  endif
else
  with_libgnat := $(with_ada)
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_cpus := powerpc
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(tl_no_cpus)))
    with_treelang := disabled for architecure $(DEB_TARGET_GNU_CPU)
endif
ifdef DEB_CROSS
  with_treelang := disabled for cross compiler package
endif
ifeq (treelang, $(findstring treelang,$(WITHOUT_LANG)))
  with_treelang := disabled by environment
endif

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

# Shared libgcc --------------------
ifeq ($(GCC_TARGET),none)
  with_libgcc := no
  with_shared_libgcc := no
else
  with_libgcc := yes
  with_shared_libgcc := yes
endif

#ifeq ($(with_common_libs),yes)
#  with_libgcc := yes
#else
#  with_libgcc := no
#  with_shared_libgcc := no
#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_systems := hurd-i386 kfreebsd-gnu knetbsd-gnu
ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(check_no_systems)))
  with_check := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif
check_no_cpus := # arm m68k
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),$(check_no_cpus)))
  with_check := disabled for architecure $(DEB_TARGET_GNU_CPU)
endif
ifeq ($(DEB_TARGET_ARCH),hurd-i386)
  with_check := disabled for architecure $(DEB_TARGET_ARCH)
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_GNU_TYPE),i386-linux)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
  # These should be included but are postponed until after Sarge.
  #with_lib64objc:= yes
  #with_lib64ffi	:= yes
  #with_lib64gcj	:= yes
  #with_lib64g2c	:= yes
endif

ifeq ($(DEB_TARGET_ARCH),amd64)
  biarch_ia32 := yes
endif
#ifeq ($(DEB_TARGET_ARCH),ia64)
#  biarch_ia32 := yes
#endif

# hppa64 build --------------------
with_hppa64 := no
ifeq ($(DEB_TARGET_GNU_CPU),hppa)
  ifneq ($(DEB_CROSS),yes)
    with_hppa64 := yes
  endif
endif

# powerpc64 build --------------------
ifeq ($(DEB_TARGET_GNU_CPU),XXXpowerpcXXX)
  biarch := yes
  with_lib64gcc := yes
  ifeq ($(TARGET_ALIAS),powerpc-linux)
    TARGET_ALIAS := powerpc64-linux
  endif
endif

# sparc64 build --------------------
ifeq ($(DEB_TARGET_GNU_CPU),sparc)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
endif

# s390x build --------------------
ifeq ($(DEB_TARGET_GNU_CPU),s390)
  biarch := yes
  with_lib64gcc	:= yes
  with_lib64cxx	:= yes
endif

# GNU locales
force_gnu_locales := yes
locale_no_systems := kfreebsd-gnu knetbsd-gnu netbsd-elf-gnu
ifeq ($(DEB_TARGET_GNU_SYSTEM), $(findstring $(DEB_TARGET_GNU_SYSTEM),$(locale_no_systems)))
  force_gnu_locales := disabled for $(DEB_TARGET_GNU_SYSTEM)
endif

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
check_inst_stamp	:= $(stampdir)/06-check-inst-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)
