Skip to content

Commit

Permalink
Update static Linux build to MozJPEG 4.0.
Browse files Browse the repository at this point in the history
Statically link with libc too so it works on Alpine and other
non-glibc based distros and add a SOURCE_DATE_EPOCH so the build is
reproducible.
  • Loading branch information
averms committed Nov 27, 2020
1 parent a8f6bc4 commit e8bfff2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
32 changes: 18 additions & 14 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
FROM debian:8-slim

RUN apt-get update \
&& apt-get install autoconf libtool nasm libpng-dev automake pkg-config build-essential wget \
-yq --no-install-suggests --no-install-recommends --force-yes

WORKDIR /src
RUN wget --no-check-certificate https://github.com/mozilla/mozjpeg/archive/v3.3.1.tar.gz -O mozjpeg-3.3.1.tar.gz
RUN tar -xzvf mozjpeg-3.3.1.tar.gz
WORKDIR /src/mozjpeg-3.3.1

RUN autoreconf -fiv \
&& ./configure LDFLAGS=-static libpng_LIBS='/usr/lib/x86_64-linux-gnu/libpng12.a -lz' --enable-static --disable-shared \
&& make -j8
FROM docker.io/library/alpine:latest
RUN apk --update --no-cache upgrade && apk add --no-cache \
build-base \
nasm \
cmake \
ninja \
curl \
libpng-dev libpng-static \
zlib-dev zlib-static
RUN adduser -G wheel -D builder
WORKDIR /home/builder
USER builder
ENV mozjpeg_version 4.0.0
# 2020-11-13 UTC
ENV SOURCE_DATE_EPOCH 1605225600
RUN mkdir bin
COPY /build.sh build.sh
RUN ["/bin/sh", "build.sh"]
22 changes: 22 additions & 0 deletions docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
set -e

curl -fLO "https://github.com/mozilla/mozjpeg/archive/v${mozjpeg_version}.tar.gz"
tar xf "v${mozjpeg_version}.tar.gz"

export CFLAGS='-pipe -flto -no-pie'
export LDFLAGS='-flto -no-pie -static -static-libgcc'

# This unsets CMAKE_SHARED_LIBRARY_LINK_C_FLAGS inside CMakeLists.txt,
# which is necessary to build a static binary. It can't be unset from the CLI.
sed -E -i.bk '/set\(LIBJPEG_TURBO_V/i\
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)' "mozjpeg-$mozjpeg_version"/CMakeLists.txt

cmake -S "mozjpeg-$mozjpeg_version" -B build -G Ninja \
-D ENABLE_SHARED=OFF \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D CMAKE_FIND_LIBRARY_SUFFIXES=.a \
-D CMAKE_LINK_SEARCH_END_STATIC=1 -D CMAKE_LINK_SEARCH_START_STATIC=1

cd build && ninja
strip cjpeg-static
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"postinstall": "node lib/install.js",
"test": "xo && ava --timeout=120s",
"build-linux": "docker build --tag imagemin/mozjpeg docker && docker run --rm --volume $(pwd)/vendor/linux:/src/out imagemin/mozjpeg cp cjpeg /src/out"
"build-linux": "docker build --tag imagemin/mozjpeg docker/ && docker create --name mozjpeg_ctr imagemin/mozjpeg && docker cp mozjpeg_ctr:/home/builder/build/cjpeg-static ./vendor/linux/cjpeg && docker container rm mozjpeg_ctr"
},
"files": [
"index.js",
Expand Down
Binary file modified vendor/linux/cjpeg
Binary file not shown.

0 comments on commit e8bfff2

Please sign in to comment.