diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
commit | 2f0649f6fe411d7e07c8d56cf8ea56db53536da8 (patch) | |
tree | 778611fb52176dce1ad06c68e87b2cb348ca0f7b /scripts | |
parent | Initial commit. (diff) | |
download | klibc-2f0649f6fe411d7e07c8d56cf8ea56db53536da8.tar.xz klibc-2f0649f6fe411d7e07c8d56cf8ea56db53536da8.zip |
Adding upstream version 2.0.13.upstream/2.0.13upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | scripts/Kbuild.include | 282 | ||||
-rw-r--r-- | scripts/Kbuild.install | 134 | ||||
-rw-r--r-- | scripts/Kbuild.klibc | 440 | ||||
-rw-r--r-- | scripts/Kbuild.klibc.include | 11 | ||||
-rw-r--r-- | scripts/Makefile.clean | 96 | ||||
-rw-r--r-- | scripts/Makefile.host | 160 | ||||
-rw-r--r-- | scripts/basic/.gitignore | 1 | ||||
-rw-r--r-- | scripts/basic/Kbuild | 6 | ||||
-rw-r--r-- | scripts/basic/fixdep.c | 390 |
9 files changed, 1520 insertions, 0 deletions
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include new file mode 100644 index 0000000..5604b3e --- /dev/null +++ b/scripts/Kbuild.include @@ -0,0 +1,282 @@ +#### +# kbuild: Generic definitions + +# Convenient variables +comma := , +squote := ' +empty := +space := $(empty) $(empty) + +### +# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o +dot-target = $(dir $@).$(notdir $@) + +### +# The temporary file to save gcc -MD generated dependencies must not +# contain a comma +depfile = $(subst $(comma),_,$(dot-target).d) + +### +# filename of target with directory and extension stripped +basetarget = $(basename $(notdir $@)) + +### +# filename of first prerequisite with directory and extension stripped +baseprereq = $(basename $(notdir $<)) + +### +# Escape single quote for use in echo statements +escsq = $(subst $(squote),'\$(squote)',$1) + +### +# Easy method for doing a status message + kecho := : + quiet_kecho := echo +silent_kecho := : +kecho := $($(quiet)kecho) + +### +# filechk is used to check if the content of a generated file is updated. +# Sample usage: +# define filechk_sample +# echo $KERNELRELEASE +# endef +# version.h : Makefile +# $(call filechk,sample) +# The rule defined shall write to stdout the content of the new file. +# The existing file will be compared with the new one. +# - If no file exist it is created +# - If the content differ the new file is used +# - If they are equal no change, and no timestamp update +# - stdin is piped in from the first prerequisite ($<) so one has +# to specify a valid file as first prerequisite (often the kbuild file) +define filechk + $(Q)set -e; \ + $(kecho) ' CHK $@'; \ + mkdir -p $(dir $@); \ + $(filechk_$(1)) < $< > $@.tmp; \ + if [ -r $@ ] && cmp -s $@ $@.tmp; then \ + rm -f $@.tmp; \ + else \ + $(kecho) ' UPD $@'; \ + mv -f $@.tmp $@; \ + fi +endef + +###### +# gcc support functions +# See documentation in Documentation/kbuild/makefiles.txt + +# cc-cross-prefix +# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-) +# Return first prefix where a prefix$(CC) is found in PATH. +# If no $(CC) found in PATH with listed prefixes return nothing +cc-cross-prefix = \ + $(word 1, $(foreach c,$(1), \ + $(shell set -e; \ + if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \ + echo $(c); \ + fi))) + +# output directory for tests below +TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/) + +# try-run +# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) +# Exit code chooses option. "$$TMP" is can be used as temporary file and +# is automatically cleaned up. +try-run = $(shell set -e; \ + TMP="$(TMPOUT).$$$$.tmp"; \ + TMPO="$(TMPOUT).$$$$.o"; \ + if ($(1)) >/dev/null 2>&1; \ + then echo "$(2)"; \ + else echo "$(3)"; \ + fi; \ + rm -f "$$TMP" "$$TMPO") + +# as-option +# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,) + +as-option = $(call try-run,\ + $(CC) $(KBUILD_CFLAGS) $(1) -c -xassembler /dev/null -o "$$TMP",$(1),$(2)) + +# as-instr +# Usage: cflags-y += $(call as-instr,instr,option1,option2) + +as-instr = $(call try-run,\ + printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -xassembler -o "$$TMP" -,$(2),$(3)) + +# cc-option +# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) + +cc-option = $(call try-run,\ + $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -Werror -c -xc /dev/null -o "$$TMP",$(1),$(2)) + +# cc-option-yn +# Usage: flag := $(call cc-option-yn,-march=winchip-c6) +cc-option-yn = $(call try-run,\ + $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -Werror -c -xc /dev/null -o "$$TMP",y,n) + +# cc-option-align +# Prefix align with either -falign or -malign +cc-option-align = $(subst -functions=0,,\ + $(call cc-option,-falign-functions=0,-malign-functions=0)) + +# cc-disable-warning +# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable) +cc-disable-warning = $(call try-run,\ + $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1))) + +# cc-name +# Expands to either gcc or clang +cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc) + +# cc-version +# Usage gcc-ver := $(call cc-version) +cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) + +# cc-fullversion +# Usage gcc-ver := $(call cc-fullversion) +cc-fullversion = $(shell $(CONFIG_SHELL) \ + $(srctree)/scripts/gcc-version.sh -p $(CC)) + +# cc-ifversion +# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) +cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3)) + +# cc-ldoption +# Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both) +cc-ldoption = $(call try-run,\ + $(CC) $(1) -nostdlib -xc /dev/null -o "$$TMP",$(1),$(2)) + +# ld-option +# Usage: LDFLAGS += $(call ld-option, -X) +ld-option = $(call try-run,\ + $(CC) /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2)) + +# ar-option +# Usage: KBUILD_ARFLAGS := $(call ar-option,D) +# Important: no spaces around options +ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) + +###### + +### +# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= +# Usage: +# $(Q)$(MAKE) $(build)=dir +build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj + +### +# Shorthand for $(Q)$(MAKE) -f scripts/Kbuild.klibc obj= +# Usage: +# $(Q)$(MAKE) $(klibc)=dir +klibc := -f $(srctree)/scripts/Kbuild.klibc obj + +# Prefix -I with $(srctree) if it is not an absolute path. +# skip if -I has no parameter +addtree = $(if $(patsubst -I%,%,$(1)), \ +$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)) + +# Find all -I options and call addtree +flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) + +# echo command. +# Short version is used, if $(quiet) equals `quiet_', otherwise full one. +echo-cmd = $(if $($(quiet)cmd_$(1)),\ + echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) + +# printing commands +cmd = @$(echo-cmd) $(cmd_$(1)) + +# Add $(obj)/ for paths that are not absolute +objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) + +### +# if_changed - execute command if any prerequisite is newer than +# target, or command line has changed +# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies +# including used config symbols +# if_changed_rule - as if_changed but execute rule instead +# See Documentation/kbuild/makefiles.txt for more info + +ifneq ($(KBUILD_NOCMDDEP),1) +# Check if both arguments has same arguments. Result is empty string if equal. +# User may override this check using make KBUILD_NOCMDDEP=1 +arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ + $(filter-out $(cmd_$@), $(cmd_$(1))) ) +else +arg-check = $(if $(strip $(cmd_$@)),,1) +endif + +# >'< substitution is for echo to work, +# >$< substitution to preserve $ when reloading .cmd file +# note: when using inline perl scripts [perl -e '...$$t=1;...'] +# in $(cmd_xxx) double $$ your perl vars +make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) + +# Find any prerequisites that is newer than target or that does not exist. +# PHONY targets skipped in both cases. +any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) + +# Execute command if command has changed or prerequisite(s) are updated. +# +if_changed = $(if $(strip $(any-prereq) $(arg-check)), \ + @set -e; \ + $(echo-cmd) $(cmd_$(1)); \ + echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) + +# Execute the command and also postprocess generated .d dependencies file. +if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \ + @set -e; \ + $(echo-cmd) $(cmd_$(1)); \ + scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\ + rm -f $(depfile); \ + mv -f $(dot-target).tmp $(dot-target).cmd) + +# Usage: $(call if_changed_rule,foo) +# Will check if $(cmd_foo) or any of the prerequisites changed, +# and if so will execute $(rule_foo). +if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \ + @set -e; \ + $(rule_$(1))) + +### +# why - tell why a a target got build +# enabled by make V=2 +# Output (listed in the order they are checked): +# (1) - due to target is PHONY +# (2) - due to target missing +# (3) - due to: file1.h file2.h +# (4) - due to command line change +# (5) - due to missing .cmd file +# (6) - due to target not in $(targets) +# (1) PHONY targets are always build +# (2) No target, so we better build it +# (3) Prerequisite is newer than target +# (4) The command line stored in the file named dir/.target.cmd +# differed from actual command line. This happens when compiler +# options changes +# (5) No dir/.target.cmd file (used to store command line) +# (6) No dir/.target.cmd file and target not listed in $(targets) +# This is a good hint that there is a bug in the kbuild file +ifeq ($(KBUILD_VERBOSE),2) +why = \ + $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ + $(if $(wildcard $@), \ + $(if $(strip $(any-prereq)),- due to: $(any-prereq), \ + $(if $(arg-check), \ + $(if $(cmd_$@),- due to command line change, \ + $(if $(filter $@, $(targets)), \ + - due to missing .cmd file, \ + - due to $(notdir $@) not in $$(targets) \ + ) \ + ) \ + ) \ + ), \ + - due to target missing \ + ) \ + ) + +echo-why = $(call escsq, $(strip $(why))) +endif diff --git a/scripts/Kbuild.install b/scripts/Kbuild.install new file mode 100644 index 0000000..df02159 --- /dev/null +++ b/scripts/Kbuild.install @@ -0,0 +1,134 @@ +# +# Install klibc +# +# File is logically seperated in two pieces. +# First piece is used when during a recursive descend of the klibc tree +# and second piece is used to do the final steps in the install +# If KLIBC_INSTALL is defined it tells us we are descending and we +# use first piece of the file. + +# This indicates the location of the final version of the shared library. +# THIS MUST BE AN ABSOLUTE PATH WITH NO FINAL SLASH. +# Leave this empty to make it the root. +# +SHLIBDIR = /lib + +# First rule +PHONY := __install install-rule +__install: + +# Install commands +install-data := install -m 644 +install-lib := install -m 755 +install-bin := install -m 755 + +# Install command +quiet_cmd_install = INSTALL $(install-y) + cmd_install = $(install-bin) $(install-y) \ + $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)bin + +# Link install command +quiet_cmd_install_links = LN $(install-link-y) + cmd_install_links = $(foreach l, $(install-link-y), ln -f $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)bin/$(word 2,$(subst =,$(space),$(l))) $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)bin/$(word 1,$(subst =,$(space),$(l))) &&) true + +ifeq ($(KLIBC_INSTALL),1) +# First part - we are descending.. + +# Reset variables (to get right type of assingment) +subdir- := + +# Read .config if it exist, otherwise ignore +-include $(objtree)/.config + +# Include Kbuild file +include $(srctree)/scripts/Kbuild.include + +# Arch specific definitions for klibc +include $(srctree)/$(KLIBCSRC)/arch/$(KLIBCARCHDIR)/MCONFIG + +include $(srctree)/$(obj)/Kbuild + +# Directories to visit +# First find directories specified in lib-?, static-y and shared-y +find-dir = $(patsubst %/,%,$(filter %/, $(1))) + +__subdir := $(call find-dir, $(lib-)) +__subdir += $(call find-dir, $(lib-y)) + +__subdir += $(foreach e, $(static-y), $(call find-dir, $(e))) +__subdir += $(foreach e, $(shared-y), $(call find-dir, $(e))) + +# Use subdir- in Kbuild file to tell kbuild to visit a specific dir +subdir- += $(__subdir) + +# Remove duplicates and add prefix +subdir- := $(addprefix $(obj)/,$(sort $(subdir-))) + +# Files to install +install-y := $(strip $(addprefix $(obj)/, $(install-y))) + +__install: $(subdir-) install-rule +ifneq ($(install-y),) + $(call cmd,install) +else + @: +endif +ifneq ($(install-link-y),) + $(call cmd,install_links) +endif + +# Descending +PHONY += $(subdir-) +$(subdir-): + $(Q)$(MAKE) KLIBC_INSTALL=1 \ + -f $(srctree)/scripts/Kbuild.install obj=$@ + +# If quiet is set, only print short version of command +cmd = @$(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))' &&) $(cmd_$(1)) + + +else +########################################################################## +# This is the first time this file is invoked, so kick off the +# install process. +# First we descend all sub-directories to let them do their install. +# Second we do the final install steps. + +# Do actual install as a three steps approach +# 1) Create directories, install headers and man pages +# 2) Tell that we now install binaries +# 3) Install binaries by descending +PHONY += header footer descend +header: + $(Q)echo " INSTALL headers + man pages to $(INSTALLROOT)$(INSTALLDIR)" + $(Q)mkdir -p $(INSTALLROOT)$(bindir) + $(Q)mkdir -p $(INSTALLROOT)$(mandir)/man1 + $(Q)mkdir -p $(INSTALLROOT)$(SHLIBDIR) + $(Q)mkdir -p $(INSTALLROOT)$(INSTALLDIR) + $(Q)-rm -rf $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include + $(Q)mkdir -p $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include + $(Q)mkdir -p $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)lib + $(Q)mkdir -p $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)bin + $(Q)cp -rfL $(KLIBCKERNELSRC)/include/. $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include/. +ifneq ($(srctree),$(objtree)) + $(Q)cp -rf $(srctree)/usr/include/. $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include/. +endif + $(Q)cp -rf usr/include/. $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include/. + $(Q)chmod -R a+rX,go-w $(INSTALLROOT)$(INSTALLDIR)/$(KCROSS)include + $(Q)$(install-data) $(srctree)/klcc/klcc.1 $(INSTALLROOT)$(mandir)/man1/$(KCROSS)klcc.1 + $(Q)$(install-bin) $(objtree)/klcc/$(KCROSS)klcc $(INSTALLROOT)$(bindir) + +footer: header + $(Q)echo " INSTALL binaries to $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)bin" + +descend: footer + $(Q)$(MAKE) KLIBC_INSTALL=1 \ + -f $(srctree)/scripts/Kbuild.install obj=$(obj) + +__install: descend + @: +endif + +# Declare the contents of the PHONY variable as phony. We keep the variable for +# if_changed. +.PHONY: $(PHONY) diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc new file mode 100644 index 0000000..accf7f1 --- /dev/null +++ b/scripts/Kbuild.klibc @@ -0,0 +1,440 @@ +# ========================================================================== +# Support for building klibc programs and klibc library +# ========================================================================== +# +# To create a kbuild file for a userspace program do the following: +# +# Kbuild: +# +# static-y := cat +# # This will compile a file named cat.c -> the executable 'cat' +# # The executable will be linked statically +# +# shared-y := cats +# # This will compile a file named cats.c -> the executable 'cats' +# # The executable will be linked shared +# +# If the userspace program consist of composite files do the following: +# Kbuild: +# +# static-y := kinit +# kinit-y := main.o netdev.c +# So kinit will be linked statically using the two .o files +# specified with kinit-y. +# +# Are part of the program located in a sub-directory do like this: +# kinit-y += ipconfig/ +# +# And in the subdirectory: +# ipconfig/Kbuild: +# lib-y := packet.o dhcp_proto.o +# # All .o files listed with lib-y will be used to create a single .a file. +# # The .a file is created before any subdirectories are visited so it +# # may be used in the sub-directory programs. +# +##### +# For a klibc libary file do like this +# klibc/Kbuild +# klib-y := error.o pipe.o zlib/ +# +##### +# Handling of compiler/linker options +# +# To set directory wide CFLAGS use: +# EXTRA_KLIBCCFLAGS := -DDEBUG +# To set directory wide AFLAGS use: +# EXTRA_KLIBCAFLAGS := -DDEBUG +# +# To set target specific CFLAGS (for .c files) use +# KLIBCCFLAGS-main.o := -DDEBUG=3 +# To set target specific AFLAGS (for .s files) use +# KLIBCAFLAGS-main.o := -DDEBUG=3 + +src := $(obj) +# Preset target and make sure it is a ':=' variable +targets := + +PHONY := __build +__build: + +# Read .config if it exist, otherwise ignore +-include $(objtree)/.config + +# Generic Kbuild routines +include $(srctree)/scripts/Kbuild.include + +# Defines used when compiling early userspace (klibc programs) +# --------------------------------------------------------------------------- + +KLIBCREQFLAGS := $(call cc-option, -fno-stack-protector, ) \ + $(call cc-option, -fwrapv, ) \ + $(call cc-option, -fno-PIE, ) \ + $(call cc-option, -fno-builtin-bcmp, ) \ + $(call cc-option, -fcommon, ) \ + -ggdb +KLIBCARCHREQFLAGS := +KLIBCOPTFLAGS := +KLIBCWARNFLAGS := -W -Wall -Wno-sign-compare -Wno-unused-parameter +KLIBCSHAREDFLAGS := +KLIBCBITSIZE := +KLIBCLDFLAGS := +KLIBCCFLAGS := + +# Defaults for arch to override +KLIBCARCHINCFLAGS = +KLIBCCRTSHARED := $(KLIBCOBJ)/shared-stub.o + +LD_IS_LLD := $(shell $(LD) --version 2>&1 | grep LLD) +LD_IMAGE_BASE_OPT := $(if $(LD_IS_LLD),--image-base,-Ttext-segment) + +# Arch specific definitions for klibc +include $(srctree)/$(KLIBCSRC)/arch/$(KLIBCARCHDIR)/MCONFIG + +# include/asm-* architecture +KLIBCASMARCH ?= $(KLIBCARCH) + +# klibc version +KLIBCMAJOR := $(shell cut -d. -f1 $(srctree)/usr/klibc/version) +KLIBCMINOR := $(shell cut -d. -f2 $(srctree)/usr/klibc/version) + +# binutils +KLIBCLD := $(LD) +KLIBCCC := $(CC) +KLIBCAR := $(AR) + +KLIBCRANLIB := $(RANLIB) -D +KLIBCSTRIP := $(if $(CONFIG_DEBUG_INFO),true,$(STRIP)) +KLIBCNM := $(NM) +KLIBCOBJCOPY := $(OBJCOPY) +KLIBCOBJDUMP := $(OBJDUMP) + +# klibc include paths +KLIBCCPPFLAGS := -nostdinc -iwithprefix include \ + -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \ + -I$(KLIBCINC)/bits$(KLIBCBITSIZE) \ + -I$(KLIBCOBJ)/../include \ + -I$(KLIBCINC) +ifeq ($(cc-name),clang) +KLIBCCPPFLAGS += -isystem $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-file-name=include) +endif + +# kernel include paths +KLIBCKERNELSRC ?= $(srctree) +KLIBCCPPFLAGS += -I$(KLIBCKERNELSRC)/include \ + $(if $(KBUILD_SRC),-I$(srctree)/include) \ + $(KLIBCARCHINCFLAGS) + +# compiler/assembler option for whether we want an executable stack +KLIBCSTACKFLAGS := -Wa,--$(if $(filter n,$(KLIBCEXECSTACK)),no)execstack + +# klibc definitions +KLIBCDEFS += -D__KLIBC__=$(KLIBCMAJOR) \ + -D__KLIBC_MINOR__=$(KLIBCMINOR) \ + -D_BITSIZE=$(KLIBCBITSIZE) +KLIBCCPPFLAGS += $(KLIBCDEFS) +KLIBCCFLAGS += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS) \ + $(KLIBCOPTFLAGS) $(KLIBCSTACKFLAGS) $(KLIBCWARNFLAGS) +KLIBCAFLAGS += -D__ASSEMBLY__ $(KLIBCCFLAGS) +KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note + +KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) $(if $(filter gcc,$(cc-name)),--print-libgcc,--print-libgcc-file-name)) +KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF) +KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o +KLIBCLIBC := $(KLIBCOBJ)/libc.a +KLIBCLIBCSHARED := $(KLIBCOBJ)/libc.so +# How to tell the linker main() is the entrypoint +KLIBCEMAIN ?= -e main + +ifdef CONFIG_DEBUG_INFO +KLIBCLDFLAGS += --build-id=sha1 +endif + +# +# This indicates the location of the final version of the shared library. +# THIS MUST BE AN ABSOLUTE PATH WITH NO FINAL SLASH. +# Leave this empty to make it the root. +# +SHLIBDIR = /lib + +export KLIBCLD KLIBCCC KLIBCAR KLIBCSTRIP KLIBCNM +export KLIBCCFLAGS KLIBCAFLAGS KLIBCLIBGCC KLIBCSHAREDFLAGS KLIBCSTRIPFLAGS +export KLIBCCRT0 KLIBCLIBC SHLIBDIR + +# Add $(obj)/ for paths that is not absolute +objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) + +# Kbuild file in the directory that is being build +include $(srctree)/$(obj)/Kbuild + +##### +# static-y + shared-y handling +kprogs := $(static-y) $(shared-y) +# kprogs based on a single .o file (with same name + .o) +kprog-objs := $(foreach p, $(kprogs), $(if $($(p)-y),,$(p))) +kprog-objs := $(addsuffix .o, $(kprog-objs)) +# kprogs which is based on several .o files +kprog-multi := $(foreach p, $(kprogs), $(if $($(p)-y),$(p))) +# objects used for kprogs with more then one .o file +kprog-objs += $(foreach p, $(kprog-multi), $($(p)-y)) +# objects build in this dir +kprog-real-objs := $(patsubst %/,,$(kprog-objs)) +# Directories we need to visit before kprogs-objs are up-to-date +kprog-dirs := $(patsubst %/,%,$(filter %/, $(kprog-objs))) +# replace all dir/ with dir/lib.a +kprog-objs := $(patsubst %/, %/lib.a, $(kprog-objs)) + +targets += $(static-y) $(shared-y) + +##### +# klib-y handling +# .o files to build in this dir +klib-real-objs := $(patsubst %/,,$(klib-y)) +# Directories we need to visit before libs are up-to-date +klib-dirs := $(patsubst %/,%,$(filter %/, $(klib-y))) +# replace all dir/ with dir/klib.list +klib-objs := $(patsubst %/, %/klib.list, $(klib-y)) + +# $(output-dirs) are a list of directories that contain object files +output-dirs := $(dir $(kprog-dirs) $(kprog-objs)) +output-dirs += $(foreach f, $(hostprogs-y) $(targets), \ + $(if $(dir $(f)), $(dir $(f)))) +output-dirs += $(dir $(klib-objs)) +output-dirs := $(strip $(sort $(filter-out ./,$(output-dirs)))) + +# prefix so we get full dir +static-y := $(addprefix $(obj)/,$(static-y)) +shared-y := $(addprefix $(obj)/,$(shared-y)) +kprog-objs := $(addprefix $(obj)/,$(kprog-objs)) +kprog-real-objs := $(addprefix $(obj)/,$(kprog-real-objs)) +output-dirs := $(addprefix $(obj)/,$(output-dirs)) +kprog-dirs := $(addprefix $(obj)/,$(kprog-dirs)) +subdir-y := $(addprefix $(obj)/,$(subdir-y)) +always := $(addprefix $(obj)/,$(always)) +targets := $(addprefix $(obj)/,$(targets)) +lib-y := $(addprefix $(obj)/,$(lib-y)) +klib-y := $(addprefix $(obj)/,$(klib-y)) +klib-objs := $(addprefix $(obj)/,$(klib-objs)) +klib-real-objs := $(addprefix $(obj)/,$(klib-real-objs)) +klib-dirs := $(addprefix $(obj)/,$(klib-dirs)) + +##### +# Handle options to gcc. Support building with separate output directory + +__klibccflags = $(KLIBCCFLAGS) $(EXTRA_KLIBCCFLAGS) $(KLIBCCFLAGS_$(*F).o) +__klibcaflags = $(KLIBCAFLAGS) $(EXTRA_KLIBCAFLAGS) $(KLIBCAFLAGS_$(*F).o) + +_klibccflags = $(call flags,__klibccflags) +_klibcaflags = $(call flags,__klibcaflags) + +klibccflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_klibccflags) +klibcaflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_klibcaflags) + +# Create output directory if not already present +_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj)) + +# Create directories for object files if directory does not exist +# Needed when lib-y := dir/file.o syntax is used +_dummy := $(foreach d,$(output-dirs), $(shell [ -d $(d) ] || mkdir -p $(d))) + +# Do we have to make a lib.a in this dir? +ifneq ($(strip $(lib-y) $(lib-n) $(lib-)),) +lib-target := $(obj)/lib.a +endif + +__build: $(subdir-y) $(lib-target) $(always) + $(Q): + +# Compile C sources (.c) +# --------------------------------------------------------------------------- + +quiet_cmd_cc_s_c = KLIBCCC $@ + cmd_cc_s_c = $(KLIBCCC) $(klibccflags) -S -o $@ $< + +%.s: %.c FORCE + $(call if_changed_dep,cc_s_c) + +quiet_cmd_cc_o_c = KLIBCCC $@ + cmd_cc_o_c = $(KLIBCCC) $(klibccflags) -c -o $@ $< + +%.o: %.c FORCE + $(call if_changed_dep,cc_o_c) + +quiet_cmd_cc_i_c = CPP $@ + cmd_cc_i_c = $(KLIBCCC) -E $(klibccflags) -o $@ $< +%.i: %.c FORCE + $(call if_changed_dep,cc_i_c) + +# Compile assembler sources (.S) +# --------------------------------------------------------------------------- + +quiet_cmd_as_o_S = KLIBCAS $@ + cmd_as_o_S = $(KLIBCCC) $(klibcaflags) -c -o $@ $< + +%.o: %.S FORCE + $(call if_changed_dep,as_o_S) + +targets += $(real-objs-y) + +# +# Rule to compile a set of .o files into one .o file +# +ifdef lib-target +quiet_cmd_link_o_target = LD $@ +# If the list of objects to link is empty, just create an empty lib.a +cmd_link_o_target = $(if $(strip $(lib-y)),\ + rm -f $@; $(KLIBCAR) Dcr $@ $(filter $(lib-y), $^),\ + rm -f $@; $(KLIBCAR) Dcrs $@) + +$(lib-target): $(lib-y) FORCE + $(call if_changed,link_o_target) +targets += $(lib-target) $(lib-y) +endif # lib-target + +# +# Create klib.list +# +# Do we have to create a klibc library file in this dir? +ifneq ($(strip $(klib-y) $(klib-n) $(klib-)),) +klib-target := $(obj)/klib.list +endif + +ifdef klib-target +# include this in build +__build: $(klib-target) $(klib-dirs) + +# descend if needed +$(sort $(addsuffix /klib.list,$(klib-dirs))): $(klib-dirs) ; + +# create klib.list +quiet_cmd_klib-list = LIST $@ + cmd_klib-list = echo $(klib-real-objs) > $@ +$(klib-target): $(klib-objs) FORCE + $(call if_changed,klib-list) +targets += $(klib-target) $(klib-real-objs) +endif # klib-target + +ifdef kprogs +# Compile klibc-programs for the target +# =========================================================================== + +__build : $(kprog-dirs) $(static-y) +ifdef KLIBCSHAREDFLAGS +__build : $(shared-y) +endif + +# Descend if needed +$(sort $(addsuffix /lib.a,$(kprog-dirs))): $(kprog-dirs) ; + +# Define dependencies for link of progs +# For the simple program: +# file.o => file +# A program with multiple objects +# filea.o, fileb.o => file +# A program with .o files in another dir +# dir/lib.a filea.o => file + +stripobj = $(subst $(obj)/,,$@) +addliba = $(addprefix $(obj)/, $(patsubst %/, %/lib.a, $(1))) +link-deps = $(if $($(stripobj)-y), $(call addliba, $($(stripobj)-y)), $@.o) \ + $(call objectify,$($(stripobj)-lib)) + +quiet_cmd_ld-static = KLIBCLD $@ + cmd_ld-static = $(KLIBCLD) $(KLIBCLDFLAGS) -o $@ \ + $(EXTRA_KLIBCLDFLAGS) \ + $(KLIBCCRT0) \ + --start-group \ + $(link-deps) \ + $(KLIBCLIBC) \ + $(KLIBCLIBGCC) \ + --end-group ; \ + cp -f $@ $@.g ; \ + $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@ + + +$(static-y): $(kprog-objs) $(lib-target) $(KLIBCCRT0) $(KLIBCLIBC) FORCE + $(call if_changed,ld-static) + +quiet_cmd_ld-shared = KLIBCLD $@ + cmd_ld-shared = $(KLIBCLD) $(KLIBCLDFLAGS) -o $@ \ + $(EXTRA_KLIBCLDFLAGS) \ + $(KLIBCEMAIN) $(KLIBCCRTSHARED) \ + --start-group \ + $(link-deps) \ + --just-symbols $(KLIBCLIBCSHARED) \ + $(KLIBCLIBGCC) \ + --end-group ; \ + cp -f $@ $@.g ; \ + $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@ + + +$(shared-y): $(kprog-objs) $(lib-target) $(KLIBCCRTSHARED) \ + $(KLIBCLIBCSHARED) FORCE + $(call if_changed,ld-shared) + +# Do not try to build KLIBC libaries if we are building klibc +ifeq ($(klibc-build),) +$(KLIBCCRT0) $(KLIBCLIBC): ; +$(KLIBCCRTSHARED) $(KLIBCLIBCSHARED): ; +endif + +targets += $(kprog-real-objs) +endif + +# Compile programs on the host +# =========================================================================== +ifdef hostprogs-y +include $(srctree)/scripts/Makefile.host +endif + +# Descending +# --------------------------------------------------------------------------- + +PHONY += $(subdir-y) $(kprog-dirs) $(klib-dirs) +$(sort $(subdir-y) $(kprog-dirs) $(klib-dirs)): $(lib-target) + $(Q)$(MAKE) $(klibc)=$@ + +# Add FORCE to the prequisites of a target to force it to be always rebuilt. +# --------------------------------------------------------------------------- + +PHONY += FORCE + +FORCE: + +# Linking +# Create a reloctable composite object file +# --------------------------------------------------------------------------- +quiet_cmd_klibcld = KLIBCLD $@ + cmd_klibcld = $(KLIBCLD) -r $(KLIBCLDFLAGS) \ + $(EXTRA_KLIBCLDFLAGS) $(KLIBCLDFLAGS_$(@F)) \ + $(filter-out FORCE,$^) -o $@ + + +# Link target to a new name +# --------------------------------------------------------------------------- +quiet_cmd_ln = LN $@ + cmd_ln = rm -f $@ && ln $< $@ + +# Strip target (remove all debugging info) +quiet_cmd_strip = STRIP $@ + cmd_strip = $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $< -o $@ + + +# Read all saved command lines and dependencies for the $(targets) we +# may be building above, using $(if_changed{,_dep}). As an +# optimization, we don't need to read them if the target does not +# exist, we will rebuild anyway in that case. +targets := $(wildcard $(sort $(targets))) +cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) + +ifneq ($(cmd_files),) + include $(cmd_files) +endif + +# Shorthand for $(Q)$(MAKE) -f scripts/Kbuild.klibc obj +# Usage: +# $(Q)$(MAKE) $(klibc)=dir +klibc := -rR -f $(srctree)/scripts/Kbuild.klibc obj + +# Declare the contents of the PHONY variable as phony. We keep the variable for +# if_changed. +.PHONY: $(PHONY) diff --git a/scripts/Kbuild.klibc.include b/scripts/Kbuild.klibc.include new file mode 100644 index 0000000..b317286 --- /dev/null +++ b/scripts/Kbuild.klibc.include @@ -0,0 +1,11 @@ + +# klibc-cc-option +# Usage: cflags-y += $(call klibc-cc-option,-march=winchip-c6,-march=i586) + +klibc-cc-option = $(call try-run,\ + $(CC) $(KLIBCCPPFLAGS) $(KLIBCCFLAGS) $(1) -c -xc /dev/null -o "$$TMP",$(1),$(2)) + +# klibc-cc-option-yn +# Usage: flag := $(call klibc-cc-option-yn,-march=winchip-c6) +klibc-cc-option-yn = $(call try-run,\ + $(CC) $(KLIBCCPPFLAGS) $(KLIBCCFLAGS) $(1) -c -xc /dev/null -o "$$TMP",y,n) diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean new file mode 100644 index 0000000..abc4e3f --- /dev/null +++ b/scripts/Makefile.clean @@ -0,0 +1,96 @@ +# ========================================================================== +# Cleaning up +# ========================================================================== + +src := $(obj) + +.PHONY := __clean +__clean: + +# Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir +# Usage: +# $(Q)$(MAKE) $(clean)=dir +clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj + +# The filename Kbuild has precedence over Makefile +kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) +include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile) + +# Figure out what we need to build from the various variables +# ========================================================================== + +subdirs := $(subdir-y) $(subdir-m) $(subdir-n) $(subdir-) +subdirs += $(patsubst %/,%,$(filter %/, $(obj-y))) +subdirs += $(patsubst %/,%,$(filter %/, $(obj-m))) +subdirs += $(patsubst %/,%,$(filter %/, $(obj-n))) +subdirs += $(patsubst %/,%,$(filter %/, $(obj-))) + +subdirs += $(patsubst %/,%,$(filter %/, $(klib-y))) +subdirs += $(patsubst %/,%,$(filter %/, $(klib-))) + +# Subdirectories we need to descend into +subdirs := $(addprefix $(obj)/,$(sort $(subdirs))) + + +# build a list of files to remove, usually releative to the current +# directory + +__clean-files := $(extra-y) $(EXTRA_TARGETS) $(always) \ + $(targets) $(clean-files) \ + $(host-progs) \ + $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \ + klib.list + +# as clean-files is given relative to the current directory, this adds +# a $(obj) prefix, except for absolute paths + +__clean-files := $(wildcard \ + $(addprefix $(obj)/, $(filter-out /%, $(__clean-files))) \ + $(filter /%, $(__clean-files))) + +# as clean-dirs is given relative to the current directory, this adds +# a $(obj) prefix, except for absolute paths + +__clean-dirs := $(wildcard \ + $(addprefix $(obj)/, $(filter-out /%, $(clean-dirs))) \ + $(filter /%, $(clean-dirs))) + +# ========================================================================== + +quiet_cmd_clean = CLEAN $(obj) + cmd_clean = rm -f $(__clean-files) +quiet_cmd_cleandir = CLEAN $(__clean-dirs) + cmd_cleandir = rm -rf $(__clean-dirs) + + +__clean: $(subdirs) +ifneq ($(strip $(__clean-files)),) + +$(call cmd,clean) +endif +ifneq ($(strip $(__clean-dirs)),) + +$(call cmd,cleandir) +endif +ifneq ($(strip $(clean-rule)),) + +$(clean-rule) +endif + @: + + +# =========================================================================== +# Generic stuff +# =========================================================================== + +# Descending +# --------------------------------------------------------------------------- + +PHONY += $(subdirs) +$(subdirs): + $(Q)$(MAKE) $(clean)=$@ + +# If quiet is set, only print short version of command + +cmd = @$(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))' &&) $(cmd_$(1)) + +# Declare the contents of the PHONY variable as phony. We keep the variable for +# if_changed. +.PHONY: $(PHONY) diff --git a/scripts/Makefile.host b/scripts/Makefile.host new file mode 100644 index 0000000..6a6b949 --- /dev/null +++ b/scripts/Makefile.host @@ -0,0 +1,160 @@ +# ========================================================================== +# Building binaries on the host system +# Binaries are used during the compilation of the kernel, for example +# to preprocess a data file. +# +# Both C and C++ is supported, but preferred language is C for such utilities. +# +# Sample syntax (see Documentation/kbuild/makefile.txt for reference) +# hostprogs-y := bin2hex +# Will compile bin2hex.c and create an executable named bin2hex +# +# hostprogs-y := lxdialog +# lxdialog-objs := checklist.o lxdialog.o +# Will compile lxdialog.c and checklist.c, and then link the executable +# lxdialog, based on checklist.o and lxdialog.o +# +# hostprogs-y := qconf +# qconf-cxxobjs := qconf.o +# qconf-objs := menu.o +# Will compile qconf as a C++ program, and menu as a C program. +# They are linked as C++ code to the executable qconf + +# hostprogs-y := conf +# conf-objs := conf.o libkconfig.so +# libkconfig-objs := expr.o type.o +# Will create a shared library named libkconfig.so that consist of +# expr.o and type.o (they are both compiled as C code and the object file +# are made as position independent code). +# conf.c is compiled as a c program, and conf.o is linked together with +# libkconfig.so as the executable conf. +# Note: Shared libraries consisting of C++ files are not supported + +__hostprogs := $(sort $(hostprogs-y)$(hostprogs-m)) + +# hostprogs-y := tools/build may have been specified. Retreive directory +obj-dirs += $(foreach f,$(__hostprogs), $(if $(dir $(f)),$(dir $(f)))) +obj-dirs := $(strip $(sort $(filter-out ./,$(obj-dirs)))) + + +# C code +# Executables compiled from a single .c file +host-csingle := $(foreach m,$(__hostprogs),$(if $($(m)-objs),,$(m))) + +# C executables linked based on several .o files +host-cmulti := $(foreach m,$(__hostprogs),\ + $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m)))) + +# Object (.o) files compiled from .c files +host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs))) + +# C++ code +# C++ executables compiled from at least on .cc file +# and zero or more .c files +host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) + +# C++ Object (.o) files compiled from .cc files +host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs))) + +# Shared libaries (only .c supported) +# Shared libraries (.so) - all .so files referenced in "xxx-objs" +host-cshlib := $(sort $(filter %.so, $(host-cobjs))) +# Remove .so files from "xxx-objs" +host-cobjs := $(filter-out %.so,$(host-cobjs)) + +#Object (.o) files used by the shared libaries +host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs)))) + +__hostprogs := $(addprefix $(obj)/,$(__hostprogs)) +host-csingle := $(addprefix $(obj)/,$(host-csingle)) +host-cmulti := $(addprefix $(obj)/,$(host-cmulti)) +host-cobjs := $(addprefix $(obj)/,$(host-cobjs)) +host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti)) +host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs)) +host-cshlib := $(addprefix $(obj)/,$(host-cshlib)) +host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs)) +obj-dirs := $(addprefix $(obj)/,$(obj-dirs)) + +##### +# Handle options to gcc. Support building with separate output directory + +# Prefix -I with $(srctree) if it is not an absolute path +addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1) +# Find all -I options and call addtree +flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) + +_hostc_flags = $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) $(HOSTCFLAGS_$(*F).o) +_hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) $(HOSTCXXFLAGS_$(*F).o) + +ifeq ($(KBUILD_SRC),) +__hostc_flags = $(_hostc_flags) +__hostcxx_flags = $(_hostcxx_flags) +else +__hostc_flags = -I$(obj) $(call flags,_hostc_flags) +__hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags) +endif + +hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags) +hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags) + +##### +# Compile programs on the host + +# Create executable from a single .c file +# host-csingle -> Executable +quiet_cmd_host-csingle = HOSTCC $@ + cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-csingle): %: %.c FORCE + $(call if_changed_dep,host-csingle) + +# Link an executable based on list of .o files, all plain c +# host-cmulti -> executable +quiet_cmd_host-cmulti = HOSTLD $@ + cmd_host-cmulti = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \ + $(addprefix $(obj)/,$($(@F)-objs)) \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-cmulti): %: $(host-cobjs) $(host-cshlib) FORCE + $(call if_changed,host-cmulti) + +# Create .o file from a single .c file +# host-cobjs -> .o +quiet_cmd_host-cobjs = HOSTCC $@ + cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $< +$(host-cobjs): %.o: %.c FORCE + $(call if_changed_dep,host-cobjs) + +# Link an executable based on list of .o files, a mixture of .c and .cc +# host-cxxmulti -> executable +quiet_cmd_host-cxxmulti = HOSTLD $@ + cmd_host-cxxmulti = $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \ + $(foreach o,objs cxxobjs,\ + $(addprefix $(obj)/,$($(@F)-$(o)))) \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-cxxmulti): %: $(host-cobjs) $(host-cxxobjs) $(host-cshlib) FORCE + $(call if_changed,host-cxxmulti) + +# Create .o file from a single .cc (C++) file +quiet_cmd_host-cxxobjs = HOSTCXX $@ + cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $< +$(host-cxxobjs): %.o: %.cc FORCE + $(call if_changed_dep,host-cxxobjs) + +# Compile .c file, create position independent .o file +# host-cshobjs -> .o +quiet_cmd_host-cshobjs = HOSTCC -fPIC $@ + cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $< +$(host-cshobjs): %.o: %.c FORCE + $(call if_changed_dep,host-cshobjs) + +# Link a shared library, based on position independent .o files +# *.o -> .so shared library (host-cshlib) +quiet_cmd_host-cshlib = HOSTLLD -shared $@ + cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \ + $(addprefix $(obj)/,$($(@F:.so=-objs))) \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-cshlib): %: $(host-cshobjs) FORCE + $(call if_changed,host-cshlib) + +targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\ + $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) diff --git a/scripts/basic/.gitignore b/scripts/basic/.gitignore new file mode 100644 index 0000000..a776371 --- /dev/null +++ b/scripts/basic/.gitignore @@ -0,0 +1 @@ +fixdep diff --git a/scripts/basic/Kbuild b/scripts/basic/Kbuild new file mode 100644 index 0000000..22e09d2 --- /dev/null +++ b/scripts/basic/Kbuild @@ -0,0 +1,6 @@ +# +# Kbuild file to build basic kbuild programs +# + +hostprogs-y := fixdep +always := $(hostprogs-y) diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c new file mode 100644 index 0000000..fed4c7f --- /dev/null +++ b/scripts/basic/fixdep.c @@ -0,0 +1,390 @@ +/* + * "Optimize" a list of dependencies as spit out by gcc -MD + * for the kernel build + * =========================================================================== + * + * Author Kai Germaschewski + * Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de> + * + * This software may be used and distributed according to the terms + * of the GNU General Public License, incorporated herein by reference. + * + * + * Introduction: + * + * gcc produces a very nice and correct list of dependencies which + * tells make when to remake a file. + * + * To use this list as-is however has the drawback that virtually + * every file in the kernel includes <linux/config.h> which then again + * includes <linux/autoconf.h> + * + * If the user re-runs make *config, linux/autoconf.h will be + * regenerated. make notices that and will rebuild every file which + * includes autoconf.h, i.e. basically all files. This is extremely + * annoying if the user just changed CONFIG_HIS_DRIVER from n to m. + * + * So we play the same trick that "mkdep" played before. We replace + * the dependency on linux/autoconf.h by a dependency on every config + * option which is mentioned in any of the listed prequisites. + * + * To be exact, split-include populates a tree in include/config/, + * e.g. include/config/his/driver.h, which contains the #define/#undef + * for the CONFIG_HIS_DRIVER option. + * + * So if the user changes his CONFIG_HIS_DRIVER option, only the objects + * which depend on "include/linux/config/his/driver.h" will be rebuilt, + * so most likely only his driver ;-) + * + * The idea above dates, by the way, back to Michael E Chastain, AFAIK. + * + * So to get dependencies right, there are two issues: + * o if any of the files the compiler read changed, we need to rebuild + * o if the command line given to the compile the file changed, we + * better rebuild as well. + * + * The former is handled by using the -MD output, the later by saving + * the command line used to compile the old object and comparing it + * to the one we would now use. + * + * Again, also this idea is pretty old and has been discussed on + * kbuild-devel a long time ago. I don't have a sensibly working + * internet connection right now, so I rather don't mention names + * without double checking. + * + * This code here has been based partially based on mkdep.c, which + * says the following about its history: + * + * Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>. + * This is a C version of syncdep.pl by Werner Almesberger. + * + * + * It is invoked as + * + * fixdep <depfile> <target> <cmdline> + * + * and will read the dependency file <depfile> + * + * The transformed dependency snipped is written to stdout. + * + * It first generates a line + * + * cmd_<target> = <cmdline> + * + * and then basically copies the .<target>.d file to stdout, in the + * process filtering out the dependency on linux/autoconf.h and adding + * dependencies on include/config/my/option.h for every + * CONFIG_MY_OPTION encountered in any of the prequisites. + * + * It will also filter out all the dependencies on *.ver. We need + * to make sure that the generated version checksum are globally up + * to date before even starting the recursive build, so it's too late + * at this point anyway. + * + * The algorithm to grep for "CONFIG_..." is bit unusual, but should + * be fast ;-) We don't even try to really parse the header files, but + * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will + * be picked up as well. It's not a problem with respect to + * correctness, since that can only give too many dependencies, thus + * we cannot miss a rebuild. Since people tend to not mention totally + * unrelated CONFIG_ options all over the place, it's not an + * efficiency problem either. + * + * (Note: it'd be easy to port over the complete mkdep state machine, + * but I don't think the added complexity is worth it) + */ +/* + * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto + * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not + * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as + * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h, + * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that + * those files will have correct dependencies. + */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <limits.h> +#include <ctype.h> +#include <arpa/inet.h> + +#define INT_CONF ntohl(0x434f4e46) +#define INT_ONFI ntohl(0x4f4e4649) +#define INT_NFIG ntohl(0x4e464947) +#define INT_FIG_ ntohl(0x4649475f) + +char *target; +char *depfile; +char *cmdline; + +static void usage(void) +{ + fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n"); + exit(1); +} + +static void print_cmdline(void) +{ + printf("cmd_%s := %s\n\n", target, cmdline); +} + +char * str_config = NULL; +int size_config = 0; +int len_config = 0; + +/* + * Grow the configuration string to a desired length. + * Usually the first growth is plenty. + */ +static void grow_config(int len) +{ + while (len_config + len > size_config) { + if (size_config == 0) + size_config = 2048; + str_config = realloc(str_config, size_config *= 2); + if (str_config == NULL) + { perror("fixdep:malloc"); exit(1); } + } +} + + + +/* + * Lookup a value in the configuration string. + */ +static int is_defined_config(const char *name, int len) +{ + const char * pconfig; + const char * plast = str_config + len_config - len; + for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) { + if (pconfig[ -1] == '\n' + && pconfig[len] == '\n' + && !memcmp(pconfig, name, len)) + return 1; + } + return 0; +} + +/* + * Add a new value to the configuration string. + */ +static void define_config(const char *name, int len) +{ + grow_config(len + 1); + + memcpy(str_config+len_config, name, len); + len_config += len; + str_config[len_config++] = '\n'; +} + +/* + * Clear the set of configuration strings. + */ +static void clear_config(void) +{ + len_config = 0; + define_config("", 0); +} + +/* + * Record the use of a CONFIG_* word. + */ +static void use_config(char *m, int slen) +{ + char s[PATH_MAX]; + char *p; + + if (is_defined_config(m, slen)) + return; + + define_config(m, slen); + + memcpy(s, m, slen); s[slen] = 0; + + for (p = s; p < s + slen; p++) { + if (*p == '_') + *p = '/'; + else + *p = tolower((int)*p); + } + printf(" $(wildcard include/config/%s.h) \\\n", s); +} + +static void parse_config_file(char *map, size_t len) +{ + int *end = (int *) (map + len); + /* start at +1, so that p can never be < map */ + int *m = (int *) map + 1; + char *p, *q; + + for (; m < end; m++) { + if (*m == INT_CONF) { p = (char *) m ; goto conf; } + if (*m == INT_ONFI) { p = (char *) m-1; goto conf; } + if (*m == INT_NFIG) { p = (char *) m-2; goto conf; } + if (*m == INT_FIG_) { p = (char *) m-3; goto conf; } + continue; + conf: + if (p > map + len - 7) + continue; + if (memcmp(p, "CONFIG_", 7)) + continue; + for (q = p + 7; q < map + len; q++) { + if (!(isalnum(*q) || *q == '_')) + goto found; + } + continue; + + found: + use_config(p+7, q-p-7); + } +} + +/* test is s ends in sub */ +static int strrcmp(char *s, char *sub) +{ + int slen = strlen(s); + int sublen = strlen(sub); + + if (sublen > slen) + return 1; + + return memcmp(s + slen - sublen, sub, sublen); +} + +static void do_config_file(char *filename) +{ + struct stat st; + int fd; + void *map; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "fixdep: "); + perror(filename); + exit(2); + } + fstat(fd, &st); + if (st.st_size == 0) { + close(fd); + return; + } + map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if ((long) map == -1) { + perror("fixdep: mmap"); + close(fd); + return; + } + + parse_config_file(map, st.st_size); + + munmap(map, st.st_size); + + close(fd); +} + +static void parse_dep_file(void *map, size_t len) +{ + char *m = map; + char *end = m + len; + char *p; + char s[PATH_MAX]; + + p = strchr(m, ':'); + if (!p) { + fprintf(stderr, "fixdep: parse error\n"); + exit(1); + } + memcpy(s, m, p-m); s[p-m] = 0; + printf("deps_%s := \\\n", target); + m = p+1; + + clear_config(); + + while (m < end) { + while (m < end && (*m == ' ' || *m == '\\' || *m == '\n')) + m++; + p = m; + while (p < end && *p != ' ') p++; + if (p == end) { + do p--; while (!isalnum(*p)); + p++; + } + memcpy(s, m, p-m); s[p-m] = 0; + if (strrcmp(s, "include/linux/autoconf.h") && + strrcmp(s, "arch/um/include/uml-config.h") && + strrcmp(s, ".ver")) { + printf(" %s \\\n", s); + do_config_file(s); + } + m = p + 1; + } + printf("\n%s: $(deps_%s)\n\n", target, target); + printf("$(deps_%s):\n", target); +} + +static void print_deps(void) +{ + struct stat st; + int fd; + void *map; + + fd = open(depfile, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "fixdep: "); + perror(depfile); + exit(2); + } + fstat(fd, &st); + if (st.st_size == 0) { + fprintf(stderr,"fixdep: %s is empty\n",depfile); + close(fd); + return; + } + map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if ((long) map == -1) { + perror("fixdep: mmap"); + close(fd); + return; + } + + parse_dep_file(map, st.st_size); + + munmap(map, st.st_size); + + close(fd); +} + +static void traps(void) +{ + static char test[] __attribute__((aligned(sizeof(int)))) = "CONF"; + int *p = (int *)test; + + if (*p != INT_CONF) { + fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n", + *p); + exit(2); + } +} + +int main(int argc, char *argv[]) +{ + traps(); + + if (argc != 4) + usage(); + + depfile = argv[1]; + target = argv[2]; + cmdline = argv[3]; + + print_cmdline(); + print_deps(); + + return 0; +} |