Skip to content

Commit

Permalink
Add support for arm64 and update mozjpeg 4.1.3
Browse files Browse the repository at this point in the history
Windows building is broken. It has to be fixed. I recommend using vcpkg.
Mac Os X arm64 binary should be change to real arm64 binary. I don't own mac, but everything builds correctly.
  • Loading branch information
MateuszKrawczuk committed Jul 10, 2023
1 parent c72b1f5 commit c30c7a9
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 40 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ jobs:
matrix:
node-version:
- 16
- 14
- 12
- 18
- 20
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- if: contains(matrix.os, 'ubuntu')
run: sudo apt-get install nasm
run: sudo apt-get install nasm cmake libpng-dev
- if: contains(matrix.os, 'macos')
run: brew install automake nasm
- uses: actions/setup-node@v2
run: brew install cmake nasm libpng zlib
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
16 changes: 8 additions & 8 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM debian:10-slim
FROM debian:12-slim

RUN apt-get update \
&& apt-get install autoconf libtool nasm libpng-dev automake pkg-config build-essential wget \
&& apt-get install cmake libtool nasm libpng-dev 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 wget --no-check-certificate https://github.com/mozilla/mozjpeg/archive/v4.1.3.tar.gz -O mozjpeg-4.1.3.tar.gz
RUN tar -xzvf mozjpeg-4.1.3.tar.gz
WORKDIR /src/mozjpeg-4.1.3

RUN autoreconf -fiv \
&& ./configure LDFLAGS=-static libpng_LIBS='/usr/lib/x86_64-linux-gnu/libpng16.a -lz' --enable-static --disable-shared \
&& make -j8
RUN mkdir result && cd result && \
cmake .. \
&& cmake --build .
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {fileURLToPath} from 'node:url';
import BinWrapper from 'bin-wrapper';

const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
const url = `https://raw.githubusercontent.com/imagemin/mozjpeg-bin/v${pkg.version}/vendor/`;
const url = `https://github.com/CybrixSystems/mozjpeg-bin/raw/${pkg.version}/vendor/`;

const binWrapper = new BinWrapper()
.src(`${url}macos/cjpeg`, 'darwin')
.src(`${url}linux/cjpeg`, 'linux')
.src(`${url}win/cjpeg.exe`, 'win32')
.src(`${url}macos/amd64/cjpeg`, 'darwin', 'x64')
.src(`${url}macos/arm64/cjpeg`, 'darwin', 'arm64')
.src(`${url}linux/amd64/cjpeg`, 'linux', 'x64')
.src(`${url}linux/arm64/cjpeg`, 'linux', 'arm64')
.src(`${url}win/x86/cjpeg.exe`, 'win32', 'x86')
.src(`${url}win/x64/cjpeg.exe`, 'win32', 'x64')
.dest(fileURLToPath(new URL('../vendor', import.meta.url)))
.use(process.platform === 'win32' ? 'cjpeg.exe' : 'cjpeg');

Expand Down
15 changes: 5 additions & 10 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import os from 'node:os';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

const cpuNumber = Math.max(os.cpus().length, 1);

bin.run(['-version']).then(() => {
console.log('mozjpeg pre-build test passed successfully');
}).catch(async error => {
console.warn(error.message);
console.warn('mozjpeg pre-build test failed');
console.info('compiling from source');

let cfgExtras = '';
const config = [];
if (process.platform === 'darwin') {
cfgExtras = 'libpng_LIBS=\'/usr/local/lib/libpng16.a -lz\' --enable-static';
config.push('-DCMAKE_FIND_FRAMEWORK=LAST -DBUILD_SHARED_LIBS=OFF');
}

const cfg = [
`./configure --enable-static --disable-shared --disable-dependency-tracking --with-jpeg8 ${cfgExtras}`,
`--prefix="${bin.dest()}" --bindir="${bin.dest()}" --libdir="${bin.dest()}"`,
`cmake ${config.join(' ')} .`,
].join(' ');

try {
const source = fileURLToPath(new URL('../vendor/source/mozjpeg.tar.gz', import.meta.url));
await binBuild.file(source, [
'autoreconf -fiv',
cfg,
`make -j${cpuNumber}`,
`make install -j${cpuNumber}`,
'cmake --build . ',
`cp cjpeg-static ${bin.dest()}/cjpeg`,
]);

console.log('mozjpeg built successfully');
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mozjpeg",
"version": "8.0.0",
"version": "10.0.0",
"description": "mozjpeg wrapper that makes it seamlessly available as a local dependency",
"license": "MIT",
"repository": "imagemin/mozjpeg-bin",
Expand All @@ -12,8 +12,8 @@
},
"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"
"test": "xo && ava --timeout=180s",
"build-linux": "docker build --tag imagemin/mozjpeg docker && docker run --rm --volume $(pwd)/vendor/linux:/src/out imagemin/mozjpeg cp result/cjpeg-static /src/out/cjpeg"
},
"files": [
"index.js",
Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
Expand All @@ -11,8 +10,6 @@ import binBuild from 'bin-build';
import compareSize from 'compare-size';
import mozjpeg from '../index.js';

const cpuNumber = os.cpus().length;

test('rebuild the mozjpeg binaries', async t => {
// Skip the test on Windows
if (process.platform === 'win32') {
Expand All @@ -21,20 +18,23 @@ test('rebuild the mozjpeg binaries', async t => {
}

const temporary = tempy.directory();
const config = [];
if (process.platform === 'darwin') {
config.push('-DCMAKE_FIND_FRAMEWORK=LAST -DBUILD_SHARED_LIBS=OFF');
}

const cfg = [
'./configure --enable-static --disable-shared --disable-dependency-tracking --with-jpeg8',
`--prefix="${temporary}" --bindir="${temporary}" --libdir="${temporary}"`,
`cmake ${config.join(' ')} -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${temporary}" .`,
].join(' ');

const source = fileURLToPath(new URL('../vendor/source/mozjpeg.tar.gz', import.meta.url));
await binBuild.file(source, [
'autoreconf -fiv',
cfg,
`make --jobs=${cpuNumber}`,
`make install --jobs=${cpuNumber}`,
'cmake --build . ',
'cmake --install . ',
]);

t.true(fs.existsSync(path.join(temporary, 'cjpeg')));
t.true(fs.existsSync(path.join(temporary, 'bin/cjpeg')));
});

test('return path to binary and verify that it is working', async t => {
Expand Down
Binary file added vendor/linux/amd64/cjpeg
Binary file not shown.
Binary file added vendor/linux/arm64/cjpeg
Binary file not shown.
Binary file removed vendor/linux/cjpeg
Binary file not shown.
File renamed without changes.
Binary file added vendor/macos/arm64/cjpeg
Binary file not shown.
Binary file modified vendor/source/mozjpeg.tar.gz
Binary file not shown.
Binary file removed vendor/win/cjpeg.exe
Binary file not shown.
Binary file added vendor/win/x64/cjpeg.exe
Binary file not shown.
Binary file added vendor/win/x86/cjpeg.exe
Binary file not shown.

0 comments on commit c30c7a9

Please sign in to comment.