Skip to content

Makefile: drop duplicate %.a from test-helper link rule#2314

Open
HaraldNordgren wants to merge 1 commit into
git:masterfrom
HaraldNordgren:makefile-test-helper-dedup-libs
Open

Makefile: drop duplicate %.a from test-helper link rule#2314
HaraldNordgren wants to merge 1 commit into
git:masterfrom
HaraldNordgren:makefile-test-helper-dedup-libs

Conversation

@HaraldNordgren
Copy link
Copy Markdown
Contributor

@HaraldNordgren HaraldNordgren commented May 27, 2026

Redefine $(LIBS) to list archive prerequisites from $^ first, then the rest of the library list to avoid brittleness in the future.

@gitgitgadget-git
Copy link
Copy Markdown

There is an issue in commit 0146976:
Makefile: drop duplicate %.a from test-helper link rule

  • Commit not signed off

@HaraldNordgren HaraldNordgren force-pushed the makefile-test-helper-dedup-libs branch from 0146976 to 421259d Compare May 27, 2026 18:35
@HaraldNordgren HaraldNordgren force-pushed the makefile-test-helper-dedup-libs branch 3 times, most recently from c17777a to f616645 Compare May 31, 2026 22:44
@HaraldNordgren
Copy link
Copy Markdown
Contributor Author

/submit

@gitgitgadget-git
Copy link
Copy Markdown

Submitted as pull.2314.git.git.1780269406949.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v1

To fetch this version to local tag pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v1

@gitgitgadget-git
Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:

>  t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
> -	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
> +	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)

I think the reason why the pattern to use only the .o files among
the prerequisites and then use only the .a files among the same
prerequisites (both filters $^) is used here is to make sure that the
linker sees object files first before library archives, so that by
the time its left-to-right scan sees the first library archive, all
the missing symbols in the object files are known.  The above change
depends on LIBS being a strict superset of all the library archive
files ($GITLIBS in the current code, but that can be updated in the
future) listed as prerequisites for the rule, but there is nothing to
guarantee that, so it looks brittle.

Exact same comment applies to the other two rules touched by this patch.

@HaraldNordgren HaraldNordgren force-pushed the makefile-test-helper-dedup-libs branch from f616645 to 82e2249 Compare June 4, 2026 07:25
@gitgitgadget-git
Copy link
Copy Markdown

Harald Nordgren wrote on the Git mailing list (how to reply to this email):

On Thu, Jun 4, 2026 at 2:33 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> >  t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
> > -     $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
> > +     $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
>
> I think the reason why the pattern to use only the .o files among
> the prerequisites and then use only the .a files among the same
> prerequisites (both filters $^) is used here is to make sure that the
> linker sees object files first before library archives, so that by
> the time its left-to-right scan sees the first library archive, all
> the missing symbols in the object files are known.  The above change
> depends on LIBS being a strict superset of all the library archive
> files ($GITLIBS in the current code, but that can be updated in the
> future) listed as prerequisites for the rule, but there is nothing to
> guarantee that, so it looks brittle.
>
> Exact same comment applies to the other two rules touched by this patch.

Hmm, there are other constructs like this that rely on $(LIBS) being a
superset of the archives, so the three rules changed here align with
the trend rather than introduce a new trend.

Not saying we shouldn't find a way to handle the overall brittleness.


Harald

@gitgitgadget-git
Copy link
Copy Markdown

Harald Nordgren wrote on the Git mailing list (how to reply to this email):

Maybe we can do this to get around the brittleness for all ~10 places:

```
-LIBS = $(filter-out %.o, $(GITLIBS)) $(EXTLIBS)
+LIBS = $(filter %.a,$^) $(filter-out $(filter %.a,$^),$(filter-out
%.o,$(GITLIBS)) $(EXTLIBS))

 BASIC_CFLAGS += $(COMPAT_CFLAGS)
 LIB_OBJS += $(COMPAT_OBJS)
@@ -3392,7 +3395,7 @@ perf: all
 t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
$(UNIT_TEST_DIR)/test-lib.o

 t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
- $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter
%.o,$^) $(filter %.a,$^) $(LIBS)
+ $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)

 check-sha1:: t/helper/test-tool$X
  t/helper/test-sha1.sh
@@ -4015,13 +4018,13 @@ fuzz-all: $(FUZZ_PROGRAMS)
 $(FUZZ_PROGRAMS): %: %.o oss-fuzz/dummy-cmd-main.o $(GITLIBS) GIT-LDFLAGS
  $(QUIET_LINK)$(FUZZ_CXX) $(FUZZ_CXXFLAGS) -o $@ $(ALL_LDFLAGS) \
  -Wl,--allow-multiple-definition \
- $(filter %.o,$^) $(filter %.a,$^) $(LIBS) $(LIB_FUZZING_ENGINE)
+ $(filter %.o,$^) $(LIBS) $(LIB_FUZZING_ENGINE)

 $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o
$(UNIT_TEST_OBJS) \
  $(GITLIBS) GIT-LDFLAGS
  $(call mkdir_p_parent_template)
  $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
- $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
+ $(filter %.o,$^) $(LIBS)

 GIT-TEST-SUITES: FORCE
  @FLAGS='$(CLAR_TEST_SUITES)'; \
```


Harald

On Thu, Jun 4, 2026 at 9:06 AM Harald Nordgren <haraldnordgren@gmail.com> wrote:
>
> On Thu, Jun 4, 2026 at 2:33 AM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
> >
> > >  t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
> > > -     $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
> > > +     $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
> >
> > I think the reason why the pattern to use only the .o files among
> > the prerequisites and then use only the .a files among the same
> > prerequisites (both filters $^) is used here is to make sure that the
> > linker sees object files first before library archives, so that by
> > the time its left-to-right scan sees the first library archive, all
> > the missing symbols in the object files are known.  The above change
> > depends on LIBS being a strict superset of all the library archive
> > files ($GITLIBS in the current code, but that can be updated in the
> > future) listed as prerequisites for the rule, but there is nothing to
> > guarantee that, so it looks brittle.
> >
> > Exact same comment applies to the other two rules touched by this patch.
>
> Hmm, there are other constructs like this that rely on $(LIBS) being a
> superset of the archives, so the three rules changed here align with
> the trend rather than introduce a new trend.
>
> Not saying we shouldn't find a way to handle the overall brittleness.
>
>
> Harald

@HaraldNordgren HaraldNordgren force-pushed the makefile-test-helper-dedup-libs branch 2 times, most recently from 2ee8b2a to 13ac9d4 Compare June 4, 2026 21:59
A handful of link recipes listed archive files twice: once explicitly
via $(filter %.a,$^) and again implicitly through $(LIBS), which
expanded to $(filter-out %.o,$(GITLIBS)) $(EXTLIBS). On macOS the
linker warned about the duplicates:

  ld: warning: ignoring duplicate libraries: 'libgit.a', 'target/release/libgitcore.a'

Redefine $(LIBS) to list archive prerequisites from $^ first, then
the rest of the library list with those archives filtered out so each
appears only once.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
@HaraldNordgren HaraldNordgren force-pushed the makefile-test-helper-dedup-libs branch from 13ac9d4 to 0ef442e Compare June 4, 2026 22:00
@HaraldNordgren
Copy link
Copy Markdown
Contributor Author

/submit

@gitgitgadget-git
Copy link
Copy Markdown

Submitted as pull.2314.v2.git.git.1780610623006.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v2

To fetch this version to local tag pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-2314/HaraldNordgren/makefile-test-helper-dedup-libs-v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant