#=============================================================================
# makefile-general: General-purpose rules for Unreal makefiles.
#
# Makefiles should define the following variables before including this file:
# 	Directories:
# 		SRC_DIRS	= Source code directories.
# 		BUILD_DIR	= Intermediate file directory.
# 	Compiler/linker options:
# 		CXX		= C++ compiler.
# 		CXXFLAGS	= C++ compiler options.
# 		LDFLAGS		= Linker options.
# 		LIBS		= Libraries.
# 	Files:
# 		OBJS		= Object files.
# 		OUT		= Output file. (Full path.)
#
# Revision history:
# 	* Created by Mike Danylchuk
#=============================================================================

# Disable implicit suffix rules.
.SUFFIXES:

# Search paths.
vpath
vpath %.cpp	$(SRC_DIRS)
vpath %.o	$(BUILD_DIR)
vpath %.d	$(BUILD_DIR)

# Add build path to object filenames.
BUILD_OBJS = $(OBJS:%.o=$(BUILD_DIR)/%.o)

# Dependencies are updated automatically, so only include them when necessary.
USE_DEPENDENCIES = unknown
ifeq ($(USE_DEPENDENCIES),unknown)
.DEFAULT : usedeps
.PHONY : usedeps
usedeps : dirs
	@$(MAKE) USE_DEPENDENCIES=yes --no-print-directory
.PHONY : nodeps
nodeps : dirs
	@$(MAKE) USE_DEPENDENCIES=no --no-print-directory
else

#-----------------------------------------------------------------------------
# Rules.
#-----------------------------------------------------------------------------

# Build a shared library.
ifeq ($(LINKSTYLE),shared)
$(OUT) : $(BUILD_OBJS)
	$(CXX) -o $@ $(LDFLAGS) $^ $(LIBS)
endif

# Build a static executable.
ifeq ($(LINKSTYLE),static)
 ifeq ($(EXECUTABLE),1)
  ifeq ($(TARGETTYPE),linux)
   $(OUT) : $(BUILD_OBJS)
	$(CXX) -o $@ $(LDFLAGS) $^ $(LIBS)
  endif
  ifeq ($(TARGETTYPE),psx2)
   $(OUT) : crt0.o $(BUILD_OBJS) $(UNREAL_STATIC_LIBS)
	$(CXX) -o $@ -T $(LCFILE) $(BUILD_DIR)/$(OBJS) $(LIBS) $(LDFLAGS)
  endif
 endif
endif

# Build a static library.
ifeq ($(LINKSTYLE),static)
 ifneq ($(EXECUTABLE),1)
  $(OUT) : $(BUILD_OBJS)
	$(STD_AR) rcs $(OUT) $(BUILD_OBJS)
 endif
endif

# Build a PSX2 core.
crt0.o : $(PSX2_LIB)/crt0.s
	$(AS) $(ASFLAGS) -o $@ $< > $(BUILD_DIR)/crt0.lst

# Compile a C++ file.
$(BUILD_DIR)/%.o : %.cpp
	$(CXX) -c $(CXXFLAGS) -o $@ $< > $(BUILD_DIR)/$*.lst

# Compile a DSM file.
$(BUILD_DIR)/%.o : %.dsm
	$(STD_DVPASM) $(DVPASMFLAGS) -o $@ $< > $(BUILD_DIR)/$*.lst

# Generate dependencies for a C++ file.
$(BUILD_DIR)/%.d : %.cpp
	@echo Generating dependencies for $(notdir $<)
	@echo $(@:%.d=%.o) $@ : \\ > $@
	@$(SHELL) -ec '$(CXX) -MM $(CXXFLAGS) $< | \
		sed -e '\''s/.*://'\'' >> $@'

# Include dependency files.
ifeq ($(USE_DEPENDENCIES),yes)
-include $(BUILD_OBJS:%.o=%.d)
endif

endif

#-----------------------------------------------------------------------------
# Maintenance.
#-----------------------------------------------------------------------------

.PHONY : dirs
dirs :
	@-mkdir -p $(BUILD_DIR)
	@-mkdir -p $(dir $(OUT))

.PHONY : clean
clean : clean-objs clean-deps clean-old clean-lst clean-tmp

.PHONY : clean-objs
clean-objs :
	-rm -f $(BUILD_DIR)/*.o

.PHONY : clean-deps
clean-deps :
	-rm -f $(BUILD_DIR)/*.d

.PHONY : clean-old
clean-old :
	-rm -f $(SRC_DIRS)/*.old
	-rm -f $(INC_DIRS)/*.old

.PHONY : clean-lst
clean-lst :
	-rm -f $(BUILD_DIR)/*.lst

.PHONY : clean-tmp
clean-tmp :
	-rm -f $(SRC_DIRS)/*~
	-rm -f $(INC_DIRS)/*~

.PHONY : clean-out
clean-out :
	-rm -f $(OUT)

#-----------------------------------------------------------------------------
# The End.
#-----------------------------------------------------------------------------
