From 29348da500b2c491d67c2866f8a1ab13a1b68899 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Aug 2024 08:51:33 +0000 Subject: [PATCH] [native_toolchain_c] Only use `nm` to inspect symbols --- .../test/clinker/objects_test.dart | 14 ++++---- .../test/clinker/treeshake_helper.dart | 8 +++-- pkgs/native_toolchain_c/test/helpers.dart | 34 +++++++++++++++++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/pkgs/native_toolchain_c/test/clinker/objects_test.dart b/pkgs/native_toolchain_c/test/clinker/objects_test.dart index 0caec889a..39ed0e041 100644 --- a/pkgs/native_toolchain_c/test/clinker/objects_test.dart +++ b/pkgs/native_toolchain_c/test/clinker/objects_test.dart @@ -58,12 +58,12 @@ Future main() async { expect(linkOutput.assets, hasLength(1)); final asset = linkOutput.assets.first; expect(asset, isA()); - final file = (asset as NativeCodeAsset).file; - expect(file, isNotNull, reason: 'Asset $asset has a file'); - final filePath = file!.toFilePath(); - expect(filePath, endsWith(os.dylibFileName(name))); - final readelf = await readelfSymbols(filePath); - expect(readelf, contains('my_other_func')); - expect(readelf, contains('my_func')); + await expectSymbols( + asset: asset as NativeCodeAsset, + symbols: [ + 'my_func', + 'my_other_func', + ], + ); }); } diff --git a/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart b/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart index 5dcaf7896..198b81822 100644 --- a/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart +++ b/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart @@ -76,14 +76,16 @@ Future runTests(List architectures) async { output: linkOutput, logger: logger, ); - final filePath = linkOutput.assets.first.file!.toFilePath(); + + final asset = linkOutput.assets.first as NativeCodeAsset; + final filePath = asset.file!.toFilePath(); final machine = await readelfMachine(filePath); expect(machine, contains(readElfMachine[architecture])); - final symbols = await readelfSymbols(filePath); + final symbols = await nmReadSymbols(asset); if (clinker.linker != linkerAutoEmpty) { - expect(symbols, matches(r'[0-9]+\smy_other_func')); + expect(symbols, contains('my_other_func')); expect(symbols, isNot(contains('my_func'))); } else { expect(symbols, contains('my_other_func')); diff --git a/pkgs/native_toolchain_c/test/helpers.dart b/pkgs/native_toolchain_c/test/helpers.dart index 69992a284..88db4640a 100644 --- a/pkgs/native_toolchain_c/test/helpers.dart +++ b/pkgs/native_toolchain_c/test/helpers.dart @@ -197,9 +197,6 @@ extension UnescapePath on String { String unescape() => replaceAll('\\', '/'); } -Future readelfSymbols(String filePath) async => - readelf(filePath, 'WCs'); - Future readelfMachine(String path) async { final result = await readelf(path, 'h'); return result.split('\n').firstWhere((e) => e.contains('Machine:')); @@ -223,3 +220,34 @@ Future readelf(String filePath, String flags) async { expect(result.exitCode, 0); return result.stdout; } + +Future nmReadSymbols(NativeCodeAsset asset) async { + final assetUri = asset.file!; + final result = await runProcess( + executable: Uri(path: 'nm'), + arguments: [ + '-D', + assetUri.toFilePath(), + ], + logger: logger, + ); + + expect(result.exitCode, 0); + return result.stdout; +} + +Future expectSymbols({ + required NativeCodeAsset asset, + required List symbols, +}) async { + if (Platform.isLinux) { + final nmOutput = await nmReadSymbols(asset); + + expect( + nmOutput, + stringContainsInOrder(symbols), + ); + } else { + throw UnimplementedError(); + } +}