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

[PATCH 03/11] meson: simplify use of the common-main library

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 29, 2025, 07:11 UTC
Message-ID
<20250129-b4-pks-meson-improvements-v1-3-ab709f0be12c@pks.im>
In-Reply-To
<20250129-b4-pks-meson-improvements-v1-0-ab709f0be12c@pks.im>

The "common-main.c" file is used by multiple executables. In order to make it easy to set it up we have created a separate library that these executables can link against. All of these executables also want to link against `libgit.a` though, which makes it necessary to specify both of these as dependencies for every executable.

Simplify this a bit by declaring the library as a source dependency: instead of creating a static library, we now instead compile the common set of files into each executable separately.

This change surfaces an issue when linking aliases for git-remote-http: we extract all objects from `git-remote-http` et al and then link them into the new executable. As such, these objects would already contain a `main()` function. But now that we also compile "common-main.c" into these aliased executables we see a linker error due to `main()` being defined twice. We fix this by only linking against `libgit.a`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 meson.build          | 34 +++++++++++++++-------------------
 t/helper/meson.build |  4 ++--
 t/meson.build        |  4 ++--
 3 files changed, 19 insertions(+), 23 deletions(-)
diff --git a/meson.build b/meson.build
index 84d100fd25..82b6e62029 100644
--- a/meson.build
+++ b/meson.build
@@ -1570,15 +1570,11 @@ if host_machine.system() == 'windows'
     error('Unsupported compiler ' + compiler.get_id())
   endif
 endif
-common_main_library = static_library('common-main',
+
+libgit_commonmain = declare_dependency(
   sources: common_main_sources,
-  c_args: libgit_c_args,
-  dependencies: libgit_dependencies,
-  include_directories: libgit_include_directories,
-)
-common_main = declare_dependency(
-  link_with: common_main_library,
   link_args: common_main_link_args,
+  dependencies: [ libgit ],
 )
 
 bin_wrappers = [ ]
@@ -1586,7 +1582,7 @@ test_dependencies = [ ]
 
 git = executable('git',
   sources: builtin_sources + 'git.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
@@ -1594,35 +1590,35 @@ bin_wrappers += git
 
 test_dependencies += executable('git-daemon',
   sources: 'daemon.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
 
 test_dependencies += executable('git-sh-i18n--envsubst',
   sources: 'sh-i18n--envsubst.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
 
 bin_wrappers += executable('git-shell',
   sources: 'shell.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
 
 test_dependencies += executable('git-http-backend',
   sources: 'http-backend.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
 
 bin_wrappers += executable('scalar',
   sources: 'scalar.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
@@ -1635,7 +1631,7 @@ if get_option('curl').enabled()
 
   git_remote_http = executable('git-remote-http',
     sources: curl_sources + 'remote-curl.c',
-    dependencies: [libgit, common_main],
+    dependencies: [libgit_commonmain],
     install: true,
     install_dir: get_option('libexecdir') / 'git-core',
   )
@@ -1643,7 +1639,7 @@ if get_option('curl').enabled()
 
   test_dependencies += executable('git-http-fetch',
     sources: curl_sources + 'http-fetch.c',
-    dependencies: [libgit, common_main],
+    dependencies: [libgit_commonmain],
     install: true,
     install_dir: get_option('libexecdir') / 'git-core',
   )
@@ -1651,7 +1647,7 @@ if get_option('curl').enabled()
   if expat.found()
     test_dependencies += executable('git-http-push',
       sources: curl_sources + 'http-push.c',
-      dependencies: [libgit, common_main],
+      dependencies: [libgit_commonmain],
       install: true,
       install_dir: get_option('libexecdir') / 'git-core',
     )
@@ -1660,7 +1656,7 @@ if get_option('curl').enabled()
   foreach alias : [ 'git-remote-https', 'git-remote-ftp', 'git-remote-ftps' ]
     test_dependencies += executable(alias,
       objects: git_remote_http.extract_all_objects(recursive: false),
-      dependencies: [libgit, common_main],
+      dependencies: [libgit],
     )
 
     install_symlink(alias + executable_suffix,
@@ -1677,7 +1673,7 @@ endif
 
 test_dependencies += executable('git-imap-send',
   sources: imap_send_sources,
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
   install: true,
   install_dir: get_option('libexecdir') / 'git-core',
 )
@@ -1685,7 +1681,7 @@ test_dependencies += executable('git-imap-send',
 foreach alias : [ 'git-receive-pack', 'git-upload-archive', 'git-upload-pack' ]
   bin_wrappers += executable(alias,
     objects: git.extract_all_objects(recursive: false),
-    dependencies: [libgit, common_main],
+    dependencies: [libgit],
   )
 
   install_symlink(alias + executable_suffix,
diff --git a/t/helper/meson.build b/t/helper/meson.build
index 5e83884246..05bf35bd26 100644
--- a/t/helper/meson.build
+++ b/t/helper/meson.build
@@ -78,14 +78,14 @@ test_tool_sources = [
 
 test_tool = executable('test-tool',
   sources: test_tool_sources,
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
 )
 bin_wrappers += test_tool
 test_dependencies += test_tool
 
 test_fake_ssh = executable('test-fake-ssh',
   sources: 'test-fake-ssh.c',
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
 )
 bin_wrappers += test_fake_ssh
 test_dependencies += test_fake_ssh
diff --git a/t/meson.build b/t/meson.build
index 14fea8dddf..8896314b88 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -39,7 +39,7 @@ clar_sources += custom_target(
 
 clar_unit_tests = executable('unit-tests',
   sources: clar_sources + clar_test_suites,
-  dependencies: [libgit, common_main],
+  dependencies: [libgit_commonmain],
 )
 test('unit-tests', clar_unit_tests)
 
@@ -72,7 +72,7 @@ foreach unit_test_program : unit_test_programs
       'unit-tests/lib-reftable.c',
       unit_test_program,
     ],
-    dependencies: [libgit, common_main],
+    dependencies: [libgit_commonmain],
   )
   test(unit_test_name, unit_test,
     workdir: meson.current_source_dir(),
-- 
2.48.1.362.g079036d154.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 7 of 35 in “meson: cleanups, improvements, smallish fixes”
  1. 00/11 meson: cleanups, improvements, smallish fixesPatrick Steinhardt, Jan 29, 2025
  2. 01/11 meson: fix exec path with enabled runtime prefixPatrick Steinhardt, Jan 29, 2025
  3. Justin ToblerJan 29, 2025
  4. Patrick SteinhardtJan 30, 2025
  5. 02/11 meson: inline the static 'git' libraryPatrick Steinhardt, Jan 29, 2025
  6. 04/11 meson: stop linking libcurl into all executablesPatrick Steinhardt, Jan 29, 2025
  7. 03/11 meson: simplify use of the common-main libraryPatrick Steinhardt, Jan 29, 2025
  8. 05/11 meson: introduce `libgit_curl` dependencyPatrick Steinhardt, Jan 29, 2025
  9. 07/11 meson: deduplicate the list of required programsPatrick Steinhardt, Jan 29, 2025
  10. Justin ToblerJan 29, 2025
  11. 06/11 meson: drop separate version libraryPatrick Steinhardt, Jan 29, 2025
  12. 08/11 meson: simplify setup of PATH environment variablePatrick Steinhardt, Jan 29, 2025
  13. Justin ToblerJan 29, 2025
  14. Patrick SteinhardtJan 30, 2025
  15. 09/11 meson: prevent finding sed(1) in a loopPatrick Steinhardt, Jan 29, 2025
  16. 10/11 meson: fix overwritten `git` variablePatrick Steinhardt, Jan 29, 2025
  17. 11/11 meson: consistently use custom program paths to resolve programsPatrick Steinhardt, Jan 29, 2025
  18. 00/13 meson: cleanups, improvements, smallish fixesPatrick Steinhardt, Jan 30, 2025
  19. 02/13 meson: fix OpenSSL fallback when not explicitly requiredPatrick Steinhardt, Jan 30, 2025
  20. 01/13 meson: fix exec path with enabled runtime prefixPatrick Steinhardt, Jan 30, 2025
  21. 03/13 meson: inline the static 'git' libraryPatrick Steinhardt, Jan 30, 2025
  22. 04/13 meson: simplify use of the common-main libraryPatrick Steinhardt, Jan 30, 2025
  23. 06/13 meson: stop linking libcurl into all executablesPatrick Steinhardt, Jan 30, 2025
  24. 05/13 meson: introduce `libgit_curl` dependencyPatrick Steinhardt, Jan 30, 2025
  25. 07/13 meson: drop separate version libraryPatrick Steinhardt, Jan 30, 2025
  26. Toon ClaesFeb 7, 2025
  27. 08/13 meson: improve PATH handlingPatrick Steinhardt, Jan 30, 2025
  28. 09/13 meson: improve handling of `sane_tool_path` optionPatrick Steinhardt, Jan 30, 2025
  29. Toon ClaesFeb 7, 2025
  30. Patrick SteinhardtFeb 7, 2025
  31. 10/13 meson: prevent finding sed(1) in a loopPatrick Steinhardt, Jan 30, 2025
  32. 11/13 meson: fix overwritten `git` variablePatrick Steinhardt, Jan 30, 2025
  33. 12/13 meson: consistently use custom program paths to resolve programsPatrick Steinhardt, Jan 30, 2025
  34. 13/13 gitlab-ci: restrict maximum number of link jobs on WindowsPatrick Steinhardt, Jan 30, 2025
  35. Justin ToblerFeb 7, 2025

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.