|
| 1 | +# Klipper build system |
| 2 | +# |
| 3 | +# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net> |
| 4 | +# |
| 5 | +# This file may be distributed under the terms of the GNU GPLv3 license. |
| 6 | + |
| 7 | +# Output directory |
| 8 | +OUT=out/ |
| 9 | + |
| 10 | +# Kconfig includes |
| 11 | +export HOSTCC := $(CC) |
| 12 | +export CONFIG_SHELL := sh |
| 13 | +export KCONFIG_AUTOHEADER := autoconf.h |
| 14 | +export KCONFIG_CONFIG := $(CURDIR)/.config |
| 15 | +-include $(KCONFIG_CONFIG) |
| 16 | + |
| 17 | +# Common command definitions |
| 18 | +CC=$(CROSS_PREFIX)gcc |
| 19 | +AS=$(CROSS_PREFIX)as |
| 20 | +LD=$(CROSS_PREFIX)ld |
| 21 | +OBJCOPY=$(CROSS_PREFIX)objcopy |
| 22 | +OBJDUMP=$(CROSS_PREFIX)objdump |
| 23 | +STRIP=$(CROSS_PREFIX)strip |
| 24 | +CPP=cpp |
| 25 | +PYTHON=python |
| 26 | + |
| 27 | +# Source files |
| 28 | +src-y=sched.c command.c stepper.c basecmd.c gpiocmds.c spicmds.c endstop.c |
| 29 | +DIRS=src src/avr src/simulator |
| 30 | + |
| 31 | +# Default compiler flags |
| 32 | +cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \ |
| 33 | + ; then echo "$(2)"; else echo "$(3)"; fi ;) |
| 34 | + |
| 35 | +CFLAGS-y := -I$(OUT) -Isrc -Os -MD -g \ |
| 36 | + -Wall -Wold-style-definition $(call cc-option,$(CC),-Wtype-limits,) \ |
| 37 | + -ffunction-sections -fdata-sections |
| 38 | +CFLAGS-y += -flto -fwhole-program |
| 39 | + |
| 40 | +LDFLAGS-y := -Wl,--gc-sections |
| 41 | + |
| 42 | +CPPFLAGS = -P -MD -MT $@ |
| 43 | + |
| 44 | +CFLAGS = $(CFLAGS-y) |
| 45 | +LDFLAGS = $(LDFLAGS-y) |
| 46 | + |
| 47 | +# Default targets |
| 48 | +target-y := $(OUT)klipper.elf |
| 49 | + |
| 50 | +all: |
| 51 | + |
| 52 | +# Run with "make V=1" to see the actual compile commands |
| 53 | +ifdef V |
| 54 | +Q= |
| 55 | +else |
| 56 | +Q=@ |
| 57 | +MAKEFLAGS += --no-print-directory |
| 58 | +endif |
| 59 | + |
| 60 | +# Include board specific makefile |
| 61 | +-include src/$(patsubst "%",%,$(CONFIG_BOARD_DIRECTORY))/Makefile |
| 62 | + |
| 63 | +################ Common build rules |
| 64 | + |
| 65 | +$(OUT)%.o: %.c $(OUT)autoconf.h $(OUT)board-link |
| 66 | +@echo " Compiling $@" |
| 67 | +$(Q)$(CC) $(CFLAGS) -c $< -o $@ |
| 68 | + |
| 69 | +################ Main build rules |
| 70 | + |
| 71 | +$(OUT)board-link: $(KCONFIG_CONFIG) |
| 72 | +@echo " Creating symbolic link $(OUT)board" |
| 73 | +$(Q)touch $@ |
| 74 | +$(Q)ln -Tsf $(PWD)/src/$(CONFIG_BOARD_DIRECTORY) $(OUT)board |
| 75 | + |
| 76 | +$(OUT)declfunc.lds: src/declfunc.lds.S |
| 77 | +@echo " Precompiling $@" |
| 78 | +$(Q)$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $@ |
| 79 | + |
| 80 | +$(OUT)klipper.o: $(patsubst %.c, $(OUT)src/%.o,$(src-y)) $(OUT)declfunc.lds |
| 81 | +@echo " Linking $@" |
| 82 | +$(Q)$(CC) $(CFLAGS) -Wl,-r -Wl,-T,$(OUT)declfunc.lds -nostdlib $(patsubst %.c, $(OUT)src/%.o,$(src-y)) -o $@ |
| 83 | + |
| 84 | +$(OUT)compile_time_request.o: $(OUT)klipper.o ./scripts/buildcommands.py |
| 85 | +@echo " Building $@" |
| 86 | +$(Q)$(OBJCOPY) -j '.compile_time_request' -O binary $< $(OUT)klipper.o.compile_time_request |
| 87 | +$(Q)$(PYTHON) ./scripts/buildcommands.py $(OUT)klipper.o.compile_time_request $(OUT)autoconf.h $(OUT)compile_time_request.c |
| 88 | +$(Q)$(CC) $(CFLAGS) -c $(OUT)compile_time_request.c -o $@ |
| 89 | + |
| 90 | +$(OUT)klipper.elf: $(OUT)klipper.o $(OUT)compile_time_request.o |
| 91 | +@echo " Linking $@" |
| 92 | +$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ |
| 93 | + |
| 94 | +################ Kconfig rules |
| 95 | + |
| 96 | +define do-kconfig |
| 97 | +$(Q)mkdir -p $(OUT)/scripts/kconfig/lxdialog |
| 98 | +$(Q)mkdir -p $(OUT)/include/config |
| 99 | +$(Q)mkdir -p $(addprefix $(OUT), $(DIRS)) |
| 100 | +$(Q)$(MAKE) -C $(OUT) -f $(CURDIR)/scripts/kconfig/Makefile srctree=$(CURDIR) src=scripts/kconfig obj=scripts/kconfig Q=$(Q) Kconfig=$(CURDIR)/src/Kconfig $1 |
| 101 | +endef |
| 102 | + |
| 103 | +$(OUT)autoconf.h : $(KCONFIG_CONFIG) ; $(call do-kconfig, silentoldconfig) |
| 104 | +$(KCONFIG_CONFIG): src/Kconfig ; $(call do-kconfig, olddefconfig) |
| 105 | +%onfig: ; $(call do-kconfig, $@) |
| 106 | +help: ; $(call do-kconfig, $@) |
| 107 | + |
| 108 | + |
| 109 | +################ Generic rules |
| 110 | + |
| 111 | +# Make definitions |
| 112 | +.PHONY : all clean distclean FORCE |
| 113 | +.DELETE_ON_ERROR: |
| 114 | + |
| 115 | +all: $(target-y) |
| 116 | + |
| 117 | +clean: |
| 118 | +$(Q)rm -rf $(OUT) |
| 119 | + |
| 120 | +distclean: clean |
| 121 | +$(Q)rm -f .config .config.old |
| 122 | + |
| 123 | +-include $(patsubst %,$(OUT)%/*.d,$(DIRS)) |
0 commit comments