##
## Copyright 2014 Intel Corporation.
##
##
## This software and the related documents are Intel copyrighted materials, and your use of them is governed by
## the express license under which they were provided to you ('License'). Unless the License provides otherwise,
## you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
## documents without Intel's prior written permission.
## This software and the related documents are provided as is, with no express or implied warranties, other than
## those that are expressly stated in the License.
##

ifeq ($(G_SYSTEM),)
	exit 0
endif

CFLAGS    ?= $(G_CFLAGS)
CXXFLAGS  ?= $(G_CXXFLAGS)

#
# Set binary architecture
#
ifeq ($(G_ARCH),intel64)
	CFLAGS   += -m64
	CXXFLAGS += -m64
endif

#
# Set build name and paths
#
ifeq ($(PROJ_NAME),)
	PROJ_NAME := common
endif
ifeq ($(OUT_NAME),)
	OUT_NAME := lib$(PROJ_NAME).a
endif


BUILD_PATH      := $(G_BUILD_ROOT)/$(G_ARCH)/$(G_CONF)
BUILD_PATH_TBB  := $(G_BUILD_ROOT)/$(G_ARCH)/$(G_CONF_TBB)
OBJ_PATH        := $(BUILD_PATH_TBB)/obj/$(PROJ_NAME)
OUT_FULL        := $(BUILD_PATH_TBB)/$(OUT_NAME)
INCLUDES        := -I$(IPPROOT)/include -I./include

ifeq ($(G_USERENDERER),1)
	CXXFLAGS += -DENABLE_RENDERING
endif

ifeq ($(G_OMP_SUPPORTED),1)
	CFLAGS   += $(G_C_OPENMP)
	CXXFLAGS += $(G_CXX_OPENMP)
endif

ifeq ($(G_USETBB),1)
	INCLUDES += -I$(TBBROOT)/include
	CXXFLAGS += -DUSE_TBB
endif

IFSUSE := $(shell cat /etc/*release | grep -c SUSE)
ifdef IFSUSE
	CFLAGS += -D__USE_UNIX98 -D_GNU_SOURCE
endif

CXXOBJ	:= $(patsubst src/%.cpp,$(OBJ_PATH)/%.o,$(wildcard src/*.cpp))
COBJ	:= $(patsubst src/%.c,$(OBJ_PATH)/%.o,$(wildcard src/*.c))

#
# Set variable targets
#
BUILD_TARGET := build

CLEAN_TARGET :=

all: out_path $(BUILD_TARGET)

build: obj_path out

out_path:
	mkdir -p $(BUILD_PATH_TBB)

obj_path:
	mkdir -p $(OBJ_PATH)

$(OBJ_PATH)/%.o : src/%.c
	$(CC) -c $(G_LIB_CFLAGS) $(CFLAGS) $(INCLUDES) -o $@ $<

$(OBJ_PATH)/%.o : src/%.cpp
	$(CXX) -c $(G_LIB_CFLAGS) $(CXXFLAGS) $(INCLUDES) -o $@ $<

out: $(COBJ) $(CXXOBJ)
	$(AR) $(ARFLAGS) $(OUT_FULL) $?

clean: $(CLEAN_TARGET)
	$(RM) $(OUT_FULL)
	$(RM) $(COBJ)
	$(RM) $(CXXOBJ)
