##############################################################################
#
#    The MIT License (MIT)
#
#    Copyright (c) 2014 Vivante Corporation
#
#    Permission is hereby granted, free of charge, to any person obtaining a
#    copy of this software and associated documentation files (the "Software"),
#    to deal in the Software without restriction, including without limitation
#    the rights to use, copy, modify, merge, publish, distribute, sublicense,
#    and/or sell copies of the Software, and to permit persons to whom the
#    Software is furnished to do so, subject to the following conditions:
#
#    The above copyright notice and this permission notice shall be included in
#    all copies or substantial portions of the Software.
#
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#    DEALINGS IN THE SOFTWARE.
#
##############################################################################


################################################################################
# Define a shortcut for the main target.

TARGET_OUTPUT = $(OBJ_DIR)/$(TARGET_NAME)

ifeq ($(OBJ_DIR),)
ifeq ($(DEBUG), 1)
OBJ_DIR	?= bin_d
else
OBJ_DIR	?= bin_r
endif
endif

################################################################################
# Specify targets.

all: $(TARGET_OUTPUT)

clean:
	@rm -f $(PREREQUISITES)
	@rm -rf $(OBJ_DIR)/* $(OBJ_DIR)
	@rm -rf $(CLEAN_EXTRA)

install: $(TARGET_OUTPUT)
ifneq ($(INSTALL_DIR),)
	@mkdir -p $(INSTALL_DIR)
	@-cp $(TARGET_OUTPUT) $(INSTALL_DIR)
endif

ifeq ($(COMMON_LITE), 1)
ifeq ($(gcdES11_CORE_WITH_EGL), 1)
  PREREQUISITES	:= .Prerequisites_cl
else
  PREREQUISITES	:= .Prerequisites_v1_cl
endif
else
ifeq ($(gcdES11_CORE_WITH_EGL), 1)
  PREREQUISITES	:= .Prerequisites_cm
else
  PREREQUISITES	:= .Prerequisites
endif
endif

ifeq ($(DEBUG), 1)
  PREREQUISITES	:= $(PREREQUISITES)_d
else
  PREREQUISITES	:= $(PREREQUISITES)_r
endif

MAKEFILE_NAME	= makefile.linux

ifeq ($(gcdSTATIC_LINK),1)
ifneq ($(USE_ARMCC), 1)
  PFLAGS += -static
else
  PFLAGS += -L--no_search_dynamic_libraries
endif
else
ifneq ($(QNX), 1)
LIBS += -lrt
endif
#PFLAGS += -Wl,-rpath $(VIVANTE_SDK_DIR)/drivers
endif

ifeq ($(PROGRAM), 1)
$(TARGET_OUTPUT): $(PREREQUISITES) $(OBJECTS)
ifeq ($(SRC_CXX),)
	$(CC) $(PFLAGS) $(OBJECTS) -o $(TARGET_OUTPUT) $(LIBS)
else
	$(CXX) $(PFLAGS) $(OBJECTS) -o $(TARGET_OUTPUT) $(LIBS)
endif

ifneq ($(USE_ARMCC), 1)
ifneq ($(DEBUG), 1)
	$(STRIP) $(TARGET_OUTPUT)
endif
endif
endif

ifeq ($(DYNAMIC), 1)
ifeq ($(USE_ARMCC), 1)
LFLAGS += --shared -L--soname=,$(TARGET_NAME)
else
LFLAGS += -Wall -shared -Wl,-soname,$(TARGET_NAME) -Wl,-z,defs
endif
$(TARGET_OUTPUT): $(PREREQUISITES) $(OBJECTS)
	$(CC) $(LFLAGS) $(OBJECTS) -o $(TARGET_OUTPUT) $(LIBS)
endif

ifeq ($(STATIC), 1)
$(TARGET_OUTPUT): $(PREREQUISITES) $(OBJECTS)
	$(AR) -r -c $(TARGET_OUTPUT) $(OBJECTS)
ifneq ($(USE_ARMCC), 1)
	$(RANLIB) $(TARGET_OUTPUT)
endif
endif

$(PREREQUISITES): $(MAKEFILE_NAME)
	@-mkdir -p $(OBJ_DIR)
	@-touch $(PREREQUISITES)

$(OBJ_DIR)/%.o: %.c $(PREREQUISITES)
	@mkdir -p $(OBJ_DIR)
	$(CC) -c $(CFLAGS) -o $@ $<

$(OBJ_DIR)/%.o: %.cpp $(PREREQUISITES)
	@mkdir -p $(OBJ_DIR)
	$(CXX) -c $(CFLAGS) -o $@ $<
