History

kbuild: create object directories simpler and faster

For the out-of-tree build, scripts/Makefile.build creates output directories, but this operation is not efficient. scripts/Makefile.lib calculates obj-dirs as follows: obj-dirs := $(dir $(multi-objs) $(obj-y)) Please notice $(sort ...) is not used here. Usually the result is as many "./" as objects here. For a lot of duplicated paths, the following command is invoked. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d))) Then, the costly shell command is run over and over again. I see many points for optimization: [1] Use $(sort ...) to cut down duplicated paths before passing them to system call [2] Use single $(shell ...) instead of repeating it with $(foreach ...) This will reduce forking. [3] We can calculate obj-dirs more simply. Most of objects are already accumulated in $(targets). So, $(dir $(targets)) is fine and more comprehensive. I also removed ugly code in arch/x86/entry/vdso/Makefile. This is now really unnecessary.

Masahiro Yamada committed 9 years ago

Commit 8a787561 parent 591f6684 files · +6 −30Browse files

Showing the changes to scripts (3 of 4 files). Show all

Unified · Split

3 files
  1. scripts/Makefile.build +6 −9
  2. scripts/Makefile.host +0 −12
  3. scripts/Makefile.lib +0 −5
scripts/Makefile.build+6−9
@@ -64,15 +64,6 @@ ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(h · 63 unmodified lines
64include scripts/Makefile.host64include scripts/Makefile.host
65endif65endif
66 66
67ifneq ($(KBUILD_SRC),)
68# Create output directory if not already present
69_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
70
71# Create directories for object files if directory does not exist
72# Needed when obj-y := dir/file.o syntax is used
73_dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
74endif
75
76ifndef obj67ifndef obj
77$(warning kbuild: Makefile.build is included improperly)68$(warning kbuild: Makefile.build is included improperly)
78endif69endif
@@ -589,6 +580,12 @@ ifneq ($(cmd_files),) · 510 unmodified lines
589 include $(cmd_files)580 include $(cmd_files)
590endif581endif
591 582
583ifneq ($(KBUILD_SRC),)
584# Create directories for object files if they do not exist
585obj-dirs := $(sort $(obj) $(patsubst %/,%, $(dir $(targets))))
586$(shell mkdir -p $(obj-dirs))
587endif
588
592# Declare the contents of the .PHONY variable as phony. We keep that589# Declare the contents of the .PHONY variable as phony. We keep that
593# information in a variable se we can use it in if_changed and friends.590# information in a variable se we can use it in if_changed and friends.
594 591
scripts/Makefile.host+0−12
@@ -48,15 +48,6 @@ host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs))) · 47 unmodified lines
48host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))48host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))
49host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))49host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))
50 50
51# output directory for programs/.o files
52# hostprogs-y := tools/build may have been specified.
53# Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation
54host-objdirs := $(dir $(__hostprogs) $(host-cobjs) $(host-cxxobjs))
55
56host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs))))
57
58
59__hostprogs := $(addprefix $(obj)/,$(__hostprogs))
60host-csingle := $(addprefix $(obj)/,$(host-csingle))51host-csingle := $(addprefix $(obj)/,$(host-csingle))
61host-cmulti := $(addprefix $(obj)/,$(host-cmulti))52host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
62host-cobjs := $(addprefix $(obj)/,$(host-cobjs))53host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
@@ -66,9 +57,6 @@ host-cshlib := $(addprefix $(obj)/,$(host-cshlib)) · 3 unmodified lines
66host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))57host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))
67host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs))58host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs))
68host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))59host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))
69host-objdirs := $(addprefix $(obj)/,$(host-objdirs))
70
71obj-dirs += $(host-objdirs)
72 60
73#####61#####
74# Handle options to gcc. Support building with separate output directory62# Handle options to gcc. Support building with separate output directory
scripts/Makefile.lib+0−5
@@ -50,15 +50,11 @@ single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m))) · 49 unmodified lines
50# objects depend on those (obviously)50# objects depend on those (obviously)
51multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))51multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
52multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))52multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
53multi-objs := $(multi-objs-y) $(multi-objs-m)
54 53
55# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to54# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
56# tell kbuild to descend55# tell kbuild to descend
57subdir-obj-y := $(filter %/built-in.o, $(obj-y))56subdir-obj-y := $(filter %/built-in.o, $(obj-y))
58 57
59# $(obj-dirs) is a list of directories that contain object files
60obj-dirs := $(dir $(multi-objs) $(obj-y))
61
62# Replace multi-part objects by their individual parts, look at local dir only58# Replace multi-part objects by their individual parts, look at local dir only
63real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)59real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
64real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)),$(m)))60real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)),$(m)))
@@ -81,7 +77,6 @@ multi-used-m := $(addprefix $(obj)/,$(multi-used-m)) · 16 unmodified lines
81multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))77multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
82multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))78multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
83subdir-ym := $(addprefix $(obj)/,$(subdir-ym))79subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
84obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
85 80
86# These flags are needed for modversions and compiling, so we define them here81# These flags are needed for modversions and compiling, so we define them here
87# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will82# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will