## linux/hpi-cli-apps/Makefile
##
##    AudioScience HPI driver
##    Copyright (C) 1997-2017  AudioScience Inc. <support@audioscience.com>
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of version 2 of the GNU General Public License as
##    published by the Free Software Foundation;
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place, Suite 330,
##    Boston, MA  02111-1307  USA
##

abs-thisdir := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))

.DEFAULT_GOAL := all

-include build_overrides.mk
# hpi_version.mk must exist
include hpi_version.mk
# Include dist file lists
include distfiles.mk

CC = $(CROSS_COMPILE)cc
AR = $(CROSS_COMPILE)ar
LD = $(CROSS_COMPILE)ld

#WARNFULL_CFLAGS = -Wextra -pedantic -std=gnu99 -Wconversion
CFLAGS += -pipe -g -O2 -Wall -DHPI_OS_LINUX -DHPI_BUILD_EXCLUDE_ALL_DEPRECATED \
	$(WARNFULL_CFLAGS)

# Find out if this is a 64-bit build
ar_gcc_arch=$(shell $(CC) -dumpmachine | awk -F- '{ print $$1 }')
ifneq (,$(filter $(ar_gcc_arch),x86_64 aarch64))
	CFLAGS += -D HPI_BUILD_64BIT
endif

bins = asihpitest asihpiassert asihpitune asihpirec asihpiplay \
	asi_firmware_updater asihpibl asihpirds asihpi_si4688
install-prefix ?= /usr/local
bin-install-dir ?= $(install-prefix)/bin

asihpirds: $(asihpirds-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asihpi_si4688: $(asihpi-si4688-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asihpitest: $(asihpitest-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asihpirec: $(asihpirec-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -DASIHPIREC -o $@ $^ -lhpi -lm

asihpiplay: $(asihpirec-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -DASIHPIPLAY -o $@ $^ -lhpi -lm

asihpitune: $(asihpitune-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asihpiassert: $(asihpiassert-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asihpibl: $(asihpibl-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -o $@ $^ -lhpi -lm

asi_firmware_updater: $(asi-firmware-updater-srcs)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDES) -D HPI_BUILD_INCLUDE_INTERNAL -o $@ $^ -lhpiudp -lm -lpthread

setup: $(src-dist-files)

build: libs $(bins)

install-bins: $(bins)
	install -d $(DESTDIR)$(bin-install-dir)
	install -m 0755 $^ $(DESTDIR)$(bin-install-dir)/

install: build install-bins

clean distclean:
	rm -f *~ *.d *.o $(bins)

all: setup build

.PHONY: all libs setup build clean distclean

# Find out if there's sibling hpi-lib directory with a version matching ours
hpi-lib-version = $(shell cat $(abs-thisdir)/../hpi-lib/hpi_version.mk | head -n 1 | cut -d ' ' -f 3)

ifeq ($(hpi-lib-version),$(hpi-version-string))
# When in staging directory or in hpk package look for libraries and headers in the hpi-lib subtarget
CFLAGS += -L$(abs-thisdir)/../hpi-lib
INCLUDES += -I$(abs-thisdir)/../hpi-lib

libs:
	$(MAKE) -C $(abs-thisdir)/../hpi-lib build
else
INCLUDES += -I$(install-prefix)/include/asihpi

libs:
	#dummy
endif
