# Copyright (c) 2001, 2002 Eliot Dresselhaus
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#  The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Basic toolchain defines.
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
INSTALL = install

ifneq ($(origin CROSS_COMPILE), undefined)
  IS_CROSS_COMPILE=yes
endif

CLIB_ARCH = $(shell $(CC) -dumpmachine)

# Where to get linux kernel includes.
# By default get linux includes from /usr/include/linux...
KERNEL_PREFIX ?= /usr

# Where to find compiler include directory (since we may
# be using -nostdinc).
CC_PREFIX = $(shell dirname `$(CC) --print-libgcc-file-name`)

# Where to get LIBC includes for cross compiles
LIBC_PREFIX ?= $(CC_PREFIX)/../../../../$(CLIB_ARCH)

# Where to find CLIB includes/libraries for cross compiles
CLIB_PREFIX ?= /usr/local/$(CLIB_ARCH)

OBJ = $(CLIB_ARCH).o
SHARED_OBJ = shared.$(OBJ)
KERNEL_OBJ = kernel.$(OBJ)
MODULE_OBJ = module.$(OBJ)

DEP = $(CLIB_ARCH).d
SHARED_DEP = shared.$(DEP)
KERNEL_DEP = kernel.$(DEP)

STATIC_LIB = $(CLIB_ARCH).a
SHARED_LIB = $(CLIB_ARCH).so
KERNEL_LIB = kernel.$(CLIB_ARCH).a

STATIC_CFLAGS = $(DEFAULT_CFLAGS)
SHARED_CFLAGS = $(STATIC_CFLAGS) -fPIC

# Compile flags common to user/kernel
CLIB_COMMON_CFLAGS += -Wall

DEBUG ?= no
ifeq ($(DEBUG),yes)
	COPTS ?= -g -O0
	CLIB_COMMON_CFLAGS += -DDEBUG
else
	COPTS ?= -O2
endif

CLIB_COMMON_CFLAGS += $(COPTS) 

CLIB_USER_CFLAGS = $(CLIB_COMMON_CFLAGS)

ifeq ($(IS_CROSS_COMPILE),yes)
  CLIB_USER_CFLAGS += -nostdinc
  CLIB_USER_CFLAGS += -idirafter $(CC_PREFIX)/include
  CLIB_USER_CFLAGS += -idirafter $(KERNEL_PREFIX)/include
  CLIB_USER_CFLAGS += -idirafter $(LIBC_PREFIX)/include
  CLIB_COMMON_CFLAGS += -idirafter $(CLIB_PREFIX)/include
endif

STATIC_CFLAGS = $(CLIB_USER_CFLAGS)
SHARED_CFLAGS = $(STATIC_CFLAGS) -fPIC

%.$(SHARED_OBJ): %.c
	$(CC) -c $(SHARED_CFLAGS) -o $@ $<

%.$(OBJ): %.c
	$(CC) -c $(STATIC_CFLAGS) -o $@ $<

# Kernel version of clib

CLIB_KERNEL_CFLAGS = $(CLIB_COMMON_CFLAGS)

CLIB_KERNEL_CFLAGS += -nostdinc
CLIB_KERNEL_CFLAGS += -idirafter $(CC_PREFIX)/include
CLIB_KERNEL_CFLAGS += -idirafter $(KERNEL_PREFIX)/include

# Kernel always uses mheap allocator (no malloc)
CLIB_KERNEL_CFLAGS += -DCLIB_MEM_MHEAP

CLIB_KERNEL_CFLAGS += -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB

CLIB_KERNEL_CFLAGS += -fno-common -fomit-frame-pointer -fno-strict-aliasing

ifeq ($(findstring mips,$(CLIB_ARCH)),mips)
  CLIB_KERNEL_CFLAGS += -G0 \
	-mno-abicalls -fno-pic -mlong-calls \
	-mcpu=r8000 -mips2 -Wa,--trap 
endif

%.$(KERNEL_OBJ): %.c
	$(CC) $(CLIB_KERNEL_CFLAGS) -c -o $@ $<

# Dependencies
%.$(DEP): %.c
	$(CC) $(CLIB_USER_CFLAGS) -c -M $< | sed -e s/.o:/.$(OBJ):/ > $@

%.$(SHARED_DEP): %.c
	$(CC) $(CLIB_USER_CFLAGS) -c -M $< | sed -e s/.o:/.$(SHARED_OBJ):/ > $@

%.$(KERNEL_DEP): %.c
	$(CC) $(CLIB_KERNEL_CFLAGS) -c -M $< | sed -e s/.o:/.$(KERNEL_OBJ):/ > $@