#! /usr/bin/make -f
# -*- makefile -*-
# debian/rules file for GNU libc.
# Copyright 1998, 1999 by Joel Klecker <espy@debian.org>
# Copyright 2000 Ben Collins <bcollins@debian.org>
# Copyright 2003 Jeff Bailey <jbailey@debian.org>
# This Makefile is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# What are the phases to building glibc?

# Unpack main tarball
# Do any necessary overlays
# Apply patches
# Determine how many builds there are to do.  For Each build:

#  Create build directory
#  Configure
#  Build
#  Test as desired
#  Install to package directories

# Run debian magic to build packages.

# Things you may need to customise:

# These are done as absolute paths so that in the case of filesystem size
# limitations, they can be overridden and spread all over.
build-tree := build-tree
stamp := $(CURDIR)/stamp-dir/
DUMMY := $(shell mkdir -p $(stamp))

# The minimum package version with which these packages are compatible.
include debian/shlibver
shlib_dep = $(libc) (>= $(shlib_dep_ver))

# The version of the C library that locales needs for its format.
include debian/locales-depver

# Beyond here you shouldn't need to customise anything:
export SHELL          = /bin/bash -e

DEB_HOST_ARCH         ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_CPU      ?= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_TYPE     ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_GNU_SYSTEM   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_MULTIARCH    ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_BUILD_ARCH        ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_GNU_CPU     ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_BUILD_GNU_TYPE    ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_SYSTEM  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)

DEB_HOST_GNU_CPU_ALT  ?=
DEB_HOST_GNU_TYPE_ALT ?=

DEB_SOURCE_PACKAGE := $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))

DEB_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
GLIBC_VERSION = $(shell echo $(DEB_VERSION) | sed -e 's/.*://' -e 's/-.*//')

DEB_BUILDDIR ?= $(build-tree)/$(DEB_HOST_ARCH)-$(curpass)

GLIBC_SOURCES = $(addprefix $(shell basename $(CURDIR))/, \
		 $(filter-out debian $(shell basename $(stamp)) $(build-tree), $(wildcard *)))
		 
# The kernel uses different CPU appreviations (uname output) than the GNU
# tools do (config.sub).  Map them.
KERNEL_HOST_CPU := $(subst powerpc,ppc,$(DEB_HOST_GNU_CPU))

# Support multiple makes at once based on number of processors
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS := -j $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# use the package settings, not the settings from the environment
define unsetenv
  unexport $(1)
  $(1) =
endef
$(foreach v, CPPFLAGS CFLAGS CXXFLAGS LDFLAGS, $(if $(filter environment,$(origin $(v))),$(eval $(call unsetenv, $(v)))))

# Default setup
GLIBC_PASSES ?= libc

prefix=/usr
bindir=$(prefix)/bin
datadir=$(prefix)/share
localedir=$(prefix)/lib/locale
sysconfdir=/etc
libexecdir=$(prefix)/lib
rootsbindir=/sbin
includedir=$(prefix)/include
docdir=$(prefix)/share/doc
mandir=$(prefix)/share/man
sbindir=$(prefix)/sbin
vardbdir=/var/lib/misc
rtlddir=/lib
slibdir=/lib/$(DEB_HOST_MULTIARCH)
libdir=/usr/lib/$(DEB_HOST_MULTIARCH)


BASE_CC = gcc
BASE_CXX = g++
DEB_GCC_VERSION ?= -4.8

RUN_TESTSUITE = yes

# Set cross and native compiler names, including version. On *i386 we need to
# change the GNU triplet as it doesn't match the compiler name.
CC     = $(DEB_HOST_GNU_TYPE)-$(BASE_CC)$(DEB_GCC_VERSION)
CXX    = $(DEB_HOST_GNU_TYPE)-$(BASE_CXX)$(DEB_GCC_VERSION)
BUILD_CC = $(DEB_BUILD_GNU_TYPE)-$(BASE_CC)
BUILD_CXX = $(DEB_BUILD_GNU_TYPE)-$(BASE_CXX)

BUILD_CFLAGS = -O2 -g
HOST_CFLAGS = -pipe -O2 -g $(call xx,extra_cflags)

configure_target := $(DEB_HOST_GNU_TYPE)

# Normally we'll just use this for --build.  If the architecture requires
# that build daemons be able to run a particular optimized library, then
# they can set the --build to match --host for that optimized build.
# Among other things this lets tests run.
configure_build := $(DEB_BUILD_GNU_TYPE)

log_build    = $(build-tree)/log-build-$(call xx,configure_target)-$(curpass)
log_test     = $(build-tree)/log-test-$(call xx,configure_target)-$(curpass)
log_results  = $(build-tree)/test-results-$(call xx,configure_target)-$(curpass)
log_expected = debian/testsuite-checking/expected-results-$(call xx,configure_target)-$(curpass)

# Which build pass are we on?
curpass = $(filter-out %_,$(subst _,_ ,$@))

# Some architectures don't get libc-profile ported right away:
no_libc_profile :=

# Provide backward compatibility for the old DEB_STAGE and DEB_BUILD_PROFILE for now
ifeq ($(DEB_STAGE),stage1)
  DEB_BUILD_PROFILES+=stage1
else
ifeq ($(DEB_BUILD_PROFILE),bootstrap)
  DEB_BUILD_PROFILES+=stage1
endif
endif

ifneq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
  DEB_ARCH_REGULAR_PACKAGES = $(libc)-dev
  DEB_INDEP_REGULAR_PACKAGES = 
  DEB_UDEB_PACKAGES = 
else
  DEB_ARCH_REGULAR_PACKAGES = $(libc) $(libc)-dev $(libc)-dbg $(libc)-pic libc-bin libc-dev-bin multiarch-support
  ifeq (,$(filter $(DEB_HOST_ARCH),$(no_libc_profile)))
    DEB_ARCH_REGULAR_PACKAGES += $(libc)-prof
  endif
  DEB_INDEP_REGULAR_PACKAGES = glibc-doc glibc-source
  DEB_UDEB_PACKAGES = $(libc)-udeb libnss-dns-udeb libnss-files-udeb
endif

# Generic kernel version check
define kernel_check
(if [ $(CURRENT_KERNEL_VERSION) -lt $(1) ]; then \
  false; \
fi)
endef

# Include libidn for both NPTL and FBTL targets.
standard-add-ons = libidn,

# Pull in all the per-arch magic!

-include debian/sysdeps/$(DEB_HOST_ARCH_OS).mk
-include debian/sysdeps/$(DEB_HOST_ARCH).mk

ifeq ($(filter nobiarch,$(DEB_BUILD_PROFILES)),)
GLIBC_PASSES += $(GLIBC_MULTILIB_PASSES)
DEB_ARCH_REGULAR_PACKAGES += $(DEB_ARCH_MULTILIB_PACKAGES)
endif

# Don't run dh_strip on this package
NOSTRIP_$(libc)-dbg = 1

# Don't put debug files from these packages in libc-dbg
NODEBUG_libc-bin = 1
NODEBUG_libc-dev-bin = 1
NODEBUG_libc6-amd64 = 1
NODEBUG_libc6-i386 = 1
NODEBUG_libc6-x32 = 1
NODEBUG_libc6-mipsn32 = 1
NODEBUG_libc6-powerpc = 1
NODEBUG_libc6-ppc64 = 1
NODEBUG_libc6-s390 = 1
NODEBUG_libc6-sparc64 = 1
NODEBUG_libc6-mips64 = 1
NODEBUG_nscd = 1

ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
  ifeq ($(threads),yes)
  DEB_ARCH_REGULAR_PACKAGES += nscd
  endif
endif

# And now the rules...
include debian/rules.d/*.mk

clean:: unpatch
	rm -f $(CURDIR)/po/*.mo
	rm -rf $(patsubst %,debian/tmp-%,$(GLIBC_PASSES))
	rm -rf $(build-tree)
	rm -rf $(stamp)
	rm -rf debian/include
	rm -f debian/control.in/libc0.1 debian/control.in/libc0.3 \
		debian/control.in/libc6 debian/control.in/libc6.1 \
		debian/*.preinst.* debian/locales.bug-presubj

# Required Debian targets
build-arch: $(stamp)info \
	$(patsubst %,$(stamp)build_%,$(GLIBC_PASSES)) \
	$(patsubst %,$(stamp)check_%,$(GLIBC_PASSES))

build-indep: $(stamp)source

build: build-arch build-indep


binary-indep: build-indep testroot debian/control $(build-tree) \
	$(patsubst %,$(stamp)binaryinst_%,$(DEB_INDEP_REGULAR_PACKAGES))

binary-arch: build-arch testroot debian/control $(build-tree) \
	$(patsubst %,$(stamp)binaryinst_%,$(DEB_ARCH_REGULAR_PACKAGES)) \
	$(patsubst %,$(stamp)binaryinst_%,$(DEB_UDEB_PACKAGES))

binary: binary-indep binary-arch

testroot:
	dh_testroot
