#	
#	File:    Makefile
#	Package: AirPlayAudioPOSIXReceiver
#	Version: AirPlay_Audio_POSIX_Receiver_211.1.p8
#	
#	Disclaimer: IMPORTANT: This Apple software is supplied to you, by Apple Inc. ("Apple"), in your
#	capacity as a current, and in good standing, Licensee in the MFi Licensing Program. Use of this
#	Apple software is governed by and subject to the terms and conditions of your MFi License,
#	including, but not limited to, the restrictions specified in the provision entitled ”Public
#	Software”, and is further subject to your agreement to the following additional terms, and your
#	agreement that the use, installation, modification or redistribution of this Apple software
#	constitutes acceptance of these additional terms. If you do not agree with these additional terms,
#	please do not use, install, modify or redistribute this Apple software.
#	
#	Subject to all of these terms and in consideration of your agreement to abide by them, Apple grants
#	you, for as long as you are a current and in good-standing MFi Licensee, a personal, non-exclusive
#	license, under Apple's copyrights in this original Apple software (the "Apple Software"), to use,
#	reproduce, and modify the Apple Software in source form, and to use, reproduce, modify, and
#	redistribute the Apple Software, with or without modifications, in binary form. While you may not
#	redistribute the Apple Software in source form, should you redistribute the Apple Software in binary
#	form, you must retain this notice and the following text and disclaimers in all such redistributions
#	of the Apple Software. Neither the name, trademarks, service marks, or logos of Apple Inc. may be
#	used to endorse or promote products derived from the Apple Software without specific prior written
#	permission from Apple. Except as expressly stated in this notice, no other rights or licenses,
#	express or implied, are granted by Apple herein, including but not limited to any patent rights that
#	may be infringed by your derivative works or by other works in which the Apple Software may be
#	incorporated.
#	
#	Unless you explicitly state otherwise, if you provide any ideas, suggestions, recommendations, bug
#	fixes or enhancements to Apple in connection with this software (“Feedback”), you hereby grant to
#	Apple a non-exclusive, fully paid-up, perpetual, irrevocable, worldwide license to make, use,
#	reproduce, incorporate, modify, display, perform, sell, make or have made derivative works of,
#	distribute (directly or indirectly) and sublicense, such Feedback in connection with Apple products
#	and services. Providing this Feedback is voluntary, but if you do provide Feedback to Apple, you
#	acknowledge and agree that Apple may exercise the license granted above without the payment of
#	royalties or further consideration to Participant.
#	
#	The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR
#	IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY
#	AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR
#	IN COMBINATION WITH YOUR PRODUCTS.
#	
#	IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
#	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#	PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
#	AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
#	(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
#	POSSIBILITY OF SUCH DAMAGE.
#	
#	Copyright (C) 2012-2014 Apple Inc. All Rights Reserved.
#

# IMPORTANT NOTE: This is a Makefile for *GNU make*
# On some systems, a different program may be the default "make" command.
# If "make daemon" gives lots of errors like "Missing dependency operator",
# then try typing "gmake daemon" instead.

#
#	Build Targets
#	-------------
#	daemon	-- 	Build AirPlay executable
#	library	-- 	Build AirPlay Library
#	clean	-- 	Clean AirPlay 
#	help	-- 	Help listing
#
#
#	Build options
#	-------------
#	openssl		-- 	1=Use OpenSSL for AES, SHA*, etc. 
#					0=Compile in AES, SHA*, etc. code directly.
#
#	debug       -- 	1=Compile in debug code, asserts, etc. 
#					0=Strip out debug code for a release build.
#
#	native      -- 	1=Compile natively on an NetBSD host
#					0=Cross Compile on a Mac
#


#
# Target OS being built for
#
TARGET_OS	=	NetBSD

#
# Detect Host OS being built on and whether this is a cross compile or native compile
#
HOST_OS := $(shell uname -s)

ifeq ($(HOST_OS), $(TARGET_OS))
COMPILATION_TYPE		= Native
else
COMPILATION_TYPE		= Cross
endif

export COMPILATION_TYPE

#
# Check if gmake is available. If not we should try using make - but it might not work depending
# on whether it is a GNU compatible make or not. Mac's make will work.
#
MAKE := $(shell which gmake)

ifeq ($(MAKE), )
MAKE	:=	$(shell which make)
endif

#
# AirPlay Makefile path
#
AIRPLAY_MAKEFILE = ../../Makefile


#
# Platform Include Makefile 
#
PLATFORM_INCLUDE_MAKEFILE = ./Platform.include.mk



include $(PLATFORM_INCLUDE_MAKEFILE)


#
# 	Platform implementation files to be compiled:
#
PLATFORM_OBJS		+= main.o
PLATFORM_OBJS      	+= PlatformImplementationNetBSD.o
PLATFORM_OBJS       	+= PlatformAudioUtilsNetBSD.o
PLATFORM_OBJS       	+= PlatformMFiServerNetBSD.o
PLATFORM_OBJS       	+= PlatformRemote.o
PLATFORM_OBJS		+= PlatformInternal.o
PLATFORM_OBJS		+= PlatformReceiverNetBSD.o
PLATFORM_OBJFILES	:= $(addprefix $(PLATFORM_OBJDIR)/,$(PLATFORM_OBJS))


#
# Platform VPath(s) to the platform source files listed above
#
VPATH			+= $(PLATFORMSRCROOT)



#
# Library Path(s) Information
#
# 	list of Library directories for the platform being ported to:
# 			- platform libraries listed below
# 			- libdns_sd for Bonjour library
#
PLATFORM_LIBRARY_PATHS		+= -L$(MDNSROOT)/mDNSNetBSD/build/prod
PLATFORM_LIBRARY_PATHS		+= -L$(PLATFORM_OBJDIR)



#
# Libraries
#
# 	Libraries to be linked to. 
#
PLATFORM_LIBS			+= -lc
PLATFORM_LIBS			+= -ldns_sd
PLATFORM_LIBS			+= -lm
PLATFORM_LIBS			+= -lpthread
PLATFORM_LIBS			+= -lrt
PLATFORM_LIBS			+= -lstdc++
PLATFORM_LIBS			+= -lAirPlay
ifeq ($(openssl),1)
PLATFORM_LIBS			+= -lcrypto
endif


#
# Compile flags to use for c & c++ files
#
COMPILE_FLAGS		+= -D_GNU_SOURCE=1
COMPILE_FLAGS		+= $(PLATFORM_COMMONFLAGS)
COMPILE_FLAGS		+= $(PLATFORM_HEADERS)

C_FLAGS			+= $(COMPILE_FLAGS)
C_FLAGS			+= $(PLATFORM_C_WARNINGS)

CPP_FLAGS		+= $(COMPILE_FLAGS)
CPP_FLAGS		+= $(PLATFORM_CPP_WARNINGS)


#
# Include paths
#
INCLUDES		 =
INCLUDES		+= -I$(SRCROOT)/Sources
INCLUDES		+= -I$(SRCROOT)/Support
INCLUDES 		+= $(PLATFORM_INCLUDES)


#
# Local variables
#
EXENAME 	= airplayd


####################################################################
#
# Rules
#
####################################################################

.PHONY : all daemon library clean help welcome
all: help

#
# Target to build airplay daemon target.
#
daemon: $(PLATFORM_OBJDIR) library $(PLATFORM_OBJDIR)/$(EXENAME)
	@echo
	@echo "====> Done Building Target daemon"


#
# Target to do final linking for airplay executable
#
$(PLATFORM_OBJDIR)/$(EXENAME): $(PLATFORM_OBJFILES)
	@echo
	@echo "====> Linking $(notdir $@)"
	$(CC) -o $@ $(PLATFORM_LIBRARY_PATHS) $(PLATFORM_OBJFILES) $(PLATFORM_LIBS) 
ifneq ($(debug), 1)
	@echo
	@echo "====> Stripping $(notdir $@)"
	$(STRIP) $@
endif


#
# Target to build airplay library target.
#
library: welcome $(PLATFORM_OBJDIR)
	@$(MAKE) platform_makefile=$(PLATFORM_INCLUDE_MAKEFILE) -f $(AIRPLAY_MAKEFILE) airplaylib


#
# Target to cleanup 
#
clean: welcome $(PLATFORM_OBJDIR)
	@$(MAKE) platform_makefile=$(PLATFORM_INCLUDE_MAKEFILE) -f $(AIRPLAY_MAKEFILE) clean



#
# Rule to ensure existence of Object directory
#
$(PLATFORM_OBJDIR):
	@-mkdir  -p $(PLATFORM_OBJDIR)


#
# Rule to build platform implementation c & c++ files
#
$(PLATFORM_OBJDIR)/%.o: %.c
	$(CC) -c $(INCLUDES) $(C_FLAGS) -o $@ $<

$(PLATFORM_OBJDIR)/%.o: %.cpp
	$(CXX) -c $(INCLUDES) $(CPP_FLAGS) -o $@ $<


welcome:
	@echo
	@echo "+---------- AirPlay $(TARGET_OS) $(COMPILATION_TYPE) Compilation Build ----------+"
	@echo "Host OS       =   $(HOST_OS)"
	@echo "Target OS     =   $(TARGET_OS)"
	@echo "Make          =   $(MAKE)"
	@echo "+---------------------------------------------------------------+"
	@echo 


#
# Target to print out help info
#
help: welcome
	@echo "--------- Platform Make Help -----------"
	@echo " To build debug mode AirPlay Executable:"
	@echo " 	$(MAKE) debug=1 daemon"
	@echo 
	@echo " To build non-debug mode AirPlay Library:"
	@echo " 	$(MAKE)  library"
	@echo 
	@echo " To clean AirPlay:"
	@echo " 	$(MAKE)  clean"
	@echo "---------------------------------------"

