COMPEL_BIN		:= ./compel/compel-host
export COMPEL_BIN

COMPEL_VERSION_HEADER	:= compel/include/version.h

$(COMPEL_VERSION_HEADER): 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)

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

compel-deps		+= compel/include/asm
compel-deps		+= $(COMPEL_VERSION_HEADER)
compel-deps		+= $(CONFIG_HEADER)
compel-deps		+= include/common/asm
compel-plugins		+= compel/plugins/std.lib.a compel/plugins/fds.lib.a

LIBCOMPEL_SO		:= libcompel.so
LIBCOMPEL_A		:= libcompel.a
export LIBCOMPEL_SO LIBCOMPEL_A
criu-deps		+= compel/$(LIBCOMPEL_A)

#
# Compel itself.
compel/Makefile: ;
compel/%: $(compel-deps) $(compel-plugins) .FORCE
	$(Q) $(MAKE) $(build)=compel $@

criu-deps		+= compel/compel-host-bin

#
# Plugins
compel/plugins/Makefile: ;
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.lib.a: $(compel-deps) .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

compel/plugins/shmem.lib.a: $(compel-deps) compel/plugins/std.lib.a .FORCE
	$(Q) $(MAKE) $(build)=compel/plugins $@

compel/plugins/fds.lib.a: $(compel-deps) compel/plugins/std.lib.a .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_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)

compel-install-targets	+= compel/$(LIBCOMPEL_SO)
compel-install-targets	+= compel/compel
compel-install-targets	+= $(compel-plugins)
