#********************************************************************************
#      Copyright:  (C) 2023 LingYun IoT System Studio
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This file is the project top Makefie
#
#        Version:  1.0.0(11/08/23)
#         Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "11/08/23 16:18:43"
#
#*******************************************************************************

PRJ_PATH=$(shell pwd)
APP_NAME = gpsd
INST_PATH= /tftp

BUILD_ARCH=$(shell uname -m)
ifneq ($(findstring $(BUILD_ARCH), "x86_64" "i386"),)
	CROSS_COMPILE=arm-linux-gnueabihf-
endif

# C source files in top-level directory
SRCFILES = $(wildcard *.c)

# common CFLAGS for our source code
CFLAGS = -Wall -Wshadow -Wundef -Wmaybe-uninitialized -D_GNU_SOURCE

# C source file in sub-directory
DIRS= booster
DIRS_PATH=$(patsubst %,${PRJ_PATH}/%,$(DIRS))
CFLAGS+=$(patsubst %,-I%,$(DIRS_PATH))
LDFLAGS+=$(patsubst %,-L%,$(DIRS_PATH))
LIBS=$(patsubst %,-l%,$(DIRS))
LDFLAGS+=${LIBS}

LDFLAGS+=-lpthread

.PHONY:libs
all: entry modules binary

entry:
	@echo "Building ${APP_NAME} on ${BUILD_ARCH}"
	@if [ ! -e booster ] ; then ln -s ../booster; fi

modules:
	@set -e; for d in ${DIRS}; do $(MAKE) CROSS_COMPILE=${CROSS_COMPILE} CFLAGS="${CFLAGS}" -C $${d}; done

binary:  ${SRCFILES}
	$(CROSS_COMPILE)gcc $(CFLAGS) -o ${APP_NAME} $^ ${LDFLAGS}
	@echo " Compile over"

clean:
	set -e; for d in ${DIRS}; do $(MAKE) clean -C $${d}; done
	@rm -f *.o $(APP_NAME)

distclean: clean
	@rm -rf cscope* tags
	@rm -f booster

install:
	cp ${APP_NAME} ${INST_PATH}
