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 itself.
compel/%: $(COMPEL_VERSION_HEADER) $(CONFIG_HEADER) .FORCE
	$(Q) $(MAKE) $(build)=compel $@

#
# Plugins
compel/plugins/%: $(COMPEL_VERSION_HEADER) $(CONFIG_HEADER) .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_VERSION_HEADER) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

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

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

compel/compel: compel/built-in.o compel/lib.a $(COMPEL_VERSION_HEADER)
	$(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/lib.a
	$(call msg-link, $@)
	$(Q) $(CC) -shared $(LIBCOMPEL_SO_CFLAGS) -o $@ -Wl,--whole-archive $^ -Wl,--no-whole-archive $(LDFLAGS)
