|
| 1 | +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:code_assets/code_assets.dart'; |
| 8 | +import 'package:hooks/hooks.dart'; |
| 9 | +import 'package:native_toolchain_c/native_toolchain_c.dart'; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +import '../helpers.dart'; |
| 13 | +import 'build_testfiles.dart'; |
| 14 | + |
| 15 | +/// Tree-shakes a static library down to so many symbols that one linker flag |
| 16 | +/// per symbol would exceed the 32,767 character command-line limit of Windows. |
| 17 | +void main() { |
| 18 | + const functionCount = 2000; |
| 19 | + const keptCount = 1500; |
| 20 | + String functionName(int i) => |
| 21 | + 'a_function_with_a_long_name_to_exceed_the_command_line_limit_' |
| 22 | + '${i.toString().padLeft(4, '0')}'; |
| 23 | + |
| 24 | + test( |
| 25 | + 'treeshake keeps $keptCount symbols', |
| 26 | + timeout: const Timeout(Duration(minutes: 5)), |
| 27 | + () async { |
| 28 | + final targetOS = OS.current; |
| 29 | + final targetArchitecture = Architecture.current; |
| 30 | + final macOSTargetVersion = targetOS == OS.macOS |
| 31 | + ? defaultMacOSVersion |
| 32 | + : null; |
| 33 | + final tempUri = await tempDirForTest(); |
| 34 | + final tempUri2 = await tempDirForTest(); |
| 35 | + |
| 36 | + // Plain C functions without `__declspec(dllexport)`, so that on Windows |
| 37 | + // only the generated module-definition file exports them. |
| 38 | + final source = tempUri.resolve('many_functions.c'); |
| 39 | + await File.fromUri(source).writeAsString( |
| 40 | + [ |
| 41 | + for (var i = 0; i < functionCount; i++) |
| 42 | + 'int ${functionName(i)}(void) { return $i; }', |
| 43 | + '', |
| 44 | + ].join('\n'), |
| 45 | + ); |
| 46 | + |
| 47 | + final archive = await buildTestArchive( |
| 48 | + tempUri, |
| 49 | + tempUri2, |
| 50 | + targetOS, |
| 51 | + targetArchitecture, |
| 52 | + extraSources: [source], |
| 53 | + macOSTargetVersion: macOSTargetVersion, |
| 54 | + ); |
| 55 | + |
| 56 | + final linkInput = |
| 57 | + (LinkInputBuilder() |
| 58 | + ..setupShared( |
| 59 | + packageName: 'testpackage', |
| 60 | + packageRoot: tempUri, |
| 61 | + outputFile: tempUri.resolve('output.json'), |
| 62 | + outputDirectoryShared: tempUri2, |
| 63 | + ) |
| 64 | + ..setupLink( |
| 65 | + assets: [], |
| 66 | + recordedUsesFile: null, |
| 67 | + assetsFromLinking: [], |
| 68 | + ) |
| 69 | + ..addExtension( |
| 70 | + CodeAssetExtension( |
| 71 | + targetOS: targetOS, |
| 72 | + targetArchitecture: targetArchitecture, |
| 73 | + linkModePreference: LinkModePreference.dynamic, |
| 74 | + cCompiler: cCompiler, |
| 75 | + macOS: macOSTargetVersion != null |
| 76 | + ? MacOSCodeConfig(targetVersion: macOSTargetVersion) |
| 77 | + : null, |
| 78 | + ), |
| 79 | + )) |
| 80 | + .build(); |
| 81 | + final linkOutputBuilder = LinkOutputBuilder(); |
| 82 | + |
| 83 | + await CLinker.library( |
| 84 | + name: 'mylibname', |
| 85 | + assetName: '', |
| 86 | + sources: [archive.toFilePath()], |
| 87 | + linkerOptions: LinkerOptions.treeshake( |
| 88 | + symbolsToKeep: [for (var i = 0; i < keptCount; i++) functionName(i)], |
| 89 | + ), |
| 90 | + ).run(input: linkInput, output: linkOutputBuilder, logger: logger); |
| 91 | + |
| 92 | + final asset = linkOutputBuilder.build().assets.code.single; |
| 93 | + await expectSymbols( |
| 94 | + asset: asset, |
| 95 | + targetOS: targetOS, |
| 96 | + symbols: [functionName(0), functionName(keptCount - 1)], |
| 97 | + symbolsNotToContain: [ |
| 98 | + functionName(keptCount), |
| 99 | + functionName(functionCount - 1), |
| 100 | + 'my_func', |
| 101 | + ], |
| 102 | + ); |
| 103 | + }, |
| 104 | + ); |
| 105 | +} |
0 commit comments