staltz / sodium-native-nodejs-mobile

Low level bindings for libsodium
MIT License
5 stars 6 forks source link

preinstall always err by ./dist-build/android-armv7-a.sh exited with 2 #15

Open Lonmee opened 1 year ago

Lonmee commented 1 year ago

ld: error: ./.libs/libaesni.a(/): not an ELF file clang-14: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: [libsodium.la] Error 1 make[2]: [install-recursive] Error 1 make[1]: [install-recursive] Error 1 make: [install-recursive] Error 1 .../android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project/node_modules/sodium-native-nodejs-mobile/preinstall.js:178 if (err) throw err ^

Error: ./dist-build/android-armv7-a.sh exited with 2 at ChildProcess. (.../android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project/node_modules/sodium-native-nodejs-mobile/preinstall.js:220:25) at ChildProcess.emit (node:events:513:28) at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) npm info run sodium-native-nodejs-mobile@3.2.0-6 install { code: 1, signal: null } npm timing command:rebuild Completed in 234933ms

achou11 commented 1 year ago

Running into this same exact error.

Some env info:

OS: macOS 11.7.8 (Big Sur) Node: 16.17.1 NPM: 8.19.4 Android SDK build tools: 30.0.2 (I think, based on android project's build.gradle) Android SDK CLI tools: 9 (latest) Android NDK: 24.0.8215888

Lonmee commented 1 year ago

Resolved by installing manually. Seems the lib file doesn't generated in time

staltz commented 1 year ago

Still a wild guess, but I think this is a macOS issue, since I mainly compile this via my Linux and haven't had problems. I recently updated libsodium as a dependency in this package, and it may be that on a macOS host it assumes that it's building for Apple hardware (thus not Android) and is using clang and it doesn't generate "ELF" because that's Linux-specific, see the portion

ld: error: ./.libs/libaesni.a(/): not an ELF file
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
staltz commented 1 year ago

That clang-14 is very suspicious. It should be using something like armv7a-linux-androideabi30-clang from NDK.

Lonmee commented 1 year ago

I had reran the preinstalling script with falsify env, it's working well. P.S. node repl in folder 'sodium-native-nodejs-mobile';

the manual module:

/**
 * node exec in
 * android/build/nodejs-native-assets-temp-build/nodejs-native-assets-armeabi-v7a/nodejs-project/node_modules/sodium-native-nodejs-mobile
 * require('./android-nodejs-builder.js')('arm',require('ini'),()=>console.log('done!'))
 * android/build/nodejs-native-assets-temp-build/nodejs-native-assets-arm64-v8a/nodejs-project/node_modules/sodium-native-nodejs-mobile
 * require('./android-nodejs-builder.js')('arm64',require('ini'),()=>console.log('done!'))
 */
const path = require('path'),
  fs = require('fs'),
  proc = require('child_process');

function buildAndroid(arch, ini, cb) {
  mkdirSync(path.join(__dirname, 'lib/android-' + arch));
  var res = path.join(__dirname, 'lib/android-' + arch, 'libsodium.so');
  if (fs.existsSync(res)) {
    return;
  }

  var buildScript =
    arch === 'arm'
      ? 'android-armv7-a.sh'
      : arch === 'arm64'
      ? 'android-armv8-a.sh'
      : ':';
  var outputDir =
    arch === 'arm'
      ? 'libsodium/libsodium-android-armv7-a/lib'
      : arch === 'arm64'
      ? 'libsodium/libsodium-android-armv8-a+crypto/lib'
      : '.';

  spawn(
    './configure-mobile',
    [],
    {cwd: __dirname, stdio: 'inherit'},
    function (err) {
      if (err) {
        throw err;
      }
      spawn(
        './dist-build/' + buildScript,
        [],
        {
          cwd: path.resolve(__dirname, 'libsodium'),
          stdio: 'inherit',
          env: {...process.env, LIBSODIUM_FULL_BUILD: 'yes'},
        },
        function (err) {
          if (err) {
            throw err;
          }

          var la = ini.decode(
            fs
              .readFileSync(path.resolve(__dirname, outputDir, 'libsodium.la'))
              .toString(),
          );

          var lib = fs.realpathSync(path.join(la.libdir, la.dlname));
          fs.rename(lib, res, function (err) {
            if (err) {
              throw err;
            }
            if (cb) {
              cb();
            }
          });
        },
      );
    },
  );
}

function spawn(cmd, args, opts, cb) {
  var c = proc.spawn(cmd, args, opts);
  c.on('exit', function (code) {
    if (code) {
      return cb(new Error(cmd + ' exited with ' + code));
    }
    cb(null);
  });
}

function mkdirSync(p) {
  try {
    fs.mkdirSync(p);
  } catch (err) {
    // do nothing
  }
}

module.exports = buildAndroid;

So is it cause by process?