upgrade-apply: Make binary fully static 74/318574/3
authorAntoni <a.adaszkiewi@samsung.com>
Thu, 16 Jan 2025 14:50:40 +0000 (15:50 +0100)
committerMateusz Moscicki <m.moscicki2@samsung.com>
Wed, 19 Feb 2025 13:43:09 +0000 (14:43 +0100)
This change ensures that `upgrade-apply` binary provided in a delta
will run on a system with an older GLib version.

Building with this change produces following warnings:
[   12s] /usr/lib64/gcc/aarch64-tizen-linux-gnu/14.2.0/../../../../aarch64-tizen-linux-gnu/bin/ld: /lib/../lib64/libtar.a(encode.o): in function `th_set_group':
[   12s] (.text.th_set_group+0x18): warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
[   12s] /usr/lib64/gcc/aarch64-tizen-linux-gnu/14.2.0/../../../../aarch64-tizen-linux-gnu/bin/ld: /lib/../lib64/libtar.a(decode.o): in function `th_get_gid':
[   12s] (.text.th_get_gid+0x58): warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
[   12s] /usr/lib64/gcc/aarch64-tizen-linux-gnu/14.2.0/../../../../aarch64-tizen-linux-gnu/bin/ld: /lib/../lib64/libtar.a(decode.o): in function `th_get_uid':
[   12s] (.text.th_get_uid+0x58): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
[   12s] /usr/lib64/gcc/aarch64-tizen-linux-gnu/14.2.0/../../../../aarch64-tizen-linux-gnu/bin/ld: /lib/../lib64/libtar.a(encode.o): in function `th_set_user':
[   12s] (.text.th_set_user+0x18): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

I have verified that this binary does not call these functions,
nor does it access libc dynamically at runtime.

Change-Id: Ib9d8834af38f57acfb7f3260bb8142197b35f819

packaging/upgrade.spec
src/upgrade-apply/CMakeLists.txt

index 4c1ee7dfde49e0a05fb263aed4d1c399583a9578..d5968879bc8e4968059d24b2468d9871bd9d75a8 100644 (file)
@@ -19,6 +19,8 @@ Buildrequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(hal-api-device)
 BuildRequires:  libtar-devel
 BuildRequires:  gtest-devel
+BuildRequires:  glibc-devel-static
+BuildRequires:  zlib-devel-static
 
 Requires:       upgrade-engine = %{version}-%{release}
 Requires:       parse-dynparts = %{version}-%{release}
index d42afa84d4db8525f45bd3ac3db13555c1002491..b01d6b0f4353a87c7171d45dff66f9d08c993989 100644 (file)
@@ -1,7 +1,6 @@
 find_package(PkgConfig)
-pkg_check_modules(DEPS REQUIRED IMPORTED_TARGET zlib)
 
-ADD_DEFINITIONS("-D_FILE_OFFSET_BITS=64")
+add_definitions("-D_FILE_OFFSET_BITS=64")
 
 set(
        upgrade-apply_SRCS
@@ -10,24 +9,21 @@ set(
        patch/brotli.c
        sha1/sha1.c
 )
+
 add_executable(upgrade-apply ${upgrade-apply_SRCS})
-target_link_libraries(upgrade-apply PRIVATE PkgConfig::DEPS)
-# Unfortunately, libtar is neither CMake- nor pkgconfig-aware.
-# Even more unfortunately, pkgconfig detection of static libraries is broken for all version of CMake before 3.24,
-# and as of writing 3.24 is not even released yet (see https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7070).
-# This would require a bit of hacking (but also libc.a) if we wanted full static linking,
-# but here we need something even different: static linking for all but libc.
-# This requires especially brutal hacks. Maybe we should use Meson next time? ;)
-# In the meantime, TODO: make this better.
-# Also, note that zlib has been used in the normal way (i.e. with dynamic linking).
-# That's because the current setup does have libz.so in the execution environment.
-# Though in the future, it might be good to make it consistent (TODO).
 
 if(DEFINE_HOST_BUILD)
-set(ADD_LIBS libbrotlidec.a libbrotlicommon.a)
+       target_link_libraries(upgrade-apply PRIVATE libtar.a libz.a libbrotlidec.a libbrotlicommon.a)
 else()
-set(ADD_LIBS libbrotlidec-static.a libbrotlicommon-static.a)
+       # Without this cmake inserts `-Bdynamic` at the end of link command, and the binary isn't fully static
+       set_target_properties(upgrade-apply PROPERTIES
+               LINK_SEARCH_START_STATIC ON
+               LINK_SEARCH_END_STATIC ON
+       )
+
+       target_link_options(upgrade-apply PRIVATE "-static")
+
+       target_link_libraries(upgrade-apply PRIVATE libtar.a libz.a libbrotlidec-static.a libbrotlicommon-static.a)
 endif()
 
-target_link_libraries(upgrade-apply PRIVATE libtar.a ${ADD_LIBS})
-install(TARGETS upgrade-apply)
+install(TARGETS upgrade-apply)
\ No newline at end of file