
srcdir	= .
include	../Makefile.common

DEFSNOARCH	:= -I$(srcdir)/../include -D_GNU_SOURCE -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DCONFIG_ROOT_CHECK_OFF=0  $(EXTRA_DEFS)
DEFS		:= -DELF_MACHINE_H='"elf_$(ARCH).h"' -DARCH_$(ARCH) -DTAINT_URL='"$(TAINT_URL)"'

PROGS		:= insmod

# COMB is the list of utilities to combine with insmod into one executable
COMB		:= 
COMBDEFS	:= $(addprefix -DCOMBINE_, $(COMB))
COMB_STATIC	:= $(addsuffix .static, $(COMB))

TARGETS_REAL	:= $(filter-out $(COMB),$(PROGS))
TARGETS	:= $(PROGS)

ifeq ($(insmod_static),yes)
TARGETS_REAL	+= insmod.static
TARGETS		+= insmod.static $(COMB_STATIC)
endif

INSMODOBJS	:= insmod.o
MODINFOOBJS	:= modinfo.o
ifeq (kallsyms,kallsyms)
KALLSYMSOBJS	:=
else
KALLSYMSOBJS	:= kallsyms.o
endif

# arch specific definitions for common 32/64 code

ifeq (no,yes)
DEFS64		:= -DELF_MACHINE_H='"elf_sparc64.h"' -DARCH_sparc64
endif

ifeq (no,yes)
DEFS64		:= -DELF_MACHINE_H='"elf_hppa64.h"' -DARCH_hppa64
endif

ifeq (no,yes)
DEFS64		:= -DELF_MACHINE_H='"elf_ppc64.h"' -DARCH_ppc64
endif

ifeq (no,yes)
DEFS64		:= -DELF_MACHINE_H='"elf_s390x.h"' -DARCH_s390x
endif

ifeq (no,yes)
DEFS64		:= -DELF_MACHINE_H='"elf_x86_64.h"' -DARCH_x86_64
endif

# arch independent definitions for common 32/64 code

ifdef DEFS64
INSMODOBJS	+= insmod_64.o
ifeq (kallsyms,kallsyms)
INSMODOBJS	+= kallsyms_64.o
else
KALLSYMSOBJS	+= kallsyms_64.o
endif
MODINFOOBJS	+= modinfo_64.o
DEFSNOARCH	+= -DCOMMON_3264
DEFS		+= -DONLY_32
DEFS64		+= -DONLY_64 $(call check_gcc,-malign-double,)
DEFS64		+= -DTAINT_URL='"$(TAINT_URL)"'
endif

INSMODOBJS	+= $(addsuffix .o, $(COMB))

ifdef DEFS64
TARGETS+=libmodutils.a($(filter-out insmod.o insmod_64.o, $(INSMODOBJS)) combined_insmod.o combined_insmod_64.o)
else
TARGETS+=libmodutils.a($(filter-out insmod.o insmod_64.o, $(INSMODOBJS)) combined_insmod.o)
endif

combined_insmod.o: insmod.c
	$(CC) $(CFLAGS) $(DEFSNOARCH) $(DEFS) $(COMBDEFS) -Dmain=combined_insmod_main -c -o $@ $<

combined_insmod_64.o: insmod.c
	$(CC) $(CFLAGS) $(DEFSNOARCH) $(DEFS64) $(COMBDEFS) -Dmain=combined_insmod_main -c -o $@ $<


#=====================================================================

# Rule for building "to-be-combined" modutils object files
%.o: %.c
	$(CC) $(CFLAGS) $(DEFSNOARCH) $(DEFS) $(COMBDEFS) -c -o $@ $<

# Rule for building 64 bit versions, only used when building common code
# for 32 and 64 bit systems.
%_64.o: %.c
	$(CC) $(CFLAGS) $(DEFSNOARCH) $(DEFS64) $(COMBDEFS) -c -o $@ $<

# Rule for building "normal" modutils executables (non-combined)
%: %.c ../obj/libobj.a ../util/libutil.a
	$(CC) $(CFLAGS) $(DEFSNOARCH) $(DEFS) -o $@ $^ $(LDFLAGS) $(LIBS) $(ZLIB_STATIC)

#=====================================================================

all: .depend $(TARGETS)

insmod: $(INSMODOBJS) ../obj/libobj.a ../util/libutil.a
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(ZLIB_STATIC)

insmod.static: $(INSMODOBJS) ../obj/libobj.a ../util/libutil.a
	$(CC) -static $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(ZLIB)

$(COMB): insmod
	ln -sf $< $@

$(COMB_STATIC): insmod.static
	ln -sf $< $@

ifneq (kallsyms,kallsyms)
kallsyms: $(KALLSYMSOBJS) ../obj/libobj.a ../util/libutil.a
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(ZLIB_STATIC)
endif

modinfo: $(MODINFOOBJS) ../obj/libobj.a ../util/libutil.a
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(ZLIB_STATIC)

install install-bin: all
	$(INSTALL) -d $(DESTDIR)$(sbindir)
	@set -ex;\
	for i in $(TARGETS_REAL); do \
	$(INSTALL) $$i $(DESTDIR)$(sbindir); \
	$(STRIP) $(DESTDIR)$(sbindir)/$$i;done;

clean:
	rm -f *.o $(PROGS) *.static libmodutils.a

realclean: clean
	rm -f .depend

distclean: realclean
	rm -f Makefile

dep depend .depend: $(addsuffix .c, $(PROGS))
	$(CC) -M $(CFLAGS) $(DEFSNOARCH) $(DEFS) $^ > .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
