#############################################################
#
# netperf
#
#############################################################
NETPERF_VER:=2.4.4
NETPERF_SOURCE:=netperf-$(NETPERF_VER).tar.gz
NETPERF_SITE:=ftp://ftp.netperf.org/netperf/
NETPERF_CAT:=zcat
NETPERF_DIR:=$(BUILD_DIR)/netperf-$(NETPERF_VER)
NETPERF_CLIENT_BINARY:=netperf
NETPERF_SERVER_BINARY:=netserver
NETPERF_SERVER_BINARIES=$(NETPERF_CLIENT_BINARY) $(NETPERF_SERVER_BINARY)

$(DL_DIR)/$(NETPERF_SOURCE):
	 $(WGET) -P $(DL_DIR) $(NETPERF_SITE)/$(NETPERF_SOURCE)

netperf-source: $(DL_DIR)/$(NETPERF_SOURCE)

$(NETPERF_DIR)/.unpacked: $(DL_DIR)/$(NETPERF_SOURCE)
	$(NETPERF_CAT) $(DL_DIR)/$(NETPERF_SOURCE) | tar -C $(BUILD_DIR) $(TAR_OPTIONS) -
	touch $(NETPERF_DIR)/.unpacked

$(NETPERF_DIR)/.configured: $(NETPERF_DIR)/.unpacked
	cd $(NETPERF_DIR); rm -rf config.cache; \
		$(TARGET_CONFIGURE_OPTS) \
		ac_cv_func_setpgrp_void=yes \
		CFLAGS="$(TARGET_CFLAGS)" \
		LDFLAGS="-L$(STAGING_DIR)/lib -L$(STAGING_DIR)/usr/local/lib -Wl,--rpath-link -Wl,$(STAGING_DIR)/usr/local/lib" \
		./configure \
		--target=$(GNU_TARGET_NAME) \
		--host=$(GNU_TARGET_NAME) \
		--build=$(GNU_HOST_NAME) \
		--prefix=/usr \
		--exec-prefix=/usr \
		--bindir=/usr/bin \
		--sbindir=/usr/sbin \
		--libexecdir=/usr/lib \
		--sysconfdir=/etc \
		--datadir=/usr/share \
		--localstatedir=/var \
		--mandir=/usr/man \
		--infodir=/usr/info \
		$(shell echo '' $(BR2_PACKAGE_NETPERF_EXTRA_CONFIGURE_OPTS)) \
		$(TARGET_CONFIGURE_FLAGS)
	touch  $(NETPERF_DIR)/.configured

$(NETPERF_DIR)/src/$(NETPERF_CLIENT_BINARY) $(NETPERF_DIR)/src/$(NETPERF_SERVER_BINARY): $(NETPERF_DIR)/.configured
netperf-make: $(NETPERF_DIR)/src/$(NETPERF_CLIENT_BINARY) $(NETPERF_DIR)/src/$(NETPERF_SERVER_BINARY)
	$(MAKE) CC=$(TARGET_CC) -C $(NETPERF_DIR)

$(STAGING_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY): $(NETPERF_DIR)/src/$(NETPERF_CLIENT_BINARY)
$(STAGING_DIR)/usr/bin/$(NETPERF_SERVER_BINARY): $(NETPERF_DIR)/src/$(NETPERF_SERVER_BINARY)
$(STAGING_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY) $(STAGING_DIR)/usr/bin/$(NETPERF_SERVER_BINARY): netperf-staginginstall
netperf-staginginstall:
	$(MAKE) DESTDIR=$(STAGING_DIR) CC=$(TARGET_CC) -C $(NETPERF_DIR) install

$(TARGET_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY): $(STAGING_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY)
	cp -af $(STAGING_DIR)/usr/bin/mipsel-linux-$(NETPERF_CLIENT_BINARY) $(TARGET_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY)
	-$(STRIPCMD) $(STRIP_STRIP_UNNEEDED) $(TARGET_DIR)/usr/bin/$(NETPERF_BINARY)

$(TARGET_DIR)/usr/bin/$(NETPERF_SERVER_BINARY): $(STAGING_DIR)/usr/bin/$(NETPERF_SERVER_BINARY)
	cp -af $(STAGING_DIR)/usr/bin/mipsel-linux-$(NETPERF_SERVER_BINARY) $(TARGET_DIR)/usr/bin/$(NETPERF_SERVER_BINARY)
	-$(STRIPCMD) $(STRIP_STRIP_UNNEEDED) $(TARGET_DIR)/usr/bin/$(NETPERF_SERVER_BINARY)

netperf_client: $(TARGET_DIR)/usr/bin/$(NETPERF_CLIENT_BINARY)

netperf_server: $(TARGET_DIR)/usr/bin/$(NETPERF_SERVER_BINARY)

netperf-clean:
	-make -C $(NETPERF_DIR) clean

netperf-dirclean:
	rm -rf $(NETPERF_DIR)

#############################################################
#
# Toplevel Makefile options
#
#############################################################
ifeq ($(strip $(BR2_PACKAGE_NETPERF_CLIENT)),y)
TARGETS+=netperf_client
endif
ifeq ($(strip $(BR2_PACKAGE_NETPERF_SERVER)),y)
TARGETS+=netperf_server
endif
