
KDIR ?= /lib/modules/$(shell uname -r)/build
# get OS Version, Hardware Type, CPU info.
os=$(shell $(PWD)/SIETIUM-VERSION-GEN os)
osver=$(shell $(PWD)/SIETIUM-VERSION-GEN osver)
gb_version=V$(shell $(PWD)/SIETIUM-VERSION-GEN gb_version)
module_version=$(shell $(PWD)/SIETIUM-VERSION-GEN module_version)
arch=$(shell $(PWD)/SIETIUM-VERSION-GEN arch)

ifeq ($(os),Kylin)
	#OSVER=CONFIG_SKYLIN_OS_V10
	KYVER=/etc/.kyinfo
	KYOS=Kylin-Server-V10-GFB-RC3-030
	result=$(shell cat $(KYVER)|grep $(KYOS))
	ifneq ($(findstring $(KYOS),$(result)),)
		OS_ID_CONFIG=GB_4_19_90
	endif
	KYVER=/etc/os-release
	KYOS=Tercel
	result=$(shell cat $(KYVER)|grep $(KYOS))
	ifneq ($(findstring $(KYOS),$(result)),)
		OS_ID_CONFIG=GB_KYLIN_SERV_TERCEL
	endif
else ifneq ($(findstring KylinSec,$(os)),)
	OSVER=CONFIG_KYLINSEC_OS
	KYVER=/etc/.kyinfo
	KYOS=KylinSec
	result=$(shell cat $(KYVER)|grep $(KYOS))
	ifneq ($(findstring $(KYOS),$(result)),)
		OS_ID_CONFIG=GB_4_19_90
	endif
else ifneq ($(findstring $(os), uos UOS),)
	OSVER=CONFIG_UOS_20
	KYVER=/etc/os-version
	KYOS=Server
	result=$(shell cat $(KYVER)|grep $(KYOS))
	ifneq ($(findstring $(KYOS),$(result)),)
		OS_ID_CONFIG=GB_4_19_90
	endif
else ifeq ($(os),Loongnix)
	OSVER=CONFIG_LOONGSON_OS
else ifeq ($(os),CentOS)
	OSVER=CONFIG_CENTOS_OS
	ifeq ($(osver), CentOS7-3.10.0-1160)
		OSCOMPILE=SYS_CENTOS7_9_2009
		KDIR := /lib/modules/3.10.0-1160.36.2.el7.x86_64/build
	else ifeq ($(osver), CentOS7-3.10.0-862)
		OSCOMPILE=SYS_CENTOS7_COMPILE_ENV
	else
		OSCOMPILE=CONFIG_CENTOS
	endif
endif

mcu_peri=CONFIG_MCU_PERI=y
ifeq ($(mcu_peri), CONFIG_MCU_PERI=y)
	mcu_peri += CONFIG_MCU_I2C=n
	mcu_peri += CONFIG_MCU_UART=n
	mcu_peri += CONFIG_MCU_GPIO=y
	mcu_peri += CONFIG_MCU_PWM=n
	mcu_peri += CONFIG_GPIO_PWM=n
	mcu_peri += CONFIG_MCU_SPI=n
	mcu_peri += CONFIG_MCU_PVT=y
	mcu_peri += CONFIG_MCU_FREQ=y
	mcu_peri += CONFIG_MCU_FAN=y
	mcu_peri += CONFIG_SN_INFO=y
	mcu_peri += CONFIG_PCIE_MSG=y
	mcu_peri += CONFIG_GB_BACKLIGHT=y
endif
MCU_CFLAGS += $(mcu_peri)

ifeq ($(findstring CONFIG_MCU_PERI=y, $(mcu_peri)),CONFIG_MCU_PERI=y)
	mcu_peri_ip += CONFIG_MCU_PERI
ifeq ($(findstring CONFIG_MCU_I2C=y, $(mcu_peri)),CONFIG_MCU_I2C=y)
	mcu_peri_ip += CONFIG_MCU_I2C
endif
ifeq ($(findstring CONFIG_MCU_UART=y, $(mcu_peri)),CONFIG_MCU_UART=y)
	mcu_peri_ip += CONFIG_MCU_UART
endif
ifeq ($(findstring CONFIG_MCU_GPIO=y, $(mcu_peri)),CONFIG_MCU_GPIO=y)
	mcu_peri_ip += CONFIG_MCU_GPIO
endif
ifeq ($(findstring CONFIG_MCU_PWM=y, $(mcu_peri)),CONFIG_MCU_PWM=y)
	mcu_peri_ip += CONFIG_MCU_PWM
endif
ifeq ($(findstring CONFIG_GPIO_PWM=y, $(mcu_peri)),CONFIG_GPIO_PWM=y)
	mcu_peri_ip += CONFIG_GPIO_PWM
endif
ifeq ($(findstring CONFIG_MCU_SPI=y, $(mcu_peri)),CONFIG_MCU_SPI=y)
	mcu_peri_ip += CONFIG_MCU_SPI
endif
ifeq ($(findstring CONFIG_MCU_PVT=y, $(mcu_peri)),CONFIG_MCU_PVT=y)
	mcu_peri_ip += CONFIG_MCU_PVT
endif
ifeq ($(findstring CONFIG_MCU_FREQ=y, $(mcu_peri)),CONFIG_MCU_FREQ=y)
	mcu_peri_ip += CONFIG_MCU_FREQ
endif
ifeq ($(findstring CONFIG_MCU_FAN=y, $(mcu_peri)),CONFIG_MCU_FAN=y)
	mcu_peri_ip += CONFIG_MCU_FAN
endif
endif
ifeq ($(findstring CONFIG_GB_BACKLIGHT=y, $(mcu_peri)),CONFIG_GB_BACKLIGHT=y)
	mcu_peri_ip += CONFIG_GB_BACKLIGHT
endif
MCU_IP_CFLAGS +=$(mcu_peri_ip)

ifeq ($(arch),mips64)
PLAT=CONFIG_MIPS
else ifeq ($(arch),loongarch)
PLAT=CONFIG_MIPS
else ifeq ($(arch),loongarch64)
PLAT=CONFIG_LOONGARCH
else ifeq ($(arch),sw_64)
PLAT=CONFIG_SW64
endif
PLATFORM=$(arch)

# defines in build
DEFINE += $(OSVER) $(OSCOMPILE)
DEFINE += $(PLAT) $(OS_ID_CONFIG)
DEFINE += OS_VERSION="$(osver)"
DEFINE += GB_VERSION=$(gb_version)
DEFINE += $(MCU_IP_CFLAGS)
DEFINES = $(addprefix -D,$(DEFINE))

# include path
INCLUDE += $(KDIR)/include/drm/
INCLUDES = $(addprefix -I,$(INCLUDE))
INCLUDES += -ggdb

OUTDIRS := objs/$(PLATFORM)/$(os)

export clib os osver PLATFORM OUTDIRS

ifeq ($(clib),y)

default:
ifeq ($(PLATFORM),mips)
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)" modules
else ifeq ($(PLATFORM),mips64)
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)" modules
else ifeq ($(PLATFORM),loongarch64)
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)" modules
else ifeq ($(os),CentOS)
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)" modules
else
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)"
endif
	@mkdir -p $(OUTDIRS)
	@ar rc $(OUTDIRS)/gbgpu_$(osver).o $(PWD)/ip/*.o $(PWD)/kms/device/*.o $(PWD)/gpu/*.o $(PWD)/vpu/vpu_mm/*.o $(PWD)/vpu/vpu_enc/*.o $(PWD)/vpu/vpu_dec/*.o $(PWD)/vpu/vpu_dec1/*.o $(PWD)/vpu/vpu_comm/*.o
	@strip --strip-debug $(OUTDIRS)/gbgpu_$(osver).o
# For Kylin kernel env compile, kernel NOT support tar/untar to obtain .o file, so support by gb.o.hex file
ifeq ($(os), Kylin)
	@xxd $(OUTDIRS)/gbgpu_$(osver).o $(OUTDIRS)/gbgpu_$(osver).o.hex
endif

else
default:
	$(MAKE) -C $(KDIR) $(CFLAGS) $(MCU_CFLAGS) M=$(PWD)  EXTRA_CFLAGS="$(INCLUDES) $(DEFINES)" modules
	@bash ./scripts/link_module.sh $(module_version)
endif


help:
	@echo "================================================="
	@echo "     the makefile need one parameter when create  .o lib:"
	@echo "     parameter: 	        'clib =n'"
	@echo "=================================================="
clean:
	@rm -rf $(PWD)/*.mod $(PWD)/*.o.hex $(PWD)/kms/*.o $(PWD)/kms/.*.o.d $(PWD)/kms/device/*o $(PWD)/vpu/vpu_mm/*.o $(PWD)/vpu/vpu_enc/*.o $(PWD)/vpu/vpu_dec/*.o $(PWD)/vpu/vpu_dec1/*.o $(PWD)/vpu/vpu_comm/*.o $(PWD)/gpu_test/*.o $(PWD)/vpu/vpu_mm/*.o.cmd $(PWD)/vpu/vpu_enc/*.o.cmd $(PWD)/vpu/vpu_dec/*.o.cmd $(PWD)/vpu/vpu_dec1/*.o.cmd $(PWD)/vpu/vpu_comm/*.o.cmd $(PWD)/vpu/vpu_mm/.*.o.cmd $(PWD)/vpu/vpu_enc/.*.o.cmd $(PWD)/vpu/vpu_dec/.*.o.cmd $(PWD)/vpu/vpu_dec1/.*.o.cmd $(PWD)/vpu/vpu_comm/.*.o.cmd $(PWD)/gpu_test/*.o.cmd $(PWD)/gpu_test/.*.o.cmd $(PWD)/kms/device/.*.o.cmd $(PWD)/kms/.*.o.cmd $(PWD)/common/*.o $(PWD)/common/.*.o.cmd $(PWD)/common/.*.o.d $(PWD)/gpu/*.o $(PWD)/gpu/.*.o.cmd $(PWD)/gpu/.*.o.d $(PWD)/gpu/core/*.o $(PWD)/gpu/core/.*.o.cmd $(PWD)/gpu/hw/.*.o.cmd $(PWD)/gpu/hw/*.o $(PWD)/gpu/hw/.*.o.d $(PWD)/ip/*.o $(PWD)/ip/.*.o.cmd *.mod.c *.mod.o *.o modules* Module* .*.o.cmd $(PWD)/objs/$(PLATFORM)/$(os)/*.o.hex $(PWD)/objs/$(PLATFORM)/$(os)/*.o $(PWD)/audio/*.o $(PWD)/audio/*o.cmd $(PWD)/audio/.*o.cmd $(PWD)/mcu_peripherals/i2c/*o $(PWD)/mcu_peripherals/i2c/*o.cmd $(PWD)/mcu_peripherals/i2c/.*o.cmd $(PWD)/mcu_peripherals/pvt/*.o $(PWD)/mcu_peripherals/pvt/*o.cmd $(PWD)/mcu_peripherals/pvt/.*o.cmd $(PWD)/mcu_peripherals/pwm/*.o $(PWD)/mcu_peripherals/pwm/*o.cmd $(PWD)/mcu_peripherals/pwm/.*o.cmd $(PWD)/mcu_peripherals/spi/*.o $(PWD)/mcu_peripherals/spi/*o.cmd $(PWD)/mcu_peripherals/spi/.*o.cmd $(PWD)/mcu_peripherals/pwm/fan/*.o $(PWD)/mcu_peripherals/pwm/fan/*o.cmd $(PWD)/mcu_peripherals/pwm/fan/.*o.cmd $(PWD)/mcu_peripherals/pwm/backlight/*o $(PWD)/mcu_peripherals/pwm/backlight/*o.cmd $(PWD)/mcu_peripherals/pwm/backlight/.*o.cmd $(PWD)/mcu_peripherals/*.o $(PWD)/mcu_peripherals/*o.cmd $(PWD)/mcu_peripherals/gpu_freq/*.o $(PWD)/mcu_peripherals/gpu_freq/*o.cmd $(PWD)/mcu_peripherals/gpio/*.o $(PWD)/mcu_peripherals/gpio/*o.cmd $(PWD)/mcu_peripherals/uart/*.o $(PWD)/mcu_peripherals/uart/*o.cmd $(PWD)/mcu_peripherals/sn_info/*.o $(PWD)/mcu_peripherals/sn_info/*o.cmd $(PWD)/mcu_peripherals/pcie_msg/*.o $(PWD)/mcu_peripherals/pcie_msg/*o.cmd  $(PWD)/mcu_peripherals/backlight/*.o $(PWD)/mcu_peripherals/backlight/*o.cmd
	@rm -rf *.ko *.deb *.rpm *.md5sum .tmp_versions/ .*.cmd *.a
