COMPEL_VERSION_HEADER	:= compel/include/version.h

$(COMPEL_VERSION_HEADER): $(SRC_DIR)/Makefile.versions
	$(call msg-gen, $(COMPEL_VERSION_HEADER))
	$(Q) echo "/* Autogenerated, do not edit */"	 			 	 > $(COMPEL_VERSION_HEADER)
	$(Q) echo "#ifndef COMPEL_SO_VERSION_H__"					>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#define COMPEL_SO_VERSION_H__"					>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#define COMPEL_SO_VERSION \"$(COMPEL_SO_VERSION)\""			>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#define COMPEL_SO_VERSION_MAJOR " $(COMPEL_SO_VERSION_MAJOR)		>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#define COMPEL_SO_VERSION_MINOR " $(COMPEL_SO_VERSION_MINOR)		>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#define COMPEL_SO_VERSION_SUBLEVEL " $(COMPEL_SO_VERSION_SUBLEVEL)	>> $(COMPEL_VERSION_HEADER)
	$(Q) echo "#endif /* COMPEL_SO_VERSION_H__ */"					>> $(COMPEL_VERSION_HEADER)

$(SRC_DIR)/compel/include/uapi/compel: $(SRC_DIR)/compel/include/uapi
	$(call msg-gen, $@)
	$(Q) ln -s $^ $@
$(COMPEL_VERSION_HEADER): $(SRC_DIR)/compel/include/uapi/compel

$(SRC_DIR)/compel/include/uapi/asm: $(SRC_DIR)/compel/arch/$(ARCH)/src/lib/include/uapi/asm
	$(call msg-gen, $@)
	$(Q) ln -s $^ $@
$(COMPEL_VERSION_HEADER): $(SRC_DIR)/compel/include/uapi/asm

$(SRC_DIR)/compel/include/asm: $(SRC_DIR)/compel/arch/$(ARCH)/src/lib/include
	$(call msg-gen, $@)
	$(Q) ln -s $^ $@
$(COMPEL_VERSION_HEADER): $(SRC_DIR)/compel/include/asm

compel-uapi-links	+= $(SRC_DIR)/compel/include/uapi/compel
compel-uapi-links	+= $(SRC_DIR)/compel/include/uapi/asm
compel-uapi-links	+= $(SRC_DIR)/compel/include/asm

compel-deps		+= $(compel-uapi-links)
compel-deps		+= $(COMPEL_VERSION_HEADER)
compel-deps		+= $(CONFIG_HEADER)
compel-deps		+= include/common/asm

#
# Compel itself.
compel/%: $(compel-deps) compel/plugins/std.built-in.o .FORCE
	$(Q) $(MAKE) $(build)=compel $@

#
# Plugins
compel/plugins/%: $(compel-deps) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

#
# GNU make 4.x supports targets matching via wide
# match targeting, where GNU make 3.x series (used on
# Travis) is not, so we have to write them here explicitly.
compel/plugins/std.built-in.o: $(compel-deps) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

compel/plugins/shmem.built-in.o: $(compel-deps) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

compel/plugins/fds.built-in.o: $(compel-deps) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

compel/compel: compel/built-in.o compel/libcompel.a | $(compel-deps)
	$(call msg-link, $@)
	$(Q) $(CC) $(CFLAGS) $^ $(WRAPFLAGS) $(LDFLAGS) -rdynamic -o $@

#
# And compel library.
LIBCOMPEL_SO		:= libcompel.so
LIBCOMPEL_SO_CFLAGS	+= $(CFLAGS) -rdynamic -Wl,-soname,$(LIBCOMPEL_SO).$(COMPEL_SO_VERSION_MAJOR)
compel/$(LIBCOMPEL_SO): compel/libcompel.a
	$(call msg-link, $@)
	$(Q) $(CC) -shared $(LIBCOMPEL_SO_CFLAGS) -o $@ -Wl,--whole-archive $^ -Wl,--no-whole-archive $(LDFLAGS)
