#
#  Rules for building the Allegro library with MacOS X. This file is included
#  by the primary makefile, and should not be used directly.
#
#  The "depend" and "install" targets use sed.
#
#  See makefile.all for a list of the available targets.



# -------- define some variables that the primary makefile will use --------

PLATFORM = MacOS X
ifdef UB
CC = misc/gcc-uni.sh
else
CC = gcc
endif
OBJ_DIR = obj/macosx/$(VERSION)
EXE = 
OBJ = .o
HTML = html

PLATFORM_DIR = obj/macosx

UNIX_TOOLS = 1

RM_OPTS = -f


# -------- local variables --------

FRAMEWORK_DIR = /Library/Frameworks
FRAMEWORK_NAME = Allegro
FRAMEWORK = $(FRAMEWORK_DIR)/$(FRAMEWORK_NAME).framework
HELPBUNDLE:=/Library/Documentation/Help/Allegro.bundle

TEMPLATE_DIR = /Developer/ProjectBuilder\ Extras/Project\ Templates/Application
ifeq ($(wildcard $(TEMPLATE_DIR)),)
	TEMPLATE_DIR = /Library/Application\ Support/Apple/Developer\ Tools/Project\ Templates/Application
endif
TEMPLATE_NAME = Allegro\ Application
TEMPLATE = $(TEMPLATE_DIR)/$(TEMPLATE_NAME)

MAN_DIR = /usr/local/man


ifdef STATICLINK

# -------- link as a static library --------
LIB_NAME = lib/macosx/lib$(VERSION)_s.a

else

# -------- link as a shared library --------
LIB_NAME = lib/macosx/lib$(VERSION)-$(shared_version).dylib
LIB_MAIN_NAME = lib/macosx/lib$(VERSION)-main.a

DYLINK_FLAGS = -compatibility_version $(compatibility_version) -current_version $(shared_version)

INSTALL_NAME = -install_name lib$(VERSION)-$(shared_major_minor).dylib
INSTALL_NAME_EMBED = -install_name "`echo "@executable_path/../Frameworks/$(FRAMEWORK_NAME).framework/Versions/$(shared_version)/$(FRAMEWORK_NAME)" | sed 's!//*!/!g'`"

ifdef EMBED
FRAMEWORK_DESCRIPTION = "embeddable"
else
FRAMEWORK_DESCRIPTION = "system wide"
endif

endif # STATICLINK



# -------- give a sensible default target for make without any args --------

.PHONY: _default

_default: default



# -------- decide what compiler options to use --------

ifdef WARNMODE
   WFLAGS = -Wall -W -Werror -Wno-unused -Wno-long-double
else
   WFLAGS = -Wall -Wno-unused -Wno-long-double
endif

ifdef TARGET_ARCH_COMPAT
   TARGET_ARCH = $(GCC_MTUNE)=$(TARGET_ARCH_COMPAT)
else
   ifdef TARGET_ARCH_EXCL
      TARGET_ARCH = -march=$(TARGET_ARCH_EXCL)
   endif
endif

ifndef TARGET_OPTS
   TARGET_OPTS = -O2 -funroll-loops -ffast-math
endif

OFLAGS = $(TARGET_ARCH) $(TARGET_OPTS)

CFLAGS = -DALLEGRO_LIB_BUILD 



ifdef DEBUGMODE

# -------- debugging build --------
CFLAGS += -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -g
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
LFLAGS = -g

else
ifdef PROFILEMODE

# -------- profiling build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -pg
SFLAGS = $(WFLAGS)
LFLAGS = -pg

else

# -------- optimised build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -fomit-frame-pointer
SFLAGS = $(WFLAGS)

ifndef SYMBOLMODE
LFLAGS = -s
else
LFLAGS =
endif

endif
endif

CFLAGS += -fno-common -pipe
ifndef STATICLINK
CFLAGS += -dynamic
endif


# -------- list platform specific objects and programs --------

VPATH = src/macosx src/unix src/misc src/c tools/macosx
CFLAGS += -DALLEGRO_NO_ASM

ifdef STATICLINK
ALLEGRO_SRC_MACOSX_FILES += src/macosx/main.m
endif

OBJECT_LIST = $(COMMON_OBJECTS) $(C_OBJECTS) $(basename $(notdir $(ALLEGRO_SRC_MACOSX_FILES)))

STATIC_LIBRARIES = -framework Cocoa -framework Carbon -framework IOKit -framework System -framework CoreAudio -framework AudioUnit -framework AudioToolbox -framework QuickTime
ifdef STATICLINK
LIBRARIES = $(STATIC_LIBRARIES)
else
LIBRARIES = -framework Cocoa
endif

PROGRAMS = fixbundle

fixbundle: tools/macosx/fixbundle

DISTCLEAN_FILES += tools/macosx/fixbundle


# -------- rules for installing and removing the library files --------

DESTDIR =
INSTALLDIR = $(DESTDIR)/usr/local
BINDIR = bin
LIBDIR = lib
INCDIR = include


ifdef STATICLINK

$(INSTALLDIR)/lib/lib$(VERSION)_s.a: $(LIB_NAME)
	install -d $(INSTALLDIR)/lib
	install $(LIB_NAME) $(INSTALLDIR)/lib
	ranlib $(INSTALLDIR)/lib/lib$(VERSION)_s.a

else

$(INSTALLDIR)/lib/lib$(VERSION)-main.a: $(LIB_MAIN_NAME)
	install -d $(INSTALLDIR)/lib
	install $(LIB_MAIN_NAME) $(INSTALLDIR)/lib
	ranlib $(INSTALLDIR)/lib/lib$(VERSION)-main.a
$(INSTALLDIR)/lib/lib$(VERSION)-$(shared_version).dylib: $(LIB_NAME)
	install $(LIB_NAME) $(INSTALLDIR)/lib
	(cd $(INSTALLDIR)/lib; ln -s -f lib$(VERSION)-$(shared_version).dylib lib$(VERSION)-$(shared_major_minor).dylib)
	(cd $(INSTALLDIR)/lib; ln -s -f lib$(VERSION)-$(shared_version).dylib lib$(VERSION)-4.dylib)
	(cd $(INSTALLDIR)/lib; ln -s -f lib$(VERSION)-$(shared_version).dylib lib$(VERSION).dylib)
endif


$(INSTALLDIR)/bin/allegro-config: $(LIB_NAME)
	install -d $(INSTALLDIR)/bin
ifdef STATICLINK
	@sed -e "s/@LINK_WITH_STATIC_LIBS@/yes/" misc/allegro-config.in >temp
else
	@sed -e "s/@LINK_WITH_STATIC_LIBS@/no/" misc/allegro-config.in >temp
endif
	@sed -e "s#@prefix@#$(INSTALLDIR)#" temp > temp2
	@sed -e "s#@INCLUDE_PREFIX@#$(INSTALLDIR)#" temp2 > temp
	@sed -e "s#@includedir@#$(INSTALLDIR)/include#" temp > temp2
	@sed -e "s#@libdir@#$(INSTALLDIR)/lib#" temp2 > temp
	@sed -e "s#@bindir@#$(INSTALLDIR)/bin#" temp > temp2
	@sed -e "s/@LIB_TO_LINK@/$(VERSION)/" temp2 > temp
	@sed -e "s/@LDFLAGS@//" temp > temp2
	@sed -e "s/@LIBS@/$(STATIC_LIBRARIES)/" temp2 > temp
	@cat temp > temp2
	@sed -e "s/@FRAMEWORKS@/-framework Cocoa -framework $(FRAMEWORK_NAME) -l$(VERSION)-main/" temp2 > temp
	@sed -e "s/accepts_frameworks=no/accepts_frameworks=yes/" temp > temp2
	@sed -e "s/{lib_type} \$$allegro_libs/{lib_type}_s \$$allegro_libs/" temp2 > temp
	@sed -e "s/-l\$${lib_type}_unsharable/-l\$${lib_type}-main $(LIBRARIES)/" temp >$(INSTALLDIR)/bin/allegro-config
	@rm -f temp temp2
	@chmod a+x $(INSTALLDIR)/bin/allegro-config
	@echo allegro-config script created in $(INSTALLDIR)/bin

$(INSTALLDIR)/bin/fixbundle: tools/macosx/fixbundle
	@install $< $(INSTALLDIR)/bin/

BUILD_HEADERS = $(INCDIR)/osxalleg.h                  \
          $(INCDIR)/allegro/platform/aintosx.h  \
          $(INCDIR)/allegro/platform/aintunix.h \
          $(INCDIR)/allegro/platform/alosxcfg.h \
          $(INCDIR)/allegro/platform/alplatf.h  \
          $(INCDIR)/allegro/platform/astdint.h  \
          $(INCDIR)/allegro/platform/alosx.h

HEADERS = $(addprefix $(INSTALLDIR)/,$(BUILD_HEADERS))

ifdef STATICLINK
   INSTALL_FILES = $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION)_s.a 
else
   INSTALL_FILES = $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION)-$(shared_version).dylib
   INSTALL_FILES += $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION)-main.a
endif

INSTALL_FILES += $(HEADERS) $(INSTALLDIR)/bin/allegro-config $(INSTALLDIR)/bin/fixbundle

install: generic-install
	@echo The $(DESCRIPTION) $(PLATFORM) library has been installed.
	@if (printenv PATH |grep -q -v "$(INSTALLDIR)/bin"); then echo "Please check that $(INSTALLDIR)/bin is in your path (see docs/build/macosx.txt)"; fi
	@echo Run make install-man if you wish to install the man pages.
	@echo Run make install-applehelp if you wish to install the Apple Help Center documentation
	@echo Run make install-framework if you wish to install the library as a framework.
	@echo Run make install-template if you wish to install the application template.

UNINSTALL_FILES = $(INSTALLDIR)/$(LIBDIR)/liballeg_s.a                     \
                  $(INSTALLDIR)/$(LIBDIR)/liballd_s.a                      \
                  $(INSTALLDIR)/$(LIBDIR)/liballp_s.a                      \
                  $(INSTALLDIR)/$(LIBDIR)/liballeg-$(shared_version).dylib \
                  $(INSTALLDIR)/$(LIBDIR)/liballd-$(shared_version).dylib  \
                  $(INSTALLDIR)/$(LIBDIR)/liballp-$(shared_version).dylib  \
                  $(INSTALLDIR)/$(LIBDIR)/liballeg-main.a                  \
                  $(INSTALLDIR)/$(LIBDIR)/liballd-main.a                   \
                  $(INSTALLDIR)/$(LIBDIR)/liballp-main.a                   \
                  $(HEADERS)                                         \
                  $(INSTALLDIR)/bin/allegro-config                   \
                  $(INSTALLDIR)/bin/fixbundle

uninstall: generic-uninstall
	rm -f $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION)-$(shared_major_minor).dylib
	rm -f $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION)-4.dylib
	rm -f $(INSTALLDIR)/$(LIBDIR)/lib$(VERSION).dylib
	-rm -fr $(FRAMEWORK)
	-rm -fr $(TEMPLATE)
	-sed -n -e "s,^@[@\\].*@\([a-zA-Z0-9_]*\)[^a-zA-Z0-9_*].*,$(MAN_DIR)/man3/\1.3,p" docs/src/allegro._tx \
	  | xargs -n 1 rm -f
	-sed -n -e "s,^@@Example.*@\(ex.*\),$(MAN_DIR)/man3/\1.3,p" docs/src/allegro._tx \
	  | xargs -n 1 rm -f
	-sed -n -e "s,^@@\(struct\|typedef\).*@\([a-zA-Z0-9_]*\),$(MAN_DIR)/man3/\2.3,p" docs/src/allegro._tx \
	  | xargs -n 1 rm -f
	@test -d $(HELPBUNDLE) && rm -fR $(HELPBUNDLE) || true

	@echo All gone!	

install-framework: $(FRAMEWORK)

install-applehelp:
	install -d $(HELPBUNDLE)
	install docs/html/*.html $(HELPBUNDLE)
	sed -n -e '1,6p' docs/html/index.html > $(HELPBUNDLE)/index.html
	echo "<META NAME=\"AppleTitle\" CONTENT=\"Allegro Help\">" >> $(HELPBUNDLE)/index.html 
	echo "<META NAME=\"AppleIcon\" CONTENT=\"alex.png\">" >> $(HELPBUNDLE)/index.html 
	sed -n -e '7,$$p' docs/html/index.html >> $(HELPBUNDLE)/index.html
	install misc/alex.png $(HELPBUNDLE) 	

$(FRAMEWORK): $(LIB_NAME)
ifdef STATICLINK
	@echo You need to compile Allegro as a dynamic library to install the framework.
else
	@rm -fr $(FRAMEWORK)
	@install -d $(FRAMEWORK)
	@install -d $(FRAMEWORK)/Versions/$(shared_version)/Headers
	@install -d $(FRAMEWORK)/Versions/$(shared_version)/Resources
ifdef EMBED
	$(CC) -dynamiclib $(DYLINK_FLAGS) $(INSTALL_NAME_EMBED) $(PFLAGS) -o $(FRAMEWORK)/Versions/$(shared_version)/$(FRAMEWORK_NAME) $(OBJECTS) $(STATIC_LIBRARIES)
else
	@install $(LIB_NAME) $(FRAMEWORK)/Versions/$(shared_version)/$(FRAMEWORK_NAME)
endif
	@install include/allegro.h $(FRAMEWORK)/Versions/$(shared_version)/Headers
	@install include/osxalleg.h $(FRAMEWORK)/Versions/$(shared_version)/Headers
	@install $(BUILD_HEADERS) $(FRAMEWORK)/Versions/$(shared_version)/Headers
	@(cd $(FRAMEWORK)/Versions; ln -s $(shared_version) Current)
	@(cd $(FRAMEWORK); ln -s Versions/Current/Headers $(FRAMEWORK)/Headers)
	@(cd $(FRAMEWORK); ln -s Versions/Current/Resources $(FRAMEWORK)/Resources)
	@(cd $(FRAMEWORK); ln -s Versions/Current/$(FRAMEWORK_NAME) $(FRAMEWORK)/$(FRAMEWORK_NAME))
	@sed -e "s/@NAME@/$(FRAMEWORK_NAME)/" misc/Info.plist >temp
	@sed -e "s/@VERSION@/$(shared_version) ($(DESCRIPTION))/" temp >$(FRAMEWORK)/Resources/Info.plist
	@rm -f temp
ifdef EMBED
	@echo "This is the embeddable version of the Allegro framework" > $(FRAMEWORK)/Resources/Embeddable
endif
	@echo The $(FRAMEWORK_DESCRIPTION) Allegro framework has been successfully installed in:
	@echo $(FRAMEWORK)
endif

install-template: $(TEMPLATE)

$(TEMPLATE): $(FRAMEWORK)
	@rm -fr $(TEMPLATE)
	@install -d $(TEMPLATE)/AllegroApp.pbproj
	@install misc/template.c $(TEMPLATE)/main.c
	@install misc/TemplateInfo.plist $(TEMPLATE)/AllegroApp.pbproj
	@install misc/project.pbxproj $(TEMPLATE)/AllegroApp.pbproj
	@install misc/project.pbxuser $(TEMPLATE)/AllegroApp.pbproj
	@echo Allegro application template for Project Builder \(XCode\) installed in:
	@echo $(TEMPLATE)


docs/man/install_allegro.3: docs/src/allegro._tx $(MAKEDOC)
	$(MAKEDOC) -man docs/man/dummyname.3 $<

install-man: docs docs/man/install_allegro.3
	@install -d  $(MAN_DIR)/man3
	@echo Installing man files to $(MAN_DIR)/man3
	@install docs/man/*.3 $(MAN_DIR)/man3/
	
	

# -------- test capabilities --------

TEST_CPP = $(CC) -fno-exceptions -c src/misc/test.cpp -o $(PLATFORM_DIR)/test.o

include makefile.tst



# -------- finally, we get to the fun part... --------

ifdef STATICLINK

# -------- link as a static library --------
define MAKE_LIB
libtool -static -s -o $(LIB_NAME) $(OBJECTS)
endef

else

# -------- link as a shared library --------

define MAKE_LIB
$(CC) -dynamiclib $(DYLINK_FLAGS) $(INSTALL_NAME) $(PFLAGS) -o $(LIB_NAME) $(OBJECTS) $(STATIC_LIBRARIES)
endef

endif # STATICLINK

$(LIB_MAIN_NAME): obj/macosx/$(VERSION)/main.o
ifndef STATICLINK
	libtool -static -s -o  $(LIB_MAIN_NAME) obj/macosx/$(VERSION)/main.o
endif


COMPILE_FLAGS = $(subst src/,-DALLEGRO_SRC ,$(findstring src/, $<))$(CFLAGS)

$(OBJ_DIR)/%.o: %.c
	$(CC) $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.cpp
	$(CC) -fno-exceptions $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.m
	$(CC) $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

demo/demo: $(OBJECTS_DEMO) $(LIB_NAME) $(LIB_MAIN_NAME)
	$(CC) $(LFLAGS) -o $@ $(OBJECTS_DEMO) $(LIB_NAME) $(LIB_MAIN_NAME) $(LIBRARIES)

*/%: $(OBJ_DIR)/%.o $(LIB_NAME) $(LIB_MAIN_NAME)
	$(CC) $(LFLAGS) -o $@ $< $(LIB_NAME) $(LIB_MAIN_NAME) $(LIBRARIES)

define LINK_WITHOUT_LIB
   $(CC) $(LFLAGS) -o $@ $^
endef

PLUGIN_LIB = lib/macosx/lib$(VERY_SHORT_VERSION)dat.a
PLUGINS_H = obj/macosx/plugins.h
PLUGIN_DEPS = $(LIB_NAME) $(PLUGIN_LIB)
PLUGIN_SCR = scr

define GENERATE_PLUGINS_H
   cat tools/plugins/*.inc > obj/macosx/plugins.h
endef

define MAKE_PLUGIN_LIB
   libtool -static -s -o $(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

define LINK_WITH_PLUGINS
   $(CC) $(LFLAGS) -o $@ $< $(strip $(PLUGIN_LIB) $(addprefix `cat < ,$(addsuffix `,$(PLUGIN_SCRIPTS))) $(LIB_NAME) $(LIB_MAIN_NAME) $(LIBRARIES))
endef


tools/macosx/%: $(OBJ_DIR)/%.o $(LIB_NAME) $(LIB_MAIN_NAME)
ifdef STATICLINK
	$(CC) $(LFLAGS) -o $@ $< $(LIB_NAME) $(LIBRARIES)
else
	$(CC) $(LFLAGS) -o $@ $< $(LIB_NAME) $(LIB_MAIN_NAME) $(LIBRARIES) -framework Carbon
endif



# -------- demo program iconification --------

.PHONY: fixdemo

fixdemo: demo/demo demo/demo.dat tools/macosx/fixbundle
	tools/macosx/fixbundle demo/demo -d demo/demo.dat GAME_PAL SHIP3




# -------- generate automatic dependencies --------

DEPEND_PARAMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_MACOSX

depend:
	$(CC) $(DEPEND_PARAMS) src/*.c src/unix/*.c src/misc/*.c src/c/*.c demo/*.c > _depend.tmp
	$(CC) $(DEPEND_PARAMS) docs/src/makedoc/*.c examples/*.c setup/*.c tests/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) tools/*.c tools/plugins/*.c tools/macosx/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x c src/macosx/*.m tests/*.cpp >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/macosx\/alleg\/\1/" _depend2.tmp > obj/macosx/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/macosx\/alld\/\1/" _depend2.tmp > obj/macosx/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o *:\)/obj\/macosx\/allp\/\1/" _depend2.tmp > obj/macosx/allp/makefile.dep
	rm _depend.tmp _depend2.tmp
