Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_toolchain_c] Support environment for cl.exe already set up #1876

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:io';
import 'package:logging/logging.dart';
import 'package:native_assets_cli/code_assets.dart';

import '../../native_toolchain_c.dart';
import '../native_toolchain/android_ndk.dart';
import '../native_toolchain/apple_clang.dart';
import '../native_toolchain/clang.dart';
Expand Down Expand Up @@ -196,29 +197,36 @@ class CompilerResolver {
return null;
}

Future<Uri?> toolchainEnvironmentScript(ToolInstance compiler) async {
final fromInput = codeConfig.cCompiler?.envScript;
if (fromInput != null) {
logger?.fine('Using envScript from input: $fromInput');
return fromInput;
Future<Map<String, String>> resolveEnvironment(ToolInstance compiler) async {
final envScriptFromConfig = codeConfig.cCompiler?.envScript;
if (envScriptFromConfig != null) {
logger?.fine('Using envScript from input: $envScriptFromConfig');
final vcvarsArgs = codeConfig.cCompiler?.envScriptArgs;
if (vcvarsArgs != null) {
logger?.fine('Using envScriptArgs from input: $vcvarsArgs');
}
return await environmentFromBatchFile(
envScriptFromConfig,
arguments: vcvarsArgs ?? [],
);
}

if (codeConfig.cCompiler?.compiler != null) {
logger?.fine('Compiler provided without envScript,'
' assuming environment is already set up.');
return {};
}

final compilerTool = compiler.tool;
assert(compilerTool == cl);
final vcvarsScript =
(await vcvars(compiler).defaultResolver!.resolve(logger: logger)).first;
return vcvarsScript.uri;
}

List<String>? toolchainEnvironmentScriptArguments() {
final fromInput = codeConfig.cCompiler?.envScriptArgs;
if (fromInput != null) {
logger?.fine('Using envScriptArgs from input: $fromInput');
return fromInput;
}

// vcvars above already has x64 or x86 in the script name.
return null;
return await environmentFromBatchFile(
vcvarsScript.uri,
arguments: [
/* vcvarsScript already has x64 or x86 in the script name. */
],
);
}

Future<ToolInstance> resolveLinker() async {
Expand Down
6 changes: 1 addition & 5 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import '../native_toolchain/msvc.dart';
import '../native_toolchain/tool_likeness.dart';
import '../native_toolchain/xcode.dart';
import '../tool/tool_instance.dart';
import '../utils/env_from_bat.dart';
import '../utils/run_process.dart';
import 'compiler_resolver.dart';
import 'language.dart';
Expand Down Expand Up @@ -334,10 +333,7 @@ class RunCBuilder {
}

Future<void> runCl({required ToolInstance tool}) async {
final vcvars = (await _resolver.toolchainEnvironmentScript(tool))!;
final vcvarsArgs = _resolver.toolchainEnvironmentScriptArguments();
final environment =
await environmentFromBatchFile(vcvars, arguments: vcvarsArgs ?? []);
final environment = await _resolver.resolveEnvironment(tool);

final isStaticLib = staticLibrary != null;
Uri? archiver_;
Expand Down
74 changes: 41 additions & 33 deletions pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,47 @@ void main() {
].firstOrNull?.uri;

final targetOS = OS.current;
final buildInputBuilder = BuildInputBuilder()
..setupShared(
packageName: 'dummy',
packageRoot: tempUri,
outputDirectory: tempUri,
outputDirectoryShared: tempUri2,
)
..config.setupBuild(
linkingEnabled: false,
dryRun: false,
)
..config.setupShared(buildAssetTypes: [CodeAsset.type])
..config.setupCode(
targetOS: targetOS,
macOS: targetOS == OS.macOS
? MacOSConfig(targetVersion: defaultMacOSVersion)
: null,
targetArchitecture: Architecture.current,
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
archiver: ar,
compiler: cc,
linker: ld,
envScript: envScript,
),
);
final buildInput = BuildInput(buildInputBuilder.json);
final resolver =
CompilerResolver(codeConfig: buildInput.config.code, logger: logger);
final compiler = await resolver.resolveCompiler();
final archiver = await resolver.resolveArchiver();
expect(compiler.uri, buildInput.config.code.cCompiler?.compiler);
expect(archiver.uri, buildInput.config.code.cCompiler?.archiver);
for (final passInEnvScript in [if (targetOS == OS.windows) true, false]) {
final buildInputBuilder = BuildInputBuilder()
..setupShared(
packageName: 'dummy',
packageRoot: tempUri,
outputDirectory: tempUri,
outputDirectoryShared: tempUri2,
)
..config.setupBuild(
linkingEnabled: false,
dryRun: false,
)
..config.setupShared(buildAssetTypes: [CodeAsset.type])
..config.setupCode(
targetOS: targetOS,
macOS: targetOS == OS.macOS
? MacOSConfig(targetVersion: defaultMacOSVersion)
: null,
targetArchitecture: Architecture.current,
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
archiver: ar,
compiler: cc,
linker: ld,
envScript: passInEnvScript ? envScript : null,
),
);
final buildInput = BuildInput(buildInputBuilder.json);
final resolver =
CompilerResolver(codeConfig: buildInput.config.code, logger: logger);
final compiler = await resolver.resolveCompiler();
final archiver = await resolver.resolveArchiver();
expect(compiler.uri, buildInput.config.code.cCompiler?.compiler);
expect(archiver.uri, buildInput.config.code.cCompiler?.archiver);
final environment = await resolver.resolveEnvironment(compiler);
if (passInEnvScript) {
expect(environment, isNot(equals({})));
} else {
expect(environment, equals({}));
}
}
});

test('No compiler found', () async {
Expand Down
Loading