# FIXME: Should this go in common.make.postamble?
-include $(LOCAL_MAKEFILEDIR)/$(PLATFORM_OS)-specific.make

include $(LOCAL_MAKEFILEDIR)/verify.make
-include $(LOCAL_MAKEFILEDIR)/common.make.postamble-$(PLATFORM_OS)
-include $(LOCAL_MAKEFILEDIR)/implicitrules-overrides.make

$(OMNIMAKE_FRAMEWORK_DIR) $(OMNIMAKE_INSTALL_DIR):
	$(SILENT) $(MKDIRS) $@

before_install:: $(OMNIMAKE_INSTALL_DIR)

# Define our subsystem and add the path to our installed custom frameworks
OTHER_CFLAGS += -F$(OMNIMAKE_FRAMEWORK_DIR)
OTHER_LIBTOOL_FLAGS += -F$(OMNIMAKE_FRAMEWORK_DIR)


include $(LOCAL_MAKEFILEDIR)/personal_build.make


# "Final" installs - set the dylib install dir

.PHONY : final_install final_install final_debug_install final_profile_install

final_install:
ifdef OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=$(OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE) \
		install
else
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=/LocalLibrary/Frameworks \
		install
endif

final_debug_install:
ifdef OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=$(OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE) \
		debug_install
else
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=/LocalLibrary/Frameworks \
		debug_install
endif

final_profile_install:
ifdef OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=$(OMNIMAKE_FINAL_DESTINATION_DIR_OVERRIDE) \
		profile_install
else
	$(SILENT) $(MAKE) OMNIMAKE_FINAL_DESTINATION_DIR=/LocalLibrary/Frameworks \
		profile_install
endif


# We override the NeXT value of -O
OPTIMIZE_BUILD_CFLAGS = -O2

# We like a lot of warnings
WARNING_CFLAGS = -Wall

# Currently the Rhapsody compiler doesn't support inline functions correctly and
# will end up procuding a ton of 'unused function' warnings.
ifeq ($(OMNIMAKE_MACHINE), Rhapsody)
WARNING_CFLAGS += -Wno-unused
endif

#
#  Localization tags
#

$(OMNIMAKE_LOCALIZATION_HFILES): $(OMNIMAKE_LOCALIZATION_FILES)
	@for file in $(OMNIMAKE_LOCALIZATION_FILES:.localization=); do \
	    cmd="$(OMNIMAKE_BUILD_LOCALIZATION) -hfile $(OMNIMAKE_DERIVED_SOURCE_DIR)/$$file.h \
				      -mfile $(OMNIMAKE_DERIVED_SOURCE_DIR)/$$file.m \
				      -localization $$file.localization \
				      -include_dir $(OMNIMAKE_UTILITY_DIR) $(BURY_STDOUT)"; \
	    $(ECHO) $$cmd ; $$cmd; \
	done

# If we don't define an explicit rule for these files, the default rule attempts to
# put them in $(OFILE_DIR)/$(OFILE_DIR)/foo.o

$(OMNIMAKE_LOCALIZATION_OFILES): $(OMNIMAKE_LOCALIZATION_MFILES)
	@for file in $(OMNIMAKE_LOCALIZATION_FILES:.localization=); do \
	    cmd="$(CC) $(ALL_CFLAGS) -c $(OMNIMAKE_DERIVED_SOURCE_DIR)/$$file.m -o $(OFILE_DIR)/$$file.o"; \
	    $(ECHO) $$cmd ; $$cmd; \
	done

#
#  Framework references stuff
#

.PHONY : omnimake_framework_symbols

omnimake_framework_symbols: $(OMNIMAKE_FRAMEWORK_SYMBOL_O)

$(OMNIMAKE_FRAMEWORK_SYMBOL_O): $(OMNIMAKE_FRAMEWORK_SYMBOL_M) $(OFILE_DIR)
	$(CC) $(ALL_CFLAGS) -c $(OMNIMAKE_FRAMEWORK_SYMBOL_M) -o $@

OMNIMAKE_GENERATE_FRAMEWORK_SYMBOLS_SCRIPT = $(OMNIMAKE_UTILITY_DIR)/generateFrameworkSymbols.perl
OMNIMAKE_GENERATE_FRAMEWORK_SYMBOLS        = $(PERL) $(OMNIMAKE_GENERATE_FRAMEWORK_SYMBOLS_SCRIPT)
OMNIMAKE_REFERENCED_FRAMEWORKS = $(filter-out -framework, $(FRAMEWORKS))

$(OMNIMAKE_FRAMEWORK_SYMBOL_M): PB.project $(OMNIMAKE_GENERATE_FRAMEWORK_SYMBOLS_SCRIPT)
	$(OMNIMAKE_GENERATE_FRAMEWORK_SYMBOLS) $(NAME) $(OMNIMAKE_PROJECT_TYPE) $(OMNIMAKE_REFERENCED_FRAMEWORKS) > $@


#  Support for building a main subsystem header file.  It will only import the public headers
# (those in HFILES) and will be named $(NAME).h
#
subsystem_header:
	/bin/ls $(HFILES) | awk '{print "#import \"" $$1 "\""}' | grep -v $(NAME).h > $(NAME).h


#
#  Support for finding subproject plist files, and merging them using
# out improved dictionary merging tool.  The output is then given to
# the standard merging tool.
#
OMNIMAKE_MERGE_DICTIONARIES=$(OMNIMAKE_TOOL_DIR)/$(OMNIMAKE_MERGE_DICTIONARIES_NAME)$(EXECUTABLE_EXT)
OMNIMAKE_FIND_INFO=$(PERL) $(OMNIMAKE_UTILITY_DIR)/findInfo.perl
OMNIMAKE_STATIC_INFO_FILES := $(shell $(OMNIMAKE_FIND_INFO))

# This hackery is necessary since gnumake seems to have some bug (or the makefiles) so that
# if we just put OMNIMAKE_DERIVED_INFO_FILES as a dependancy of OMNIMAKE_MERGED_INFO, it doesn't even
# get attempted.  Wacky.
$(OMNIMAKE_MERGED_INFO): $(OMNIMAKE_DERIVED_SOURCE_DIR)
	@($(MAKE) OMNIMAKE_ROOT=$(OMNIMAKE_ROOT) $(OMNIMAKE_DERIVED_INFO_FILES) _omnimake_real_merged_info)

_omnimake_real_merged_info: $(OMNIMAKE_STATIC_INFO_FILES)
	@(if [ -x $(OMNIMAKE_MERGE_DICTIONARIES) ]; then \
	    cmd="$(OMNIMAKE_MERGE_DICTIONARIES) $(OMNIMAKE_STATIC_INFO_FILES) $(OMNIMAKE_DERIVED_INFO_FILES) -o $(OMNIMAKE_MERGED_INFO)"; \
		$(ECHO) $$cmd ; $$cmd ; \
	else \
	    $(ECHO) $(OMNIMAKE_MERGE_DICTIONARIES) not found -- ignoring; \
	    $(ECHO) "{}" > $(OMNIMAKE_MERGED_INFO); \
	fi)

#
# Support for updating files from CVS
#

# The 'oupdate' alias doesn't work in Makefiles since it is a zsh alias
update:
	@/usr/local/bin/AutoUpdate $(OMNI_CVS_FLAGS)

#
# Target for getting the project type
#
showProjectType:
	@echo OMNIMAKE_PROJECT_TYPE = $(OMNIMAKE_PROJECT_TYPE)
	@echo PROJECT_TYPE = $(PROJECT_TYPE)

#
# support for determining the make environment
#
OMNIMAKE_SHOW_ENVIRONMENT_NAME=showEnvironment
OMNIMAKE_SHOW_ENVIRONMENT=$(OMNIMAKE_TOOL_DIR)/$(OMNIMAKE_SHOW_ENVIRONMENT_NAME)$(EXECUTABLE_EXT)

update_environment:
	@($(OMNIMAKE_SHOW_ENVIRONMENT)) > PB.makeVariables

-include Makefile.postamble-$(PLATFORM_OS)

#
# Support for mixed-mode targets.  The NeXT Makefiles explicitly turn off optimization
# and profiling for debug builds rather than leaving them alone.  This prevents you
# from setting the variable on the command line to get a mixed mode build.
#
debug-optimized:
	$(SILENT) $(MAKE) DEBUG=YES OPTIMIZE=YES \
		BUILD_TYPE=debug \
		BUILD_TYPE_SUFFIX=_debug \
		APP_WRAPPER_EXTENSION=.debug \
		$(RESET_CUMULATIVE_VARIABLES)

# This is like the above, except that it pretends to be the optimized target.  This
# makes it easier to mix this with code that has been compiled with the 'default'
# target (ie, your framework or whatever won't have a _debug appended).
optimized-debug:
	$(SILENT) $(MAKE) DEBUG=YES OPTIMIZE=YES

#
# Convience target for: omnimake clean; omnimake
#
remake:
	$(SILENT) $(MAKE) clean
	$(SILENT) $(MAKE)

#
# An easy way to force relinking.  Useful when redefining the install_name value
# used when linking frameworks.
#
relink:
	$(SILENT) $(ECHO) Deleting final product and relinking...
	$(SILENT) $(RM) -rf $(BUILD_OUTPUT_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION)
	$(SILENT) $(MAKE)

relink_final_install:
	$(SILENT) $(ECHO) Deleting final product, relinking, and reinstalling...
	$(SILENT) $(RM) -rf $(BUILD_OUTPUT_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION)
	$(SILENT) $(MAKE) final_install
