#
# Copyright (c) 2011-2015, Emulex
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice, 
#    this list of conditions and the following disclaimer in the documentation 
#    and/or other materials provided with the distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
#

APP		= elxsdkutil
OS		= $(shell uname -s)
UNAME_P         = $(shell uname -p)

ifdef CROSS

ifeq ($(CROSS),arm64)
# Setup the cross compiling tools
AARCH64_LIB_DIR ?= /opt/usr/lib/aarch64
CC = aarch64-linux-gnu-gcc
LDFLAGS = -L$(AARCH64_LIB_DIR) -Wl,--rpath=$(AARCH64_LIB_DIR)
# Override uname -p to pick up ARM64 definitions
UNAME_P = aarch64
else
$(error "Unsupported architecture $(CROSS)")
endif

endif

ifndef KERN_VERS
KERNEL		= $(shell uname -r)
else
KERNEL          = $(KERN_VERS)
endif

ELXCDUL_DIR	= ../elxcdul
IOCTL_HDR_DIR	= ../../../driver/common/ocs
TOOLS_COMMON	= ../common
ELXCDUL_LIB     = $(ELXCDUL_DIR)/$(OBJDIR)/elxcdul.a
LIBXML2_INC	:= $(shell xml2-config --cflags)
LIBXML2_LIB	:= $(shell xml2-config --libs)
# LIBXML2_INC	= /usr/include/libxml2
# LIBXML2_LIB	= -lxml2 -lz -lm

DEFINES		=
INCLUDES	= -I. -I$(ELXCDUL_DIR) -I$(IOCTL_HDR_DIR) -I$(TOOLS_COMMON) $(LIBXML2_INC)
WARNINGS	= -Wall -Werror
DEBUG		= -g

LDLIBS		+= $(LIBXML2_LIB)
CFLAGS		= $(DEFINES) $(INCLUDES) $(WARNINGS) $(DEBUG)

SRCS		= elxu_main.c elxu_dwnld.c elxu_dump.c elxu_sky.c elxu_lancer.c elxu_mbox.c elxu_common.c elxu_mgmt.c elxu_device.c elxu_commands.c

vpath %c ../common

ifeq ($(OS), Linux)
SUSE		= $(shell grep -c SUSE /etc/issue)
SRCS		+= elxu_linux.c $(TOOLS_COMMON)/ocsu_io.c $(TOOLS_COMMON)/ocs_socket.c
OBJDIR		= linux_objs

# Override the library for ARM64
ifeq ($(UNAME_P),aarch64)
ELXCDUL_LIB     = $(ELXCDUL_DIR)/$(OBJDIR)/elxcdul.arm64.a
endif

endif
ifeq ($(OS), FreeBSD)
SRCS		+= elxu_bsd.c
OBJDIR		= bsd_objs
endif

OBJS		= $(patsubst %.c, $(OBJDIR)/%.o, $(notdir $(SRCS)))

$(OBJDIR)/%.o: %.c
	$(CC) -c $(CFLAGS) $< -o $@
	
all:		$(OBJDIR) $(OBJS) $(APP)

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(APP): $(OBJS) $(ELXCDUL_LIB)
	@echo "Building - $(OS)"
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

ifeq ($(OS), Linux)
ifeq ($(SUSE), 1)
$(OBJDIR)/elxu_linux.o: elxu_linux.c
	$(CC) -c -o $@ $(CFLAGS) -D__EXPORTED_HEADERS__ -I/usr/src/linux/include elxu_linux.c
else
$(OBJDIR)/elxu_linux.o: elxu_linux.c
	$(CC) -c -o $@ $(CFLAGS) -D__EXPORTED_HEADERS__ -I/lib/modules/$(KERNEL)/build/include elxu_linux.c
endif
endif

clean:
	rm -f $(APP)
	rm -f $(OBJDIR)/*.o
	rm -f *.o

clobber:    clean
	rm -f $(APP) .depend tags TAGS
	rm -rf doc $(OBJDIR) elxcdul
	rm -f elxsdkutil-doc.tgz elxsdkutil-src.tgz

help:
	@echo  'Cleaning targets:'
	@echo  '  clean		  - Remove most files generated by building $(APP)'
	@echo  '  clobber 	  - Remove all generated files including doc and dependencies'
	@echo  ''
	@echo  'Generic targets:'
	@echo  '  all             - Build $(APP)'
	@echo  '  docdist         - Create Doxygen based documentation'
	@echo  ''
	@echo  'To cross-compile for ARM64, add below to any target:'
	@echo  '  CROSS=arm64'

.depend: $(IOCTL_HDR_DIR) $(ELXCDUL_DDIR) $(ELXCDUL_DFILES)
	$(CC) -MM $(CFLAGS) $(SRCS) > .depend

-include .depend

RELEASE_FILES	= \
	elxu_ufi.h \
	elxu_sky.c \
	elxu_sky.h \
	elxu_bsd.c \
	elxu_commands.c \
	elxu_commands.h \
	elxu_common.c \
	elxu_common.h \
	elxu_device.c \
	elxu_device.h \
	elxu_dump.c \
	elxu_dump.h \
	elxu_dwnld.c \
	elxu_dwnld.h \
	elxu_lancer.c \
	elxu_lancer.h \
	elxu_linux.c \
	elxu_main.c \
	elxu_mbox.c \
	elxu_mbox.h \
	elxu_mgmt.c \
	elxu_mgmt.h \
	elx_pt_ioctl.h \
	elxu_sli4.h \
	elxu.h \
	version.h \
	Doxyfile \
	BSDmakefile \
	Makefile \
	main.doxygen \
	elxsdkutil-doc.tgz

list_release_files:
	@echo $(RELEASE_FILES)


docdist:
	@echo "Creating document tar file: ${APP}-doc.tgz"
	@doxygen  Doxyfile > doxygen.log 2>&1
	@cp ../../../driver/common/doxy/images/common/*.png doc/html
	@cp ../../../driver/common/doxy/images/elxsdkutil/*.jpg doc/html
	@tar cfz $(APP)-doc.tgz doc
