# $URL: http://subversion:8080/svn/gsc/trunk/drivers/LINUX/SIO4%20and%20SIO8/SIO4_Linux_1.x.x_GSC_DN/utils/makefile $
# $Rev: 53094 $
# $Date: 2023-06-13 10:21:23 -0500 (Tue, 13 Jun 2023) $

# SIO4: Utilities: make file

default: all

CC			= gcc
CC_FLAGS	= -c -Wall -I../include
DEP_FILE	= makefile.dep
TARGET		= ../lib/sio4_utils.a

.NOTPARALLEL: %.o

ECHO	:= ${shell ls /bin/echo 2>/dev/null | wc -l}
ifeq ("${ECHO}","1")
ECHO	:= /bin/echo
else
ECHO	:= echo
endif

OBJ_FILES	:=						\
			util_async.o			\
			util_board_jumpers.o	\
			util_cable.o			\
			util_channel.o			\
			util_close.o			\
			util_count.o			\
			util_cts.o				\
			util_dcd.o				\
			util_dcedte.o			\
			util_driver.o			\
			util_feature.o			\
			util_fw_type_config.o	\
			util_fifo.o				\
			util_host.o				\
			util_id.o				\
			util_init_board.o		\
			util_int.o				\
			util_label.o			\
			util_monosync.o			\
			util_mp.o				\
			util_open.o				\
			util_osc.o				\
			util_reg.o				\
			util_reset_channel.o	\
			util_rx.o				\
			util_rx_fifo_status.o	\
			util_timeout.o			\
			util_tx.o				\
			util_tx_fifo_status.o	\
			util_zilog.o			\
									\
			gsc_util_args.o			\
			gsc_util_reg.o			\
									\
			os_util_id.o			\
			os_util_sem.o			\
			os_util_sleep.o			\
			os_util_thread.o

# Sort object file list to compile most recentlty changed sources first.
# The gsc_util_*.c files come from common sources so may be absent.
# The os_util_*.c files also come from common sources and also may be absent.
# These files are therefore removed from the "ls" input then added back below.
OBJ_GSC:= $(shell echo ${OBJ_FILES} | tr '\040' '\n' | grep gsc_util_)
OBJ_OS:= $(shell echo ${OBJ_FILES} | tr '\040' '\n' | grep os_util_)
OBJ_FILES:= $(shell echo ${OBJ_FILES} | tr '\040' '\n' | grep -v gsc_util_)
OBJ_FILES:= $(shell echo ${OBJ_FILES} | tr '\040' '\n' | grep -v os_util_)
OBJ_FILES:= $(shell ls -t $(patsubst %.o,%.c,${OBJ_FILES}))
# The excluded files are appended after the local .c sources are sorted.
OBJ_FILES:= $(patsubst %.c,%.o,${OBJ_FILES}) ${OBJ_GSC} ${OBJ_OS}

.c.o:
	@echo == Compiling: $<
	@-chmod +rw ${DEP_FILE}
	@# Get the dependency list for this module.
	@-${CC} -MM ${CC_FLAGS} $< > .tmp1
	@# Remove the trailing white space and backslash.
	@-sed -e "s/[ ]*[\\\\]//g" < .tmp1 > .tmp2
	@# Put everything on seperate lines.
	@-tr [:space:] \\n < .tmp2 > .tmp3
	@# Remove all of the system include files.
	@-grep -v "^[ ]*/" < .tmp3 > .tmp4
	@# Remove all empty lines.
	@-grep [[:alnum:]] < .tmp4 > .tmp5
	@# Put everything on the same line.
	@-tr '\n' '\040' < .tmp5 > .tmp6
	@-${ECHO} -e '\012' >> .tmp6
	@# Add all of the other dependencies to the end of this file.
	@-echo >> ${DEP_FILE}
	@-grep -v "^[ ]*$@" < ${DEP_FILE} >> .tmp6
	@# Remove blank lines from the list.
	@-grep "[[:alnum:]]" < .tmp6 > .tmp7
	@# Sort the list and put it in the dependency file.
	@-sort < .tmp7 > ${DEP_FILE}
	@# Cleanup.
	@rm -f ${DEP_FILE}.tmp* .tmp*
	@# Compile the module.
	@${CC} ${CC_FLAGS} $< -o $@

all: ${TARGET}
	@echo ==== All Done

release: ${TARGET}
	@rm -f *.o
	@echo ==== release Done

clean::
	@echo ==== Cleaning ${TARGET} ...
	@rm -f *.a *.o ${TARGET}
	@-chmod +rw ${DEP_FILE}
	@echo > ${DEP_FILE}

${TARGET}: makefile ${OBJ_FILES}
	@echo ==== Linking: $@
	@ld -r -o $@ ${OBJ_FILES}

${DEP_FILE}:
	@echo ==== Creating: $@
	@echo > ${DEP_FILE}

include ${DEP_FILE}
