From b010585963bb9dd7462ff6d3de489589c9bbfd8b Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Mon, 11 Sep 2023 21:52:32 +0200 Subject: [PATCH] Verify that compiler flags have an effect --- .../lib/src/native_toolchain/xcode.dart | 4 +- .../test/cbuilder/cbuilder_test.dart | 98 +++++++++---------- 2 files changed, 50 insertions(+), 52 deletions(-) diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/xcode.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/xcode.dart index 4f239ef41..48c25c3c0 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/xcode.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/xcode.dart @@ -90,11 +90,11 @@ class XCodeSdkResolver implements ToolResolver { } assert(result.exitCode == 0); final uriSymbolic = Uri.directory(result.stdout.trim()); - logger?.fine('Found $sdk at ${uriSymbolic.toFilePath()}}'); + logger?.fine('Found $sdk at ${uriSymbolic.toFilePath()}'); final uri = Uri.directory( await Directory.fromUri(uriSymbolic).resolveSymbolicLinks()); if (uriSymbolic != uri) { - logger?.fine('Found $sdk at ${uri.toFilePath()}}'); + logger?.fine('Found $sdk at ${uri.toFilePath()}'); } assert(await Directory.fromUri(uri).exists()); return [ToolInstance(tool: tool, uri: uri)]; diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 9f1c0427f..018050a3d 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -202,62 +202,60 @@ void main() { } test('CBuilder flags', () async { - await inTempDir( - // https://github.com/dart-lang/sdk/issues/40159 - keepTemp: Platform.isWindows, - (tempUri) async { - final addCUri = - packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); - const name = 'add'; - - final logMessages = []; - final logger = createCapturingLogger(logMessages); + await inTempDir((tempUri) async { + final helloWorldCppCcUri = packageUri.resolve( + 'test/cbuilder/testfiles/hello_world_cpp/src/hello_world_cpp.cc'); + const name = 'hello_world_cpp'; - final buildConfig = BuildConfig( - outDir: tempUri, - packageRoot: tempUri, - targetArchitecture: Architecture.current, - targetOs: OS.current, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, - cCompiler: CCompilerConfig( - cc: cc, - envScript: envScript, - envScriptArgs: envScriptArgs, - ), - ); - final buildOutput = BuildOutput(); + final logMessages = []; + final logger = createCapturingLogger(logMessages); - final flag = switch (buildConfig.targetOs) { - OS.windows => '/O2', - _ => '-O2', - }; + final buildConfig = BuildConfig( + outDir: tempUri, + packageRoot: tempUri, + targetArchitecture: Architecture.current, + targetOs: OS.current, + buildMode: BuildMode.release, + linkModePreference: LinkModePreference.dynamic, + cCompiler: CCompilerConfig( + cc: cc, + envScript: envScript, + envScriptArgs: envScriptArgs, + ), + ); + final buildOutput = BuildOutput(); - final cbuilder = CBuilder.library( - sources: [addCUri.toFilePath()], - name: name, - assetId: name, - flags: [flag], - ); - await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, - logger: logger, - ); + final cLanguageFlag = switch (buildConfig.targetOs) { + OS.windows => '/TC', + _ => '-xc', + }; - final dylibUri = tempUri.resolve(Target.current.os.dylibFileName(name)); + final cbuilder = CBuilder.executable( + sources: [helloWorldCppCcUri.toFilePath()], + name: name, + flags: [cLanguageFlag], + ); + await expectLater( + () async { + await cbuilder.run( + buildConfig: buildConfig, + buildOutput: buildOutput, + logger: logger, + ); + }, + throwsA(isA()), + ); - final dylib = DynamicLibrary.open(dylibUri.toFilePath()); - final add = dylib.lookupFunction('add'); - expect(add(1, 2), 3); + final compilerInvocation = logMessages.firstWhere( + (message) => message.contains(helloWorldCppCcUri.toFilePath()), + ); + expect(compilerInvocation, contains(cLanguageFlag)); - final compilerInvocation = logMessages.singleWhere( - (message) => message.contains(addCUri.toFilePath()), - ); - expect(compilerInvocation, contains(flag)); - }, - ); + expect( + logMessages, + anyElement(contains("'iostream' file not found")), + ); + }); }); test('CBuilder includes', () async {