export SOURCE=$(shell /bin/pwd)

#
# gnumake 3.77 support
#
export USE_APPLE_PB_SUPPORT = all

#
# Incremental Build option
#
ifndef INCR_EXPORTHDRS
ifeq ($(shell test -d $$OBJROOT/EXPORT_HDRS;echo $$?),0)
export INCR_EXPORTHDRS	= TRUE
else
export INCR_EXPORTHDRS	= FALSE
endif
endif

#
# Component List
#
ifndef COMPONENT_LIST 
export COMPONENT_LIST 	= osfmk bsd iokit pexpert libkern libsa
export COMPONENT_LIST_UC := $(shell echo -n $(COMPONENT_LIST) | $(TR) a-z A-Z)
endif
ifndef	COMPONENT
export COMPONENT 	:= $(firstword $(subst /, ,$(shell $(RELPATH) $(SRCROOT) $(SOURCE))))
export COMPONENT_IMPORT_LIST := $(filter-out $(COMPONENT),$(COMPONENT_LIST)) 
else
ifeq	($(COMPONENT), .)
export COMPONENT        := $(firstword $(subst /, ,$(shell $(RELPATH) $(SRCROOT) $(SOURCE))))
export COMPONENT_IMPORT_LIST := $(filter-out $(COMPONENT),$(COMPONENT_LIST)) 
endif
endif

# Architecture options
#
#  supported configurations : PPC I386
#
ifndef ARCH_CONFIGS
ifdef RC_ARCHS
export ARCH_CONFIGS 	:= $(shell echo -n $(RC_ARCHS) | $(TR) a-z A-Z)
else
export ARCH_CONFIGS 	:= $(shell arch | $(TR) a-z A-Z)
endif
endif
ifdef	ARCH_CONFIG
ifndef ARCH_CONFIG_LC
export ARCH_CONFIG_LC 	:= $(shell echo -n $(ARCH_CONFIG) | $(TR) A-Z a-z)
endif
endif


# Kernel Configuration options  
#
# supported configurations : RELEASE DEBUG PROFILE
#
ifndef KERNEL_CONFIGS
export KERNEL_CONFIGS 	= RELEASE
endif
ifndef KERNEL_CONFIG
export KERNEL_CONFIG 	= $(firstword $(KERNEL_CONFIGS))
endif

#
# Kernel Configuration to install
#
#  supported install architecture : PPC I386
#
export INSTALL_TYPE 	= RELEASE
ifndef INSTALL_ARCHS
ifdef RC_ARCHS
export INSTALL_ARCHS 	= $(ARCH_CONFIGS)
else
export INSTALL_ARCHS 	= $(ARCH_CONFIGS)
endif
export INSTALL_ARCHS_LC := $(shell echo -n $(ARCH_CONFIGS) | $(TR) A-Z a-z)
endif

export INSTALL_ARCH_DEFAULT	= PPC

#
# Standard defines list
#
export DEFINES = -DAPPLE -DNeXT -DKERNEL -DKERNEL_PRIVATE -DXNU_KERNEL_PRIVATE -DPRIVATE -D__MACHO__=1 -Dvolatile=__volatile $(IDENT)

#
# Compiler command
#
KCC  = /usr/bin/cc
KC++ = /usr/bin/c++
CC   = $(KCC)

#
# Compiler warning flags
#

CWARNFLAGS_STD = \
	-Wall -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes \
	-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
	-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
	-Wnested-externs -Wredundant-decls

export CWARNFLAGS ?= $(CWARNFLAGS_STD)

MWARNFLAGS_STD = \
	-Wall -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes \
	-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
	-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
	-Wnested-externs -Wredundant-decls

export MWARNFLAGS ?= $(MWARNFLAGS_STD)

CXXWARNFLAGS_STD = \
	-Wall -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes \
	-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
	-Wshadow -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls \
	-fpermissive

export CXXWARNFLAGS ?= $(CXXWARNFLAGS_STD)


#
# Setup for parallel sub-makes when doing an RC build
#
ifndef MAKEJOBS
ifeq "YES" "$(RC_XBS)"
export MAKEJOBS = --jobs=3
endif
endif

#
# Default CFLAGS
#
ifdef RC_CFLAGS
export OTHER_CFLAGS	= $(subst $(addprefix -arch ,$(RC_ARCHS)),,$(RC_CFLAGS))
endif

export CFLAGS_GEN = -static -g -nostdinc -nostdlib -no-cpp-precomp \
	-fno-builtin -finline -fno-keep-inline-functions -msoft-float \
	-fsigned-bitfields $(OTHER_CFLAGS)

export CFLAGS_RELEASE 	= 
export CFLAGS_DEBUG 	= 
export CFLAGS_PROFILE 	=  -pg

export CFLAGS_PPC 	= -arch ppc -Dppc -DPPC -D__PPC__ -DPAGE_SIZE_FIXED
export CFLAGS_I386 	= -arch i386 -Di386 -DI386 -D__I386__ -DPAGE_SIZE_FIXED \
	-march=i686 -mpreferred-stack-boundary=2 -falign-functions=4 -mcpu=pentium4 -force_cpusubtype_ALL

export CFLAGS_RELEASEPPC = -O2 -mcpu=750 -mmultiple -fschedule-insns
export CFLAGS_RELEASE_TRACEPPC = -O2 -mcpu=750 -mmultiple -fschedule-insns
export CFLAGS_DEBUGPPC = -O2 -mcpu=750 -mmultiple -fschedule-insns
export CFLAGS_PROFILEPPC = -O2 -mcpu=750 -mmultiple -fschedule-insns
export CFLAGS_RELEASEI386 = -Os
export CFLAGS_DEBUGI386 = -Os
export CFLAGS_PROFILEI386 = -Os

export CFLAGS 	= $(CFLAGS_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),CFLAGS_)) \
		  $($(addsuffix $(KERNEL_CONFIG),CFLAGS_)) \
		  $($(addsuffix $(ARCH_CONFIG), $(addsuffix $(KERNEL_CONFIG),CFLAGS_))) \
		  $(DEFINES)

# Default C++ flags
#
CXXFLAGS_GEN  = -fno-rtti -fno-exceptions -fcheck-new -fapple-kext -fpermissive

CXXFLAGS      = $(CXXFLAGS_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),CXXFLAGS_)) \
		  $($(addsuffix $(KERNEL_CONFIG),CXXFLAGS_))

#
# Assembler command
#
AS	= /usr/bin/cc
S_KCC	= /usr/bin/cc

#
# Default SFLAGS
#
export SFLAGS_GEN = -static -D__ASSEMBLER__ -force_cpusubtype_ALL $(OTHER_CFLAGS)

export SFLAGS_RELEASE 	= 
export SFLAGS_DEBUG 	= 
export SFLAGS_PROFILE 	= 

export SFLAGS_PPC 	= $(CFLAGS_PPC)
export SFLAGS_I386 	= $(CFLAGS_I386)

export SFLAGS 	= $(SFLAGS_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),SFLAGS_)) \
		  $($(addsuffix $(KERNEL_CONFIG),SFLAGS_)) \
		  $(DEFINES)

#
# Linker command
#
LD	= /usr/bin/ld

#
# Default LDFLAGS
#
export LDFLAGS_COMPONENT_GEN = -static -r $(COMP_LDFLAGS_COMPONENT_GEN)

export LDFLAGS_COMPONENT_RELEASE =  $(COMP_LDFLAGS_COMPONENT_RELEASE)
export LDFLAGS_COMPONENT_DEBUG   =  $(COMP_LDFLAGS_COMPONENT_DEBUG)
export LDFLAGS_COMPONENT_PROFILE =  $(COMP_LDFLAGS_COMPONENT_PROFILE)

export LDFLAGS_COMPONENT_PPC 	= -arch ppc $(COMP_LDFLAGS_COMPONENT_PPC)
export LDFLAGS_COMPONENT_I386 	= -arch i386 $(COMP_LDFLAGS_COMPONENT_i386)

export LDFLAGS_COMPONENT	= $(LDFLAGS_COMPONENT_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),LDFLAGS_COMPONENT_)) \
		  $($(addsuffix $(KERNEL_CONFIG),LDFLAGS_COMPONENT_))

export LDFLAGS_KERNEL_GEN = \
	-static \
	-force_cpusubtype_ALL \
	-e __start \
	-segalign 0x1000 \
	-sectalign __TEXT __text 0x1000 \
	-sectalign __DATA __common 0x1000 \
	-sectalign __DATA __bss 0x1000 \
	-sectcreate __PRELINK __text /dev/null \
	-sectcreate __PRELINK __symtab /dev/null \
	-sectcreate __PRELINK __info /dev/null

export LDFLAGS_KERNEL_RELEASE 	=
#  -noseglinkedit
export LDFLAGS_KERNEL_DEBUG 	= 
export LDFLAGS_KERNEL_PROFILE 	= 

export LDFLAGS_KERNEL_PPC      = \
	-arch ppc \
	-segaddr __VECTORS 0x0 \
	-segaddr __HIB 0x7000  \
	-segaddr __TEXT 0xe000
 
export LDFLAGS_KERNEL_I386     = \
	-arch i386 \
	-segaddr __HIB  0xC0100000 \
	-segaddr __TEXT 0xC0111000

export LDFLAGS_KERNEL	= $(LDFLAGS_KERNEL_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),LDFLAGS_KERNEL_)) \
		  $($(addsuffix $(KERNEL_CONFIG),LDFLAGS_KERNEL_))


#
# Default runtime libraries to be linked with the kernel
#
export LD_KERNEL_LIBS	= -lcc_kext

#
# Default INCFLAGS
#
export INCFLAGS_IMPORT 	= $(patsubst %, -I$(OBJROOT)/EXPORT_HDRS/%, $(COMPONENT_IMPORT_LIST))
export INCFLAGS_EXTERN 	= -I$(OBJROOT)/EXTERN_HDRS -I$(SRCROOT)/EXTERNAL_HEADERS -I$(SRCROOT)/EXTERNAL_HEADERS/bsd
export INCFLAGS_GEN	= -I$(SRCROOT)/$(COMPONENT) -I$(OBJROOT)/EXPORT_HDRS/$(COMPONENT)
export INCFLAGS_POSIX	= -I$(OBJROOT)/EXPORT_HDRS/bsd
export INCFLAGS_LOCAL	= -I.

export INCFLAGS 	= $(INCFLAGS_LOCAL) $(INCFLAGS_GEN) $(INCFLAGS_IMPORT) $(INCFLAGS_EXTERN) $(INCFLAGS_MAKEFILE)

#
# Default MIGFLAGS
#
export MIGFLAGS	= $(DEFINES) $(INCFLAGS) $($(addsuffix $(ARCH_CONFIG),CFLAGS_))

#
# Default VPATH
#
empty:=
space:= $(empty) $(empty)
export VPATH_IMPORT 	= $(subst $(space),:,$(patsubst %,$(OBJROOT)/EXPORT_HDRS/%,$(strip $(COMPONENT_IMPORT_LIST)))):
export VPATH_EXTERN 	= $(OBJROOT)/EXTERN_HDRS:
export VPATH_GEN	= .:$(SOURCE):

export VPATH	 	= $(VPATH_GEN)$(VPATH_IMPORT)$(VPATH_EXTERN)$(VPATH_MAKEFILE)

#
# Macros that control installation of kernel and it's header files
#
# install flags for header files
# 
INSTALL_FLAGS = -c -m 0444
FILE_INSTALL_FLAGS = -c -m 0644
DATA_INSTALL_FLAGS = -c -m 0644

#
# Header file destinations
#
FRAMEDIR = System/Library/Frameworks
ifndef INCDIR
    INCDIR = /usr/include
endif
ifndef LCLDIR
    LCLDIR = $(FRAMEDIR)/System.framework/Versions/B/PrivateHeaders
endif

KINCVERS = A
KINCFRAME = $(FRAMEDIR)/Kernel.framework
KINCDIR = $(KINCFRAME)/Versions/$(KINCVERS)/Headers
KPINCDIR = $(KINCFRAME)/Versions/$(KINCVERS)/PrivateHeaders
KRESDIR = $(KINCFRAME)/Versions/$(KINCVERS)/Resources

XNU_PRIVATE_UNIFDEF = -UMACH_KERNEL_PRIVATE -UBSD_KERNEL_PRIVATE -UIOKIT_KERNEL_PRIVATE -ULIBKERN_KERNEL_PRIVATE -ULIBSA_KERNEL_PRIVATE -UPEXPERT_KERNEL_PRIVATE -UXNU_KERNEL_PRIVATE

SPINCFRAME_UNIFDEF = $(XNU_PRIVATE_UNIFDEF) -UKERNEL_PRIVATE -UKERNEL -DPRIVATE
SINCFRAME_UNIFDEF  = $(XNU_PRIVATE_UNIFDEF) -UKERNEL_PRIVATE -UKERNEL -UPRIVATE
KPINCFRAME_UNIFDEF = $(XNU_PRIVATE_UNIFDEF) -DKERNEL_PRIVATE -DPRIVATE -DKERNEL
KINCFRAME_UNIFDEF  = $(XNU_PRIVATE_UNIFDEF) -UKERNEL_PRIVATE -UPRIVATE -DKERNEL

#
# Compononent Header file destinations
#
EXPDIR = EXPORT_HDRS/$(COMPONENT)

#
# Strip Flags
#
export STRIP_FLAGS_RELEASE	= -S -x 
export STRIP_FLAGS_RELEASE_TRACE	= -S -x 
export STRIP_FLAGS_DEBUG	= -S 
export STRIP_FLAGS_DEBUG_TRACE	= -S 
export STRIP_FLAGS_PROFILE	= -S -x

export STRIP_FLAGS 	= $($(addsuffix $(KERNEL_CONFIG),STRIP_FLAGS_)) 

#
# Man Page destination
#
MANDIR = usr/share/man

#
#  This must be here before any rules are possibly defined by the
#  machine dependent makefile fragment so that a plain "make" command
#  always works.  The config program will emit an appropriate rule to
#  cause "all" to depend on every kernel configuration it generates.
#

default: all

