#
# Copyright (C) 2020-2023 Iopsys
#

include $(TOPDIR)/rules.mk

PKG_NAME:=libethernet
PKG_VERSION:=7.2.117

LOCAL_DEV=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=e917cdcfa0beb15a452f204521bea3d3571190b3
PKG_SOURCE_URL:=https://dev.iopsys.eu/iopsys/libethernet.git
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/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=ECONET
  TARGET_CFLAGS +=-DIOPSYS_ECONET
else ifeq ($(CONFIG_TARGET_mediatek),y)
  TARGET_PLATFORM=MEDIATEK
  TARGET_CFLAGS +=-DIOPSYS_MEDIATEK
else
  $(info Unexpected CONFIG_TARGET, use default LINUX)
  TARGET_PLATFORM=LINUX
  TARGET_CFLAGS +=-DIOPSYS_LINUX
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/libethernet
  SECTION:=libs
  CATEGORY:=Libraries
  SUBMENU:=IOPSYS HAL libs
  MENU:=1
  TITLE:= Ethernet library (libethernet)
  DEPENDS+=+libnl +libnl-route +libeasy +TARGET_brcmbca:bcm963xx-bsp
  DEPENDS+=+TARGET_airoha:ecnt_api +TARGET_airoha:kmod-npu +MODULE_DEFAULT_xpon_bsp:xpon_bsp
endef

define Package/libethernet/description
  Library provides ethernet HAL APIs
endef

define Package/libethernet/config
  config LIBETHERNET_DEBUG
	depends on PACKAGE_libethernet
	bool "Enable ethernet debugging"
	default n

endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
        $(CP) -rf ~/git/libethernet/* $(PKG_BUILD_DIR)/
endef
endif

define Build/InstallDev/libethernet
	$(INSTALL_DIR) $(1)/usr/include
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/ethernet.h $(1)/usr/include/
	$(CP) $(PKG_BUILD_DIR)/libethernet.so $(1)/usr/lib/
endef

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

define Package/libethernet/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/libethernet.so* $(1)/usr/lib/
endef

$(eval $(call BuildPackage,libethernet))
