Skip to content

Commit

Permalink
[native_toolchain_c] Export environmentFromBatchFile (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Dec 15, 2023
1 parent 2b1fbf8 commit 8107f37
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion pkgs/native_toolchain_c/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.3.3-wip
## 0.3.3

- Export `environmentFromBatchFile`.
- Bump `package:native_assets_cli` to 0.3.2.

## 0.3.2
Expand Down
1 change: 1 addition & 0 deletions pkgs/native_toolchain_c/lib/native_toolchain_c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
library;

export 'src/cbuilder/cbuilder.dart';
export 'src/utils/env_from_bat.dart';
3 changes: 2 additions & 1 deletion pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ class RunCBuilder {
Future<void> runCl({required ToolInstance compiler}) async {
final vcvars = (await _resolver.toolchainEnvironmentScript(compiler))!;
final vcvarsArgs = _resolver.toolchainEnvironmentScriptArguments();
final environment = await envFromBat(vcvars, arguments: vcvarsArgs ?? []);
final environment =
await environmentFromBatchFile(vcvars, arguments: vcvarsArgs ?? []);

final isStaticLib = staticLibrary != null;
Uri? archiver_;
Expand Down
20 changes: 15 additions & 5 deletions pkgs/native_toolchain_c/lib/src/utils/env_from_bat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

import 'dart:io';

Future<Map<String, String>> envFromBat(
/// Extracts the environment variables set by [batchFile].
///
/// If provided, passes [arguments] to the batch file invocation.
///
/// Note: needs to run [batchFile] to extract the modifications to the
/// environment variables.
Future<Map<String, String>> environmentFromBatchFile(
Uri batchFile, {
List<String> arguments = const [],
}) async {
Expand All @@ -20,8 +26,8 @@ Future<Map<String, String>> envFromBat(
assert(processResult.exitCode == 0);
final resultSplit = (processResult.stdout as String).split(separator);
assert(resultSplit.length == 2);
final unmodifiedParsed = parseDefines(resultSplit.first.trim());
final modifiedParsed = parseDefines(resultSplit[1].trim());
final unmodifiedParsed = _parseDefines(resultSplit.first.trim());
final modifiedParsed = _parseDefines(resultSplit[1].trim());
final result = <String, String>{};
for (final entry in modifiedParsed.entries) {
final key = entry.key;
Expand All @@ -33,8 +39,12 @@ Future<Map<String, String>> envFromBat(
return result;
}

// Ensures it doesn't return empty keys.
Map<String, String> parseDefines(String defines) {
/// Parses a string of defines.
///
/// Expected format is separate lines of `KEY=value`.
///
/// Ensures it doesn't return empty keys.
Map<String, String> _parseDefines(String defines) {
final result = <String, String>{};
final lines = defines.trim().split('\r\n');
for (final line in lines) {
Expand Down
4 changes: 2 additions & 2 deletions pkgs/native_toolchain_c/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: native_toolchain_c
description: >-
A library to invoke the native C compiler installed on the host machine.
version: 0.3.3-wip
version: 0.3.3
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_toolchain_c

topics:
Expand All @@ -12,7 +12,7 @@ topics:
- native-toolchain

environment:
sdk: '>=3.1.0 <4.0.0'
sdk: ">=3.1.0 <4.0.0"

dependencies:
cli_config: ^0.1.1
Expand Down
16 changes: 8 additions & 8 deletions pkgs/native_toolchain_c/test/native_toolchain/msvc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void main() {
expect(instances.isNotEmpty, true);
final instance = instances.first;
expect(instance.tool, vcvars32);
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -113,7 +113,7 @@ void main() {
expect(instances.isNotEmpty, true);
final instance = instances.first;
expect(instance.tool, vcvars64);
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -128,7 +128,7 @@ void main() {
expect(instances.isNotEmpty, true);
final instance = instances.first;
expect(instance.tool, vcvarsarm64);
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -137,7 +137,7 @@ void main() {
final instances = await vcvars32.defaultResolver!.resolve(logger: logger);
expect(instances.isNotEmpty, true);
final instance = instances.first;
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -146,7 +146,7 @@ void main() {
final instances = await vcvars64.defaultResolver!.resolve(logger: logger);
expect(instances.isNotEmpty, true);
final instance = instances.first;
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -156,7 +156,7 @@ void main() {
await vcvarsarm64.defaultResolver!.resolve(logger: logger);
expect(instances.isNotEmpty, true);
final instance = instances.first;
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand All @@ -165,7 +165,7 @@ void main() {
final instances = await vcvarsall.defaultResolver!.resolve(logger: logger);
expect(instances.isNotEmpty, true);
final instance = instances.first;
final env = await envFromBat(
final env = await environmentFromBatchFile(
instance.uri,
arguments: [
'x64',
Expand All @@ -181,7 +181,7 @@ void main() {
final instances = await vsDevCmd.defaultResolver!.resolve(logger: logger);
expect(instances.isNotEmpty, true);
final instance = instances.first;
final env = await envFromBat(instance.uri);
final env = await environmentFromBatchFile(instance.uri);
expect(env['INCLUDE'] != null, true);
expect(env['WindowsSdkDir'] != null, true); // stdio.h
});
Expand Down

0 comments on commit 8107f37

Please sign in to comment.