Skip to content

Commit

Permalink
Verify that compiler flags have an effect
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Sep 11, 2023
1 parent fd36b05 commit b010585
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pkgs/native_toolchain_c/lib/src/native_toolchain/xcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
98 changes: 48 additions & 50 deletions pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <String>[];
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 = <String>[];
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<ProcessException>()),
);

final dylib = DynamicLibrary.open(dylibUri.toFilePath());
final add = dylib.lookupFunction<Int32 Function(Int32, Int32),
int Function(int, int)>('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 {
Expand Down

0 comments on commit b010585

Please sign in to comment.