NATIVEINCLUDES += -DNATIVE_INCLUDES
NATIVEINCLUDES += -I$(RIOTBOARD)/native/include/
NATIVEINCLUDES += -I$(RIOTBASE)/core/include/
NATIVEINCLUDES += -I$(RIOTBASE)/drivers/include/

ifeq ($(OS),Darwin)
  DEBUGGER ?= lldb
else
  DEBUGGER ?= gdb
endif

RESET ?= $(RIOTBOARD)/native/dist/reset.sh
FLASHER = true
FLASHFILE ?= $(ELFFILE)

TERMPROG ?= $(FLASHFILE)

export VALGRIND ?= valgrind
export CGANNOTATE ?= cg_annotate
export GPROF ?= gprof

# basic cflags:
CFLAGS += -Wall -Wextra -pedantic $(CFLAGS_DBG)
CFLAGS_DBG ?= -g3

# default std set to gnu99 of not overwritten by user
ifeq (,$(filter -std=%, $(CFLAGS)))
  CFLAGS += -std=gnu11
endif

ifeq ($(OS_ARCH),x86_64)
  CFLAGS += -m32
endif
ifneq (,$(filter -DDEVELHELP,$(CFLAGS)))
  CFLAGS += -fstack-protector-all
endif
ifeq ($(OS),FreeBSD)
  ifeq ($(OS_ARCH),amd64)
    CFLAGS += -m32 -DCOMPAT_32BIT -B/usr/lib32
  endif
endif
ifeq ($(OS),Darwin)
  CFLAGS += -Wno-deprecated-declarations
endif

# unwanted (CXXUWFLAGS) and extra (CXXEXFLAGS) flags for c++
CXXUWFLAGS +=
CXXEXFLAGS +=

ifeq ($(OS_ARCH),x86_64)
  LINKFLAGS += -m32
endif
ifeq ($(OS),FreeBSD)
  ifeq ($(OS_ARCH),amd64)
    LINKFLAGS += -m32 -DCOMPAT_32BIT -L/usr/lib32 -B/usr/lib32
  endif
  LINKFLAGS += -L $(BINDIR)
else
  LINKFLAGS += -ldl
endif

# clean up unused functions
CFLAGS += -ffunction-sections -fdata-sections
ifeq ($(OS),Darwin)
  LINKFLAGS += -Wl,-dead_strip
else
  LINKFLAGS += -Wl,--gc-sections
endif
LINKFLAGS += -ffunction-sections

# set the tap interface for term/valgrind
ifneq (,$(filter netdev_default gnrc_netdev_default,$(USEMODULE)))
  PORT ?= tap0
endif

# Configure default eeprom file
EEPROM_FILE ?= $(BINDIR)/native.eeprom

# set the eeprom file flags only when the periph_eeprom feature is used.
# NOTE: This can be turned into normal conditional syntax once #9913 is fixed
EEPROM_FILE_FLAGS = $(if $(filter periph_eeprom,$(FEATURES_USED)),--eeprom $(EEPROM_FILE),)
TERMFLAGS += $(EEPROM_FILE_FLAGS)

TERMFLAGS += $(PORT)

ASFLAGS =
ifeq ($(shell basename $(DEBUGGER)),lldb)
  DEBUGGER_FLAGS = -- $(ELFFILE) $(TERMFLAGS)
else
  DEBUGGER_FLAGS = -q --args $(ELFFILE) $(TERMFLAGS)
endif
term-valgrind: export VALGRIND_FLAGS ?= \
	--leak-check=full \
	--track-origins=yes \
	--fullpath-after=$(RIOTBASE)/ \
	--read-var-info=yes
debug-valgrind-server: export VALGRIND_FLAGS ?= --vgdb=yes --vgdb-error=0 -v \
	--leak-check=full --track-origins=yes --fullpath-after=$(RIOTBASE) \
	--read-var-info=yes
term-cachegrind: export CACHEGRIND_FLAGS += --tool=cachegrind
term-gprof: TERMPROG = GMON_OUT_PREFIX=gmon.out $(ELFFILE)
all-valgrind: CFLAGS += -DHAVE_VALGRIND_H
all-valgrind: NATIVEINCLUDES += $(shell pkg-config valgrind --cflags)
all-gprof: CFLAGS += -pg
all-gprof: LINKFLAGS += -pg
all-asan: CFLAGS += -fsanitize=address -fno-omit-frame-pointer
all-asan: CFLAGS += -DNATIVE_MEMORY
all-asan: LINKFLAGS += -fsanitize=address -fno-omit-frame-pointer
all-asan: export AFL_USE_ASAN=1

INCLUDES += $(NATIVEINCLUDES)

CFLAGS += -DDEBUG_ASSERT_VERBOSE

# workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624
ifneq ($(shell gcc --version | head -1 | grep -E ' (4.6|4.7)'),)
  CFLAGS += -DHAVE_NO_BUILTIN_BSWAP16
endif

# backward compatability with glibc <= 2.17 for native
ifeq ($(CPU),native)
  ifeq ($(OS),Linux)
    ifeq ($(shell ldd --version |  awk '/^ldd/{if ($$NF < 2.17) {print "yes"} else {print "no"} }'),yes)
	  LINKFLAGS += -lrt
    endif
  endif
endif

# clumsy way to enable building native on osx:
BUILDOSXNATIVE = 0
ifeq ($(CPU),native)
  ifeq ($(OS),Darwin)
    BUILDOSXNATIVE = 1
  endif
endif

all: # do not override first target

all-debug: all
	@$(COLOR_ECHO) '$(COLOR_RED)`all-debug` is deprecated, just use `all`$(COLOR_RESET)'

all-gprof: all

all-asan: all

all-valgrind: all

all-cachegrind: all

term-valgrind:
	$(VALGRIND) $(VALGRIND_FLAGS) $(ELFFILE) $(PORT)

debug-valgrind-server:
	$(VALGRIND) $(VALGRIND_FLAGS) $(ELFFILE) $(PORT)

debug-valgrind:
	$(eval VALGRIND_PID ?= $(shell pgrep -n memcheck-x86-li -u $(USER) | cut -d" " -f1))
	$(eval DEBUGGER_FLAGS := -ex "target remote | vgdb --pid=$(VALGRIND_PID)" $(DEBUGGER_FLAGS))
	$(DEBUGGER) $(DEBUGGER_FLAGS)

term-cachegrind:
	$(VALGRIND) $(CACHEGRIND_FLAGS) $(ELFFILE) $(PORT)

term-gprof: term

eval-gprof:
	$(GPROF) $(ELFFILE) $(shell ls -rt gmon.out* | tail -1)

eval-cachegrind:
	$(CGANNOTATE) $(shell ls -rt cachegrind.out* | tail -1)

UNDEF += $(BINDIR)/cpu/startup.o