diff --git a/meson.build b/meson.build index 9db764f00..c6f53eba0 100644 --- a/meson.build +++ b/meson.build @@ -17,6 +17,19 @@ # # Finally, src/nix/meson.build defines the Nix command itself, relying on all prior meson files. # +# libstore, libexpr, and libfetchers have some special handling to make static builds work. +# Their use static constructors for dynamic registration of primops, store backends, etc +# gets borked during static link. We can't simply wholesale apply `link_whole :` either, +# because these libraries get linked multiple times since Lix's components are transitively +# dependent. So instead, each of those libraries have two dependency objects: +# liblix{store,expr,fetchers} and liblix{store,expr,fetchers}_mstatic ("maybe static"). +# The _mstatic versions should be used in the `dependencies :` arguments to ALL EXECUTABLES +# but executables ONLY. When we are not building statically (default_library != 'static'), +# they are equivalent. When we are building statically, the _mstatic version will be +# `link_whole :` rather than `link_with :`. +# FIXME: This hack should be removed when https://git.lix.systems/lix-project/lix/issues/359 +# is fixed. +# # Unit tests are setup in tests/unit/meson.build, under the test suite "check". # # Functional tests are a bit more complicated. Generally they're defined in @@ -79,6 +92,8 @@ if not fs.is_absolute(sysconfdir) sysconfdir = '/' / sysconfdir endif +is_static = get_option('default_library') == 'static' + # All of this has to go before the rest of the dependency checking, # so that internal-api-docs can be built with -Denable-build=false diff --git a/src/libexpr/meson.build b/src/libexpr/meson.build index fda6fde2c..6845bab32 100644 --- a/src/libexpr/meson.build +++ b/src/libexpr/meson.build @@ -161,6 +161,16 @@ liblixexpr = declare_dependency( link_with : libexpr, ) +# FIXME: remove when https://git.lix.systems/lix-project/lix/issues/359 is fixed. +if is_static + liblixexpr_mstatic = declare_dependency( + include_directories : include_directories('.'), + link_whole : libexpr, + ) +else + liblixexpr_mstatic = liblixexpr +endif + # FIXME: not using the pkg-config module because it creates way too many deps # while meson migration is in progress, and we want to not include boost here configure_file( diff --git a/src/libfetchers/meson.build b/src/libfetchers/meson.build index dbb85b84c..1e5aa60f8 100644 --- a/src/libfetchers/meson.build +++ b/src/libfetchers/meson.build @@ -55,3 +55,13 @@ liblixfetchers = declare_dependency( include_directories : include_directories('.'), link_with : libfetchers, ) + +# FIXME: remove when https://git.lix.systems/lix-project/lix/issues/359 is fixed. +if is_static + liblixfetchers_mstatic = declare_dependency( + include_directories : include_directories('.'), + link_whole : libfetchers, + ) +else + liblixfetchers_mstatic = liblixfetchers +endif diff --git a/src/libstore/meson.build b/src/libstore/meson.build index f776e9621..335d86c10 100644 --- a/src/libstore/meson.build +++ b/src/libstore/meson.build @@ -233,6 +233,16 @@ liblixstore = declare_dependency( link_with : libstore, ) +# FIXME: remove when https://git.lix.systems/lix-project/lix/issues/359 is fixed. +if is_static + liblixstore_mstatic = declare_dependency( + include_directories : include_directories('.'), + link_whole : libstore, + ) +else + liblixstore_mstatic = liblixstore +endif + # FIXME: not using the pkg-config module because it creates way too many deps # while meson migration is in progress, and we want to not include boost here configure_file( diff --git a/src/nix/meson.build b/src/nix/meson.build index e41399b5d..c6e97a555 100644 --- a/src/nix/meson.build +++ b/src/nix/meson.build @@ -82,9 +82,9 @@ nix = executable( dependencies : [ liblixcmd, liblixutil, - liblixstore, - liblixexpr, - liblixfetchers, + liblixstore_mstatic, + liblixexpr_mstatic, + liblixfetchers_mstatic, liblixmain, boehm, nlohmann_json, diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 35a11a5d3..bbe99561c 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -64,7 +64,7 @@ libutil_tester = executable( gtest, boehm, liblixutil, - liblixexpr, + liblixexpr_mstatic, liblixutil_test_support, nlohmann_json, ], @@ -129,7 +129,7 @@ libstore_tester = executable( dependencies : [ liblixstore_test_support, liblixutil_test_support, - liblixstore, + liblixstore_mstatic, liblixutil, rapidcheck, gtest, @@ -190,10 +190,10 @@ libexpr_tester = executable( dependencies : [ liblixexpr_test_support, liblixstore_test_support, - liblixstore, + liblixstore_mstatic, liblixutil, - liblixexpr, - liblixfetchers, + liblixexpr_mstatic, + liblixfetchers_mstatic, rapidcheck, boehm, gtest, @@ -220,8 +220,8 @@ libcmd_tester = executable( liblixcmd, liblixutil, liblixmain, - liblixexpr, - liblixstore, + liblixexpr_mstatic, + liblixstore_mstatic, gtest, boost, ],