# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Build the Azure Integrated HSM resilience-library C samples against the
# INSTALLED library and header (as shipped by the azihsmr-samples package).
#
# Prerequisites (install first):
#   * libazihsmr-dev  - provides /usr/include/azihsm_api_resilient.h and the
#                       -lazihsm_api_resilient development symlink
#   * libazihsmr      - provides the runtime shared object (pulled in by -dev)
#
# The sources sit next to this Makefile (the packaged examples directory), so we
# add `-I.` for the local common.h; the public header <azihsm_api_resilient.h>
# and the library are found on the default system include/link paths. The library
# carries a versioned soname (DT_SONAME = libazihsm_api_resilient.so.1) that the
# runtime package installs on the ldconfig path, so no rpath/LD_LIBRARY_PATH is
# needed at run time.
#
# Usage (from a writable copy of this directory):
#   make            # build every sample as <name>.app
#   make clean

CC       ?= cc
CFLAGS   ?= -std=c11 -Wall -Wextra -O2
CPPFLAGS += -I.
LDLIBS   += -lazihsm_api_resilient

# common.c is the shared helper linked into every sample, not a sample itself.
COMMON   = common
SRCS     = $(filter-out $(COMMON).c,$(wildcard *.c))
SAMPLES  = $(SRCS:.c=)

BINARIES = $(addsuffix .app,$(SAMPLES))

all: $(BINARIES)

# Each sample is its own binary, linked with the shared common.o helper.
%.app: %.o common.o
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LDLIBS)

# Compile a source into an object. common.h is a prerequisite of every object.
%.o: %.c common.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

clean:
	rm -f *.o *.app *.trace.log

.PHONY: all clean
