HALCOMP_SUBMAKEFILE= hal/components/Submakefile
# for debugging this file - to prevent recompilation of
# everything under the sun with every small change
HALCOMP_SUBMAKEFILE= hal/components/Submakefile
# for debugging this file - to prevent recompilation of
# everything under the sun with every small change
#HALCOMP_SUBMAKEFILE=

ifeq ($(BUILD_KBUILD),yes)
COMPS := $(patsubst $(BASEPWD)/%,%,$(wildcard \
	$(BASEPWD)/hal/components/*.comp $(BASEPWD)/hal/drivers/*.comp \
	$(BASEPWD)/machinetalk/msgcomponents/*.comp))
include $(patsubst %.comp, $(BASEPWD)/halcomp-srcs/%.mak, $(COMPS))
else
COMPS := $(sort $(wildcard hal/components/*.comp))

USER_COMP_COMPS := $(sort $(wildcard hal/user_comps/*.comp))

COMP_DOCPAGES := $(patsubst hal/components/%.comp, ../man/man9/%.asciidoc, \
	$(COMPS))

USER_COMP_DOCPAGES := $(patsubst hal/user_comps/%.comp, ../man/man9/%.asciidoc, \
	$(USER_COMP_COMPS))

MSG_COMPS := $(wildcard machinetalk/msgcomponents/*.comp)

MSG_COMP_DOCPAGES := $(patsubst machinetalk/msgcomponents/%.comp, \
	../man/man9/%.asciidoc, $(MSG_COMPS))

DRIVER_COMPS := $(wildcard hal/drivers/*.comp)

DRIVER_COMP_DOCPAGES := $(patsubst hal/drivers/%.comp, ../man/man9/%.asciidoc, \
	$(COMP_DRIVERS))

docpages: $(COMP_DOCPAGES) $(DRIVER_COMP_DOCPAGES) $(MSG_COMP_DOCPAGES) $(USER_COMP_DOCPAGES)

ifeq ($(BUILD_DOCS),yes)
    TARGETS += docpages
    .PHONY: docpages
endif

ifeq ($(TRIVIAL_BUILD)+$(BUILD_THREAD_MODULES),no+yes)
-include $(patsubst %.comp, $(BASEPWD)/halcomp-srcs/%.mak,\
	$(COMPS) $(DRIVER_COMPS) $(MSG_COMPS))
endif # TRIVIAL_BUILD == no
endif # KERNELRELEASE != ''

ifeq ($(BUILD_THREAD_MODULES),yes)
obj-m += $(patsubst hal/drivers/%.comp, %.o, \
	$(patsubst hal/components/%.comp, %.o, \
	$(patsubst machinetalk/msgcomponents/%.comp, %.o, \
	$(COMPS) $(DRIVER_COMPS) $(MSG_COMPS))))
endif

$(COMP_DOCPAGES): ../man/man9/%.asciidoc: hal/components/%.comp  ../bin/comp
	$(ECHO) Making comp docpage $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)../bin/comp --document \
	--frontmatter="edit-repo: $(EDIT_REPO)" 	\
	--frontmatter="edit-branch: $(EDIT_BRANCH)" 	\
	-o $@ $<

$(USER_COMP_DOCPAGES): ../man/man9/%.asciidoc: hal/user_comps/%.comp  ../bin/comp
	$(ECHO) Making comp docpage $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)../bin/comp --document \
	--frontmatter="edit-repo: $(EDIT_REPO)" 	\
	--frontmatter="edit-branch: $(EDIT_BRANCH)" 	\
	-o $@ $<

$(DRIVER_COMP_DOCPAGES): ../man/man9/%.asciidoc: hal/drivers/%.comp ../bin/comp
	$(ECHO) Making driver comp docpage $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)../bin/comp --document  \
	--frontmatter="edit-repo: $(EDIT_REPO)" 	\
	--frontmatter="edit-branch: $(EDIT_BRANCH)" 	\
	-o $@ $<

$(MSG_COMP_DOCPAGES): ../man/man9/%.asciidoc: \
		machinetalk/msgcomponents/%.comp ../bin/comp
	$(ECHO) Making message comp docpage $(notdir $@)
	@mkdir -p $(dir $@)
	$(Q)../bin/comp --document  \
	--frontmatter="edit-repo: $(EDIT_REPO)" 	\
	--frontmatter="edit-branch: $(EDIT_BRANCH)" 	\
	-o $@ $<

ifeq ($(BUILD_ALL_FLAVORS),yes)
# .c and .mak files are identical for all flavors
#
# in the top-level make, build them once in a common location
halcomp-srcs/%.c: %.comp ../bin/comp
	$(ECHO) "Preprocessing $(notdir $<)"
	@mkdir -p $(dir $@)
	$(Q)../bin/comp --require-license -o $@ $<

halcomp-srcs/%.mak: %.comp $(HALCOMP_SUBMAKEFILE)
	$(ECHO) "Generating comp build rules for $(notdir $<)"
	@mkdir -p $(dir $@)
	$(Q)echo "$(notdir $*)-objs := halcomp-srcs/$*.o" > $@.tmp
	$(Q)echo "\$$(RTLIBDIR)/$(notdir $*)\$$(MODULE_EXT): \\" >> $@.tmp
	$(Q)echo "    \$$(OBJDIR)/halcomp-srcs/$*.o" >> $@.tmp
	$(Q)mv -f $@.tmp $@

# Generate .c and .mak files before the modules target
modules: $(patsubst %.comp, halcomp-srcs/%.c, \
	$(COMPS) $(DRIVER_COMPS) $(MSG_COMPS))
modules: $(patsubst %.comp, halcomp-srcs/%.mak, \
	$(COMPS) $(DRIVER_COMPS) $(MSG_COMPS))
endif # BUILD_ALL_FLAVORS == yes

# ifeq ($(BUILD_THREAD_MODULES),yes)
# # to build flavors separately, source files need to be linked
# # are these still neededby kthreads build?
# # are they used at all?
# HALCOMP_DIR_STAMPS := \
# 	halcomp-srcs/hal/components/.dir-stamp \
# 	halcomp-srcs/hal/drivers/.dir-stamp
# $(HALCOMP_DIR_STAMPS):
# 	@mkdir -p $(dir $@) && touch $@

# ifeq ($(BUILD_KBUILD),no)
# # do we still need this?  These don't seem to be usedin userland threads
# $(OBJDIR)/halcomp-srcs/hal/%.c: halcomp-srcs/hal/%.c \
# 		$(patsubst %,$(OBJDIR)/%,$(HALCOMP_DIR_STAMPS))
# 	cp $< $@
# else
# #do we need this?  This shouldbe taken care of by the wholesale copy operation
# halcomp-srcs/hal/%.c: $(BASEPWD)/halcomp-srcs/hal/%.c $(HALCOMP_DIR_STAMPS)
# 	cp $< $@
# endif
# endif

HALSTREAMERSRCS := hal/components/streamer_usr.c
USERSRCS += $(HALSTREAMERSRCS)

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

HALSAMPLERSRCS := hal/components/sampler_usr.c
USERSRCS += $(HALSAMPLERSRCS)

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

# build instructions for the delayline module
obj-m += delayline.o
# the list of parts
delayline-objs := hal/components/delayline.o $(MATHSTUB)

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