
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

ifndef INCR_INSTALLHDRS
ifeq ($(shell test -d $$DSTROOT/$INCDIR;echo $$?),0)
export INCR_INSTALLHDRS	= TRUE
else
export INCR_INSTALLHDRS	= FALSE
endif
endif

#
# Component List
#
export COMPONENT_LIST 	= osfmk bsd iokit pexpert libkern libsa
export COMPONENT 	= $(firstword $(subst /, ,$(shell $(RELPATH) $(SRCROOT) $(SOURCE))))
export COMPONENT_IMPORT_LIST = $(filter-out $(COMPONENT),$(COMPONENT_LIST)) 

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


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

#
# Kernel Configuration to install
#
#  supported install architecture : PPC I386
#
export INSTALL_TYPE 	= RELEASE
ifdef RC_ARCHS
export INSTALL_ARCHS 	= $(ARCH_CONFIGS)
else
export INSTALL_ARCHS 	= $(ARCH_CONFIGS)
endif
export INSTALL_ARCH	= $(ARCH_CONFIG)

export INSTALL_ARCH_DEFAULT	= PPC

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

#
# Compiler command
#
CC	= /usr/bin/cc
KCC	= /usr/bin/cc

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

export CFLAGS_GEN = -static -g -nostdinc -nostdlib -traditional-cpp -fno-builtin -finline -fno-keep-inline-functions -msoft-float -fsigned-bitfields -Wpointer-arith $(OTHER_CFLAGS) -fpermissive

export CFLAGS_RELEASE 	= 
export CFLAGS_DEBUG 	= -fno-omit-frame-pointer
export CFLAGS_PROFILE 	= 

export CFLAGS_PPC 	= -arch ppc -Dppc -DPPC -D__PPC__ -D_BIG_ENDIAN=__BIG_ENDIAN__
export CFLAGS_I386 	= -arch i386 -Di386 -DI386 -D__I386__ -D_BIG_ENDIAN=__LITTLE_ENDIAN__

export CFLAGS_RELEASEPPC = -O2 -mcpu=750 -fschedule-insns
export CFLAGS_RELEASE_TRACEPPC = -O2 -mcpu=750 -fschedule-insns
export CFLAGS_DEBUGPPC = -O1 -mcpu=750 -fschedule-insns
export CFLAGS_RELEASEI386 = -O2
export CFLAGS_DEBUGI386 = -O2

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

# Default CCFLAGS
#(we do not call it CPPFLAGS as that has a special meaning in unix tradition
# and in gcc: CPPFLAGS is for C Pre-Processor flags.  CCFLAGS has precedent
# in ProjectBuilder because of the .cc extension)
#
CPPFLAGS_GEN  = -x c++ -fno-rtti -fno-exceptions -fcheck-new -fvtable-thunks
CPPFLAGS_PPC  = 
CPPFLAGS_I386 =

CPPFLAGS      = $(CPPFLAGS_GEN) \
		  $($(addsuffix $(ARCH_CONFIG),CCFLAGS_)) \
		  $($(addsuffix $(KERNEL_CONFIG),CCFLAGS_))

#
# 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 

export LDFLAGS_COMPONENT_RELEASE 	= 
export LDFLAGS_COMPONENT_DEBUG 		= 
export LDFLAGS_COMPONENT_PROFILE 	= 

export LDFLAGS_COMPONENT_PPC 	= -arch ppc
export LDFLAGS_COMPONENT_I386 	= -arch 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 -segalign 0x1000

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

export LDFLAGS_KERNEL_PPC      = -arch ppc -segaddr __VECTORS 0x0 -segaddr __TEXT 0x11000 -e __start -sectalign __DATA __common 0x1000 -sectalign __DATA __bss 0x1000
export LDFLAGS_KERNEL_I386     = -arch i386 -segaddr __TEXT 0x100000 -segaddr __LINKEDIT 0x780000 -e _pstart

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) 
export INCFLAGS_POSIX	= -I$(OBJROOT)/EXPORT_HDRS/bsd
export INCFLAGS_LOCAL	= -I.

export INCFLAGS 	= -nostdinc $(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

#
# 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

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

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

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

#
#  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

