#
# $Id: //poco/Main/build/rules/global#18 $
#
# global
#
# Global build configuration
#
# Environment variables:
# POCO_BASE:   Path to POCO source tree. Must be defined.
# POCO_BUILD:  Path to directory where build files are put.
#              Defaults to $(POCO_BASE)
# POCO_CONFIG: Build configuration to use.
#              Defaults to `uname`.
# POCO_TARGET_OSNAME: Target system operating system name (for cross builds)
# POCO_TARGET_OSARCH: Target system architecture (forr cross builds)
#

#
# Check for POCO_BASE
#
ifndef POCO_BASE
$(error POCO_BASE is not defined.)
endif

#
# Include some optional make configuration
#
sinclude $(POCO_BASE)/config.build

#
# Check for POCO_BUILD
#
ifndef POCO_BUILD
POCO_BUILD = $(POCO_BASE)
endif

#
# Determine OS
#
POCO_HOST_OSNAME = $(shell uname)
ifeq ($(findstring CYGWIN,$(POCO_HOST_OSNAME)),CYGWIN)
POCO_HOST_OSNAME = CYGWIN
endif

#
# If POCO_CONFIG is not set, use the OS name as configuration name
#
ifndef POCO_CONFIG
POCO_CONFIG = $(POCO_HOST_OSNAME)
endif

#
# Check if a 64bit build is requested
#
ifndef OSARCH_64BITS
OSARCH_64BITS = 0
endif

ifeq ($(OSARCH_64BITS),1)
OSARCH_POSTFIX = 64
else
OSARCH_POSTFIX =
endif

#
# Include System Specific Settings
#
include $(POCO_BASE)/build/config/$(POCO_CONFIG)

#
# Determine operating system
#
ifndef POCO_TARGET_OSNAME
OSNAME   := $(POCO_HOST_OSNAME)
else
OSNAME   := $(POCO_TARGET_OSNAME)
endif
ifndef POCO_TARGET_OSARCH
OSARCH   := $(subst /,-,$(shell uname -m | tr ' ' _))
else
OSARCH   := $(POCO_TARGET_OSARCH)
endif
HOSTNAME := $(shell hostname)

#
# Find out current component
#
COMPONENT := $(shell $(POCO_BASE)/build/script/projname)

#
# Define standard directories
#
SRCDIR   = src
INCDIR   = include
LIBDIR   = lib/$(OSNAME)/$(OSARCH)
BINDIR   = bin/$(OSNAME)/$(OSARCH)
OBJDIR   = obj/$(OSNAME)/$(OSARCH)
DEPDIR   = .dep/$(OSNAME)/$(OSARCH)
LIBPATH  = $(POCO_BUILD)/$(LIBDIR)
BINPATH  = $(POCO_BUILD)/$(COMPONENT)/$(BINDIR)
OBJPATH  = $(POCO_BUILD)/$(COMPONENT)/$(OBJDIR)
DEPPATH  = $(POCO_BUILD)/$(COMPONENT)/$(DEPDIR)

ifndef LIBPREFIX
LIBPREFIX = lib
endif

#
# Build component list
#
COMPONENTS := $(shell cat $(POCO_BASE)/components)

#
# Read global library version number
#
LIBVERSION := $(shell cat $(POCO_BASE)/libversion)

#
# Determine link mode
#
ifndef LINKMODE
LINKMODE = BOTH
endif

ifeq ($(LINKMODE),SHARED)
DEFAULT_TARGET = all_shared
endif
ifeq ($(LINKMODE),STATIC)
DEFAULT_TARGET = all_static
endif
ifeq ($(LINKMODE),BOTH)
DEFAULT_TARGET = all_static all_shared
endif

#
# Compose compiler flags
#
COMMONFLAGS = -DPOCO_BUILD_HOST=$(HOSTNAME) $(POCO_FLAGS)
CFLAGS     += $(COMMONFLAGS) $(SYSFLAGS)
CXXFLAGS   += $(COMMONFLAGS) $(SYSFLAGS)
LINKFLAGS  += $(COMMONFLAGS) $(SYSFLAGS)

ifeq ($(OSARCH_64BITS),1)
CFLAGS    += $(CFLAGS64)
CXXFLAGS  += $(CXXFLAGS64)
LINKFLAGS += $(LINKFLAGS64)
else
CFLAGS    += $(CFLAGS32)
CXXFLAGS  += $(CXXFLAGS32)
LINKFLAGS += $(LINKFLAGS32)
endif

#
# Compose object file path
#
OBJPATH_RELEASE_STATIC = $(OBJPATH)/release_static$(OSARCH_POSTFIX)
OBJPATH_DEBUG_STATIC   = $(OBJPATH)/debug_static$(OSARCH_POSTFIX)
OBJPATH_RELEASE_SHARED = $(OBJPATH)/release_shared$(OSARCH_POSTFIX)
OBJPATH_DEBUG_SHARED   = $(OBJPATH)/debug_shared$(OSARCH_POSTFIX)

#
# Build Include directory List
#
INCLUDE = -Iinclude $(foreach p,$(COMPONENTS),-I$(POCO_BASE)/$(p)/$(INCDIR))

#
# Build Library Directory List
#
LIBRARY = -L$(LIBPATH)

#
# Strip Command definition
#
ifeq ($(strip $(STRIP)),)
STRIPCMD = 
else
STRIPCMD = $(STRIP) $@
endif

#
# Remote debugging support
#
ifeq ($(strip $(STRIPDBG)),)
CPYDBG =
STRIPDBGCMD = 
else
CPYDBG = cp $@ $@x
STRIPDBGCMD = $(STRIP) $@x
endif

#
# Make CC and CXX environment vars
#
export CC
export CXX
