zstd icon indicating copy to clipboard operation
zstd copied to clipboard

How to build zstd pure single binary staic ?

Open bhzhu203 opened this issue 3 years ago • 4 comments

How to build zstd pure single binary staic ?

I can not find the way to build zstd staic .

The meson build either not working : meson setup -Dbin_programs=true -Dbin_contrib=true -Dstatic_runtime=true builddir DESTDIR=./staging ninja install ,but showing this:

''' ❰bhzhu❙~/source/zstd/build/meson/builddir/staging/usr/local/bin(git≠dev)❱✔≻ ldd zstd linux-vdso.so.1 (0x00007ffff7ffd000) /libjemalloc.so (0x00007ffff7d3a000) libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007ffff7a1a000) libz.so.1 => /lib/libz.so.1 (0x00007ffff7801000) liblzma.so.5 => /lib/liblzma.so.5 (0x00007ffff75dc000) liblz4.so.1 => /usr/lib/liblz4.so.1 (0x00007ffff73c2000) libpthread.so.0 => /lib/libpthread.so.0 (0x00007ffff71a4000) libc.so.6 => /lib/libc.so.6 (0x00007ffff6ddf000) libm.so.6 => /lib/libm.so.6 (0x00007ffff6aa0000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007ffff688c000) libdl.so.2 => /lib/libdl.so.2 (0x00007ffff6688000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007ffff666e000) /lib64/ld-linux-x86-64.so.2 => /lib//ld-linux-x86-64.so.2 (0x0000555555400000) ❰bhzhu❙~/source/zstd/build/meson/builddir/staging/usr/local/bin(git≠dev)❱✔≻ ldd pzstd linux-vdso.so.1 (0x00007ffff7ffd000) /libjemalloc.so (0x00007ffff7d3a000) libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007ffff7a1a000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007ffff7806000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007ffff77ec000) libpthread.so.0 => /lib/libpthread.so.0 (0x00007ffff75ce000) libc.so.6 => /lib/libc.so.6 (0x00007ffff720b000) libm.so.6 => /lib/libm.so.6 (0x00007ffff6eca000) libdl.so.2 => /lib/libdl.so.2 (0x00007ffff6cc6000) /lib64/ld-linux-x86-64.so.2 => /lib//ld-linux-x86-64.so.2 (0x0000555555400000) ❰bhzhu❙~/source/zstd/build/meson/builddir/staging/usr/local/bin(git≠dev)❱✔≻ '''

Golang and rustlang can build the binary staic , the staic binary can run on many Linux Distros

bhzhu203 avatar May 13 '22 06:05 bhzhu203

The static_runtime option specifies in the help description:

$ meson configure
[...]
  static_runtime                  false                             [true, false]                     Link to static run-time libraries on MSVC
[...]

Note it's specifically calling out MSVC. The only thing this option does is set /MT as a compile option. Really, it's a duplicate of Meson's builtin -Db_vscrt=mt option.

Golang and rustlang can build the binary staic , the staic binary can run on many Linux Distros

They do that by privately building a copy of all those dependencies.

Do you have static versions of a system libz, liblz4, liblzma etc. installed?

As with any C program, the typical way to ensure building fully statically is to add LDFLAGS=-static before configuring the project; Meson supports this. In current development versions of Meson, you can also pass the builtin option -Dprefer_static=true to make sure dependencies are found statically, if possible.

eli-schwartz avatar May 13 '22 06:05 eli-schwartz

Thank you for your response. @eli-schwartz

  1. I have tried the build option in the root directory (LDFLAGS='-static' make) :
    Single thread make
make[1]: Entering directory '/home/bhzhu/source/zstd/lib'
compiling multi-threaded dynamic library 1.5.2
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
collect2: error: ld returned 1 exit status

Multi thread make to have a chance to build the static binary zstd sucessfully ,but seems not completely(LDFLAGS=-static make -j10)

compiling multi-threaded dynamic library 1.5.2
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:161: obj/conf_a89fc3ff9cd173be78320e35325feb62/dynamic/libzstd.so.1.5.2] Error 1
make[1]: *** [Makefile:148: libzstd.so.1.5.2] Error 2
make[1]: Leaving directory '/home/bhzhu/source/zstd/lib'
make: *** [Makefile:63: lib-release] Error 2
make: *** Waiting for unfinished jobs....
==> building with threading support
==> no zlib, building zstd without .gz support
==> no liblzma, building zstd without .xz/.lzma support
==> building zstd with .lz4 compression support
LINK obj/conf_5c3762ada301f6b269c0289f2483dc85/zstd
zstd build completed
make[1]: Leaving directory '/home/bhzhu/source/zstd/programs'
  1. meson build is also failed LDFLAGS='-static' meson build ; cd build ; ninja
ninja 
[1/4] Linking target lib/libzstd.so.1.5.2
FAILED: lib/libzstd.so.1.5.2 
cc  -o lib/libzstd.so.1.5.2 lib/libzstd.so.1.5.2.p/.._.._.._lib_common_entropy_common.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_fse_decompress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_threading.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_pool.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_zstd_common.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_error_private.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_common_xxhash.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_hist.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_fse_compress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_huf_compress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_compress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_compress_literals.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_compress_sequences.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_compress_superblock.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstdmt_compress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_fast.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_double_fast.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_lazy.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_opt.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_compress_zstd_ldm.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_decompress_huf_decompress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_decompress_zstd_decompress.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_decompress_zstd_decompress_block.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_decompress_zstd_ddict.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_dictBuilder_cover.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_dictBuilder_fastcover.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_dictBuilder_divsufsort.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_dictBuilder_zdict.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_decompress_huf_decompress_amd64.S.o lib/libzstd.so.1.5.2.p/.._.._.._lib_legacy_zstd_v05.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_legacy_zstd_v06.c.o lib/libzstd.so.1.5.2.p/.._.._.._lib_legacy_zstd_v07.c.o -Wl,--as-needed -Wl,--no-undefined -Wl,-O1 -shared -fPIC -Wl,--start-group -Wl,-soname,libzstd.so.1 -Wl,--end-group -static -pthread
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Seems that the build scripts of zstd is not fully compatible with '-static' option . If there is no dependencies static library , could the build scripts report that ? Thanks

bhzhu203 avatar May 16 '22 02:05 bhzhu203

You will also need to configure meson to build the library as a static library using -Ddefault_library=static. For example I configured zstd like this:

$ LDFLAGS=-static meson setup builddir-st -Ddefault_library=static -Dzlib=disabled -Dlzma=disabled -Dlz4=disabled
$ ninja -C builddir-st
ninja: Entering directory `builddir-st'
[50/50] Linking target programs/zstd
$ readelf -d builddir-st/programs/zstd

There is no dynamic section in this file.
$ builddir-st/programs/zstd --help
*** zstd command line interface 64-bits v1.5.2, by Yann Collet ***

(I don't have static versions of zlib/lzma/lz4 so I simply skipped those features.)

eli-schwartz avatar May 16 '22 04:05 eli-schwartz

Thanks for your comment now I can build it in multi stage Dockerfile

FROM gcc:12 AS zstd_build

ARG ZTSD_SOURCE_URL="https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz"

RUN apt-get update && \
      apt-get install --no-install-recommends --assume-yes python3 ninja-build && \
      apt-get clean && \
    curl -o /tmp/get-pip.py -L 'https://bootstrap.pypa.io/get-pip.py' && \
      python3 /tmp/get-pip.py && \
    pip3 install meson && \
    mkdir -p /tmp/zstd && \
    cd /tmp/zstd && \
    curl -Lo zstd.source $ZTSD_SOURCE_URL && \
    file zstd.source | grep -q 'gzip compressed data' && mv zstd.source zstd.source.gz && gzip -d zstd.source.gz && \
    file zstd.source | grep -q 'tar archive' && mv zstd.source zstd.source.tar && tar -xf zstd.source.tar --strip-components=1 && rm zstd.source.tar && \
    LDFLAGS=-static \
    meson setup \
      -Dbin_programs=true \
      -Dstatic_runtime=true \
      -Ddefault_library=static \
      -Dzlib=disabled -Dlzma=disabled -Dlz4=disabled \
      build/meson builddir-st && \
    ninja -C builddir-st && \
    ninja -C builddir-st install && \
    /usr/local/bin/zstd -v

PikachuEXE avatar Aug 16 '22 07:08 PikachuEXE