HALCMDSRCS := hal/utils/halcmd.c hal/utils/halcmd_commands.c hal/utils/halcmd_main.c
HALCMDCCSRCS := hal/utils/halcmd_rtapiapp.cc
HALSHSRCS := hal/utils/halcmd.c hal/utils/halcmd_commands.c hal/utils/halsh.c hal/utils/halcmd_rtapiapp.cc

ifneq ($(READLINE_LIBS),)
HALCMDSRCS += hal/utils/halcmd_completion.c
endif
USERSRCS += $(sort $(HALCMDSRCS) $(HALCMDCCSRCS)) $(HALSHSRCS)

$(call TOOBJSDEPS, $(HALSHSRCS)) : EXTRAFLAGS = -fPIC
$(call TOOBJSDEPS, hal/utils/halsh.c) : EXTRAFLAGS += $(TCL_CFLAGS)
../tcl/hal.so: $(call TOOBJS, $(HALSHSRCS)) \
	../lib/liblinuxcncini.so.0 \
	../lib/liblinuxcnchal.so.0 \
	../lib/libmachinetalk-pb2++.so.0 \
	../lib/libmtalk.so.0 \
	../lib/librtapi_math.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -shared $^  $(TCL_LIBS) -o $@
TARGETS += ../tcl/hal.so

$(call TOOBJSDEPS, $(HALCMDCCSRCS)) : EXTRAFLAGS =  \
	$(PROTOBUF_CFLAGS) $(CZMQ_CFLAGS) -fPIC

../bin/halcmd: $(call TOOBJS, $(HALCMDSRCS))  $(call TOOBJS, $(HALCMDCCSRCS)) \
	../lib/liblinuxcncini.so.0 \
	../lib/liblinuxcnchal.so.0 \
	../lib/libmachinetalk-pb2++.so.0 \
	../lib/libmtalk.so.0 \
	../lib/librtapi_math.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(READLINE_LIBS) \
	$(PROTOBUF_LIBS) $(CZMQ_LIBS) $(AVAHI_LIBS) -lm -lstdc++
TARGETS += ../bin/halcmd

ifdef TARGET_PLATFORM_SOCFPGA
HM2UTILRCS := hal/utils/mksocmemio.c
$(call TOOBJSDEPS, $(HM2UTILRCS)) : EXTRAFLAGS = -Wall -Werror -std=c99
USERSRCS += $(HM2UTILRCS)
../bin/mksocmemio: $(call TOOBJS, $(HM2UTILRCS))
	$(ECHO) Linking $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) $(LDFLAGS) -g -Wall -o $@ $^
TARGETS += ../bin/mksocmemio
endif

ifdef HAVE_GTK
HALMETERSRCS := \
    hal/utils/meter.c \
    hal/utils/miscgtk.c

USERSRCS += $(HALMETERSRCS)

../bin/halmeter: $(call TOOBJS, $(HALMETERSRCS)) ../lib/liblinuxcnchal.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^  $(GTK_LIBS)
TARGETS += ../bin/halmeter

HALSCOPESRCS := \
    hal/utils/scope.c \
    hal/utils/scope_horiz.c \
    hal/utils/scope_vert.c \
    hal/utils/scope_trig.c \
    hal/utils/scope_disp.c \
    hal/utils/scope_files.c \
    hal/utils/miscgtk.c

USERSRCS += $(HALSCOPESRCS)

../bin/halscope: $(call TOOBJS, $(HALSCOPESRCS)) \
	../lib/liblinuxcnchal.so.0 \
	../lib/librtapi_math.so.0
	$(ECHO) Linking $(notdir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(GTK_LIBS)
TARGETS += ../bin/halscope

HALGTKSRCS := \
    hal/utils/scope.c \
    hal/utils/scope_horiz.c \
    hal/utils/scope_vert.c \
    hal/utils/scope_trig.c \
    hal/utils/scope_disp.c \
    hal/utils/scope_files.c \
    hal/utils/meter.c \
    hal/utils/miscgtk.c
$(call TOOBJSDEPS, $(HALGTKSRCS)) : EXTRAFLAGS = $(GTK_CFLAGS)
$(call TOOBJS, $(HALGTKSRCS)): Makefile.inc
endif

ifeq ($(BUILD_DRIVERS),yes)
# normal
#PCI_DEBUG_FLAGS =
# debug build for now
PCI_DEBUG_FLAGS = -DDEBUG_PRINTS

PCIWRITESRCS := hal/utils/pci_write.c hal/utils/upci.c
$(call TOOBJSDEPS, $(PCIWRITESRCS)) : EXTRAFLAGS = $(PCI_DEBUG_FLAGS)
USERSRCS += $(PCIWRITESRCS)
../libexec/pci_write: $(call TOOBJS, $(PCIWRITESRCS))
	$(ECHO) Linking $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^
TARGETS += ../libexec/pci_write

PCIREADSRCS := hal/utils/pci_read.c hal/utils/upci.c
$(call TOOBJSDEPS, $(PCIREADSRCS)) : EXTRAFLAGS = $(PCI_DEBUG_FLAGS)
USERSRCS += $(PCIREADSRCS)
../libexec/pci_read: $(call TOOBJS, $(PCIREADSRCS))
	$(ECHO) Linking $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) $(LDFLAGS) -o $@ $^
TARGETS += ../libexec/pci_read

endif

../bin/comp ../bin/instcomp: ../bin/%: objects/hal/utils/%.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 $@

TARGETS += ../bin/comp ../bin/instcomp
objects/%.py: %.g
	@mkdir -p $(dir $@)
	$(ECHO) Parsing python $<
	$(Q)$(YAPPS2) $< $@
