Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building on Apple Silicon #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

karlvr
Copy link

@karlvr karlvr commented Sep 15, 2022

Currently on Apple Silicon without Rosetta, this package fails to build because optipng uses neon instructions that don't work on arm64. This PR applies the fix from #118 to install.js so we can build and use optipng-bin on Apple Silicon without Rosetta.

I believe this will fix #118 and #123.

Without this patch we get the following when we try an npm install:

node_modules/.pnpm/[email protected]/node_modules/optipng-bin: Running postinstall script, failed in 5.3s
.../node_modules/optipng-bin postinstall$ node lib/install.js
│ spawn Unknown system error -86
│ optipng pre-build test failed
│ compiling from source
│ Error: Command failed: /bin/sh -c make install
│ pngrtran.c:99:1: warning: unused function 'png_rtran_ok' [-Wunused-function]
│ png_rtran_ok(png_structrp png_ptr, int need_IHDR)
│ ^
│ 1 warning generated.
│ pngrutil.c:3536:20: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│                    png_isaligned(dp, png_uint_16) &&
│                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ ./pngpriv.h:597:29: note: expanded from macro 'png_isaligned'
│    (((type)((const char*)ptr-(const char*)0) & \
│                             ^~~~~~~~~~~~~~~
│ pngrutil.c:3537:20: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│                    png_isaligned(sp, png_uint_16) &&
│                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ ./pngpriv.h:597:29: note: expanded from macro 'png_isaligned'
│    (((type)((const char*)ptr-(const char*)0) & \
│                             ^~~~~~~~~~~~~~~
│ pngrutil.c:3544:23: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│                   if (png_isaligned(dp, png_uint_32) &&
│                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ ./pngpriv.h:597:29: note: expanded from macro 'png_isaligned'
│    (((type)((const char*)ptr-(const char*)0) & \
│                             ^~~~~~~~~~~~~~~
│ pngrutil.c:3545:23: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│                       png_isaligned(sp, png_uint_32) &&
│                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ ./pngpriv.h:597:29: note: expanded from macro 'png_isaligned'
│    (((type)((const char*)ptr-(const char*)0) & \
│                             ^~~~~~~~~~~~~~~
│ pngrutil.c:4604:34: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│          int extra = (int)((temp - (png_bytep)0) & 0x0f);
│                                  ^ ~~~~~~~~~~~~
│ pngrutil.c:4608:30: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
│          extra = (int)((temp - (png_bytep)0) & 0x0f);
│                              ^ ~~~~~~~~~~~~
│ 6 warnings generated.
│ ld: warning: option -s is obsolete and being ignored
│ Undefined symbols for architecture arm64:
│   "_png_init_filter_functions_neon", referenced from:
│       _png_read_filter_row in libpng.a(pngrutil.o)
│ ld: symbol(s) not found for architecture arm64
│ clang: error: linker command failed with exit code 1 (use -v to see invocation)
│ make[1]: *** [optipng] Error 1
│ make: *** [install] Error 2

But with the patch we get:

node_modules/.pnpm/[email protected]/node_modules/optipng-bin: Running postinstall script, done in 5s

(I was using pnpm instead of npm)

@karlvr karlvr changed the title Disable building neon on Apple Silicon Fix building on Apple Silicon Sep 15, 2022
@jimblue
Copy link

jimblue commented Nov 4, 2022

Hello,
I'm using imagemin-optipng 8.0.0(9.0.0 is not on NPM) and I'm having the exact same issue on my MacBook Air m1.
Could you please release a new version of imagemin-optipng with this fix ?
Thank you 🙏🏼

@wangxiuwen
Copy link

export CPPFLAGS="-DPNG_ARM_NEON_OPT=0" && pnpm install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linux arm64 support
3 participants