MODEST_BUILD_OS := UNDEF

#********************
# Version
#***************
PROJECT_VERSION_MAJOR := 0
PROJECT_VERSION_MINOR := 0
PROJECT_VERSION_PATCH := 6

PROJECT_VERSION_STRING := $(PROJECT_VERSION_MAJOR).$(PROJECT_VERSION_MINOR).$(PROJECT_VERSION_PATCH)

#********************
# Flags
#***************
MODEST_CFLAGS ?= -Wall -Werror -pipe -pedantic
MODEST_LFLAGS ?=

#********************
# Include
#***************
# include dirs
INCLUDE_DIR := $(TARGET)
INCLUDE_DIR_API := include

#********************
# Libraries
#***************
# lib name
LIB_NAME := modest
LIB_NAME_SUFFIX := .so
LIB_NAME_SUFFIX_STATIC := _static.a

# lib dirs
LIB_DIR_BASE := lib

# This variables used with "call" $(call MODEST_LIBRARY_WITH_VERSION), 
# for use actual variables like a LIB_NAME_SUFFIX
MODEST_LIBRARY_NAME ?= lib$(LIB_NAME)$(LIB_NAME_SUFFIX)
MODEST_LIBRARY_NAME_STATIC ?=lib$(LIB_NAME)$(LIB_NAME_SUFFIX_STATIC)
MODEST_LIBRARY_NAME_WITH_VERSION = lib$(LIB_NAME)-$(PROJECT_VERSION_STRING)$(LIB_NAME_SUFFIX)

MODEST_LIBRARY ?= $(LIB_DIR_BASE)/$(MODEST_LIBRARY_NAME)
MODEST_LIBRARY_STATIC ?= $(LIB_DIR_BASE)/$(MODEST_LIBRARY_NAME_STATIC)
MODEST_LIBRARY_WITH_VERSION = $(LIB_DIR_BASE)/$(MODEST_LIBRARY_NAME_WITH_VERSION)

MODEST_LIBRARY_WITH_VERSION_MAJOR = $(LIB_DIR_BASE)/lib$(LIB_NAME)-$(PROJECT_VERSION_MAJOR)$(LIB_NAME_SUFFIX)
MODEST_LIBRARY_WITH_VERSION_MAJOR_MINOR = $(LIB_DIR_BASE)/lib$(LIB_NAME)-$(PROJECT_VERSION_MAJOR).$(PROJECT_VERSION_MINOR)$(LIB_NAME_SUFFIX)

#********************
# Binaries
#***************
# binaries dirs
BINARY_DIR_BASE := bin

#********************
# Test
#***************
TEST_DIR := test
TEST_DIR_BASE := test_suite

#********************
# Build
#***************
MODEST_SOURCE ?= $(SRCDIR)
MODEST_BUILD_SHARED_AFTER :=
MODEST_BUILD_STATIC_AFTER :=

MODEST_BUILD_CLEAN_AFTER :=

#********************
# DEFAULT
#***************
SED ?= sed

MODEST_DIR_SEPARATOR ?= /

# flags
PROJECT_OPTIMIZATION_LEVEL ?= -O2
MODEST_CFLAGS  += -I$(INCLUDE_DIR)
MODEST_LDFLAGS +=

OS ?= $(shell uname -s)
prefix ?= /usr/local
exec_prefix ?= $(prefix)

MODEST_CLONE_SED_HEADER_COMMAND = find $(INCLUDE_DIR_API) -name "*.h" -exec sed -i.bak -e 's,\s*\#\s*include\s*"\([^"]*\)",\#include <\1>,g' {} \;

#********************
# Set -D
#***************
ifeq ($(MyCORE_BUILD_DEBUG),YES)
	override PROJECT_OPTIMIZATION_LEVEL :=
	MODEST_CFLAGS += -g3 -ggdb3 -O0 -fno-omit-frame-pointer -DMyCORE_BUILD_DEBUG
endif

ifeq ($(MyCORE_BUILD_WITHOUT_THREADS),YES)
	MODEST_CFLAGS += -DMyCORE_BUILD_WITHOUT_THREADS
endif

ifeq ($(MyCORE_WITH_PERF),YES)
	MODEST_CFLAGS += -DMyCORE_WITH_PERF
endif

#********************
# Utils
#***************
define MODEST_UTILS_NEW_LINE


endef
MODEST_UTILS_TO_UPCASE = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))

#*******************************
# Load platform specific rules for build
# We try to include all Rules.mk file in /source/myport/*/Rules.mk
#*******************
ifndef MODEST_PORT_NAME
	MYPORT_BUILD_RULES := $(foreach path,$(wildcard $(MODEST_SOURCE)/myport/*/),$(dir $(path))Rules.mk)
	-include $(MYPORT_BUILD_RULES)
else
	MYPORT_BUILD_RULES := $(MODEST_SOURCE)/myport/$(MODEST_PORT_NAME)/Rules.mk
	-include $(MYPORT_BUILD_RULES)
endif # def MODEST_PORT_NAME

#*******************************
# POSIX
#*******************
ifeq ($(MODEST_BUILD_OS),UNDEF)
	MODEST_CFLAGS += -fPIC
	MODEST_CFLAGS += -D_POSIX_C_SOURCE=199309L
	MODEST_CFLAGS += $(PROJECT_OPTIMIZATION_LEVEL) -Wno-unused-variable -Wno-unused-function -std=c99
	
	MODEST_BUILD_SHARED_AFTER += ln -sf $(call MODEST_LIBRARY_NAME_WITH_VERSION) $(call MODEST_LIBRARY) $(MODEST_UTILS_NEW_LINE)
	MODEST_BUILD_SHARED_AFTER += ln -sf $(call MODEST_LIBRARY_NAME_WITH_VERSION) $(call MODEST_LIBRARY_WITH_VERSION_MAJOR) $(MODEST_UTILS_NEW_LINE)
	MODEST_BUILD_SHARED_AFTER += ln -sf $(call MODEST_LIBRARY_NAME_WITH_VERSION) $(call MODEST_LIBRARY_WITH_VERSION_MAJOR_MINOR) $(MODEST_UTILS_NEW_LINE)
	
	MODEST_BUILD_CLEAN_AFTER += rm -f $(call MODEST_LIBRARY) $(MODEST_UTILS_NEW_LINE)
	MODEST_BUILD_CLEAN_AFTER += rm -f $(call MODEST_LIBRARY_WITH_VERSION_MAJOR) $(MODEST_UTILS_NEW_LINE)
	MODEST_BUILD_CLEAN_AFTER += rm -f $(call MODEST_LIBRARY_WITH_VERSION_MAJOR_MINOR) $(MODEST_UTILS_NEW_LINE)

# build without threads
ifneq ($(MyCORE_BUILD_WITHOUT_THREADS),YES)
	MODEST_LDFLAGS += -pthread
endif

	# Need set
	MODEST_BUILD_OS := $(OS)
	# this name eq source/myport/<namedir>
	MODEST_PORT_NAME := posix
endif 
# end of POSIX
