#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2019 Joyent, Inc.
#

#
# This makefile could be simplified substantially. However, it does
# everything explicitly to try and work with a wide variety of different
# makes.
#
# The following values should be passed in by the invoker of the
# Makefile:
#
#	CC		C Compiler to use
#	CFLAGS32	32-bit CFLAGS
#	CFLAGS64	64-bit CFLAGS
#	CTFCONVERT	Path to ctfconvert
#	CTFMERGE	Path to ctfmerge
#	DEBUGFLAGS	The set of debug flags to use
#	BUILDDIR	Directory things should be built in
#	CHECK32		Program to check 32-bit output
#	CHECK64		Program to check 64-bit output
#

OBJS_C_32 =	$(BUILDDIR)/test-global.32.c.o \
		$(BUILDDIR)/test-scoped.32.c.o

OBJS_C_64 =	$(BUILDDIR)/test-global.64.c.o \
		$(BUILDDIR)/test-scoped.64.c.o

OBJS_M_32 =	$(BUILDDIR)/test-global.32.m.o \
		$(BUILDDIR)/test-scoped.32.m.o

OBJS_M_64 =	$(BUILDDIR)/test-global.64.m.o \
		$(BUILDDIR)/test-scoped.64.m.o

BINS =		$(BUILDDIR)/test-merge-reduction-32c.so.1 \
		$(BUILDDIR)/test-merge-reduction-32m.so.1 \
		$(BUILDDIR)/test-merge-reduction-64c.so.1 \
		$(BUILDDIR)/test-merge-reduction-64m.so.1 \

CFLAGS = -fPIC
LDFLAGS = -shared -Wl,-Mmapfile-vers -Wl,-ztext -Wl,-zdefs \
	  -htest-merge-reduction.so.1

build: $(BINS)

$(BUILDDIR)/%.32.c.o: %.c
	$(CC) $(CFLAGS) $(CFLAGS32) $(DEBUGFLAGS) -o $@ -c $<

$(BUILDDIR)/%.64.c.o: %.c
	$(CC) $(CFLAGS) $(CFLAGS64) $(DEBUGFLAGS) -o $@ -c $<

$(BUILDDIR)/%.32.m.o: %.c
	$(CC) $(CFLAGS) $(CFLAGS32) $(DEBUGFLAGS) -o $@ -c $<
	$(CTFCONVERT) $@

$(BUILDDIR)/%.64.m.o: %.c
	$(CC) $(CFLAGS) $(CFLAGS64) $(DEBUGFLAGS) -o $@ -c $<
	$(CTFCONVERT) $@

$(BUILDDIR)/test-merge-reduction-32c.so.1: $(OBJS_C_32)
	$(CC) $(CFLAGS32) $(CFLAGS) $(LDFLAGS) $(DEBUGFLAGS) -o $@ $(OBJS_C_32)
	$(CTFCONVERT) $@

$(BUILDDIR)/test-merge-reduction-64c.so.1: $(OBJS_C_64)
	$(CC) $(CFLAGS64) $(CFLAGS) $(LDFLAGS) $(DEBUGFLAGS) -o $@ $(OBJS_C_64)
	$(CTFCONVERT) $@

$(BUILDDIR)/test-merge-reduction-32m.so.1: $(OBJS_M_32)
	$(CC) $(CFLAGS32) $(CFLAGS) $(LDFLAGS) $(DEBUGFLAGS) -o $@ $(OBJS_M_32)
	$(CTFMERGE) -t -o $@ $(OBJS_M_32)

$(BUILDDIR)/test-merge-reduction-64m.so.1: $(OBJS_M_64)
	$(CC) $(CFLAGS64) $(CFLAGS) $(LDFLAGS) $(DEBUGFLAGS) -o $@ $(OBJS_M_64)
	$(CTFMERGE) -t -o $@ $(OBJS_M_64)

run-test:
	$(CHECK32) $(BUILDDIR)/test-merge-reduction-32c.so.1
	$(CHECK64) $(BUILDDIR)/test-merge-reduction-64c.so.1
	$(CHECK32) $(BUILDDIR)/test-merge-reduction-32m.so.1
	$(CHECK64) $(BUILDDIR)/test-merge-reduction-64m.so.1
