Makefile
author Mathieu Lacage <mathieu.lacage@inria.fr>
Wed, 18 May 2011 00:02:42 +0200
changeset 45 532107bb1271
parent 38 2ec6ad583c2e
child 46 e24d020aa31d
permissions -rw-r--r--
support objdir != srcdir

SRCDIR=$(dir $(firstword $(MAKEFILE_LIST)))
INSTALL_DIR=/usr
KCONFIG_CONFIG?=config
VPATH=$(SRCDIR)
KERNEL_DIR=net-next-2.6
KERNEL_VERSION=fed66381d65a35198639f564365e61a7f256bf79

SIM_SRC=\
fs.c random.c sim.c softirq.c time.c \
glue.c sim-device.c sysctl.c timer.c \
hrtimer.c sched.c sim-socket.c sysfs.c workqueue.c \
print.c security.c slab.c tasklet.c \
proc.c seq.c socket.c tasklet-hrtimer.c \
cred.c pid.c modules.c


OPT?=no
opt_yes=-O3 -fomit-frame-pointer 
opt_no=-O0
PIC?=yes
pic_yes=-fpic -DPIC
pic_no=-mcmodel=large
PIC_CFLAGS=$(pic_$(PIC))
KERNEL_LIB=lib$(KERNEL_DIR).so
SIM_OBJ=$(addprefix sim/,$(addsuffix .o,$(basename $(SIM_SRC))))
CFLAGS+= \
 $(opt_$(OPT)) -g3 -D__KERNEL__ -Wall -Wstrict-prototypes -Wno-trigraphs \
 -fno-inline -iwithprefix $(SRCDIR)$(KERNEL_DIR)/include -DKBUILD_BASENAME=\"clnt\" \
 -fno-strict-aliasing -fno-common -fno-delete-null-pointer-checks \
 -fno-stack-protector \
 -DKBUILD_MODNAME=\"nsc\" -DMODVERSIONS -DEXPORT_SYMTAB \
 -include autoconf.h \
 -U__FreeBSD__ -D__linux__=1 -Dlinux=1 -D__linux=1 \
 -I$(SRCDIR)sim/include -I$(SRCDIR)$(KERNEL_DIR)/include \
 $(PIC_CFLAGS) -D_DEBUG -I$(PWD)

include $(SRCDIR)processor.mk

ifeq ($(PROCESSOR_SIZE),64)
CFLAGS+= -DCONFIG_64BIT
endif

LDFLAGS += -shared -nostdlib -g3 -Wl,-O1 -Wl,-Tlinker.lds 

modules:=
all-obj-for-clean:=

all: library modules

-include objs.mk

AUTOCONF=autoconf.h
# note: the directory order below matters to ensure that we match the kernel order
dirs=kernel mm crypto lib drivers/base drivers/net net
empty:=
space:= $(empty) $(empty)
colon:= :
comma= ,
kernel_to_keep=notifier.o params.o time.o sysctl.o mutex.o \
rwsem.o semaphore.o srcu.o rcupdate.o rcutiny.o kfifo.o
mm_to_keep=util.o
crypto_to_keep=aead.o ahash.o shash.o api.o algapi.o cipher.o compress.o proc.o
drivers/base_to_keep=class.o core.o bus.o dd.o driver.o devres.o module.o
drivers/net_to_keep=loopback.o
lib_to_keep=klist.o kobject.o kref.o hweight.o int_sqrt.o checksum.o \
find_last_bit.o find_next_bit.o bitmap.o nlattr.o idr.o libcrc32c.o \
ctype.o string.o kasprintf.o rbtree.o sha1.o textsearch.o vsprintf.o \
rwsem-spinlock.o scatterlist.o ratelimit.o hexdump.o dec_and_lock.o \
div64.o

objs.mk: Makefile.print timeconst.h
	-for i in 1; do \
	$(foreach d,$(dirs), \
           $(MAKE) -s -f $< srcdir=$(SRCDIR)$(KERNEL_DIR)/$(d) \
	    objdir=$(KERNEL_DIR)/$(d) \
            config=$(PWD)/$(KCONFIG_CONFIG) \
	    to_keep=$(subst $(space),$(colon),$($(d)_to_keep)) print;) \
	done > $@
$(KCONFIG_CONFIG):
	make -C $(SRCDIR)$(KERNEL_DIR) ARCH=sim KCONFIG_CONFIG=$(PWD)/$(KCONFIG_CONFIG) defconfig 
$(AUTOCONF): generate-autoconf.py $(KCONFIG_CONFIG)
	$^ > $@
timeconst.h: $(KCONFIG_CONFIG)
	perl $(SRCDIR)$(KERNEL_DIR)/kernel/timeconst.pl $(shell grep CONFIG_HZ= $(KCONFIG_CONFIG) | sed -e 's/CONFIG_HZ=\(\w\)/\1/g') > $@
linker.lds: ./generate-linker-script.py
	ld -shared --verbose | ./$^ > $@

KERNEL_BUILTIN=$(addprefix $(KERNEL_DIR)/,$(addsuffix /builtin.o,$(dirs)))
OBJS=$(SIM_OBJ) $(foreach builtin,$(KERNEL_BUILTIN),$(if $($(builtin)),$($(builtin))))

%/builtin.o:
	mkdir -p $(dir $@); if test -n "$($@)"; then for f in $($@); do $(AR) Tcru $@ $$f; done; else $(AR) Tcru $@; fi
%.ko:%.o
	$(CC) -shared -o $@ -nostdlib $^
%.o:%.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $^ -o $@

library: $(KERNEL_LIB)
modules: $(modules) 

install: modules library
	mkdir -p $(INSTALL_DIR)
	mkdir -p $(INSTALL_DIR)/lib
	mkdir -p $(INSTALL_DIR)/include
	mkdir -p $(INSTALL_DIR)/include/sim
	mkdir -p $(INSTALL_DIR)/lib/$(KERNEL_DIR)
	cp $(KERNEL_LIB) $(INSTALL_DIR)/lib
	for mod in $(modules); do \
	  TMP=`dirname $$mod |sed -e 's;^[^/]\+/;;'`; \
	  mkdir -p $(INSTALL_DIR)/lib/$(KERNEL_DIR)/$$TMP; \
	  cp $$mod $(INSTALL_DIR)/lib/$(KERNEL_DIR)/$$TMP/`basename $$mod`; \
	done
	for h in sim-init.h sim-types.h sim.h; do \
	  cp sim/include/$$h $(INSTALL_DIR)/include/sim/$$h; \
	done

install-dir:

setup: .patch.ts $(KERNEL_DIR)/arch/sim
$(KERNEL_DIR)/arch/sim:
	if test ! -L $@; then ln -s $(PWD)/sim $(KERNEL_DIR)/arch/sim; fi
.patch.ts: kernel.patch
	CWD=`pwd` && cd $(KERNEL_DIR) && patch -p1 < $$CWD/kernel.patch && cd $$CWD && touch .patch.ts
unpatch: 
	CWD=`pwd` && cd $(KERNEL_DIR) && git reset --hard $(KERNEL_VERSION) && cd $$CWD && rm -f .patch.ts
ALL_OBJS=$(OBJS) $(KERNEL_LIB) $(modules) $(all-obj-for-clean)
clean:
	@for f in $(foreach m,$(modules),$($(m))); do rm -f $$f; done
	@for f in $(ALL_OBJS); do rm -f $$f; done
	@rm -rf $(INSTALL_DIR)  linker.lds $(AUTOCONF) objs.mk 2>/dev/null

$(KERNEL_LIB): $(AUTOCONF) objs.mk $(OBJS) linker.lds
	$(CC) $(LDFLAGS) -Wl,--whole-archive $(OBJS) -o $@

defconfig:
	make -C $(KERNEL_DIR) ARCH=sim defconfig
allmodconfig:
	make -C $(KERNEL_DIR) ARCH=sim allmodconfig
menuconfig:
	$(MAKE) -C $(KERNEL_DIR) ARCH=sim menuconfig