@@ -140,4 +140,120 @@ void main() {
140140 endsWith (targetOS.libraryFileName (name, DynamicLoadingBundled ())),
141141 );
142142 });
143+
144+ test (
145+ 'CBuilder static library does not pass linker flags to clang -c' ,
146+ () async {
147+ final fileSystem = MemoryFileSystem (
148+ style: Platform .isWindows
149+ ? FileSystemStyle .windows
150+ : FileSystemStyle .posix,
151+ );
152+
153+ final outputDirectoryShared = await tempDirForTest ();
154+ final packageRoot = await tempDirForTest ();
155+
156+ const name = 'mylib' ;
157+ const targetOS = OS .android;
158+ const targetArchitecture = Architecture .arm64;
159+
160+ final ccUri = packageRoot.resolve (OS .current.executableFileName ('clang' ));
161+ final arUri = packageRoot.resolve (
162+ OS .current.executableFileName ('llvm-ar' ),
163+ );
164+ final ldUri = packageRoot.resolve (
165+ OS .current.executableFileName ('ld.lld' ),
166+ );
167+ for (final toolUri in [ccUri, arUri, ldUri]) {
168+ fileSystem.file (toolUri)
169+ ..createSync (recursive: true )
170+ ..writeAsStringSync ('' );
171+ }
172+
173+ final sourceUri = packageRoot.resolve ('src.cc' );
174+ fileSystem.file (sourceUri)
175+ ..createSync (recursive: true )
176+ ..writeAsStringSync ('int foo() { return 0; }\n ' );
177+
178+ final ccPath = ccUri.toFilePath ();
179+ final arPath = arUri.toFilePath ();
180+
181+ const versionStdout = 'clang version 14.0.0\n ' ;
182+ const arVersionStdout = 'LLVM version 14.0.0\n ' ;
183+ final fakeProcessManager = FakeProcessManager ([
184+ FakeCommand (command: [ccPath, '--version' ], stdout: versionStdout),
185+ FakeCommand (command: [ccPath, '--version' ], stdout: versionStdout),
186+ FakeCommand (command: [arPath, '--version' ], stdout: arVersionStdout),
187+ FakeCommand (
188+ onRun: (command) {
189+ final outIndex = command.indexOf ('-o' );
190+ expect (outIndex, greaterThanOrEqualTo (0 ));
191+ fileSystem.file (command[outIndex + 1 ]).createSync (recursive: true );
192+ },
193+ ),
194+ FakeCommand (
195+ onRun: (command) {
196+ fileSystem.file (command[2 ]).createSync (recursive: true );
197+ },
198+ ),
199+ ]);
200+
201+ final buildInputBuilder = BuildInputBuilder ()
202+ ..setupShared (
203+ packageName: name,
204+ packageRoot: packageRoot,
205+ outputFile: outputDirectoryShared.resolve ('output.json' ),
206+ outputDirectoryShared: outputDirectoryShared,
207+ )
208+ ..config.setupBuild (linkingEnabled: false )
209+ ..addExtension (
210+ CodeAssetExtension (
211+ targetOS: targetOS,
212+ targetArchitecture: targetArchitecture,
213+ android: AndroidCodeConfig (targetNdkApi: 21 ),
214+ linkModePreference: LinkModePreference .static ,
215+ cCompiler: CCompilerConfig (
216+ archiver: arUri,
217+ compiler: ccUri,
218+ linker: ldUri,
219+ ),
220+ ),
221+ );
222+ final buildInput = buildInputBuilder.build ();
223+ final buildOutput = BuildOutputBuilder ();
224+
225+ final cbuilder = CBuilder .library (
226+ name: name,
227+ assetName: name,
228+ sources: [sourceUri.toFilePath ()],
229+ language: .cpp,
230+ cppLinkStdLib: 'c++_static' ,
231+ libraries: ['bar' ],
232+ libraryDirectories: ['baz' ],
233+ );
234+
235+ await cbuilder.run (
236+ input: buildInput,
237+ output: buildOutput,
238+ logger: logger,
239+ processManager: fakeProcessManager,
240+ fileSystem: fileSystem,
241+ );
242+
243+ expect (fakeProcessManager.allCommandsConsumed, true );
244+ final invocations = fakeProcessManager.invocations;
245+ expect (invocations.length, 5 );
246+
247+ final compileCommand = invocations[3 ].command;
248+ expect (compileCommand.first, ccPath);
249+ expect (compileCommand, contains ('-c' ));
250+ expect (compileCommand, containsAllInOrder (['-x' , 'c++' ]));
251+ expect (compileCommand, isNot (contains ('-Wl,-z,max-page-size=16384' )));
252+ expect (compileCommand, isNot (contains ('-Wl,-Bstatic' )));
253+ expect (compileCommand, isNot (contains ('-Wl,-Bdynamic' )));
254+ expect (compileCommand, isNot (contains ('-l' )));
255+ expect (compileCommand, isNot (contains ('-Wl,-rpath,\$ ORIGIN' )));
256+ expect (compileCommand, isNot (contains ('-lbar' )));
257+ },
258+ );
143259}
0 commit comments