git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Splitting common-main

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 15, 2023, 00:34 UTC
Message-ID
<xmqqh6q6m2q4.fsf@gitster.g>
In-Reply-To
<ZLHcaFvvZh88TrLb@google.com>
Josh Steadmon <steadmon@google.com> writes:
Show 7 quoted lines
> Sorry, I don't think I'm understanding your proposal here properly,
> please let me know where I'm going wrong: isn't this functionally
> equivalent to my patch, just with different filenames? Now main() would
> live in main.c (vs. my common-main.c), while check_bug_if_BUG() and
> common_exit() would live in common-main.c (now a misnomer, vs. my
> common-exit.c). I'm not following how that changes anything so I'm
> pretty sure I've misunderstood.

Sorry, the old discussion has expired out of my brain, and asking what I had in mind back then is too late.

Your common-main.c has stuff _other than_ main(), and the remaining main() has tons of Git specific stuff. It may be one way to split, but I did not find a reason to convince myself that it was a good split.

What I was wondering as a straw-man alternative was to have main.c that has only this and nothing else:

    $ cat >main.c <<\EOF
    #include "git-compat-util.h" /* or whatever defines git_main() */
    int main(int ac, char **av)
    {
	return git_main(ac, av);
    }
    EOF
Then in common-main.c, rename main() to git_main().

I was not saying such a split would be superiour compared to how you moved only some stuff out of common-main.c to a separate file. I had trouble equally with your split and with the above strawman, because I did not (and do not) quite see how one would evaluate the result (go back to the message you are responding to for details).

> The issue I was trying to solve (whether for a unit-test framework or
> for the fuzzing engine) is that we don't have direct control over their
> main(), and so we can't rename it to avoid conflicts with our main().

Sure. And if we by default use a very thin main() that calls git_main(), it would be very easy for them to replace that main.o file with their own implementation of main(); as long as they eventually call git_main(), they can borrow what we do in ours.

> I guess there may be some linker magic we could do to avoid the conflict
> and have (our) main() call (their, renamed) main()?

We can throw a main.o that has the implementation of our default "main" function into "libgit.a".

Then, when we link our "git" program (either built-in programs that are reachable from git.o, or standalone programs like remote-curl.o that have their own cmd_main()), we list our object files (but we do not have to list main.o) and tuck libgit.a at the end of the linker command line. As the start-up runtime code needs to find symbol "main", and the linker sees no object files listed has "main", the linker goes in and finds main.o stored in libgit.a (which has "main" defined) and that will end up being linked.

If on the other hand when we link somebody else's program that has its own "main()", we list the object files that make up the program, including the one that has their "main()", before "libgit.a" and the linker does not bother trying to find "main" in libgit.a:main.o so the resulting binary will use their main().

Is that what you are looking for?
Previous: Josh SteadmonNext: Jeff Hostetler
Message 5 of 32 in “Add an external testing library for unit tests”
  1. 0/4 Add an external testing library for unit testssteadmon@google.com, May 17, 2023
  2. 1/4 common-main: split common_exit() into a new filesteadmon@google.com, May 17, 2023
  3. Junio C HamanoMay 18, 2023
  4. Splitting common-main (Was: Re: [PATCH RFC v2 1/4] common-main: split common_exit() into a new file)Josh Steadmon, Jul 14, 2023
  5. Junio C HamanoJul 15, 2023
  6. Jeff HostetlerAug 14, 2023
  7. 2/4 unit tests: Add a project plan documentsteadmon@google.com, May 17, 2023
  8. Phillip WoodMay 18, 2023
  9. Glen ChooMay 18, 2023
  10. Josh SteadmonMay 24, 2023
  11. Phillip WoodJun 1, 2023
  12. 3/4 Add C TAP harnesssteadmon@google.com, May 17, 2023
  13. Phillip WoodMay 18, 2023
  14. Josh SteadmonMay 18, 2023
  15. 4/4 unit test: add basic example and build rulessteadmon@google.com, May 17, 2023
  16. Phillip WoodMay 18, 2023
  17. 0/1 Add a project document for adding unit testsJosh Steadmon, Jun 9, 2023
  18. 1/1 unit tests: Add a project plan documentJosh Steadmon, Jun 9, 2023
  19. Junio C HamanoJun 13, 2023
  20. Josh SteadmonJun 30, 2023
  21. Linus ArverJun 29, 2023
  22. Josh SteadmonJun 29, 2023
  23. Linus ArverJun 30, 2023
  24. Glen ChooJul 6, 2023
  25. Junio C HamanoJul 6, 2023
  26. Glen ChooJul 6, 2023
  27. Josh SteadmonJun 30, 2023
  28. Junio C HamanoJun 29, 2023
  29. Linus ArverJun 30, 2023
  30. Phillip WoodJun 30, 2023
  31. K WanJun 30, 2023
  32. Josh SteadmonJun 30, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.