##############################################################################
## Source-level Makefile for jzintv
##
## Before editing this file, consider overriding details with .mak files
## in the buildcfg/ directory.  See buildcfg/README.txt for details.
##############################################################################

##############################################################################
## Project directory structure
##############################################################################
B = ../bin
L = ../lib
R = ../rom

##############################################################################
## Include local customizations, if any.
##############################################################################
-include buildcfg/*.mak

##############################################################################
## Project-wide build flags
##############################################################################

# Set our target architecture(s).  Since MacOS supports fat binaries, this
# can list multiple architectures.
ARCH ?= -arch x86_64

# Set "X" to be the executable extension, and "O" to be the object extension.
X =        
O = o

# Set "RM" to be the remove/delete command, if not overridden.
RM ?= rm -f 
 
# Set to 0 or comment out to disable GNU Readline support.
# Set to 1 to enable it.
# Note! It must really be GNU Readline, and not the Editline wrapper that
# comes with XCode.  Edit RL_CFLAGS, RL_LFLAGS below to match where you've
# installed it.
GNU_READLINE ?= 1

# Changed to use SDL frameworks .-- Fri. Aug. 6 JJT
#
# The instructions for installing the SDL framework suggest that it be placed
# in your ~/Library/Frameworks directory.  Override this variable if this is
# not the case.
SDL_FRAMEWORK ?= /Library/Frameworks/SDL.framework

# Flags for using the so-called "portable syntax" for including SDL headers
# (such as #include "SDL.h")
SDL_PORT_SYN_FLAGS := -I$(SDL_FRAMEWORK)/Headers/

# Note: -F$(SDL_FRAMEWORK)/.. ensures that gcc can find the SDL framework even
# if SDL_FRAMEWORK had to be overrriden.  See above.
SDL1_CFLAGS := -DUSE_SDL=1
SDL1_LFLAGS := -F$(SDL_FRAMEWORK)/.. -framework SDL \
               -framework AppKit -framework Foundation -lobjc

SDL1_CFLAGS += $(SDL1_CFLAGS_EXTRA)
SDL1_LFLAGS += $(SDL1_LFLAGS_EXTRA)

# Override these in a .mak file.  See buildcfg/00-compiler*.txt for examples.
LTO    ?= -flto
ifeq ($(CC),cc)
    CC = clang -std=gnu99
endif
ifeq ($(CXX),c++)
    CXX = clang++ -std=gnu++14
    CXXFLAGS += -fvisibility=hidden
endif

# This is used only to compile plat/SDL1_osx_main.m.
CC_MFILE ?= clang $(ARCH)

# This must be 10.9 or higher with current XCode / Clang 11.
# This number can go as low as to 10.6 with GCC.
MACOS_VERSION_MIN ?= 10.9

# Default optimization flags, if none specified in a buildcfg/*.mak file.
OPT_FLAGS ?= -O3

CFLAGS   += $(ARCH) -mmacosx-version-min=$(MACOS_VERSION_MIN)
CFLAGS   += -I. -I.. $(EXTRA)

CXXFLAGS += $(ARCH) -mmacosx-version-min=$(MACOS_VERSION_MIN)
CXXFLAGS += -I. -I.. $(EXTRA) 

LFLAGS   += -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib

# Note: plat/SDL1_osx_main.$(O) is *not* included in OBJS_SDL1, as it needs
# to be handled specially.  -- Tues Aug. 31 JJT
OBJS       = jzintv.$(O) 
PROG_SDL1  = $(B)/jzintv$(X)
PROG_NULL  = $(B)/jzintv_batch$(X)
TOCLEAN   += $(PROG_SDL1) $(PROG_NULL) core
TOCLEAN   += libjzintv_common.a libjzintv_sdl.a libjzintv_null.a
TOCLEAN   += plat/SDL1_osx_main.$(O) jzintv_fromcommon$(X)
OBJS_SDL1 += pads/pads_cgc_linux.$(O)

CFLAGS    += $(SDL1_CFLAGS) 
CXXFLAGS  += $(SDL1_CFLAGS) 
# Reserving SDL to the FINAL link --Sat Aug. 7 JJT
#LFLAGS += $(SDL1_LFLAGS)

# XXX: Make this work on Mojave through a terrible, terrible hack. --JZ
LFLAGS += -Wl,-platform_version,macos,$(MACOS_VERSION_MIN),10.13.0

# Forcibly override static linkage, which is not supported on Mac.
SLFLAGS = $(LFLAGS)

# CFLAGS_MFILE is a more limited CFLAGS set for building the .m file.
# It omits the sanitizer and LTO flags.
CFLAGS_MFILE := $(CFLAGS) $(WARN_M)

# Add sanitizer, optimization and LTO flags to main CFLAGS / CXXFLAGS.
CFLAGS    += $(SANI) $(LTO) $(OPT_FLAGS) $(WARN)
CXXFLAGS  += $(SANI) $(LTO) $(OPT_FLAGS) $(WARNXX)
 
ifeq ($(GNU_READLINE),1)
# Assuming you've built Readline yourself, point to the right header dir
# and library to explicitly link.
RL_CFLAGS = -DUSE_GNU_READLINE -I/usr/local/include
RL_LFLAGS = /usr/local/lib/libreadline.a -ltermcap
endif

##############################################################################
## Generic build-rules
##############################################################################

all: build

#libjzintv_common.a libjzintv_sdl.a libjzintv_null.a

##############################################################################
## Makefile.common includes all the subMakefiles and such
##############################################################################
include Makefile.common
OBJS_SDL1 += plat/SDL1_osx_main.$(O)
 
$(PROG_SDL1): $(OBJS) $(OBJS_SDL1)
	$(CXX) -o $(PROG_SDL1) $(OBJS) $(OBJS_SDL1) $(CFLAGS) $(LFLAGS) $(SDL1_LFLAGS) $(RL_LFLAGS)

$(PROG_NULL): $(OBJS) $(OBJS_NULL)
	$(CXX) -o $(PROG_NULL) $(OBJS) $(OBJS_NULL) $(CFLAGS) $(LFLAGS) $(RL_LFLAGS)

#Library for use with the OS X GUI project.
libjzintv_common.a : $(OBJS)
	libtool -o libjzintv_common.a $(OBJS)

libjzintv_sdl.a : $(OBJS_SDL1)
	libtool -o libjzintv_sdl.a $(OBJS_SDL1)

libjzintv_null.a : $(OBJS_NULL)
	libtool -o libjzintv_null.a $(OBJS_NULL)

#Test program for libjzintv_common.a . If everything is OK, this program should
#behave exactly like jzintv.
jzintv_fromcommon : libjzintv_common.a libjzintv_sdl.a
	$(CC) -o jzintv_fromcommon $(CFLAGS) $(LFLAGS) $(SDL1_LFLAGS) -L. -ljzintv_common -ljzintv_sdl

clean:
	$(RM) $(OBJS) 
	$(RM) $(OBJS_SDL1) 
	$(RM) $(OBJS_NULL) 
	$(RM) $(TOCLEAN)

plat/SDL1_osx_main.$(O): plat/SDL1_osx_main.m plat/SDL1_osx_main.h
	$(CC_MFILE) -o $@ $(CFLAGS_MFILE) -fno-objc-arc $(SDL_PORT_SYN_FLAGS) -Wno-error -c $<
	
%.$(O): %.c
	$(CC) -o $@  $(CFLAGS) -c $<

%.$(O): %.cpp
	$(CXX) -o $@  $(CXXFLAGS) -c $<

build: jzIntv SDK-1600
