USER_COMP_PY = \
	pyvcp hal_input \
	hal_gpio_mcp23017 \
	hal_pwm_pca9685 \
	hal_storage \
	hal_temp_ads7828 \
	hal_temp_bbb \
	hal_temp_atlas \
	hal_gy86 \
	gladevcp

$(patsubst %, ../bin/%, $(USER_COMP_PY)) : ../bin/%: hal/user_comps/%.py
	@$(ECHO) Syntax checking python script $(notdir $@)
	$(Q)$(PYTHON) -c 'import sys; compile(open(sys.argv[1]).read(), sys.argv[1], "exec")' $<
	$(ECHO) Copying python script $(notdir $@)
	$(Q)(echo '#!$(PYTHON)'; sed '1 { /^#!/d; }' $<) > $@.tmp && chmod +x $@.tmp && mv -f $@.tmp $@

PYTARGETS += $(patsubst %, ../bin/%, $(USER_COMP_PY))

# USER_COMP_COMPS is defined in hal/components/Submakefile
USER_COMP_BINS := $(patsubst hal/user_comps/%.comp, ../bin/%, $(USER_COMP_COMPS))

../bin/%: hal/user_comps/%.comp ../bin/comp ../lib/liblinuxcnchal.so.0 ../lib/librtapi_math.so.0
	$(ECHO) "Preprocessing usercomp $(notdir $<)"
	@mkdir -p objects/hal/user_comps
	$(Q)../bin/comp -o $(patsubst  hal/user_comps/%.comp, objects/hal/user_comps/%.c, $<) $<
	$(ECHO) "Compiling usercomp $(notdir $<)"
	$(Q)$(CC) -DULAPI $(INCLUDE) -o $@ $(LDFLAGS) \
		$(patsubst  hal/user_comps/%.comp, objects/hal/user_comps/%.c, $<) \
		../lib/liblinuxcnchal.so.0 \
		../lib/librtapi_math.so.0

TARGETS +=  $(USER_COMP_BINS)

ifdef HAVE_LIBMODBUS3
MODBUSSRCS := hal/user_comps/gs2_vfd.c
MODBUSLDFLAGS := $(GLIB_LIBS) $(LIBMODBUS_LIBS)
MODBUSCCFLAGS := $(GLIB_CFLAGS) $(LIBMODBUS_CFLAGS)
USERSRCS += $(MODBUSSRCS)

$(call TOOBJSDEPS, $(MODBUSSRCS)) : EXTRAFLAGS += $(MODBUSCCFLAGS)

../bin/gs2_vfd: $(call TOOBJS, $(MODBUSSRCS)) ../lib/liblinuxcnchal.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) -o $@ $^ $(LDFLAGS) $(MODBUSLDFLAGS)
TARGETS += ../bin/gs2_vfd
endif # HAVE_LIBMODBUS

ifeq ($(HIDRAW_H_USABLE),yes)
SHUTTLEXPRESS_SRC = hal/user_comps/shuttlexpress.c
USERSRCS += $(SHUTTLEXPRESS_SRC)
../bin/shuttlexpress: $(call TOOBJS, $(SHUTTLEXPRESS_SRC)) ../lib/liblinuxcnchal.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^
TARGETS += ../bin/shuttlexpress
endif

ifdef HAVE_LIBUSB10

XHC_HB04_SRC = hal/user_comps/xhc-hb04.cc
USERSRCS += $(XHC_HB04_SRC)
$(call TOOBJSDEPS, $(XHC_HB04_SRC)) : EXTRAFLAGS += $(LIBUSB10_CFLAGS)
../bin/xhc-hb04: $(call TOOBJS, $(XHC_HB04_SRC)) ../lib/liblinuxcnchal.so.0 ../lib/liblinuxcncini.so.0 ../lib/librtapi_math.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^ -lstdc++ $(LIBUSB10_LIBS)
TARGETS += ../bin/xhc-hb04

endif # HAVE_LIBUSB10

../include/%.h: ./hal/user_comps/%.h
	cp $^ $@
../include/%.hh: ./hal/user_comps/%.hh
	cp $^ $@
