# Makefile for building Golly on Mac OS 10.11+.
# It assumes you've built the wxWidgets libraries with commands like these:
#
# cd /path/to/wxWidgets/wxWidgets-3.1.5  (use 3.1.5+ on Mac OS 10.11+)
# mkdir build-osx
# cd build-osx
# ../configure --disable-arttango --disable-sys-libs --disable-shared --with-osx_cocoa --with-macosx-version-min=10.11 --enable-universal_binary=arm64,x86_64
# make

# In the lines below, change WX_DIR to where you installed wxWidgets,
# change WXLIB_DIR to where you built the wx libraries, and change
# WX_RELEASE to match your wxWidgets version (first two digits).

WX_RELEASE = 3.2
WX_DIR = /Users/akt/HD/wxWidgets/wxWidgets-3.2.7
WXLIB_DIR = $(WX_DIR)/build-osx

WXINC_DIR = $(WXLIB_DIR)/lib/wx/include/osx_cocoa-unicode-static-$(WX_RELEASE)
WXUNICODEFLAG = u

APP_NAME = Golly
APP_VERSION = 5.0

# these directory paths are relative to the location of this makefile:
EXEDIR = ..
DOCSDIR = ../docs
BASEDIR = ../gollybase
CMDDIR = ../cmdline
LUADIR = ../lua
SOUNDDIR = ../sound
OTHERGUIS = ../gui-android ../gui-common ../gui-ios ../gui-web

# for building binary and source distributions:
RELEASENAME = $(EXEDIR)/golly-$(APP_VERSION)
SHAREDFILES = $(EXEDIR)/Help $(EXEDIR)/Patterns $(EXEDIR)/Scripts $(EXEDIR)/Rules
BINFILES = $(EXEDIR)/$(APP_NAME).app $(EXEDIR)/bgolly \
   $(DOCSDIR)/ReadMe.html $(DOCSDIR)/License.html
SRCFILES = $(DOCSDIR) $(BASEDIR) $(CMDDIR) $(SOUNDDIR) $(OTHERGUIS)
GUIFILES = makefile-gtk makefile-mac makefile-win local-win-template.mk local-gtk-template.mk \
   golly.rc Info.plist.in wx*.h wx*.cpp bitmaps icons
LUAFILES = $(LUADIR)/*.h $(LUADIR)/*.c $(LUADIR)/*.hpp $(LUADIR)/Makefile $(LUADIR)/ReadMe.html

# for Python 3 script support:
### PYTHON_INCLUDE = -I`/usr/local/bin/python3 -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())"`
### following works with Python 3.12 (using installer from python.org) on macOS Sonoma 14.5:
PYTHON_INCLUDE = `python3-config --includes`
# PYTHON_LINK is no longer needed

# Variables:

LUALIB = $(LUADIR)/liblua.a

LIBDIRNAME = $(WXLIB_DIR)/lib
TOOLKIT = OSX_COCOA
TOOLKIT_LOWERCASE = osx_cocoa
TOOLKIT_VERSION = 

CXXC = g++ -mmacosx-version-min=10.11

CPPFLAGS = -DWX_PRECOMP -DNO_GCC_PRAGMA -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES \
   -I${WXINC_DIR} -I${WX_DIR}/include \
   -I$(BASEDIR) -I$(LUADIR)

CXXFLAGS = -arch arm64 -arch x86_64 \
   -D__WX$(TOOLKIT)__ -DVERSION=$(APP_VERSION) $(CPPFLAGS) \
   -DZLIB -O3 -Wall -Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -Wno-c++11-extensions \
   -Wno-deprecated-declarations -Wno-potentially-evaluated-expression -fno-strict-aliasing -fno-common

# some script functions need to call @autoreleasepool to avoid memory leaks
AUTO_RELEASE = -x objective-c++ -DAUTORELEASE

# comment out next 2 lines to disable sound play:
CXXFLAGS += -DENABLE_SOUND
CPPFLAGS += -I$(SOUNDDIR)

LDFLAGS = -arch arm64 -arch x86_64 \
   -Wl,-dead_strip -Wl,-S \
   -framework IOKit -framework Carbon -framework Cocoa -framework System -framework QuartzCore

# for bgolly:
CXXBASE = -arch arm64 -arch x86_64 \
   -DVERSION=$(APP_VERSION) -DZLIB -O3 -Wall -Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -Wno-c++11-extensions \
   -Wno-deprecated-declarations -fno-strict-aliasing -fno-common -I$(BASEDIR)
LDBASE = -arch arm64 -arch x86_64 -lz

EXTRALIBS = -lz -lpthread -liconv -lcurl
EXTRALIBS_GUI = -framework WebKit -framework AudioToolbox -framework OpenGL -framework AGL

OBJDIR = ObjOSX
BASEH = $(BASEDIR)/bigint.h $(BASEDIR)/ghashbase.h $(BASEDIR)/hlifealgo.h $(BASEDIR)/jvnalgo.h \
   $(BASEDIR)/platform.h $(BASEDIR)/lifealgo.h $(BASEDIR)/lifepoll.h $(BASEDIR)/liferender.h $(BASEDIR)/liferules.h \
   $(BASEDIR)/qlifealgo.h $(BASEDIR)/ltlalgo.h $(BASEDIR)/readpattern.h $(BASEDIR)/util.h $(BASEDIR)/viewport.h \
   $(BASEDIR)/writepattern.h $(BASEDIR)/ruletreealgo.h $(BASEDIR)/generationsalgo.h $(BASEDIR)/ruletable_algo.h \
   $(BASEDIR)/ruleloaderalgo.h $(BASEDIR)/superalgo.h
BASEOBJ = $(OBJDIR)/bigint.o $(OBJDIR)/lifealgo.o $(OBJDIR)/hlifealgo.o \
   $(OBJDIR)/hlifedraw.o $(OBJDIR)/qlifealgo.o $(OBJDIR)/qlifedraw.o $(OBJDIR)/ltlalgo.o $(OBJDIR)/ltldraw.o \
   $(OBJDIR)/jvnalgo.o $(OBJDIR)/ruletreealgo.o $(OBJDIR)/ruletable_algo.o $(OBJDIR)/ruleloaderalgo.o \
   $(OBJDIR)/ghashbase.o $(OBJDIR)/ghashdraw.o $(OBJDIR)/readpattern.o \
   $(OBJDIR)/writepattern.o $(OBJDIR)/liferules.o $(OBJDIR)/util.o \
   $(OBJDIR)/liferender.o $(OBJDIR)/viewport.o $(OBJDIR)/lifepoll.o \
   $(OBJDIR)/generationsalgo.o $(OBJDIR)/superalgo.o
WXH = wxalgos.h wxedit.h wxgolly.h wxhelp.h wxinfo.h wxlayer.h wxmain.h wxprefs.h \
   wxlua.h wxpython.h wxrender.h wxrule.h wxscript.h wxselect.h wxstatus.h \
   wxtimeline.h wxundo.h wxutils.h wxview.h wxoverlay.h
WXOBJ = $(OBJDIR)/wxutils.o $(OBJDIR)/wxprefs.o $(OBJDIR)/wxalgos.o $(OBJDIR)/wxrule.o \
   $(OBJDIR)/wxinfo.o $(OBJDIR)/wxhelp.o $(OBJDIR)/wxstatus.o $(OBJDIR)/wxview.o $(OBJDIR)/wxoverlay.o \
   $(OBJDIR)/wxrender.o $(OBJDIR)/wxscript.o $(OBJDIR)/wxlua.o $(OBJDIR)/wxpython.o \
   $(OBJDIR)/wxfile.o $(OBJDIR)/wxedit.o $(OBJDIR)/wxcontrol.o $(OBJDIR)/wxtimeline.o \
   $(OBJDIR)/wxundo.o $(OBJDIR)/wxselect.o $(OBJDIR)/wxlayer.o $(OBJDIR)/wxmain.o $(OBJDIR)/wxgolly.o

PORTNAME = $(TOOLKIT_LOWERCASE)$(TOOLKIT_VERSION)
###WXBASEPORT = _carbon
###WXDEBUGFLAG = d
__WXLIB_HTML_p = -lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)_html-$(WX_RELEASE)
__WXLIB_ADV_p = -lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)_adv-$(WX_RELEASE)
__WXLIB_CORE_p = -lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)_core-$(WX_RELEASE)
__WXLIB_BASE_p = -lwx_base$(WXBASEPORT)$(WXUNICODEFLAG)$(WXDEBUGFLAG)-$(WX_RELEASE)
__WXLIB_NET_p = -lwx_base$(WXBASEPORT)$(WXUNICODEFLAG)$(WXDEBUGFLAG)_net-$(WX_RELEASE)
__WXLIB_GL_p = -lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)_gl-$(WX_RELEASE)
__LIB_TIFF_p = -lwxtiff$(WXDEBUGFLAG)-$(WX_RELEASE)
__LIB_PNG_p = -lwxpng$(WXDEBUGFLAG)-$(WX_RELEASE)
__LIB_JPEG_p = -lwxjpeg$(WXDEBUGFLAG)-$(WX_RELEASE)
__LIB_ZLIB_p = -lwxzlib$(WXDEBUGFLAG)-$(WX_RELEASE)

# Targets:

all: $(OBJDIR) app.bin app_bundle bgolly

$(BASEOBJ): $(BASEH)
$(WXOBJ): $(BASEH) $(WXH) icons/appicon.xpm bitmaps/*.xpm

$(OBJDIR):
	mkdir -p $(OBJDIR)

clean:
	rm -f $(OBJDIR)/*.o $(EXEDIR)/bgolly
	rm -rf $(EXEDIR)/$(APP_NAME).app
	-(cd $(LUADIR) && $(MAKE) clean)

bgolly: $(BASEOBJ) $(OBJDIR)/bgolly.o
	$(CXXC) $(CXXBASE) -o $(EXEDIR)/bgolly $(BASEOBJ) $(OBJDIR)/bgolly.o $(LDBASE)

$(OBJDIR)/bgolly.o: $(CMDDIR)/bgolly.cpp
	$(CXXC) $(CXXBASE) -c -o $@ $(CMDDIR)/bgolly.cpp

app.bin: $(BASEOBJ) $(WXOBJ) $(LUALIB)
	$(CXXC) -o $@ $(BASEOBJ) $(WXOBJ) $(LUALIB) $(LDFLAGS) -L$(LIBDIRNAME) \
$(__WXLIB_HTML_p) $(__WXLIB_ADV_p) $(__WXLIB_CORE_p) \
$(__WXLIB_BASE_p) $(__WXLIB_NET_p) $(__WXLIB_GL_p) \
$(__LIB_TIFF_p) $(__LIB_PNG_p) $(__LIB_JPEG_p) $(__LIB_ZLIB_p) \
$(EXTRALIBS) $(EXTRALIBS_GUI) $(PYTHON_LINK)

$(EXEDIR)/$(APP_NAME).app/Contents/PkgInfo: app.bin Info.plist.in \
icons/app.icns icons/file-mc.icns icons/file-rle.icns
	mkdir -p $(EXEDIR)/$(APP_NAME).app/Contents
	mkdir -p $(EXEDIR)/$(APP_NAME).app/Contents/MacOS
	mkdir -p $(EXEDIR)/$(APP_NAME).app/Contents/Resources
	
	sed -e "s/VERSION/$(APP_VERSION)/" \
	Info.plist.in >$(EXEDIR)/$(APP_NAME).app/Contents/Info.plist
	
	echo -n "APPLGoLy" >$(EXEDIR)/$(APP_NAME).app/Contents/PkgInfo
	
	mv -f app.bin $(EXEDIR)/$(APP_NAME).app/Contents/MacOS/$(APP_NAME)
	
	cp -f icons/*.icns $(EXEDIR)/$(APP_NAME).app/Contents/Resources

### no need now that we use --disable-sys-libs
### 	echo "\nWARNING: If any of the following locations start with /usr/local you've picked up local stuff!\n"
### 	otool -L -arch all $(EXEDIR)/$(APP_NAME).app/Contents/MacOS/Golly

app_bundle: $(EXEDIR)/$(APP_NAME).app/Contents/PkgInfo

$(LUALIB):
	(cd $(LUADIR) && $(MAKE) all)

$(OBJDIR)/bigint.o: $(BASEDIR)/bigint.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/bigint.cpp

$(OBJDIR)/lifealgo.o: $(BASEDIR)/lifealgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/lifealgo.cpp

$(OBJDIR)/hlifealgo.o: $(BASEDIR)/hlifealgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/hlifealgo.cpp

$(OBJDIR)/hlifedraw.o: $(BASEDIR)/hlifedraw.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/hlifedraw.cpp

$(OBJDIR)/qlifealgo.o: $(BASEDIR)/qlifealgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/qlifealgo.cpp

$(OBJDIR)/qlifedraw.o: $(BASEDIR)/qlifedraw.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/qlifedraw.cpp

$(OBJDIR)/ltlalgo.o: $(BASEDIR)/ltlalgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ltlalgo.cpp

$(OBJDIR)/ltldraw.o: $(BASEDIR)/ltldraw.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ltldraw.cpp

$(OBJDIR)/jvnalgo.o: $(BASEDIR)/jvnalgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/jvnalgo.cpp

$(OBJDIR)/ruleloaderalgo.o: $(BASEDIR)/ruleloaderalgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ruleloaderalgo.cpp

$(OBJDIR)/ruletable_algo.o: $(BASEDIR)/ruletable_algo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ruletable_algo.cpp

$(OBJDIR)/ruletreealgo.o: $(BASEDIR)/ruletreealgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ruletreealgo.cpp

$(OBJDIR)/generationsalgo.o: $(BASEDIR)/generationsalgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/generationsalgo.cpp

$(OBJDIR)/superalgo.o: $(BASEDIR)/superalgo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/superalgo.cpp

$(OBJDIR)/ghashbase.o: $(BASEDIR)/ghashbase.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ghashbase.cpp

$(OBJDIR)/ghashdraw.o: $(BASEDIR)/ghashdraw.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/ghashdraw.cpp

$(OBJDIR)/liferules.o: $(BASEDIR)/liferules.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/liferules.cpp

$(OBJDIR)/liferender.o: $(BASEDIR)/liferender.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/liferender.cpp

$(OBJDIR)/readpattern.o: $(BASEDIR)/readpattern.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/readpattern.cpp

$(OBJDIR)/writepattern.o: $(BASEDIR)/writepattern.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/writepattern.cpp

$(OBJDIR)/util.o: $(BASEDIR)/util.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/util.cpp

$(OBJDIR)/viewport.o: $(BASEDIR)/viewport.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/viewport.cpp

$(OBJDIR)/lifepoll.o: $(BASEDIR)/lifepoll.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ $(BASEDIR)/lifepoll.cpp

$(OBJDIR)/wxutils.o: wxutils.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxutils.cpp

$(OBJDIR)/wxprefs.o: wxprefs.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxprefs.cpp

$(OBJDIR)/wxalgos.o: wxalgos.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxalgos.cpp

$(OBJDIR)/wxrule.o: wxrule.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxrule.cpp

$(OBJDIR)/wxinfo.o: wxinfo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxinfo.cpp

$(OBJDIR)/wxhelp.o: wxhelp.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxhelp.cpp

$(OBJDIR)/wxstatus.o: wxstatus.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxstatus.cpp

$(OBJDIR)/wxview.o: wxview.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxview.cpp

$(OBJDIR)/wxoverlay.o: wxoverlay.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxoverlay.cpp

$(OBJDIR)/wxrender.o: wxrender.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxrender.cpp

$(OBJDIR)/wxlua.o: wxlua.cpp
	$(CXXC) $(CXXFLAGS) $(AUTO_RELEASE) -c -o $@ wxlua.cpp

$(OBJDIR)/wxpython.o: wxpython.cpp
	$(CXXC) $(CXXFLAGS) $(PYTHON_INCLUDE) $(AUTO_RELEASE) -c -o $@ wxpython.cpp

$(OBJDIR)/wxscript.o: wxscript.cpp $(SOUNDDIR)/cute_sound.h $(SOUNDDIR)/stb_vorbis.c
	$(CXXC) $(CXXFLAGS) -c -o $@ wxscript.cpp

$(OBJDIR)/wxfile.o: wxfile.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxfile.cpp

$(OBJDIR)/wxedit.o: wxedit.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxedit.cpp

$(OBJDIR)/wxselect.o: wxselect.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxselect.cpp

$(OBJDIR)/wxcontrol.o: wxcontrol.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxcontrol.cpp

$(OBJDIR)/wxtimeline.o: wxtimeline.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxtimeline.cpp

$(OBJDIR)/wxundo.o: wxundo.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxundo.cpp

$(OBJDIR)/wxlayer.o: wxlayer.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxlayer.cpp

$(OBJDIR)/wxmain.o: wxmain.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxmain.cpp

$(OBJDIR)/wxgolly.o: wxgolly.cpp
	$(CXXC) $(CXXFLAGS) -c -o $@ wxgolly.cpp

srcdist:
	-rm -rf $(RELEASENAME)-src
	mkdir $(RELEASENAME)-src
	mkdir $(RELEASENAME)-src/gui-wx
	mkdir $(RELEASENAME)-src/lua
	cp -rp $(SRCFILES) $(SHAREDFILES) $(RELEASENAME)-src
	cp -rp $(GUIFILES) $(RELEASENAME)-src/gui-wx
	cp -rp $(LUAFILES) $(RELEASENAME)-src/lua
	find $(RELEASENAME)-src -name '.[^.]*' -delete
	find $(RELEASENAME)-src -name '*.o' -delete
	find $(RELEASENAME)-src -name 'makefile' -delete
	find $(RELEASENAME)-src/Scripts/Python -name '*.pyc' -delete
	#### BUGGY!!! tar -cf - ./$(RELEASENAME)-src | gzip > $(RELEASENAME)-src.tar.gz
	echo Now create zip archive of $(RELEASENAME)-src

bindist: all
	-rm -rf $(RELEASENAME)-mac
	mkdir $(RELEASENAME)-mac
	cp -rp $(BINFILES) $(SHAREDFILES) $(RELEASENAME)-mac
	find $(RELEASENAME)-mac -name '.[^.]*' -delete
	find $(RELEASENAME)-mac/Scripts/Python -name '*.pyc' -delete
	echo Now create zip archive of $(RELEASENAME)-mac
