greshake / i3status-rust

Very resourcefriendly and feature-rich replacement for i3status, written in pure Rust
GNU General Public License v3.0
2.86k stars 472 forks source link

Produce Static Binaries for Releases #401

Open greshake opened 5 years ago

greshake commented 5 years ago

I tried setting up static builds using Docker containers based on either Alpine and Ubuntu with musl versions of Rust. Unfortunately the builds fail because proc_macro used by serde_derive can't be used in static builds. I'm not quite sure if there will be a workaround for this.

You can try this yourself with this Dockerfile (build with docker build -t i3status-rust .):

FROM alpine:3.10

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories

RUN apk update && \
    apk add dbus-dev pulseaudio rustup

RUN /usr/bin/rustup-init -y \
    --default-host x86_64-unknown-linux-musl \
    --default-toolchain stable

Execute this from the main repo folder to get a build:

docker run --rm -it -v "$(pwd)":/src i3status-rust sh -c 'cd src && cargo build --release'
ammgws commented 4 years ago

@atheriel I saw this comment today and gave it a go - it built successfully.

Dockerfile:

FROM alpine:3.11

RUN apk update && \
    apk add dbus-dev pulseaudio build-base

RUN apk add --update-cache \
--repository https://alpine.global.ssl.fastly.net/alpine/edge/community \
rustup

RUN /usr/bin/rustup-init -y \
    --default-host x86_64-unknown-linux-musl \
    --default-toolchain stable

ENV PATH /root/.cargo/bin:$PATH

docker run --rm -it -v "$(pwd)":/src i3status-rust sh -c 'cd src && RUSTFLAGS="-C target-feature=-crt-static" cargo build --release'

ammgws commented 3 years ago

Last time I gave it a go, I was able to compile, but the resulting binary wasn't executable. Maybe someone else can try?

GladOSkar commented 3 years ago

I actually get this compile error:

error: failed to run custom build command for `openssl-sys v0.9.60`

Caused by:
  process didn't exit successfully: `/src/target/release/build/openssl-sys-1f4459c902b3ad26/build-script-main` (exit code: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=OPENSSL_STATIC
  cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "`\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"` did not exit successfully: exit code: 1\n--- stderr\nPackage openssl was not found in the pkg-config search path.\nPerhaps you should add the directory containing `openssl.pc\'\nto the PKG_CONFIG_PATH environment variable\nPackage \'openssl\', required by \'virtual:world\', not found\n"

  --- stderr
  thread 'main' panicked at '

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  proceed without this knowledge. If OpenSSL is installed and this crate had
  trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
  compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found
  automatically, please open a bug at https://github.com/sfackler/rust-openssl
  and include information about your system as well as this message.

  $HOST = x86_64-unknown-linux-musl
  $TARGET = x86_64-unknown-linux-musl
  openssl-sys = 0.9.60

  ', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.60/build/find_normal.rs:173:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
MaxVerevkin commented 3 years ago

Info


Setting cargo config to

❯ cat .cargo/config.toml                              
[build]
rustflags = ["-C", "target-feature=-crt-static"]

makes it possible to use i3status-rust, but the binary is no longer static.

ammgws commented 3 years ago

Yeah I couldn't get past that either. Wonder if it's just not possible to do this with our codebase?