Skip to content

Commit f98fe47

Browse files
committed
kbuild: link $(real-obj-y) instead of $(obj-y) into built-in.a
In Kbuild, Makefiles can add the same object to obj-y multiple times. So, obj-y += foo.o obj-y += foo.o is fine. However, this is not true when the same object is added multiple times via composite objects. For example, obj-y += foo.o bar.o foo-objs := foo-bar-common.o foo-only.o bar-objs := foo-bar-common.o bar-only.o causes build error because two instances of foo-bar-common.o are linked into the vmlinux. Makefiles tend to invent ugly work-around, for example - lib/zstd/Makefile - drivers/net/ethernet/cavium/liquidio/Makefile The technique used in Kbuild to avoid the multiple definition error is to use $(filter $(obj-y), $^). Here, $^ lists the names of all the prerequisites with duplicated names removed. By replacing it with $(filter $(real-obj-y), $^) we can do likewise for composite objects. For built-in objects, we do not need to keep the composite object structure. We can simply expand them, and link $(real-obj-y) to built-in.a. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
1 parent f5f3368 commit f98fe47

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

‎scripts/Makefile.build‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ endif
7373

7474
ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
7575
lib-target := $(obj)/lib.a
76-
obj-y += $(obj)/lib-ksyms.o
76+
real-obj-y += $(obj)/lib-ksyms.o
7777
endif
7878

79-
ifneq ($(strip $(obj-y) $(need-builtin)),)
79+
ifneq ($(strip $(real-obj-y) $(need-builtin)),)
8080
builtin-target := $(obj)/built-in.a
8181
endif
8282

@@ -412,7 +412,7 @@ endif
412412
$(obj)/%.o: $(src)/%.S $(objtool_dep) FORCE
413413
$(call if_changed_rule,as_o_S)
414414

415-
targets += $(real-obj-y) $(real-obj-m) $(lib-y)
415+
targets += $(filter-out $(subdir-obj-y), $(real-obj-y)) $(real-obj-m) $(lib-y)
416416
targets += $(extra-y) $(MAKECMDGOALS) $(always)
417417

418418
# Linker scripts preprocessor (.lds.S -> .lds)
@@ -455,12 +455,12 @@ cmd_make_empty_builtin = rm -f $@; $(AR) rcSTP$(KBUILD_ARFLAGS)
455455
quiet_cmd_link_o_target = AR $@
456456

457457
# If the list of objects to link is empty, just create an empty built-in.a
458-
cmd_link_o_target = $(if $(strip $(obj-y)),\
459-
$(cmd_make_builtin) $@ $(filter $(obj-y), $^) \
458+
cmd_link_o_target = $(if $(strip $(real-obj-y)),\
459+
$(cmd_make_builtin) $@ $(filter $(real-obj-y), $^) \
460460
$(cmd_secanalysis),\
461461
$(cmd_make_empty_builtin) $@)
462462

463-
$(builtin-target): $(obj-y) FORCE
463+
$(builtin-target): $(real-obj-y) FORCE
464464
$(call if_changed,link_o_target)
465465

466466
targets += $(builtin-target)
@@ -534,23 +534,16 @@ $($(subst $(obj)/,,$(@:.o=-m)))), $^)
534534

535535
cmd_link_multi-link = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
536536

537-
quiet_cmd_link_multi-y = AR $@
538-
cmd_link_multi-y = rm -f $@; $(AR) rcSTP$(KBUILD_ARFLAGS) $@ $(link_multi_deps)
539-
540537
quiet_cmd_link_multi-m = LD [M] $@
541538
cmd_link_multi-m = $(cmd_link_multi-link)
542539

543-
$(multi-used-y): FORCE
544-
$(call if_changed,link_multi-y)
545-
$(call multi_depend, $(multi-used-y), .o, -objs -y)
546-
547540
$(multi-used-m): FORCE
548541
$(call if_changed,link_multi-m)
549542
@{ echo $(@:.o=.ko); echo $(link_multi_deps); \
550543
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
551544
$(call multi_depend, $(multi-used-m), .o, -objs -y -m)
552545

553-
targets += $(multi-used-y) $(multi-used-m)
546+
targets += $(multi-used-m)
554547
targets := $(filter-out $(PHONY), $(targets))
555548

556549
# Descending

‎scripts/Makefile.lib‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
5151
# tell kbuild to descend
5252
subdir-obj-y := $(filter %/built-in.a, $(obj-y))
5353

54-
# Replace multi-part objects by their individual parts, look at local dir only
55-
real-obj-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)))
54+
# Replace multi-part objects by their individual parts,
55+
# including built-in.a from subdirectories
56+
real-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
5657
real-obj-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)))
5758

5859
# DTB
@@ -66,14 +67,12 @@ extra-y := $(addprefix $(obj)/,$(extra-y))
6667
always := $(addprefix $(obj)/,$(always))
6768
targets := $(addprefix $(obj)/,$(targets))
6869
modorder := $(addprefix $(obj)/,$(modorder))
69-
obj-y := $(addprefix $(obj)/,$(obj-y))
7070
obj-m := $(addprefix $(obj)/,$(obj-m))
7171
lib-y := $(addprefix $(obj)/,$(lib-y))
7272
subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
7373
real-obj-y := $(addprefix $(obj)/,$(real-obj-y))
7474
real-obj-m := $(addprefix $(obj)/,$(real-obj-m))
7575
single-used-m := $(addprefix $(obj)/,$(single-used-m))
76-
multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
7776
multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
7877
subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
7978

0 commit comments

Comments
 (0)