# -*- makefile -*-
# rules.conf
#	- used to build debian/control and debian/rules.parameters
#	- assumes unpacked sources

include debian/rules.defs

# The binutils version needed.
# The oldest suitable versions for the various platforms can be found in
# INSTALL/specific.html ; we take a tighter dependency if possible to be on
# the safe side (something like newest( version in stable, versions for the
# various platforms in INSTALL/specific.html) ).
BINUTILSV = 2.15-7
ifdef DEB_CROSS
BINUTILS_BUILD_DEP = binutils$(TS) (>= $(BINUTILSV))
else
BINUTILS_BUILD_DEP = binutils (>= $(BINUTILSV)) | binutils-multiarch (>= $(BINUTILSV))
endif

# libc-dev dependencies
libc_ver := 2.3.2.ds1-16
ifeq ($(distribution),Ubuntu)
  libc_ver := 2.3.5-1ubuntu4
endif
ifeq ($(findstring linux,$(DEB_TARGET_GNU_SYSTEM)),linux)
  ifeq ($(DEB_TARGET_ARCH), $(findstring $(DEB_TARGET_ARCH),alpha ia64))
    LIBC_DEP = libc6.1-dev$(LS) (>= $(libc_ver))
  else
    LIBC_DEP = libc6-dev$(LS) (>= $(libc_ver))
  endif
else
  ifeq ($(DEB_TARGET_GNU_SYSTEM),gnu)
    LIBC_DEP = libc0.3-dev$(LS)
  endif
  ifeq ($(DEB_TARGET_GNU_SYSTEM),kfreebsd-gnu)
    LIBC_DEP = libc0.1-dev$(LS)
  endif
endif

LIBC_BUILD_DEP = libc6.1-dev (>= $(libc_ver)) [alpha ia64] | libc0.3-dev [hurd-i386] | libc0.1-dev [kfreebsd-i386] | libc6-dev (>= $(libc_ver))
LIBC_BIARCH_BUILD_DEP = libc6-dev-sparc64 [sparc], libc6-dev-s390x [s390],

ifdef DEB_CROSS
  # When building a cross compiler, the libc-dev build dependancy must be
  # sensitive to the target architecture; the host architecture is irrelevant.
  LIBC_BUILD_DEP = $(LIBC_DEP)
  LIBC_BIARCH_BUILD_DEP =
endif

SOURCE_VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$NF}')
DEB_VERSION := $(shell echo $(SOURCE_VERSION) | sed 's/ds[0-9]*//')
DEB_VERSION_NOREL := $(shell echo $(DEB_VERSION) | sed 's/-[^-]*$$//')
BASE_VERSION := $(shell echo $(DEB_VERSION) | sed 's/.*:\([1-9]\.[0-9]\).*-.*/\1/')

# The version number can be found in gcc/version.c e.g.
# char *version_string = "2.95 19990710 (prerelease)";
VER := $(strip $(shell grep version_string $(srcdir)/gcc/version.c \
			| sed -e 's/^.*= *"//' -e 's/ .*$$//' -e 's/".*$$//'))
# The numeric part of the gcc version number (x.yy.zz)
VERNO := $(strip $(shell echo $(VER) | sed -e 's/gcc-//'))
NEXTVERNO := $(shell echo $(VERNO) | \
		awk -F. '{OFS="."; if (NF==2) $$3=1; else $$NF += 1; print}')
# first version with a new path component in gcc_lib_dir (i.e. GCC_VERSION
# or TARGET_ALIAS changes)
BREAKS_VERSION = 1:3.3.6-7
GPC_DATE := $(shell sed -n 's/^.define GPC_VERSION_STRING "\(.*\)"/\1/p' $(srcdir)/gcc/p/version.h || true)
DEB_GPC_VERSION := $(shell echo $(DEB_VERSION) \
			   | sed 's/.*:\(.*\)-\(.*\)/2:\1.$(GPC_DATE)-\2/')
# semiautomatic ...
DEB_SOVERSION := 1:$(VERNO)-1
DEB_SOVERSION := $(DEB_VERSION)
DEB_SOVERSION := 1:3.3.4-1
DEB_STDCXX_SOVERSION := $(DEB_SOVERSION)
DEB_FFI_SOVERSION := $(DEB_SOVERSION)
DEB_OBJC_SOVERSION := 1:3.3.4-4
DEB_GCC_SOVERSION := 1:3.3.4-3

# manual ...
GCC_SONAME := 1

CXX_SONAME := $(strip $(shell awk -F= \
			'/^libtool_VERSION/ {split($$2,v,":"); print v[1]}' \
			$(srcdir)/libstdc++-v3/configure.in))

OBJC_SONAME := $(strip $(shell awk '/^LIBOBJC_VERSION/ \
					{split($$3,v,":"); print v[1]}' \
				$(srcdir)/libobjc/Makefile.in))

F77_SONAME := $(strip $(shell awk -F= \
			'/^VERSION_MAJOR/ {split($$2,v,":"); print v[1]}' \
				$(srcdir)/libf2c/Makefile.in))

GCJ_SONAME := $(strip $(shell tail -1 $(srcdir)/libjava/libtool-version \
				  | awk -F: '{ print $$1 }' ))

FFI_SONAME := $(strip $(shell tail -1 $(srcdir)/libffi/libtool-version \
				  | awk -F: '{ print $$1 }' ))

GNAT_SONAME := $(strip $(shell \
		grep '[^_]Library_Version.*:' $(srcdir)/gcc/ada/gnatvsn.ads \
		  | sed -e 's/.*GNAT Lib v\(.*\)[ "].*/\1/'))

GNAT_VERSION := $(BASE_VERSION)

GPC_BASE_VERSION := 2.1

pkg_ver := -$(BASE_VERSION)
gpc_pkg_ver := -$(GPC_BASE_VERSION)$(pkg_ver)

ctrl_flags = \
	-DLIBGCC_CV=$(DEB_GCC_SOVERSION) \
	-DCV=$(DEB_VERSION) \
	-DSOFT_CV=$(DEB_VERSION_NOREL) \
	-DNV=1:$(NEXTVERNO) \
	-DBREAKV=$(BREAKS_VERSION) \
	-DGPC_CV=$(DEB_GPC_VERSION) \
	-DBINUTILSV=$(BINUTILSV) \
	-DSRCNAME=$(PKGSOURCE) \
	-D__$(DEB_TARGET_GNU_CPU)__ \
	-DARCH=$(DEB_TARGET_GNU_CPU)
ifeq ($(with_objc)-$(with_objc_gc),yes-yes)
  ctrl_flags +=	-DOBJC_GC
endif

ctrl_flags += \
	-DLIBC_DEP="$(LIBC_DEP)" \
	-DLIBC_BIARCH_BUILD_DEP="$(LIBC_BIARCH_BUILD_DEP)" \
	-DBINUTILS_BUILD_DEP="$(BINUTILS_BUILD_DEP)" \
	-DLIBC_BUILD_DEP="$(LIBC_BUILD_DEP)" \
	-DTP=$(TP) \
	-DTS=$(TS) \
	-DLS=$(LS)

ifdef DEB_CROSS
  ifeq ($(DEB_TARGET_ARCH),sparc)
    TARGETBD = , libc6-dev-sparc64-sparc-cross
  endif
  ifeq ($(DEB_TARGET_ARCH),s390)
    TARGETBD = , libc6-dev-s390x-s390-cross
  endif
  ifeq ($(DEB_TARGET_ARCH),ia64)
    TARGETBD = , libunwind7-dev-ia64-cross (>= 0.98.3-3)
  endif
  ctrl_flags += \
	-DTARGET=$(DEB_TARGET_ARCH) \
	-DTARGETBD="$(TARGETBD)"
else
  # add '-DPRI=optional' to ctrl_flags if this is not the default compiler
  ctrl_flags += \
	-DPRI=optional
endif

ifeq ($(with_base_only),yes)
  ctrl_flags += \
	-DBASE_ONLY=yes
endif

control: control-file parameters-file versioned-files

ifdef DEB_CROSS
  languages = c c++
  addons = libgcc lib64gcc libcxx lib64cxx cdev c++dev
else
  languages = c c++
  addons = libcxx #libgcc cdev c++dev libnof libs lib64cxx libnof 
  #languages += ada f77 java objc pascal treelang
  #addons += fastjar fdev fixincl javadev libg2c libffi libgcj libobjc \
  #	   lib64ffi lib64gcj lib64objc lib64g2c objcdev proto libgnat \
  #	   lib64gcc
endif

ifneq ($(GFDL_INVARIANT_FREE),yes)
  addons += gfdldoc
endif

control-file:
	echo "addons: $$addons"; \
	m4 $(ctrl_flags) \
	  -DPV=$(pkg_ver) \
	  -DGPC_PV=$(gpc_pkg_ver) \
	  -DCXX_SO=$(CXX_SONAME) \
	  -DGCC_SO=$(GCC_SONAME) \
	  -DOBJC_SO=$(OBJC_SONAME) \
	  -DG2C_SO=$(F77_SONAME) \
	  -DGCJ_SO=$(GCJ_SONAME) \
	  -DGNAT_SO=$(GNAT_SONAME) \
	  -DGNAT_V=$(GNAT_VERSION) \
	  -DFFI_SO=$(FFI_SONAME) \
	  -Denabled_languages="$(languages) $(addons)" \
	  -Dada_no_archs="$(foreach arch,$(ada_no_archs),!$(arch))" \
	  -Djava_no_archs="$(foreach arch,$(java_no_archs),!$(arch))" \
	  -Dpascal_no_archs="$(foreach arch,$(pascal_no_archs),!$(arch))" \
	  -Dlibgc_no_archs="$(foreach arch,$(libgc_no_archs),!$(arch))" \
	  -Dcheck_no_archs="$(foreach arch,$(check_no_archs),!$(arch))" \
	  -Dlocale_no_archs="$(foreach arch,$(locale_no_archs) $(check_no_archs),!$(arch))" \
		debian/control.m4 > debian/control.tmp2
	uniq debian/control.tmp2 > debian/control.tmp
	rm -f debian/control.tmp2
	[ -e debian/control ] \
	  && cmp -s debian/control debian/control.tmp \
	  && rm -f debian/control.tmp && exit 0; \
	  mv debian/control.tmp debian/control; touch $(control_stamp)

parameters-file:
	rm -f debian/rules.parameters.tmp
	( \
		echo '# configuration parameters taken from upstream source files'; \
		echo 'VER		:= $(VER)'; \
		echo 'BASE_VERSION	:= $(BASE_VERSION)'; \
		echo 'SOURCE_VERSION	:= $(SOURCE_VERSION)'; \
		echo 'DEB_VERSION	:= $(DEB_VERSION)'; \
		echo 'GPC_BASE_VERSION	:= $(GPC_BASE_VERSION)'; \
		echo 'DEB_GPC_VERSION	:= $(DEB_GPC_VERSION)'; \
		echo 'DEB_SOVERSION	:= $(DEB_SOVERSION)'; \
		echo 'DEB_FFI_SOVERSION	:= $(DEB_FFI_SOVERSION)'; \
		echo 'DEB_GCC_SOVERSION	:= $(DEB_GCC_SOVERSION)'; \
		echo 'DEB_OBJC_SOVERSION	:= $(DEB_OBJC_SOVERSION)'; \
		echo 'DEB_STDCXX_SOVERSION	:= $(DEB_STDCXX_SOVERSION)'; \
		echo 'GCC_SONAME	:= $(GCC_SONAME)'; \
		echo 'CXX_SONAME	:= $(CXX_SONAME)'; \
		echo 'F77_SONAME	:= $(F77_SONAME)'; \
		echo 'OBJC_SONAME	:= $(OBJC_SONAME)'; \
		echo 'GCJ_SONAME	:= $(GCJ_SONAME)'; \
		echo 'GNAT_VERSION	:= $(GNAT_VERSION)'; \
		echo 'GNAT_SONAME	:= $(GNAT_SONAME)'; \
		echo 'FFI_SONAME	:= $(FFI_SONAME)'; \
		echo 'LIBC_DEP		:= $(LIBC_DEP)'; \
	) > debian/rules.parameters.tmp
	[ -e debian/rules.parameters ] \
	  && cmp -s debian/rules.parameters debian/rules.parameters.tmp \
	  && rm -f debian/rules.parameters.tmp && exit 0; \
	  mv debian/rules.parameters.tmp debian/rules.parameters; \
	  touch $(control_stamp)

versioned-files:
	for f in debian/*-BV*; do \
	  f2=$$(echo $$f \
		| sed 's/BV/$(BASE_VERSION)/;s/PV/$(GPC_BASE_VERSION)/'); \
	  sed -e 's/@BV@/$(BASE_VERSION)/g' \
	      -e 's/@PV@/$(GPC_BASE_VERSION)/g' \
	      -e 's/@GFDL@/$(if $(filter yes,$(GFDL_INVARIANT_FREE)),#)/g' \
	    $$f > $$f2; \
	  touch -r $$f $$f2; \
	done
