## linux/hpi-lib/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.
##

abs-thisdir := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
set-intersection = $(filter $(1),$(2))

.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
LDCONFIG = $(CROSS_COMPILE)ldconfig
ARFLAGS = crv

# https://www.gnu.org/software/libtool/manual/libtool.html#Release-numbers
LIB_VER_MAJOR = 10
LIB_VER_MINOR = 3
LIB_VER_INC = 0
lib-version-suffix = $(LIB_VER_MAJOR).$(LIB_VER_MINOR).$(LIB_VER_INC)

#WARNFULL_CFLAGS = -Wextra -pedantic -std=gnu99 -Wconversion
CFLAGS += -ggdb -fPIC -pipe -Wall $(WARNFULL_CFLAGS) \
	-DHPI_BUILD_DEBUG \
	-DHPI_BUILD_INCLUDE_INTERNAL \
	-DHPI_BUILD_EXCLUDE_ALL_DEPRECATED \
	-DnotHPI_BUILD_HPIDUMP 

install-prefix ?= /usr/local
include-install-dir ?= $(install-prefix)/include/asihpi

static-libs = libhpi.a libhpiudp.a libhpimux.a
dynamic-libs = libhpi.so libhpiudp.so libhpimux.so
libs = $(static-libs) $(dynamic-libs)

# 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
	lib-install-dir-candidates = $(install-prefix)/lib64 $(install-prefix)/lib
else
	lib-install-dir-candidates = $(install-prefix)/lib
endif

#### Detect library destination directory
# Use ldconfig to get a list of directories searched for dynamic libraries
# then intersect it with a list of candidates and take the first.
sys-lib-paths = $(shell $(LD) --verbose | grep SEARCH_DIR | sed 's/SEARCH_DIR("=\?\([^"]\+\)"); */\1\n/g' | grep -vE '^$$')
lib-install-dir ?= $(firstword $(call set-intersection,$(sys-lib-paths),$(lib-install-dir-candidates)))

ifeq ($(lib-install-dir),)
	$(error No suitable library destination directory found)
endif

%.d : %.c
	set -e; $(CC) -M $(CFLAGS) -I. $(INCLUDES) $< > $@

# libhpi: Local adapters only
libhpi-build-prefix = build/libhpi
libhpi-objs = $(addprefix $(libhpi-build-prefix)/,hpiwrap.o $(patsubst %.c,%.o,$(filter %c,$(common-sources))))
$(libhpi-objs) : $(libhpi-build-prefix)/%.o : $(abs-thisdir)/%.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -I. $(INCLUDES) -c $< -o $@
libhpi.a: $(libhpi-objs)
	-rm -f $@
	$(AR) $(ARFLAGS) $@ $^
libhpi.so: $(libhpi-objs)
	-rm -f $@
	$(CC) $(LDFLAGS) --shared -fPIC -Wl,-soname,$@.$(LIB_VER_MAJOR) $^ -lm -lrt -o $@.$(lib-version-suffix)
	ln -sf $@.$(lib-version-suffix) $@.$(LIB_VER_MAJOR)
	ln -sf $@.$(lib-version-suffix) $@

# libhpiudp: Network adapters only
libhpiudp-build-prefix = build/libhpiudp
libhpiudp-objs = $(addprefix $(libhpiudp-build-prefix)/,hpiudp.o hpicmn.o hpifilestore.o hpios.o \
	hpiudp_cache.o $(patsubst %.c,%.o,$(filter %c,$(common-sources))))
$(libhpiudp-objs) : $(libhpiudp-build-prefix)/%.o : $(abs-thisdir)/%.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -I. $(INCLUDES) -c $< -o $@
libhpiudp.a: $(libhpiudp-objs)
	-rm -f $@
	$(AR) $(ARFLAGS) $@ $^
libhpiudp.so: $(libhpiudp-objs)
	-rm -f $@
	$(CC) $(LDFLAGS) --shared -fPIC -Wl,-soname,$@.$(LIB_VER_MAJOR) $^ -lm -lrt -lpthread -o $@.$(lib-version-suffix)
	ln -sf $@.$(lib-version-suffix) $@.$(LIB_VER_MAJOR)
	ln -sf $@.$(lib-version-suffix) $@

# libhpimux: Mux messages for both kinds of adapters
libhpimux-build-prefix = build/libhpimux
libhpimux-objs = $(addprefix $(libhpimux-build-prefix)/,hpiwrap.o hpimux.o hpiudp.o hpicmn.o hpios.o \
	hpissx2.o hpifilestore.o hpiudp_cache.o $(patsubst %.c,%.o,$(filter %c,$(common-sources))))
$(libhpimux-objs) : $(libhpimux-build-prefix)/%.o : $(abs-thisdir)/%.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -DHPI_BUILD_MULTIINTERFACE -DHPI_BUILD_SSX2 -I. $(INCLUDES) -c $< -o $@
libhpimux.a: $(libhpimux-objs)
	-rm -f $@
	$(AR) $(ARFLAGS) $@ $^
libhpimux.so: $(libhpimux-objs)
	-rm -f $@
	$(CC) $(LDFLAGS) --shared -fPIC -Wl,-soname,$@.$(LIB_VER_MAJOR) $^ -lm -lrt -lpthread -o $@.$(lib-version-suffix)
	ln -sf $@.$(lib-version-suffix) $@.$(LIB_VER_MAJOR)
	ln -sf $@.$(lib-version-suffix) $@

setup: $(src-dist-files)

build: setup $(libs)

install-headers: $(installed-headers)
	install -d $(DESTDIR)$(include-install-dir)
	install -m 0644 $^ $(DESTDIR)$(include-install-dir)/

install-static: $(static-libs)
	install -d $(DESTDIR)$(lib-install-dir)
	install -m 0755 $^ $(DESTDIR)$(lib-install-dir)/

install-dynamic: $(dynamic-libs)
	install -d $(DESTDIR)$(lib-install-dir)
	# Copy lib*.so.MAJOR.MINOR.INC
	install -m 0755 $(addsuffix .$(lib-version-suffix),$^) $(DESTDIR)$(lib-install-dir)/
	# Copy (and preserve) symlinks to libraries
	cp -P $^ $(addsuffix .*,$^) $(DESTDIR)$(lib-install-dir)/

install: install-headers install-static install-dynamic

distclean clean:
	rm -f *~ *.d *.o *.a *.so

all: setup $(libs)

.PHONY: all build clean install install-headers install-static install-dynamic
