dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
157 stars 44 forks source link

[native_assets_cli]: Could not open the dependent shared library #1713

Open lindeer opened 1 week ago

lindeer commented 1 week ago

similar with #190 , my Dart library (no flutter) had 2 .so files: liba.so and libb.so, created by Process.start('make', ...) and I put them all in buildConfig.outputDirectory, it passed compilation, but run with an exception Failed to load dynamic library '.dart_tool/native_assets_builder/3e4910e7689af6c76b0eb2007f74b95f/out/liba.so': libb.so: cannot open shared object file: No such file or directory. How should I write my build.dart?

const packageName = 'my_pkg';
const _repoLibName = 'liba.so';
const _depLibName = 'libb.so';

Future<void> _builder(BuildConfig buildConfig, BuildOutput buildOutput) async {
  final pkgRoot = buildConfig.packageRoot;

  final proc = await Process.start(
    'make',
    [
      '-j',
      _repoLibName,
    ],
    workingDirectory: srcDir.path,
  );
  stderr.addStream(proc.stdout);

  final linkMode = _linkMode(buildConfig.linkModePreference);
  final libName = buildConfig.targetOS.libraryFileName(packageName, linkMode);

  Uri moveLib(String name, String target) {
    final uri = pkgRoot.resolve(p.join('src', name));
    final file = File.fromUri(uri).resolveSymbolicLinksSync();
    final targetUri = buildConfig.outputDirectory.resolve(target);
    File(file).renameSync(targetUri.path);
    return targetUri;
  }

  final libUri = moveLib(_repoLibName, libName);
  moveLib(_depLibName, _depLibName);

  buildOutput.addAsset(NativeCodeAsset(
    package: packageName,
    name: 'src/lib_$packageName.dart',
    linkMode: linkMode,
    os: buildConfig.targetOS,
    file: libUri,
    architecture: buildConfig.targetArchitecture,
  ));

  final src = [
    'src/src/my_pkg.cpp',
    'src/a/a.cpp',
    'src/b/b.cpp',
  ];

  buildOutput.addDependencies([
    ...src.map((s) => pkgRoot.resolve(s)),
    pkgRoot.resolve('build.dart'),
  ]);
}

pubspec.yaml:

dependencies:
  ffi: ^2.1.3
  native_assets_cli: ^0.8.0

dev_dependencies:
  ffigen: ^11.0.0
  lints: ^3.0.0
  test: ^1.21.0
dcharkes commented 4 days ago

How should I write my build.dart?

Report both dynamic libraries with addAsset in hook/build.dart.

Though, since #190 is not fully supported yet, it still might not work after that.

lindeer commented 3 days ago

@dcharkes any example code? not figure out 'report bot dynamic libraries', would have a try...

dcharkes commented 3 days ago

@dcharkes any example code? not figure out 'report bot dynamic libraries', would have a try...

https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder/test_data/native_dynamic_linking