Skip to content

Commit

Permalink
fix: Build for darwin-arm64 (Apple M1)
Browse files Browse the repository at this point in the history
- `liblzma-config.sh`: Run autogen.sh if configure fails, fixes Apple M1 build failure (*)
- refactor `build` action in `bindings.gyp` so that config and build scripts output is shown, making troubleshooting easier
  - `build` action refactored into `build` and `configure` actions with dependencies between them
  - since `configure` is a separate action instead of a command that runs as part of the 'input' key, it is free to output anything
  - script output is made less verbose and more clear, so it's not necessary to send to log file
- added `manual:config` and `manual:build` targets to `package.json` so it's easier to troubleshoot step-by-step

(*) The drawback of this is that the fallback to autoconf requires a full autoconf type build environment.
  • Loading branch information
beorn committed Jan 10, 2022
1 parent 66acc60 commit ce62636
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.log
build/
binding*/
node_modules/
Expand All @@ -8,3 +9,4 @@ README.md.xz
.nyc_output
/.idea/
prebuilds/
/.vscode
15 changes: 9 additions & 6 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@
"conditions" : [
[ 'OS!="win"' , {
"actions" : [
{
"action_name" : "configure",
'inputs': ['liblzma-config.sh'],
'outputs': ['build/liblzma'],
'action': ['eval', 'sh liblzma-config.sh build deps/xz-5.2.3.tar.bz2'],
},
{
"action_name" : "build",
# a hack to run deps/xz-5.2.3 ./configure during `node-gyp configure`
'inputs': ['<!@(sh liblzma-config.sh "<(module_root_dir)/build" "<(module_root_dir)/deps/xz-5.2.3.tar.bz2")'],
'outputs': [''],
'action': [
'sh', '<(module_root_dir)/liblzma-build.sh', '<(module_root_dir)/build'
]
'inputs': ['build/liblzma', 'liblzma-build.sh'],
'outputs': ['build/liblzma/Release'],
'action': ['eval', 'sh liblzma-build.sh build'],
}
]
}, {
Expand Down
9 changes: 8 additions & 1 deletion liblzma-build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/bin/sh

echo
echo "--- liblzma-build.sh $*"
echo "--- CWD = $PWD"
echo

set -e

case $(uname | tr '[:upper:]' '[:lower:]') in
*bsd) alias make='gmake';;
*)
esac

cd "$1/liblzma"
set -x
cd "$1" && cd liblzma
make
make install
50 changes: 43 additions & 7 deletions liblzma-config.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
#!/bin/sh
set -e

echo
echo "--- liblzma-config.sh $*"
echo "--- CWD = $PWD"
echo

SRC_TARBALL="$2"
TARGET_DIR="$1/liblzma"

mkdir -p "$TARGET_DIR"

tar xjf "$SRC_TARBALL" -C "$TARGET_DIR"

cd "$TARGET_DIR"
TARGET_DIR="$(pwd)" # ensure absolute since --prefix needs it

function autoconf() {
( cd xz-* ; sh ./autogen.sh --no-po4a )
return $?
}

function configure() {
sh xz-*/configure \
--quiet --enable-silent-rules \
--prefix="$TARGET_DIR/build" \
CFLAGS="-fPIC $CFLAGS" \
--enable-static \
--disable-xz \
--disable-xzdec \
--disable-lzmadec \
--disable-lzmainfo \
--disable-lzma-links \
--disable-rpath \
--disable-shared \
--disable-scripts
return $?
}

set -x

tar xvjf "$SRC_TARBALL" >node_liblzma_config.log 2>&1
configure
if [ $? -ne 0 ] ; then
set +x
echo
echo "--- ./configure failed => trying to run autoconf first"
echo "--- NOTE: This requires a full autoconf build environment, and so may also fail"
echo
set -x
autoconf && configure
fi

export CFLAGS="-fPIC $CFLAGS"
sh xz-*/configure --enable-static --disable-shared --disable-scripts --disable-lzmainfo \
--disable-lzma-links --disable-lzmadec --disable-xzdec --disable-xz --disable-rpath \
--prefix="$TARGET_DIR/build" CFLAGS="$CFLAGS" >>node_liblzma_config.log 2>&1
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"test": "mocha --expose-gc -s 1000 -t 5000",
"prepare": "npm run prepare-win32 || true",
"prepare-win32": "cd deps && 7z x -y xz-5.2.3-windows.7z bin_i686/liblzma.dll bin_x86-64/liblzma.dll include doc/liblzma.def",
"jshint": "jshint ."
"jshint": "jshint .",
"manual:config": "./liblzma-config.sh build deps/xz-5.2.3.tar.bz2",
"manual:build": "./liblzma-build.sh build"
},
"gypfile": true,
"bugs": {
Expand Down

0 comments on commit ce62636

Please sign in to comment.