HALLIBDIR := hal/lib
PBDIR := ../include/machinetalk/protobuf
INCLUDES += $(HALLIBDIR)
INCLUDES += $(PBDIR)
LIBS := ../lib

../include/%.h: ./hal/lib/%.h
	$(ECHO) Copying header file $@
	$(Q)cp $< $@

# link in basic nanonpb support routines
HALLIBSRCS := $(HALLIBDIR)/hal_lib.c \
	$(HALLIBDIR)/hal_group.c \
	$(HALLIBDIR)/hal_ring.c \
	$(HALLIBDIR)/hal_rcomp.c \
	$(HALLIBDIR)/hal_vtable.c \
	$(HALLIBDIR)/hal_funct.c \
	$(HALLIBDIR)/hal_procfs.c \
	$(HALLIBDIR)/hal_thread.c \
	$(HALLIBDIR)/hal_param.c \
	$(HALLIBDIR)/hal_signal.c \
	$(HALLIBDIR)/hal_pin.c \
	$(HALLIBDIR)/hal_comp.c \
	$(HALLIBDIR)/hal_memory.c \
	$(HALLIBDIR)/hal_misc.c \
	$(HALLIBDIR)/hal_instance.c \
	$(HALLIBDIR)/hal_object.c \
	$(HALLIBDIR)/hal_object_selectors.c \
	$(HALLIBDIR)/hal_accessor.c \
	$(HALLIBDIR)/hal_iring.c \
	rtapi/rtapi_heap.c

# protobuf support functions which depend on HAL - on RT host only
HALLIBMTALK_SRCS := $(addprefix $(HALLIBDIR)/, \
	halpb.cc)

HALLIBMTALK_CXXFLAGS := -DULAPI $(PROTOBUF_CFLAGS) $(CZMQ_CFLAGS)
HALLIBMTALK_LDFLAGS := $(PROTOBUF_LIBS) $(CZMQ_LIBS)

$(call TOOBJSDEPS, $(HALLIBMTALK_SRCS)) : EXTRAFLAGS=-fPIC $(HALLIBMTALK_CXXFLAGS)

# ULAPI: all thread-specific code now comes in through the ulapi library
# (liblinuxcnculapi.so) which autoloads the proper ulapi on demand

$(call TOOBJSDEPS, $(HALLIBSRCS)): EXTRAFLAGS += $(THREADS_RTFLAGS) -fPIC

# Add to list of sources to compile -DULAPI ->
# objects/{$(HALLIBDIR)/hal_lib.o}
USERSRCS += $(HALLIBSRCS)
USERSRCS += $(HALLIBMTALK_SRCS)

HALLIB := ../lib/liblinuxcnchal.so


# for now, link in ulapi  plus any referenced  objs directly
# cause: http://fedoraproject.org/wiki/UnderstandingDSOLinkChange

$(HALLIB).0: \
	$(call TOOBJS, $(HALLIBSRCS)) \
	$(call TOOBJS, $(ULAPI_AUTOLOAD_SRCS)) \
	$(call TOOBJS, $(SHMDRVAPISRCS)) \
	$(call TOOBJS, $(HALLIBMTALK_SRCS)) \
	$(call TOOBJS, $(LIBINISRCS)) \
	$(call TOOBJS, machinetalk/lib/syslog_async.c) \
	$(LIBS)/libmachinetalk-pb2++.so
	$(ECHO) Creating shared library $(notdir $@)
	@mkdir -p ../lib
	@rm -f $@
	$(Q)$(CC) $(LDFLAGS) -Wl,-rpath,$(EMC2_RTLIB_DIR) \
	    -Wl,-soname,$(notdir $@) -shared \
	    -o $@ $^ -lstdc++ -ldl -lrt $(PROTOBUF_LIBS) $(PROTOCXXLIB) $(CZMQ_LIBS) $(RT_LDFLAGS)

HALMODULESRCS := $(HALLIBDIR)/halmodule.cc
PYSRCS += $(HALMODULESRCS)

HALMODULE := ../lib/python/_hal.so
$(HALMODULE): $(call TOOBJS, $(HALMODULESRCS)) $(HALLIB)
	$(ECHO) Linking python module $(notdir $@)
	$(Q)$(CXX) $(LDFLAGS) -shared -o $@ $^

TARGETS += $(HALLIB) $(HALLIB).0
PYTARGETS += $(HALMODULE)


obj-m += hal_lib.o
hal_lib-objs := hal/lib/hal_lib.o $(MATHSTUB)
hal_lib-objs += hal/lib/hal_group.o
hal_lib-objs += hal/lib/hal_ring.o
hal_lib-objs += hal/lib/hal_rcomp.o
hal_lib-objs += hal/lib/hal_vtable.o
hal_lib-objs += hal/lib/hal_funct.o
hal_lib-objs += hal/lib/hal_procfs.o
hal_lib-objs += hal/lib/hal_thread.o
hal_lib-objs += hal/lib/hal_signal.o
hal_lib-objs += hal/lib/hal_pin.o
hal_lib-objs += hal/lib/hal_param.o
hal_lib-objs += hal/lib/hal_comp.o
hal_lib-objs += hal/lib/hal_memory.o
hal_lib-objs += hal/lib/hal_misc.o
hal_lib-objs += hal/lib/hal_instance.o
hal_lib-objs += hal/lib/hal_object.o
hal_lib-objs += hal/lib/hal_object_selectors.o
hal_lib-objs += hal/lib/hal_accessor.o
hal_lib-objs += hal/lib/hal_iring.o

$(RTLIBDIR)/hal_lib$(MODULE_EXT): $(addprefix $(OBJDIR)/,$(hal_lib-objs))
