include $(__nmk_dir)/utils.mk
include $(__nmk_dir)msg.mk
include ../scripts/feature-tests.mak

CONFIG_HEADER := include/config.h

ifeq ($(call try-cc,$(FEATURE_TEST_LIBBSD_DEV),-lbsd),y)
        LIBS	+= -lbsd
        DEFINES	+= -DCONFIG_HAS_LIBBSD
endif

ifeq ($(call pkg-config-check,libselinux),y)
        LIBS	+= -lselinux
        DEFINES	+= -DCONFIG_HAS_SELINUX
endif

FEATURES_LIST	:= TCP_REPAIR STRLCPY STRLCAT PTRACE_PEEKSIGINFO \
	SETPROCTITLE_INIT MEMFD

# $1 - config name
define gen-feature-test
ifeq ($$(call try-cc,$$(FEATURE_TEST_$(1)),$$(LIBS),$$(DEFINES)),y)
	$(Q) @echo '#define CONFIG_HAS_$(1)' >> $$@
	$(Q) @echo '' >> $$@
endif
endef

define config-header-rule
$(CONFIG_HEADER): include/config-base.h
	$$(call msg-gen, $$@)
	$(Q) @echo '#ifndef __CR_CONFIG_H__'				> $$@
	$(Q) @echo '#define __CR_CONFIG_H__'				>> $$@
	$(Q) @echo ''							>> $$@
	$(Q) @echo '#include "config-base.h"'				>> $$@
	$(Q) @echo ''							>> $$@
$(call map,gen-feature-test,$(FEATURES_LIST))
ifeq ($$(VDSO),y)
	$(Q) @echo '#define CONFIG_VDSO'				>> $$@
	$(Q) @echo ''							>> $$@
endif
ifeq ($$(piegen-y),y)
	$(Q) @echo '#define CONFIG_PIEGEN'				>> $$@
	$(Q) @echo ''							>> $$@
endif
	$(Q) @echo '#endif /* __CR_CONFIG_H__ */'			>> $$@
endef

$(eval $(config-header-rule))

$(CONFIG_HEADER): ../scripts/feature-tests.mak
config: $(CONFIG_HEADER)
.PHONY: config
