

HALNGDIR := hal/cython
HALNGMKDIR :=  $(HALNGDIR)/machinekit

PYTARGETS += ../lib/python/machinekit/__init__.py

../lib/python/machinekit/__init__.py:
	@mkdir -p ../lib/python/machinekit
	@touch $@

INCLUDES += $(HALNGDIR)

HALSO_SRCS := $(addprefix $(HALNGMKDIR)/, \
	hal.c)
HALSO_CXXSRCS := \
	hal/utils/halcmd_rtapiapp.cc

USERSRCS += $(HALSO_SRCS)
$(call TOOBJSDEPS, $(HALSO_SRCS)) : EXTRAFLAGS=-fPIC -Ihal/utils -I.
$(call TOOBJSDEPS, $(HALSO_CXXSRCS)) : EXTRAFLAGS=-fPIC -Ihal/utils -I.

# shotgun approach to dependencies: will alwas hit the target (and more).
$(HALNGMKDIR)/hal.c: $(wildcard $(HALNGMKDIR)/*.pyx $(HALNGMKDIR)/*.pxd)

TARGETS += ../lib/python/machinekit/hal.so
../lib/python/machinekit/hal.so: \
		../lib/liblinuxcnchal.so.0 \
		../lib/libmtalk.so.0 \
		$(patsubst %.c,objects/%.o,$(HALSO_SRCS)) \
		$(patsubst %.cc,objects/%.o,$(HALSO_CXXSRCS))
	$(ECHO) Linking python module $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) -g $(LDFLAGS) -shared -o $@ $^


RTAPISO_CSRCS := $(addprefix $(HALNGMKDIR)/, \
	rtapi.c)

$(RTAPISO_CSRCS): $(wildcard $(HALNGMKDIR)/*.pyx $(HALNGMKDIR)/*.pxd)

RTAPISO_CXXSRCS := \
	hal/utils/halcmd_rtapiapp.cc

USERSRCS += $(RTAPISO_CSRCS) $(RTAPISO_CXXSRCS)
$(call TOOBJSDEPS, $(RTAPISO_CSRCS)) : EXTRAFLAGS=-fPIC -Ihal/utils -I.
$(call TOOBJSDEPS, $(RTAPISO_CXXSRCS)) : EXTRAFLAGS=-fPIC -Ihal/utils -I.

TARGETS += ../lib/python/machinekit/rtapi.so

../lib/python/machinekit/rtapi.so: \
		$(patsubst %.c,objects/%.o,$(RTAPISO_CSRCS)) \
		$(patsubst %.cc,objects/%.o,$(RTAPISO_CXXSRCS)) \
		../lib/liblinuxcnchal.so.0 \
		../lib/libmachinetalk-pb2++.so.0 \
		../lib/libmtalk.so.0
	$(ECHO) Linking $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) -g $(LDFLAGS) -shared -o $@ $^


COMPATSO_CSRCS := $(addprefix $(HALNGMKDIR)/, \
	compat.c)

$(COMPATSO_CSRCS): $(wildcard $(HALNGMKDIR)/*.pyx $(HALNGMKDIR)/*.pxd)

USERSRCS += $(COMPATSO_CSRCS)
$(call TOOBJSDEPS, $(COMPATSO_CSRCS)) : EXTRAFLAGS=-fPIC
TARGETS += ../lib/python/machinekit/compat.so
../lib/python/machinekit/compat.so: \
		$(patsubst %.c,objects/%.o,$(COMPATSO_CSRCS)) \
		../lib/liblinuxcnchal.so.0
	$(ECHO) Linking python module $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) -g $(LDFLAGS) -shared -o $@ $^


SHMCOMMONSO_CSRCS := $(addprefix $(HALNGMKDIR)/, \
	shmcommon.c ) \
	rtapi/shmdrv/shmdrvapi.c

USERSRCS += $(SHMCOMMONSO_CSRCS)
$(call TOOBJSDEPS, $(SHMCOMMONSO_CSRCS)) : EXTRAFLAGS=-fPIC -Irtapi/shmdrv
TARGETS += ../lib/python/machinekit/shmcommon.so
../lib/python/machinekit/shmcommon.so: \
		$(patsubst %.c,objects/%.o,$(SHMCOMMONSO_CSRCS))
	$(ECHO) Linking python module $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)$(CC) -g  -shared -o $@ $^ $(LDFLAGS) -lrt

$(HALNGDIR)/machinekit/%.c: $(addprefix $(HALNGDIR)/machinekit/, %.pyx %.pxd)
	$(ECHO) Cython compiling $<
	$(Q)$(CYTHON) -o $@ $<

clean: cython-clean
cython-clean:
	for f in compat hal rtapi shmcommon; do \
	    rm -f hal/cython/machinekit/$$f.c; \
	done
