# -*- 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-5
ifdef DEB_CROSS
  BINUTILS_BUILD_DEP = binutils-$(DEB_TARGET_GNU_TYPE)
else
  BINUTILS_BUILD_DEP = binutils (>= $(BINUTILSV)) | binutils-multiarch (>= $(BINUTILSV))
endif

# libc-dev dependencies
libc_ver := 2.3.2.ds1-16
ifeq ($(DEB_TARGET_GNU_SYSTEM),linux)
  ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),alpha ia64))
    LIBC_DEP = libc6.1-dev (>= $(libc_ver))
  else
    LIBC_DEP = libc6-dev (>= $(libc_ver))
  endif
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM),gnu)
  LIBC_DEP = libc0.3-dev
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM),kfreebsd-gnu)
  LIBC_DEP = libc0.1-dev
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM),knetbsd-gnu)
  LIBC_DEP = libc0.1-dev
endif
ifeq ($(DEB_TARGET_GNU_SYSTEM),netbsd-elf-gnu)
  LIBC_DEP = libc12-dev
endif
LIBC_BUILD_DEP = libc6.1-dev (>= $(libc_ver)) [alpha ia64] | libc0.3-dev (>= $(libc_ver)) | libc0.1-dev (>= $(libc_ver)) | libc12-dev (>= $(libc_ver)) | libc6-dev (>= $(libc_ver))

SOURCE_VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$NF}')
DEB_VERSION := $(shell echo $(SOURCE_VERSION) \
	| sed -e 's/.*://' -e 's/ds[0-9]*//')
# epoch used for gcc versions up to 3.3.x, now used for some remaining
# libraries: libgcc1, libobjc1, libffi2, libffi2-dev, libg2c0
EPOCH := 1
DEB_EVERSION := $(EPOCH):$(DEB_VERSION)
BASE_VERSION := $(shell echo $(DEB_VERSION) | sed -e '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)
NEXTVER := $(shell echo $(VER) | \
		awk -F. '{OFS="."; if (NF==2) $$3=1; else $$NF += 1; print}')
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 := $(VER)-1
DEB_SOVERSION := $(DEB_VERSION)
DEB_SOVERSION := 3.4.1-3
DEB_SOEVERSION := $(EPOCH):3.4.1-3
DEB_STDCXX_SOVERSION := 3.4.3-1
DEB_FFI_SOVERSION := $(DEB_SOVERSION)

# manual ...
ifeq ($(DEB_TARGET_GNU_CPU), $(findstring $(DEB_TARGET_GNU_CPU),hppa m68k))
  GCC_SONAME := 2
  DEB_LIBGCC_SOVERSION := $(DEB_SOVERSION)
  DEB_LIBGCC_VERSION := $(DEB_VERSION)
else
  GCC_SONAME := 1
  DEB_LIBGCC_SOVERSION := $(DEB_SOEVERSION)
  DEB_LIBGCC_VERSION :=  $(DEB_EVERSION)
endif

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

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/.*"\([^"]*\)".*/\1/'))

GNAT_VERSION := $(BASE_VERSION)

GPC_BASE_VERSION := 2.1

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

ctrl_flags = \
	-DCV=$(DEB_VERSION) \
	-DNV=$(NEXTVER) \
	-DCEV=$(DEB_EVERSION) \
	-DNEV=1:$(NEXTVER) \
	-DGPC_CV=$(DEB_GPC_VERSION) \
	-DBINUTILSV=$(BINUTILSV) \
	-DSRCNAME=$(PKGSOURCE) \
	-D__$(DEB_TARGET_GNU_CPU)__ \
	-DARCH=$(DEB_TARGET_ARCH)
ifeq ($(with_objc)-$(with_objc_gc),yes-yes)
  ctrl_flags +=	-DOBJC_GC
endif
ctrl_flags +=	-DG2C_CEV=1:3.3.4-4

ifeq ($(with_oldarmabi),yes)
ctrl_flags += \
	-DOLDARMABI=yes
endif

# hack to rename the amd64 libstdc++ packages ...
ifeq ($(with_oldcxxabi),yes)
  ctrl_flags += \
	-DOLDARMABI=yes
endif

# hack to rename the amd64 libstdc++ packages ...
ifeq ($(with_oldcxxabi),yes)
  ctrl_flags += \
	-DOLDARMABI=yes
endif

ctrl_flags += \
	-DLIBC_DEP="$(LIBC_DEP)" \
	-DBINUTILS_BUILD_DEP="$(BINUTILS_BUILD_DEP)" \
	-DLIBC_BUILD_DEP="$(LIBC_BUILD_DEP)"

ifdef DEB_CROSS
  ctrl_flags += \
	-DTARGET=$(DEB_TARGET_ARCH) \
	-DTP=$(TP) \
	-DTS=$(TS) \
	-DPRI=optional
else
  # XXX change when 3.4 becomes 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 = libcxx lib64cxx cdev c++dev
  ifeq ($(with_libgcc),yes)
    addons += libgcc lib64gcc 
  endif
else
  languages = ada c c++ f77 java objc pascal treelang
  addons = cdev c++dev fastjar fdev fixincl javadev libcxx libg2c \
	   libgcc libffi libgcj libgnat libnof libobjc libs \
	   lib64gcc lib64cxx lib64ffi lib64gcj lib64gnat \
	   lib64objc lib64g2c libnof objcdev proto
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_cpus) $(ada_no_systems),!$(arch))" \
	  -Djava_no_archs="$(foreach arch,$(java_no_cpus) $(java_no_systems),!$(arch))" \
	  -Dpascal_no_archs="$(foreach arch,$(pascal_no_cpus) $(pascal_no_systems),!$(arch))" \
	  -Dlibgc_no_archs="$(foreach arch,$(libgc_no_cpus) $(libgc_no_systems),!$(arch))" \
	  -Dcheck_no_archs="$(foreach arch,$(check_no_cpus) $(check_no_systems),!$(arch))" \
	  -Dlocale_no_archs="$(foreach arch,$(locale_no_cpus) $(locale_no_systems),!$(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 'DEB_EVERSION	:= $(DEB_EVERSION)'; \
		echo 'GPC_BASE_VERSION	:= $(GPC_BASE_VERSION)'; \
		echo 'DEB_GPC_VERSION	:= $(DEB_GPC_VERSION)'; \
		echo 'DEB_SOVERSION	:= $(DEB_SOVERSION)'; \
		echo 'DEB_SOEVERSION	:= $(DEB_SOEVERSION)'; \
		echo 'DEB_LIBGCC_SOVERSION	:= $(DEB_LIBGCC_SOVERSION)'; \
		echo 'DEB_LIBGCC_VERSION	:= $(DEB_LIBGCC_VERSION)'; \
		echo 'DEB_STDCXX_SOVERSION	:= $(DEB_STDCXX_SOVERSION)'; \
		echo 'DEB_FFI_SOVERSION	:= $(DEB_FFI_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' \
	    $$f > $$f2; \
	  touch -r $$f $$f2; \
	done
