# This file is part of BOINC.
# https://boinc.berkeley.edu
# Copyright (C) 2025 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# BOINC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
#

# stand-alone makefile for uc2 BOINC example application on Macintosh.
# Updated 7/11/12 for OS 10.7 and XCode 4.3
# Updated 8/3/12 for TrueType fonts
# Updated 11/8/12 to add slide_show
# Updated 4/14/15 for compatibility with Xcode 6
# Updated 4/30/20 for compatibility with Xcode 11 and OS 10.13
# Updated 2/6/23 added arm64 support
# Updated 9/28/25 to replace boinc_zip with the libzip library dependency
#
## First, build the BOINC libraries using boinc/mac_build/BuildMacBOINC.sh
## This file assumes the locations of the needed libraries are those
## resulting from following the instructions found in the file:
##     boinc/mac_build/HowToBuildBOINC_XCode.rtf
## In Terminal, CD to the example_app directory:
##     cd [path]/example_app/
## and run this make file:
##     make -f Makefile_mac2
##
## NOTE: this requires the libzip library which can be installed by the
## scipt mac_build/buildlibzip.sh, which uses CMake via the command line.
#

BOINC_DIR = ../..
BOINC_API_DIR = $(BOINC_DIR)/api
BOINC_LIB_DIR = $(BOINC_DIR)/lib
BOINC_ZIP_DIR = $(BOINC_DIR)/zip
BOINC_BUILD_DIR = $(BOINC_DIR)/mac_build/build/Deployment
BOINC_CONFIG_DIR = $(BOINC_DIR)/clientgui/mac
FREETYPE_DIR = $(BOINC_DIR)/../freetype-2.11.0
FTGL_DIR = $(BOINC_DIR)/../ftgl-2.1.3~rc5
LIBZIP_DIR = $(BOINC_DIR)/../libzip
FRAMEWORKS_DIR = /System/Library/Frameworks

CXXFLAGS_ALL = \
    $(ISYSROOT) \
    -g \
    -stdlib=libc++ \
    -DAPP_GRAPHICS \
    -DMAC_OS_X_VERSION_MAX_ALLOWED=101300 \
    -DMAC_OS_X_VERSION_MIN_REQUIRED=101300 \
    -fvisibility=hidden \
    -fvisibility-inlines-hidden \
    -I$(BOINC_CONFIG_DIR) \
    -I$(BOINC_DIR) \
    -I$(BOINC_LIB_DIR) \
    -I$(BOINC_API_DIR) \
    -I$(BOINC_ZIP_DIR) \
    -I$(FREETYPE_DIR)/include \
    -I$(PREFIX)/include/freetype2 \
    -I$(FTGL_DIR)/src \
    -I$(PREFIX)/include

LDFLAGS_ALL = \
    $(SYSLIBROOT) \
    -Wl,-L$(BOINC_BUILD_DIR) \
    -Wl,-L$(FREETYPE_DIR)/objs/.libs \
    -Wl,-L$(FTGL_DIR)/src/.libs \
    -Wl,-L$(LIBZIP_DIR)/lib \
    -Wl,-L$(PREFIX)/lib \
    -Wl,-L. \
    -Wl,-L/usr/lib

CXXFLAGS_x86_64 = -arch x86_64 $(CXXFLAGS_ALL)
LDFLAGS_x86_64 = -Wl,-arch,x86_64 $(LDFLAGS_ALL)

CXXFLAGS_arm64 = -arch arm64 $(CXXFLAGS_ALL)
LDFLAGS_arm64 = -Wl,-arch,arm64 $(LDFLAGS_ALL)


OBJ = \
    uc2_x86_64.o \
    ttfont_x86_64.o \
    uc2_graphics_x86_64.o \
    slide_show_x86_64.o \
    uc2_arm64.o \
    ttfont_arm64.o \
    uc2_graphics_arm64.o \
    slide_show_arm64.o


PROGS = \
    uc2_x86_64 uc2_graphics_x86_64 slide_show_x86_64 uc2_arm64 uc2_graphics_arm64 slide_show_arm64

all: $(PROGS)

clean:
	/bin/rm -f $(PROGS) $(OBJ)

uc2_x86_64: export MACOSX_DEPLOYMENT_TARGET=10.13
uc2_graphics_x86_64: export MACOSX_DEPLOYMENT_TARGET=10.13
slide_show_x86_64: export MACOSX_DEPLOYMENT_TARGET=10.13
uc2_arm64: export MACOSX_DEPLOYMENT_TARGET=10.13
uc2_graphics_arm64: export MACOSX_DEPLOYMENT_TARGET=10.13
slide_show_arm64: export MACOSX_DEPLOYMENT_TARGET=10.13

# target uc2_x86_64: MACOSX_DEPLOYMENT_TARGET=10.13
uc2_x86_64.o: uc2.cpp
	$(CXX) $(CXXFLAGS_x86_64) -c uc2.cpp -o uc2_x86_64.o
uc2_arm64.o: uc2.cpp
	$(CXX) $(CXXFLAGS_arm64) -c uc2.cpp -o uc2_arm64.o

ttfont_x86_64.o: $(BOINC_API_DIR)/ttfont.cpp
	$(CXX) $(CXXFLAGS_x86_64) -c $(BOINC_API_DIR)/ttfont.cpp -o ttfont_x86_64.o
ttfont_arm64.o: $(BOINC_API_DIR)/ttfont.cpp
	$(CXX) $(CXXFLAGS_arm64) -c $(BOINC_API_DIR)/ttfont.cpp -o ttfont_arm64.o

uc2_graphics_x86_64.o: uc2_graphics.cpp
	$(CXX) $(CXXFLAGS_x86_64) -c uc2_graphics.cpp -o uc2_graphics_x86_64.o
uc2_graphics_arm64.o: uc2_graphics.cpp
	$(CXX) $(CXXFLAGS_arm64) -c uc2_graphics.cpp -o uc2_graphics_arm64.o

slide_show_x86_64.o: slide_show.cpp
	$(CXX) $(CXXFLAGS_x86_64) -c slide_show.cpp -o slide_show_x86_64.o
slide_show_arm64.o: slide_show.cpp
	$(CXX) $(CXXFLAGS_arm64) -c slide_show.cpp -o slide_show_arm64.o

uc2_x86_64: uc2_x86_64.o $(BOINC_BUILD_DIR)/libboinc_api.a $(BOINC_BUILD_DIR)/libboinc.a
	$(CXX) $(CXXFLAGS_x86_64) $(LDFLAGS_x86_64) -o uc2_x86_64 uc2_x86_64.o -lboinc_api -lboinc
uc2_arm64: uc2_arm64.o $(BOINC_BUILD_DIR)/libboinc_api.a $(BOINC_BUILD_DIR)/libboinc.a
	$(CXX) $(CXXFLAGS_arm64) $(LDFLAGS_arm64) -o uc2_arm64 uc2_arm64.o -lboinc_api -lboinc

uc2_graphics_x86_64: uc2_graphics_x86_64.o ttfont_x86_64.o $(BOINC_BUILD_DIR)/libboinc.a\
    $(BOINC_BUILD_DIR)/libboinc_graphics2.a
	$(CXX) $(CXXFLAGS_x86_64) $(LDFLAGS_x86_64) -o uc2_graphics_x86_64\
    uc2_graphics_x86_64.o ttfont_x86_64.o -lboinc_graphics2 -lboinc_api -lboinc -ljpeg\
    -lfreetype -lftgl -lz -lbz2 -framework AppKit -framework GLUT -framework OpenGL\
    -framework IOSurface
uc2_graphics_arm64: uc2_graphics_arm64.o ttfont_arm64.o $(BOINC_BUILD_DIR)/libboinc.a\
    $(BOINC_BUILD_DIR)/libboinc_graphics2.a
	$(CXX) $(CXXFLAGS_arm64) $(LDFLAGS_arm64) -o uc2_graphics_arm64\
    uc2_graphics_arm64.o ttfont_arm64.o -lboinc_graphics2 -lboinc_api -lboinc -ljpeg\
    -lfreetype -lftgl -lz -lbz2 -framework AppKit -framework GLUT -framework OpenGL\
    -framework IOSurface

slide_show_x86_64: slide_show_x86_64.o $(BOINC_BUILD_DIR)/libboinc.a\
    $(BOINC_BUILD_DIR)/libboinc_graphics2.a
	$(CXX) $(CXXFLAGS_x86_64) $(LDFLAGS_x86_64) -o slide_show_x86_64 slide_show_x86_64.o\
    -lboinc_graphics2 -lboinc_api -lboinc -lboinc_zip -ljpeg -lzip -lz\
    -framework AppKit -framework GLUT -framework OpenGL -framework IOSurface
slide_show_arm64: slide_show_arm64.o $(BOINC_BUILD_DIR)/libboinc.a\
    $(BOINC_BUILD_DIR)/libboinc_graphics2.a
	$(CXX) $(CXXFLAGS_arm64) $(LDFLAGS_arm64) -o slide_show_arm64 slide_show_arm64.o\
    -lboinc_graphics2 -lboinc_api -lboinc -lboinc_zip -ljpeg -lzip -lz\
    -framework AppKit -framework GLUT -framework OpenGL -framework IOSurface
