# -*- mode: makefile;-*-
#
# Copyright (C) 1999-2012 Apple Inc. All rights reserved.
#
# MakeInc.def contains global definitions for building,
# linking, and installing files.
#

#
# Architecture Configuration options
#
SUPPORTED_ARCH_CONFIGS := X86_64

#
# Kernel Configuration options  
#
SUPPORTED_KERNEL_CONFIGS = RELEASE DEVELOPMENT DEBUG PROFILE

#
# Machine Configuration options  
#

SUPPORTED_X86_64_MACHINE_CONFIGS = NONE


#
# Platform options
#
SUPPORTED_PLATFORMS = MacOSX iPhoneOS iPhoneSimulator

#
# Setup up *_LC variables during recursive invocations
#

ifndef CURRENT_ARCH_CONFIG_LC
	export CURRENT_ARCH_CONFIG_LC 	:= $(shell printf "%s" "$(CURRENT_ARCH_CONFIG)" | $(TR) A-Z a-z)
endif

ifndef CURRENT_KERNEL_CONFIG_LC
	export CURRENT_KERNEL_CONFIG_LC := $(shell printf "%s" "$(CURRENT_KERNEL_CONFIG)" | $(TR) A-Z a-z)
endif

ifndef CURRENT_MACHINE_CONFIG_LC
	export CURRENT_MACHINE_CONFIG_LC := $(shell printf "%s" "$(CURRENT_MACHINE_CONFIG)" | $(TR) A-Z a-z)
endif


#
# Component List
#
COMPONENT_LIST 	= osfmk bsd libkern iokit pexpert libsa security
COMPONENT 	= $(if $(word 2,$(subst /, ,$(RELATIVE_SOURCE_PATH))),$(word 2,$(subst /, ,$(RELATIVE_SOURCE_PATH))),$(firstword $(subst /, ,$(RELATIVE_SOURCE_PATH))))
COMPONENT_IMPORT_LIST = $(filter-out $(COMPONENT),$(COMPONENT_LIST)) 


#
# Deployment target flag
#
ifeq ($(PLATFORM),MacOSX)
    DEPLOYMENT_TARGET_FLAGS = -mmacosx-version-min=$(SDKVERSION)
else ifeq ($(PLATFORM),iPhoneOS)
    DEPLOYMENT_TARGET_FLAGS = -miphoneos-version-min=$(SDKVERSION)
else ifeq ($(PLATFORM),iPhoneSimulator)
    DEPLOYMENT_TARGET_FLAGS =
else
    DEPLOYMENT_TARGET_FLAGS =
endif


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

#
# Compiler command
#
KCC  = $(CC)
KC++ = $(CXX)

#
# Compiler warning flags
#

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

# Can be overridden in Makefile.template or Makefile.$arch
export CWARNFLAGS ?= $(CWARNFLAGS_STD)

define add_perfile_cflags
$(1)_CWARNFLAGS_ADD += $2
endef

CXXWARNFLAGS_STD = \
	-Wall -Werror -Wno-format-y2k -Wextra -Wpointer-arith -Wreturn-type \
	-Wcast-qual -Wwrite-strings -Wswitch -Wcast-align -Wchar-subscripts \
	-Wredundant-decls -Wextra-tokens

# overloaded-virtual warnings are non-fatal (9000888)
CXXWARNFLAGS_STD += -Wno-error=overloaded-virtual

# Can be overridden in Makefile.template or Makefile.$arch
export CXXWARNFLAGS ?= $(CXXWARNFLAGS_STD)

define add_perfile_cxxflags
$(1)_CXXWARNFLAGS_ADD += $2
endef

#
# Default ARCH_FLAGS, for use with compiler/linker/assembler/mig drivers

ARCH_FLAGS_X86_64	  = -arch x86_64


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

#
# Debug info
#
DSYMKERNELSYSDIR = mach_kernel.sys.dSYM
DSYMINFODIR	= Contents
DSYMKGMACROSDIR	= Contents/Resources
DSYMLLDBMACROSDIR = Contents/Resources/Python
DSYMDWARFDIR	= Contents/Resources/DWARF

DEBUG_CFLAGS := -gdwarf-2
BUILD_DSYM := 1

#
# We must not use -fno-keep-inline-functions, or it will remove the dtrace
# probes from the kernel.
#
CFLAGS_GEN = $(DEBUG_CFLAGS) -nostdinc \
	-freorder-blocks -fno-builtin -fno-common \
	-fsigned-bitfields $(OTHER_CFLAGS)

CFLAGS_RELEASE 	= 
CFLAGS_DEVELOPMENT 	=
CFLAGS_DEBUG 	= 
CFLAGS_PROFILE 	=  -pg

CFLAGS_X86_64	= -Dx86_64 -DX86_64 -D__X86_64__ -DLP64 \
				-DPAGE_SIZE_FIXED -mkernel -msoft-float \
				-fno-limit-debug-info # Workaround for 11076603


CFLAGS_RELEASEX86_64 = -O2
CFLAGS_DEVELOPMENTX86_64 = -O2
# No space optimization for the DEBUG kernel for the benefit of gdb:
CFLAGS_DEBUGX86_64 = -O0
CFLAGS_PROFILEX86_64 = -O2

CFLAGS_RELEASEARM = -O2
CFLAGS_DEVELOPMENTARM = -O2
CFLAGS_DEBUGARM = -O0
CFLAGS_PROFILEARM = -O2

CFLAGS 	= $(CFLAGS_GEN) \
		  $($(addsuffix $(CURRENT_MACHINE_CONFIG),MACHINE_FLAGS_$(CURRENT_ARCH_CONFIG)_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),CFLAGS_)) \
		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),CFLAGS_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG), $(addsuffix $(CURRENT_KERNEL_CONFIG),CFLAGS_))) \
		  $(DEPLOYMENT_TARGET_FLAGS) \
		  $(DEFINES)

#
# Default C++ flags
#

OTHER_CXXFLAGS	=

CXXFLAGS_GEN  = -fapple-kext $(OTHER_CXXFLAGS)

# For the moment, do not use gnu++11
#CXXFLAGS_ARM = -std=gnu++11

CXXFLAGS      = $(CXXFLAGS_GEN) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),CXXFLAGS_)) \
		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),CXXFLAGS_))


#
# Assembler command
#
AS	= $(CC)
S_KCC	= $(CC)

#
# Default SFLAGS
#
SFLAGS_GEN = -D__ASSEMBLER__ $(OTHER_CFLAGS)

SFLAGS_RELEASE 	= 
SFLAGS_DEVELOPMENT 	= 
SFLAGS_DEBUG 	= 
SFLAGS_PROFILE 	= 

SFLAGS_X86_64 	= $(CFLAGS_X86_64)

SFLAGS 	= $(SFLAGS_GEN) \
		  $($(addsuffix $(CURRENT_MACHINE_CONFIG),MACHINE_FLAGS_$(CURRENT_ARCH_CONFIG)_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),SFLAGS_)) \
		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),SFLAGS_)) \
		  $(DEPLOYMENT_TARGET_FLAGS) \
		  $(DEFINES)

#
# Linker command
#
LD	= $(KC++) -nostdlib

#
# Default LDFLAGS
#
# Availability of DWARF allows DTrace CTF (compressed type format) to be constructed.
# ctf_insert creates the CTF section.  It needs reserved padding in the
# headers for the load command segment and the CTF section structures.
#
LDFLAGS_KERNEL_GEN = \
	-nostdlib \
	-fapple-kext \
	-Wl,-e,__start \
	-Wl,-sectalign,__TEXT,__text,0x1000 \
	-Wl,-sectalign,__DATA,__common,0x1000 \
	-Wl,-sectalign,__DATA,__bss,0x1000 \
	-Wl,-sectcreate,__PRELINK_TEXT,__text,/dev/null \
	-Wl,-sectcreate,__PRELINK_STATE,__kernel,/dev/null \
        -Wl,-sectcreate,__PRELINK_STATE,__kexts,/dev/null \
	-Wl,-sectcreate,__PRELINK_INFO,__info,/dev/null \
	-Wl,-new_linker \
	-Wl,-pagezero_size,0x0 \
	-Wl,-version_load_command \
	-Wl,-function_starts \
	-Wl,-headerpad,152

LDFLAGS_KERNEL_RELEASE 	=
LDFLAGS_KERNEL_DEVELOPMENT 	=
LDFLAGS_KERNEL_DEBUG 	= 
LDFLAGS_KERNEL_PROFILE 	= 

# KASLR static slide config:
ifndef SLIDE
SLIDE=0x00
endif
KERNEL_MIN_ADDRESS      = 0xffffff8000000000
KERNEL_BASE_OFFSET      = 0x100000
KERNEL_STATIC_SLIDE     = $(shell printf "0x%016x" \
			  $$[ $(SLIDE) << 21 ])
KERNEL_STATIC_BASE      = $(shell printf "0x%016x" \
			  $$[ $(KERNEL_MIN_ADDRESS) + $(KERNEL_BASE_OFFSET) ])
KERNEL_HIB_SECTION_BASE = $(shell printf "0x%016x" \
			  $$[ $(KERNEL_STATIC_BASE) + $(KERNEL_STATIC_SLIDE) ])
KERNEL_TEXT_BASE        = $(shell printf "0x%016x" \
			  $$[ $(KERNEL_HIB_SECTION_BASE) + 0x100000 ])

LDFLAGS_KERNEL_RELEASEX86_64 = \
	-Wl,-pie \
	-Wl,-segaddr,__HIB,$(KERNEL_HIB_SECTION_BASE) \
	-Wl,-image_base,$(KERNEL_TEXT_BASE) \
	-Wl,-seg_page_size,__TEXT,0x200000 \
	-Wl,-sectalign,__DATA,__const,0x1000 \
	-Wl,-sectalign,__DATA,__sysctl_set,0x1000 \
	-Wl,-sectalign,__HIB,__bootPT,0x1000 \
	-Wl,-sectalign,__HIB,__desc,0x1000 \
	-Wl,-sectalign,__HIB,__data,0x1000 \
	-Wl,-sectalign,__HIB,__text,0x1000 \
	-Wl,-sectalign,__HIB,__const,0x1000 \
	-Wl,-sectalign,__HIB,__bss,0x1000 \
	-Wl,-sectalign,__HIB,__common,0x1000 \
	$(LDFLAGS_NOSTRIP_FLAG)

# Define KERNEL_BASE_OFFSET so known at compile time:
CFLAGS_X86_64 += -DKERNEL_BASE_OFFSET=$(KERNEL_BASE_OFFSET)

LDFLAGS_KERNEL_DEBUGX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)
LDFLAGS_KERNEL_DEVELOPMENTX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)
LDFLAGS_KERNEL_PROFILEX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)


LDFLAGS_KERNEL	= $(LDFLAGS_KERNEL_GEN) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG),LDFLAGS_KERNEL_)) \
		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),LDFLAGS_KERNEL_)) \
		  $($(addsuffix $(CURRENT_ARCH_CONFIG), $(addsuffix $(CURRENT_KERNEL_CONFIG),LDFLAGS_KERNEL_))) \
		  $(DEPLOYMENT_TARGET_FLAGS)

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

#
# DTrace support
#
ifeq ($(CURRENT_KERNEL_CONFIG),RELEASE)
ifneq ($(filter ARM%,$(CURRENT_ARCH_CONFIG)),)
DO_CTFCONVERT = 0
DO_CTFMERGE   = 0
DO_CTFMACHO   = 0
else
DO_CTFCONVERT = $(SUPPORTS_CTFCONVERT)
DO_CTFMERGE   = 1
DO_CTFMACHO   = $(NEEDS_CTF_MACHOS)
endif
else
DO_CTFCONVERT = $(SUPPORTS_CTFCONVERT)
DO_CTFMERGE   = 1
DO_CTFMACHO   = $(NEEDS_CTF_MACHOS)
endif

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

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

#
# Default MIGFLAGS
#
MIGFLAGS	= $(DEFINES) $(INCFLAGS) $($(addsuffix $(CURRENT_ARCH_CONFIG),CFLAGS_)) $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
		$(DEPLOYMENT_TARGET_FLAGS)

#
# Support for LLVM Link Time Optimization (LTO)
#
# CFLAGS_NOLTO_FLAG is needed on a per-file basis (for files
# that deal poorly with LTO, or files that must be machine
# code *.o files for xnu to build (i.e, setsegname runs on
# them).
#
# LDFLAGS_NOSTRIP_FLAG is used to support configurations that
# do not utilize an export list.  For these configs to build,
# we need to prevent the LTO logic from dead stripping them.

ifeq ($(BUILD_LTO),1)
CFLAGS_GEN	+= -flto
CXXFLAGS_GEN	+= -flto
LDFLAGS_KERNEL_GEN	+= -Wl,-mllvm,-disable-simplify-libcalls -Wl,-object_path_lto,$(TARGET)/lto.o # -Wl,-mllvm -Wl,-disable-fp-elim
LDFLAGS_NOSTRIP_FLAG = -rdynamic
CFLAGS_NOLTO_FLAG = -fno-lto
SUPPORTS_CTFCONVERT	= 0
NEEDS_CTF_MACHOS	= 1
else
LDFLAGS_NOSTRIP_FLAG =
CFLAGS_NOLTO_FLAG =
SUPPORTS_CTFCONVERT	= 1
NEEDS_CTF_MACHOS	= 0
endif

#
# Default VPATH
#
export VPATH = .:$(SOURCE)

#
# Macros that control installation of kernel and its header files
#
# install flags for header files
# 
INSTALL_FLAGS = -c -S -m 0444
DATA_INSTALL_FLAGS = -c -S -m 0644
EXEC_INSTALL_FLAGS = -c -S -m 0755

#
# Header file destinations
#
ifeq ($(RC_ProjectName),xnu_headers_Sim)
	HEADER_INSTALL_PREFIX = $(SDKROOT)
else
	HEADER_INSTALL_PREFIX = 
endif

FRAMEDIR = $(HEADER_INSTALL_PREFIX)/System/Library/Frameworks

SINCVERS = B
SINCFRAME = $(FRAMEDIR)/System.framework
SINCDIR = $(SINCFRAME)/Versions/$(SINCVERS)/Headers
SPINCDIR = $(SINCFRAME)/Versions/$(SINCVERS)/PrivateHeaders
SRESDIR = $(SINCFRAME)/Versions/$(SINCVERS)/Resources

ifndef INCDIR
    INCDIR = $(HEADER_INSTALL_PREFIX)/usr/include
endif
ifndef LCLDIR
    LCLDIR = $(SPINCDIR)
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


PLATFORM_UNIFDEF = $(foreach x,$(SUPPORTED_PLATFORMS),$(if $(filter $(PLATFORM),$(x)),-DPLATFORM_$(x) $(foreach token,$(PLATFORM_UNIFDEF_BLACKLIST_TOKENS_$(x)),-U$(token)),-UPLATFORM_$(x)))

SPINCFRAME_UNIFDEF = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -DPRIVATE -U_OPEN_SOURCE_ -U__OPEN_SOURCE__
SINCFRAME_UNIFDEF  = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -UPRIVATE -D_OPEN_SOURCE_ -D__OPEN_SOURCE__
KPINCFRAME_UNIFDEF = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -DKERNEL_PRIVATE -DPRIVATE -DKERNEL -U_OPEN_SOURCE_ -U__OPEN_SOURCE__
KINCFRAME_UNIFDEF  = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UPRIVATE -DKERNEL -D_OPEN_SOURCE_ -D__OPEN_SOURCE__


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

#
# Strip Flags
#
STRIP_FLAGS_RELEASE	= -S -x 
STRIP_FLAGS_DEVELOPMENT	= -S -x 
STRIP_FLAGS_DEBUG	= -S 
STRIP_FLAGS_PROFILE	= -S -x

STRIP_FLAGS 	= $($(addsuffix $(CURRENT_KERNEL_CONFIG),STRIP_FLAGS_)) 

#
# dsymutil flags
#
DSYMUTIL_FLAGS_X86_64 = --arch=x86_64

DSYMUTIL_FLAGS = $($(addsuffix $(CURRENT_ARCH_CONFIG),DSYMUTIL_FLAGS_))

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

#
# DEBUG alias location
#
DEVELOPER_EXTRAS_DIR = /AppleInternal/CoreOS/xnu_debug

#
# mach_kernel install location
#
INSTALL_KERNEL_DIR = /

#
# System.kext pseudo-kext install location
#
INSTALL_EXTENSIONS_DIR = /System/Library/Extensions

#
# KDK location for iOS
#
INSTALL_KERNEL_SYM_DIR = /System/Library/Extensions/KDK

#
# Misc. Etc.
#
INSTALL_SHARE_MISC_DIR = /usr/share/misc
INSTALL_DTRACE_SCRIPTS_DIR = /usr/lib/dtrace

#
# Overrides for XBS build aliases
#
ifeq ($(RC_ProjectName),xnu_debug)
INSTALL_KERNEL_DIR := $(DEVELOPER_EXTRAS_DIR)
DELETE_KERNEL_FRAMEWORK_AND_MISC = 1
INSTALL_XNU_DEBUG_FILES = 1
endif
ifeq ($(PLATFORM),iPhoneOS)
INSTALL_PRIMARY_KERNEL_LIKE_NON_PRIMARY = 1
INSTALL_KERNEL_SYM_TO_KDK = 1
USE_BINARY_PLIST = 1
else ifeq ($(PLATFORM),iPhoneSimulator)
INSTALL_SYSTEM_FRAMEWORK_SYMLINKS = 1
USE_BINARY_PLIST = 1
endif

# vim: set ft=make:
