# GCC specific toolchain setup
# We assume that we're using a standard GCC/binutils environment

CCDEF := -D
CCOPT := -O2
CCNOOPT := -O0
CCDBG := -g
CCINC := -I
CCLIB := -L
CCAS := -xassembler-with-cpp
CCSHR := -fPIC

CXXDEF := -D
CXXOPT := -O2
CXXNOOPT := -O0
CXXDBG := -g
CXXINC := -I
CXXLIB := -L
CXXSHR := -fPIC

LDDBG := -g
# Reevaluation is required here
ifeq ($(findstring darwin,$(HOST)),darwin)
	LDSHR = -dynamiclib -install_name $(SONAME)
else
	LDSHR = -shared -Wl,-soname,$(SONAME)
endif

ARFLG := cru

# Definitions for various attributes
CFLAGS := $(CFLAGS) -D_ALIGNED="__attribute__((aligned))" -DSTMTEXPR=1

# Extensions for coverage target
ifeq ($(MAKECMDGOALS),coverage)
  COVCFLAGS ?= -fprofile-arcs -ftest-coverage
  COVCXXFLAGS ?= -fprofile-arcs -ftest-coverage
  COVLDFLAGS ?= -lgcov

  CFLAGS := $(CFLAGS) $(COVCFLAGS)
  CXXFLAGS := $(CXXFLAGS) $(COVCXXFLAGS)
  LDFLAGS := $(LDFLAGS) $(COVLDFLAGS)
endif

# Extensions for profile target
ifeq ($(MAKECMDGOALS),profile)
  PROFCFLAGS ?= -pg
  PROFCXXFLAGS ?= -pg
  PROFLDFLAGS ?= -pg

  CFLAGS := $(CFLAGS) $(PROFCFLAGS)
  CXXFLAGS := $(CXXFLAGS) $(PROFCXXFLAGS)
  LDFLAGS := $(LDFLAGS) $(PROFLDFLAGS)
endif

# Extensions for sanitize target
ifeq ($(MAKECMDGOALS),sanitize)
  SANCFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover
  SANCXXFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover
  SANLDFLAGS ?= -fsanitize=address -fsanitize=undefined -fno-sanitize-recover

  CFLAGS := $(CFLAGS) $(SANCFLAGS)
  CXXFLAGS := $(CXXFLAGS) $(SANCXXFLAGS)
  LDFLAGS := $(LDFLAGS) $(SANLDFLAGS)
endif

# RISC OS module extensions
ifeq ($(COMPONENT_TYPE),riscos-module)
  ifneq ($(HOST),arm-unknown-riscos)
    # Note: this also rejects the gnueabi(hf) RISC OS hosts, too, as the
    # tooling for those is not able to generate code compatible with RISC OS
    # module environments.
    $(error Attempting to build a RISC OS module for a non-RISC OS host)
  endif

  CFLAGS := $(CFLAGS) -mmodule
  CXXFLAGS := $(CXXFLAGS) -mmodule
  LDFLAGS := $(LDFLAGS) -mmodule
endif

# RISC OS backtrace extensions
ifneq ($(findstring -riscos,$(HOST)),)
  CFLAGS := $(CFLAGS) -mpoke-function-name
  CXXFLAGS := $(CXXFLAGS) -mpoke-function-name
  ifneq ($(findstring -gnueabi,$(HOST)),)
    # Note: this assumes we're not generating RISC OS module code (see above)
    CFLAGS := $(CFLAGS) -funwind-tables
    CXXFLAGS := $(CXXFLAGS) -funwind-tables
  endif
endif

###############################################################################
# Mac OS X Universal Binaries
###############################################################################

ifeq ($(findstring darwin,$(HOST)),darwin)
	ifneq ($(UNIVERSAL),)
		UNIVERSAL_FLAGS := $(foreach arch,$(UNIVERSAL),-arch $(arch) )
		CC_CAN_BUILD_AND_DEP := no
		CC_CANNOT_DEP := yes
		
		CFLAGS := $(CFLAGS) $(UNIVERSAL_FLAGS)
		LDFLAGS := $(LDFLAGS) $(UNIVERSAL_FLAGS)
		CXXFLAGS := $(CXXFLAGS) $(UNIVERSAL_FLAGS)
		ARFLAGS := $(ARFLAGS) -s 
	endif
endif

###############################################################################
# Other settings
###############################################################################

# Determine if the compiler supports simultaneous build & dep.
ccvsn := $(shell $(CC) -dumpversion -dumpfullversion)
# ccvsn = x.y.z
GCCVER := $(word 1,$(subst ., ,$(ccvsn)))
# GCCVER = x

# If the major version (x, above) is not 2, then assume build & dep.
# This will break if using a version of GCC < 2, but that's unlikely.
ifneq ($(GCCVER),2)
  CC_CAN_BUILD_AND_DEP ?= yes
endif

