#
# Copyright (c) 2012 by Tensilica Inc. ALL RIGHTS RESERVED.
#
# 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.
#

XTENSA_TOOLS_ROOT = $(shell xt-run --show-config=xttools)
include $(XTENSA_TOOLS_ROOT)/misc/defs.mk

PIL_LD_FLAGS = -mlsp=pimultload -Wl,--shared-pagesize=128 -Wl,-pie -Wl,--no-relax -Wl,--multi-section-pil

# No need to use -fpic for now
#PIL_CC_FLAGS = -fpic -mlongcalls
PIL_CC_FLAGS = -mlongcalls

# Variables common to both types of file
shell = /bin/sh

CC  = xt-clang
CXX = xt-clang++

CFLAGS = -g -Wall

mult_section_library.o : library.c
	$(CC) $(CFLAGS) $(PIL_CC_FLAGS) -o $@ -c $^

mult_section_library.lib : mult_section_library.o 
	$(CC) $(CFLAGS) $(PIL_LD_FLAGS) -o $@ $< -ldynlib

# generate stripped and packaged libraries
# the stripped library can be loaded on the fly, instead of linking with main module
packaged_mult_section_library.o : mult_section_library.lib
	xt-pkg-loadlib -p -e mult_section_lib -s stripped_mult_section.lib -o $@ $^

mult_section_cpp_library.o : cpplibrary.cpp
	$(CXX) $(CFLAGS) $(PIL_CC_FLAGS) -fno-exceptions -o $@ -c $^

mult_section_cpp_library.lib : mult_section_cpp_library.o
	$(CXX) $(CFLAGS) -fno-exceptions  $(PIL_LD_FLAGS) -o $@ $< -ldynlib

# generate stripped and packaged libraries
# the stripped library can be loaded on the fly, instead of linking with main module
packaged_mult_section_cpp_library.o : mult_section_cpp_library.lib
	xt-pkg-loadlib -p -e mult_section_cpp_lib -s stripped_mult_section_cpp.lib -o $@ $^

mult_section_load_test.o : mult_section_load_test.c
	$(CC) $(CFLAGS) -o $@ -c $^

mult_section_load_test.exe : packaged_mult_section_library.o packaged_mult_section_cpp_library.o mult_section_load_test.o
	$(CC) $(CFLAGS) $^ -o $@ -lmloader

# load on-the-fly test code here; define LOAD_LIB_OTF for conditional compilation
mult_section_load_test_otf.o : mult_section_load_test.c
	$(CC) $(CFLAGS) -D LOAD_LIB_OTF -o $@ -c $^

# the libraries are not linked with main module
mult_section_load_test_otf.exe : packaged_mult_section_library.o packaged_mult_section_cpp_library.o mult_section_load_test_otf.o
	$(CC) $(CFLAGS) mult_section_load_test_otf.o -o $@ -lmloader

run: mult_section_load_test.exe
	xt-run $^ 

# load the stripped lib on the fly
run_ext: mult_section_load_test_otf.exe
	xt-run $^ 

# Generic Targets here

clean:
	$(RM) *.o *.lib *~ a.out *.exe
all: 
	@echo nothing to be done for all

.PHONY: clean all run run_ext

