[klibc] kbuild: enable reproducible builds
This adds a new setting: KBUILD_REPRODUCIBLE
If it is non-empty, ar and ranlib will be called in such
a way that timestamps do not affect the build output.
This allows one to get perfectly reproducible builds.
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Signed-off-by: maximilian attems <max@stro.at>
diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc
index 79d196f..fb399f5 100644
--- a/scripts/Kbuild.klibc
+++ b/scripts/Kbuild.klibc
@@ -93,11 +93,16 @@
KLIBCLD := $(LD)
KLIBCCC := $(CC)
KLIBCAR := $(AR)
-KLIBCRANLIB := $(RANLIB)
+
+# klibc-ar is a macro that invokes KLIBCAR and takes 2 arguments of ar commands.
+# The second will be used for reproducible builds, the first otherwise.
+klibc-ar = $(KLIBCAR) $(if $(KBUILD_REPRODUCIBLE),$(2),$(1))
+
+KLIBCRANLIB := $(call klibc-ar,s,Ds)
KLIBCSTRIP := $(STRIP)
KLIBCNM := $(NM)
-KLIBCOBJCOPY := $(OBJCOPY)
-KLIBCOBJDUMP := $(OBJDUMP)
+KLIBCOBJCOPY := $(OBJCOPY)
+KLIBCOBJDUMP := $(OBJDUMP)
# klibc include paths
KLIBCCPPFLAGS := -nostdinc -iwithprefix include \
@@ -265,8 +270,8 @@
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) cru $@ $(filter $(lib-y), $^),\
- rm -f $@; $(KLIBCAR) crs $@)
+ rm -f $@; $(call klibc-ar,cru,Dcr) $@ $(filter $(lib-y), $^),\
+ rm -f $@; $(call klibc-ar,crs,Dcrs) $@)
$(lib-target): $(lib-y) FORCE
$(call if_changed,link_o_target)
diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild
index ab5212b..af40367 100644
--- a/usr/klibc/Kbuild
+++ b/usr/klibc/Kbuild
@@ -109,7 +109,7 @@
targets += libc.a __static_init.o
quiet_cmd_libc = KLIBCAR $@
cmd_libc = rm -f $@; \
- $(KLIBCAR) cq $@ \
+ $(call klibc-ar,cq,Dcq) $@ \
$(call objectify,__static_init.o) $(klib-o-files); \
$(KLIBCRANLIB) $@