#
# Copyright (C) 2020-2023 Iopsys
#

include $(TOPDIR)/rules.mk

PKG_NAME:=libdsl
PKG_VERSION:=7.3.2

LOCAL_DEV=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/hal/libdsl.git
PKG_SOURCE_VERSION:=1aa9c40f9503311652e562617b1e15533257adcc
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)_$(PKG_SOURCE_VERSION).tar.xz
PKG_MIRROR_HASH:=skip
endif

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_LICENSE:=LGPL-2.1-only
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk

ifeq ($(CONFIG_TARGET_brcmbca),y)
  TARGET_PLATFORM=BROADCOM
  CONFIG_ID=$(SUBTARGET:bcm%=BCM%)
  CHIP_ID=$(CONFIG_ID:BCM9%=%)
  TARGET_CFLAGS +=-DIOPSYS_BROADCOM -DCHIP_$(CHIP_ID) -DCONFIG_$(CONFIG_ID) \
	-I$(STAGING_DIR)/usr/include/bcm963xx/bcmdrivers/broadcom/include/bcm963xx \
	-I$(STAGING_DIR)/usr/include/bcm963xx/shared/opensource/include/bcm963xx \
	-I$(STAGING_DIR)/usr/include/bcm963xx/bcmdrivers/opensource/include/bcm963xx \
	-I$(STAGING_DIR)/usr/include/bcm963xx/userspace/public/include
else ifeq ($(CONFIG_TARGET_x86),y)
  TARGET_PLATFORM=TEST
  TARGET_CFLAGS +=-DIOPSYS_TEST
else ifeq ($(CONFIG_TARGET_armvirt),y)
  TARGET_PLATFORM=TEST
  TARGET_CFLAGS +=-DIOPSYS_TEST
else ifeq ($(CONFIG_TARGET_airoha),y)
  TARGET_PLATFORM=AIROHA
  TARGET_CFLAGS +=-DIOPSYS_AIROHA
endif

TARGET_CFLAGS += \
	-I$(STAGING_DIR)/usr/include \
	-I$(STAGING_DIR)/usr/include/openssl \
	-I$(STAGING_DIR)/usr/include/libnl3

MAKE_FLAGS += \
	CFLAGS="$(TARGET_CFLAGS) -Wall -I./" \
	LDFLAGS="$(TARGET_LDFLAGS)" \
	FPIC="$(FPIC)" \
	PLATFORM="$(TARGET_PLATFORM)" \
	subdirs="$(subdirs)"

define Package/libdsl
  SECTION:=libs
  CATEGORY:=Libraries
  SUBMENU:=IOPSYS HAL libs
  MENU:=1
  TITLE:= xDSL library (libdsl)
  DEPENDS+=TARGET_brcmbca:bcm963xx-bsp +TARGET_airoha:libeasy
endef

define Package/libdsl/description
  Library provides xDSL/GFAST HAL APIs
endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
        $(CP) -rf ./libdsl/* $(PKG_BUILD_DIR)/
endef
endif

define Package/libdsl/config
  if PACKAGE_libdsl
	  config LIBDSL_DEBUG
		depends on PACKAGE_libdsl
		bool "Enable dsl debugging"
		default n

	  config LIBDSL_TEST
		depends on PACKAGE_libdsl
		bool "Enable dsl test program"
		default n

  endif
endef

define Build/InstallDev/libdsl
	$(INSTALL_DIR) $(1)/usr/include/xdsl
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/xdsl.h $(1)/usr/include/xdsl
	$(CP) $(PKG_BUILD_DIR)/xtm.h $(1)/usr/include/xdsl
	$(CP) $(PKG_BUILD_DIR)/common.h $(1)/usr/include/xdsl
	$(CP) $(PKG_BUILD_DIR)/libdsl.so* $(1)/usr/lib/
endef

define Build/InstallDev
	$(call Build/InstallDev/libdsl,$(1),$(2))
endef

ifeq ($(CONFIG_LIBDSL_TEST),y)
define Build/Compile/libdsl
	$(MAKE) -C "$(PKG_BUILD_DIR)/test" $(MAKE_FLAGS)
endef
endif

define Package/libdsl/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(INSTALL_DIR) $(1)/usr/bin
	$(CP) $(PKG_BUILD_DIR)/libdsl.so* $(1)/usr/lib/
ifeq ($(CONFIG_LIBDSL_TEST),y)
	$(CP) $(PKG_BUILD_DIR)/test/libdsl_test $(1)/usr/bin/
endif
endef

$(eval $(call BuildPackage,libdsl))
