Skip to content

Commit f7ffe96

Browse files
authored
[native_toolchain_c] Don't pass /INCLUDE per symbol with a generated .def on Windows (#3718)
1 parent 77a5e41 commit f7ffe96

3 files changed

Lines changed: 120 additions & 8 deletions

File tree

‎pkgs/native_toolchain_c/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## 0.19.6-wip
22

33
- Default `linkerOptions` to `LinkerOptions.manual()` in `CLinker.run` when omitted so static archives link all symbols by default.
4+
- Fix `LinkerOptions.treeshake` on Windows for thousands of symbols: no longer
5+
pass an `/INCLUDE:` flag per symbol in addition to the generated
6+
module-definition file, which exceeded the Windows command-line length limit.
7+
The generated module-definition file also no longer names the DLL `MyDLL`.
48

59
## 0.19.5
610

‎pkgs/native_toolchain_c/lib/src/cbuilder/linker_options.dart‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,14 @@ extension LinkerOptionsExt on LinkerOptions {
186186
'/link',
187187
if (_keepAllSymbols) ...sourceFiles.map((e) => '/WHOLEARCHIVE:$e'),
188188
..._linkerFlags,
189-
..._symbols.map(
190-
(symbol) => '/INCLUDE:${targetArch == .ia32 ? '_' : ''}$symbol',
191-
),
189+
// The generated module-definition file exports, and therefore keeps, all
190+
// symbols. Passing an `/INCLUDE:` per symbol as well is redundant, and for
191+
// thousands of symbols exceeds the 32,767 character command-line limit of
192+
// Windows.
193+
if (_linkerScriptMode is! GenerateLinkerScript)
194+
..._symbols.map(
195+
(symbol) => '/INCLUDE:${targetArch == .ia32 ? '_' : ''}$symbol',
196+
),
192197
if (_linkerScriptMode is ManualLinkerScript)
193198
'/DEF:${_linkerScriptMode.script.toFilePath()}'
194199
else if (_linkerScriptMode is GenerateLinkerScript)
@@ -239,11 +244,9 @@ extension LinkerOptionsExt on LinkerOptions {
239244
final tempDir = fileSystem.systemTempDirectory.createTempSync();
240245
final symbolsFileUri = tempDir.uri.resolve('symbols.def');
241246
final symbolsFile = fileSystem.file(symbolsFileUri)..createSync();
242-
symbolsFile.writeAsStringSync('''
243-
LIBRARY MyDLL
244-
EXPORTS
245-
${symbols.map((s) => ' $s').join('\n')}
246-
''');
247+
symbolsFile.writeAsStringSync(
248+
['EXPORTS', for (final symbol in symbols) ' $symbol', ''].join('\n'),
249+
);
247250
return symbolsFileUri.toFilePath();
248251
}
249252
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)