#
# liblswsock - LSW Socket Library
#
# Copyright (C) 2020-2025 Vaunix Technology Corporation
#
#  Author - NJB
#  Comments - 02-02-2020  NJB   Initial Version of Driver Code Make file
#

V ?= 0

ifeq ($(V), 0)
E = @
P = @echo 
else
E = 
P = @true
endif

TARGET_LIB = libLSWhid.so
TEST_APP   = LSWtest

SRCDIR     = libLSWhid/src
INCDIR     = -IlibLSWhid/include

SRC        = $(wildcard $(SRCDIR)/*.c)
OBJS       = $(SRC:.c=.o)

TEST_SRC   = Test/LSWtest.c
TEST_OBJ   = $(TEST_SRC:.c=.o)

CC         = $(CROSS_COMPILE)gcc

CFLAGS     += -g -Wall -Wextra -Wno-unused -fPIC
LDFLAGS    += -lpthread -lm -lusb-1.0

.PHONY: all clean install copylib test

all: install test

# Compile source files into objects with include directory
%.o : %.c
	$P '  CC      $(@F)'
	$E $(CC) -c $(INCDIR) $(CFLAGS) $< -o $@

# Link all object files into shared library
$(TARGET_LIB): $(OBJS)
	$P '  LD      $(@F)'
	$E $(CC) -shared -Wl,-soname,$(TARGET_LIB) $^ -o $@ $(LDFLAGS)

# Copy the library to /usr/lib/
copylib: $(TARGET_LIB)
	$E sudo cp $(TARGET_LIB) /usr/lib/
	$E sudo ldconfig

# Compile test app object file with include dir
$(TEST_OBJ): $(TEST_SRC)
	$P '  CC      $(@F)'
	$E $(CC) -c $(INCDIR) $(CFLAGS) $< -o $@

# Link test app against installed library
$(TEST_APP): $(TEST_OBJ)
	$P '  LD      $(@F)'
	$E $(CC) -o $@ $(TEST_OBJ) -lLSWhid $(LDFLAGS)

# Build test app after copying library
test: copylib $(TEST_APP)

clean:
	$P 'Cleaning all build files'
	$E $(RM) $(OBJS) $(TEST_OBJ) $(TARGET_LIB) $(TEST_APP)

install: copylib
