Note that we cannot cross compile static binaries out of the box yet
with Guix. For instance if we use:
  guix build --target=arm-linux-gnueabihf --file=scripts/guix.scm

We end up with the following error:
    Backtrace:
    In guix/ui.scm:
        462:3 19 (_)
    In ice-9/boot-9.scm:
      1747:15 18 (with-exception-handler
                  #<procedure 9d77630 at ice-9/b…> …)
      1752:10 17 (with-exception-handler _ _ #:unwind? _ # _)
    In guix/ui.scm:
        449:6 16 (_)
    In guix/scripts/build.scm:
        715:5 15 (_)
    In srfi/srfi-1.scm:
       673:15 14 (append-map _ _ . _)
       586:17 13 (map1 ("i686-linux"))
    In guix/scripts/build.scm:
       717:21 12 (_ _)
    In guix/store.scm:
      1377:11 11 (map/accumulate-builds
                  #<store-connection 256.99 82587…> …)
       1295:8 10 (call-with-build-handler
                  #<procedure 9d77600 at guix/s…> …)
    In guix/scripts/build.scm:
       676:18  9 (_ _)
    In guix/store.scm:
      2165:25  8 (run-with-store
                  #<store-connection 256.99 8258758> #<p…> …)
      1993:13  7 (_ #<store-connection 256.99 8258758>)
    In guix/packages.scm:
      1899:40  6 (_)
      1517:16  5 (package->bag _ _ _ #:graft? _)
      1614:22  4 (thunk)
      1353:35  3 (lower* . _)
    In ice-9/boot-9.scm:
      1685:16  2 (raise-exception _ #:continuable? _)
      1685:16  1 (raise-exception _ #:continuable? _)
      1685:16  0 (raise-exception _ #:continuable? _)

    ice-9/boot-9.scm:1685:16: In procedure raise-exception:
    In procedure struct-vtable: Wrong type argument in position 1
    (expecting struct): #f

But if we remove the transformed packages from guix.scm, for instance by
removing them from the list of packages to build at the end of the file:
     (list libsamsung-ipc
           libsamsung-ipc-static
    -      (with-fixed-android-make-stub libsamsung-ipc-clang-android)
    -      (with-fixed-android-make-stub libsamsung-ipc-gcc-android)
           libsamsung-ipc-clang-autotools
           libsamsung-ipc-gcc-autotools)
cross compilation then works.

Thanks to the help from people on #autotools on liberachat for
suggestions on how to improve this patch.

Signed-off-by: Denis 'GNUtoo' Carikli <gnu...@cyberdimension.org>
---
 configure.ac                  | 18 ++++++++-
 samsung-ipc/tests/Makefile.am |  3 +-
 scripts/guix.scm              | 70 ++++++++++++++++++++++++++++++++++-
 tools/Makefile.am             |  6 +--
 tools/ipc-modem/Makefile.am   |  2 +-
 5 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8d84140..f164c53 100644
--- a/configure.ac
+++ b/configure.ac
@@ -28,6 +28,10 @@ AC_SUBST(OPENSSL_LIBS)
 # strict cflags
 AC_SUBST(STRICT_CFLAGS)
 
+#------------------------------------------------------------------------------
+# Static build of test utilities
+AC_SUBST(ALL_STATIC_LDFLAGS)
+
 #------------------------------------------------------------------------------
 # python
 AC_SUBST(PYTHON3)
@@ -57,6 +61,14 @@ AC_ARG_ENABLE(debug,
   [debug="no"])
 AM_CONDITIONAL( [WANT_DEBUG], [test x"$debug" = x"yes"])
 
+#------------------------------------------------------------------------------
+
+AC_ARG_ENABLE(static-progs,
+  [AS_HELP_STRING([--enable-static-progs],
+                  [Build static (test) tools (default=disabled)])],
+  [static_progs=$enableval],
+  [static_progs="no"])
+
 #------------------------------------------------------------------------------
 AC_ARG_ENABLE(strict-cflags,
   [AS_HELP_STRING([--enable-strict-cflags],
@@ -132,6 +144,9 @@ AC_CONFIG_FILES([
 
 
 #------------------------------------------------------------------------------
+AS_IF([test x"$static_progs" = x"yes"],
+      [ALL_STATIC_LDFLAGS=-all-static], [])
+
 AS_IF([test x"$strict_cflags" = x"yes"],
       [STRICT_CFLAGS=`$srcdir/strict-cflags.sh`], [])
 
@@ -153,6 +168,7 @@ AC_PROG_LIBTOOL
 AC_SUBST(CFLAGS)
 AC_SUBST(CPPFLAGS)
 AC_SUBST(LDFLAGS)
+AC_SUBST(ALL_STATIC_LDFLAGS)
 
 AC_OUTPUT
 #------------------------------------------------------------------------------
@@ -165,7 +181,7 @@ echo
 echo "Compiler flags:"
 echo
 echo "  CFLAGS..................: $CFLAGS $STRICT_CFLAGS"
-echo "  LDFLAGS.................: $LDFLAGS"
+echo "  LDFLAGS.................: $LDFLAGS $ALL_STATIC_LDFLAGS"
 echo
 echo
 echo "Interpreters paths:"
diff --git a/samsung-ipc/tests/Makefile.am b/samsung-ipc/tests/Makefile.am
index 494be9a..ad4280e 100644
--- a/samsung-ipc/tests/Makefile.am
+++ b/samsung-ipc/tests/Makefile.am
@@ -7,6 +7,8 @@ AM_CFLAGS = \
        $(OPENSSL_CFLAGS) \
        $(NULL)
 
+AM_LDFLAGS = $(ALL_STATIC_LDFLAGS)
+
 if WANT_STRICT_CFLAGS
 AM_CFLAGS += $(STRICT_CFLAGS)
 endif
@@ -24,7 +26,6 @@ libsamsung_ipc_test_SOURCES = \
        $(NULL)
 
 libsamsung_ipc_test_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
-libsamsung_ipc_test_LDFLAGS =
 
 # TODO: Find a way to make test more modular and represent each run of
 # libsamsung-ipc-test in TEST while having it implemented in a single
diff --git a/scripts/guix.scm b/scripts/guix.scm
index b908571..bc2f7c3 100644
--- a/scripts/guix.scm
+++ b/scripts/guix.scm
@@ -196,6 +196,72 @@ found in many Samsung smartphones and tablets.")
     (home-page "https://www.replicant.us";)
     (license license:gpl2+)))
 
+(define is-file-static
+  #~(lambda (readelf path)
+      ;; str is also eof if the file doesn't exist
+      (if (not (file-exists? tool)) #f
+          (let* ((port (open-input-pipe (string-append readelf " --dyn-syms "
+                                                       path)))
+                 (str (read-line port)))
+            (close-pipe port)
+            (eof-object? str)))))
+
+(define-public libsamsung-ipc-static
+  (package
+    (inherit libsamsung-ipc)
+    (name "libsamsung-ipc-static")
+    (native-inputs (list autoconf
+                         automake
+                         binutils
+                         ddrescue
+                         libtool
+                         pkg-config
+                         python
+                         python-sh))
+    (inputs (list curl openssl
+                  `(,(canonical-package openssl) "static")))
+    (arguments
+     (list #:modules '((ice-9 popen)
+                       (ice-9 rdelim)
+                       (guix build utils)
+                       (guix build gnu-build-system))
+           #:tests? #f
+           #:phases #~(modify-phases %standard-phases
+                        ;; https-send-sms depends on curl and Guix doesn't have
+                        ;; a static libcurl.
+                        (add-after 'unpack 'remove-https-send-sms
+                          (lambda _
+                            (substitute* "tools/Makefile.am"
+                              (("https-send-sms \\\\")
+                               "\\"))))
+                        (add-after 'compress-documentation 'check-static-files
+                          (lambda _
+                            (display "Checking static files:\n")
+                            (map-in-order (lambda (tool)
+                                            (if (#$is-file-static
+                                                 (string-append 
#$(this-package-native-input
+                                                                   "binutils")
+                                                  "/bin/readelf") tool)
+                                                (display (string-append
+                                                          "[ OK ] "
+                                                          (basename tool) ": "
+                                                          tool "\n"))
+                                                ((lambda _
+                                                   (display (string-append
+                                                             "[ !! ] "
+                                                             (basename tool)
+                                                             ": " tool "\n"))
+                                                   (#f)))))
+                                          (list
+                                           
"samsung-ipc/tests/libsamsung-ipc-test"
+                                           "tools/ipc-test"
+                                           "tools/nv_data-imei"
+                                           "tools/nv_data-md5"
+                                           "tools/ipc-modem/ipc-modem")))))
+           #:configure-flags #~(list "--enable-debug" "--enable-static=yes"
+                                     "--enable-shared=no"
+                                     "--enable-static-progs")))))
+
 (define-public libsamsung-ipc-gcc-android
   (package
     (inherit libsamsung-ipc)
@@ -262,6 +328,8 @@ found in many Samsung smartphones and tablets.")
 ;; 
+--------------------------------+----------+----------+--------------+--------------+
 
 (list libsamsung-ipc
+      libsamsung-ipc-static
       (with-fixed-android-make-stub libsamsung-ipc-clang-android)
       (with-fixed-android-make-stub libsamsung-ipc-gcc-android)
-      libsamsung-ipc-clang-autotools libsamsung-ipc-gcc-autotools)
+      libsamsung-ipc-clang-autotools
+      libsamsung-ipc-gcc-autotools)
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 911ea30..024df50 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -6,6 +6,8 @@ AM_CFLAGS = \
        $(LIBCURL_CFLAGS) \
        $(NULL)
 
+AM_LDFLAGS = $(ALL_STATIC_LDFLAGS)
+
 if WANT_STRICT_CFLAGS
 AM_CFLAGS += $(STRICT_CFLAGS)
 endif
@@ -31,16 +33,12 @@ EXTRA_DIST += $(TESTS)
 
 https_send_sms_SOURCES = https-send-sms.c
 https_send_sms_LDADD = $(LIBCURL_LIBS)
-https_send_sms_LDFLAGS =
 
 ipc_test_SOURCES = ipc-test.c
 ipc_test_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
-ipc_test_LDFLAGS =
 
 nv_data_md5_SOURCES = nv_data-md5.c
 nv_data_md5_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
-nv_data_md5_LDFLAGS =
 
 nv_data_imei_SOURCES = nv_data-imei.c nv_data-imei.h
 nv_data_imei_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
-nv_data_imei_LDFLAGS =
diff --git a/tools/ipc-modem/Makefile.am b/tools/ipc-modem/Makefile.am
index efced7e..64cde49 100644
--- a/tools/ipc-modem/Makefile.am
+++ b/tools/ipc-modem/Makefile.am
@@ -25,4 +25,4 @@ ipc-modem-log.c \
 ipc-modem-log.h
 
 ipc_modem_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
-ipc_modem_LDFLAGS = -lpthread
+ipc_modem_LDFLAGS = -lpthread $(ALL_STATIC_LDFLAGS)
-- 
2.39.1

_______________________________________________
Replicant mailing list
Replicant@osuosl.org
https://lists.osuosl.org/mailman/listinfo/replicant

Reply via email to