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

Build mode #62

Merged
merged 5 commits into from
Jun 8, 2023
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
10 changes: 10 additions & 0 deletions pkgs/c_compiler/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class RunCBuilder {
'-o',
outDir.resolve('out.o').toFilePath(),
],
// TODO(https://github.com/dart-lang/native/issues/50): The defines
// should probably be configurable. That way, the mapping from
// build_mode to defines can be defined in a project-dependent way in
// each project build.dart.
'-D${buildConfig.buildMode.name.toUpperCase()}'
],
logger: logger,
captureOutput: false,
Expand Down Expand Up @@ -170,6 +175,11 @@ class RunCBuilder {
final result = await runProcess(
executable: compiler.uri,
arguments: [
// TODO(https://github.com/dart-lang/native/issues/50): The defines
// should probably be configurable. That way, the mapping from
// build_mode to defines can be defined in a project-dependent way in
// each project build.dart.
'/D${buildConfig.buildMode.name.toUpperCase()}',
if (executable != null) ...[
...sources.map((e) => e.toFilePath()),
'/link',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void main() {
packageRoot: tempUri,
target: Target.current,
linkModePreference: LinkModePreference.dynamic,
buildMode: BuildMode.release,
cCompiler: CCompilerConfig(
cc: cc,
envScript: envScript,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Future<Uri> buildLib(
packageRoot: tempUri,
target: target,
targetAndroidNdkApi: androidNdkApi,
buildMode: BuildMode.release,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
Expand Down
1 change: 1 addition & 0 deletions pkgs/c_compiler/test/cbuilder/cbuilder_cross_ios_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
buildMode: BuildMode.release,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
buildMode: BuildMode.release,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
buildMode: BuildMode.release,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
buildMode: BuildMode.release,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
Expand Down
87 changes: 47 additions & 40 deletions pkgs/c_compiler/test/cbuilder/cbuilder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,55 @@ import 'package:test/test.dart';
import '../helpers.dart';

void main() {
test('Cbuilder executable', () async {
await inTempDir((tempUri) async {
final helloWorldCUri = packageUri
.resolve('test/cbuilder/testfiles/hello_world/src/hello_world.c');
if (!await File.fromUri(helloWorldCUri).exists()) {
throw Exception('Run the test from the root directory.');
}
const name = 'hello_world';
for (final buildMode in BuildMode.values) {
test('Cbuilder executable $buildMode', () async {
await inTempDir((tempUri) async {
final helloWorldCUri = packageUri
.resolve('test/cbuilder/testfiles/hello_world/src/hello_world.c');
if (!await File.fromUri(helloWorldCUri).exists()) {
throw Exception('Run the test from the root directory.');
}
const name = 'hello_world';

final buildConfig = BuildConfig(
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
// Ignored by executables.
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
cc: cc,
envScript: envScript,
envScriptArgs: envScriptArgs,
),
);
final buildOutput = BuildOutput();
final cbuilder = CBuilder.executable(
name: name,
sources: [helloWorldCUri.toFilePath()],
);
await cbuilder.run(
buildConfig: buildConfig,
buildOutput: buildOutput,
logger: logger,
);
final buildConfig = BuildConfig(
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
buildMode: buildMode,
// Ignored by executables.
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
cc: cc,
envScript: envScript,
envScriptArgs: envScriptArgs,
),
);
final buildOutput = BuildOutput();
final cbuilder = CBuilder.executable(
name: name,
sources: [helloWorldCUri.toFilePath()],
);
await cbuilder.run(
buildConfig: buildConfig,
buildOutput: buildOutput,
logger: logger,
);

final executableUri =
tempUri.resolve(Target.current.os.executableFileName(name));
expect(await File.fromUri(executableUri).exists(), true);
final result = await runProcess(
executable: executableUri,
logger: logger,
);
expect(result.exitCode, 0);
expect(result.stdout.trim(), 'Hello world.');
final executableUri =
tempUri.resolve(Target.current.os.executableFileName(name));
expect(await File.fromUri(executableUri).exists(), true);
final result = await runProcess(
executable: executableUri,
logger: logger,
);
expect(result.exitCode, 0);
if (buildMode == BuildMode.debug) {
expect(result.stdout.trim(), startsWith('Running in debug mode.'));
}
expect(result.stdout.trim(), endsWith('Hello world.'));
});
});
});
}

for (final dryRun in [true, false]) {
final testSuffix = dryRun ? ' dry_run' : '';
Expand All @@ -78,6 +84,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
cc: cc,
Expand Down
2 changes: 2 additions & 0 deletions pkgs/c_compiler/test/cbuilder/compiler_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
ar: ar,
Expand All @@ -67,6 +68,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.windowsX64,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
);
final resolver = CompilerResolver(
Expand Down
9 changes: 8 additions & 1 deletion pkgs/c_compiler/test/cbuilder/testfiles/add/src/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

#include <stdint.h>

#ifdef DEBUG
#include <stdio.h>
#endif

#if _WIN32
#define FFI_EXPORT __declspec(dllexport)
#else
#define FFI_EXPORT
#endif

FFI_EXPORT int32_t add(int32_t a, int32_t b) {
return a + b;
#ifdef DEBUG
printf("Adding %i and %i.\n", a, b);
#endif
return a + b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <stdio.h>

int main() {
printf("Hello world.\n");
return 0;
#ifdef DEBUG
printf("Running in debug mode.\n");
#endif
printf("Hello world.\n");
return 0;
}
9 changes: 8 additions & 1 deletion pkgs/native_assets_cli/example/native_add/src/native_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

#include "native_add.h"

#ifdef DEBUG
#include <stdio.h>
#endif

int32_t add(int32_t a, int32_t b) {
return a + b;
#ifdef DEBUG
printf("Adding %i and %i.\n", a, b);
#endif
return a + b;
}
1 change: 1 addition & 0 deletions pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library native_assets_cli;

export 'src/model/asset.dart';
export 'src/model/build_config.dart';
export 'src/model/build_mode.dart';
export 'src/model/build_output.dart';
export 'src/model/dependencies.dart';
export 'src/model/ios_sdk.dart';
Expand Down
18 changes: 18 additions & 0 deletions pkgs/native_assets_cli/lib/src/model/build_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:pub_semver/pub_semver.dart';

import '../utils/map.dart';
import '../utils/yaml.dart';
import 'build_mode.dart';
import 'ios_sdk.dart';
import 'link_mode_preference.dart';
import 'metadata.dart';
Expand Down Expand Up @@ -65,6 +66,10 @@ class BuildConfig {
bool get dryRun => _dryRun ?? false;
late final bool? _dryRun;

/// The build mode that the code should be compiled in.
BuildMode get buildMode => _buildMode;
late final BuildMode _buildMode;

/// The underlying config.
///
/// Can be used for easier access to values on [dependencyMetadata].
Expand All @@ -74,6 +79,7 @@ class BuildConfig {
factory BuildConfig({
required Uri outDir,
required Uri packageRoot,
required BuildMode buildMode,
required Target target,
IOSSdk? targetIOSSdk,
int? targetAndroidNdkApi,
Expand All @@ -85,6 +91,7 @@ class BuildConfig {
final nonValidated = BuildConfig._()
.._outDir = outDir
.._packageRoot = packageRoot
.._buildMode = buildMode
.._target = target
.._targetIOSSdk = targetIOSSdk
.._targetAndroidNdkApi = targetAndroidNdkApi
Expand All @@ -107,6 +114,7 @@ class BuildConfig {
static String checksum({
required Uri packageRoot,
required Target target,
required BuildMode buildMode,
IOSSdk? targetIOSSdk,
int? targetAndroidNdkApi,
CCompilerConfig? cCompiler,
Expand All @@ -119,6 +127,7 @@ class BuildConfig {
target.toString(),
targetIOSSdk.toString(),
targetAndroidNdkApi.toString(),
buildMode.toString(),
linkModePreference.toString(),
cCompiler?.ar.toString(),
cCompiler?.cc.toString(),
Expand Down Expand Up @@ -228,6 +237,12 @@ class BuildConfig {
(config) => _outDir = config.path(outDirConfigKey, mustExist: true),
(config) =>
_packageRoot = config.path(packageRootConfigKey, mustExist: true),
(config) => _buildMode = BuildMode.fromString(
config.string(
BuildMode.configKey,
validValues: BuildMode.values.map((e) => '$e'),
),
),
(config) {
_target = Target.fromString(
config.string(
Expand Down Expand Up @@ -312,6 +327,7 @@ class BuildConfig {
return {
outDirConfigKey: _outDir.toFilePath(),
packageRootConfigKey: _packageRoot.toFilePath(),
BuildMode.configKey: _buildMode.toString(),
Target.configKey: _target.toString(),
if (_targetIOSSdk != null) IOSSdk.configKey: _targetIOSSdk.toString(),
if (_targetAndroidNdkApi != null)
Expand All @@ -337,6 +353,7 @@ class BuildConfig {
}
if (other._outDir != _outDir) return false;
if (other._packageRoot != _packageRoot) return false;
if (other._buildMode != _buildMode) return false;
if (other._target != _target) return false;
if (other._targetIOSSdk != _targetIOSSdk) return false;
if (other._targetAndroidNdkApi != _targetAndroidNdkApi) return false;
Expand All @@ -352,6 +369,7 @@ class BuildConfig {
int get hashCode => Object.hash(
_outDir,
_packageRoot,
_buildMode,
_target,
_targetIOSSdk,
_targetAndroidNdkApi,
Expand Down
26 changes: 26 additions & 0 deletions pkgs/native_assets_cli/lib/src/model/build_mode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class BuildMode {
final String name;

const BuildMode._(this.name);

static const debug = BuildMode._('debug');
static const release = BuildMode._('release');

static const values = [
debug,
release,
];

factory BuildMode.fromString(String target) =>
values.firstWhere((e) => e.name == target);

/// The `package:config` key preferably used.
static const String configKey = 'build_mode';

@override
String toString() => name;
}
1 change: 1 addition & 0 deletions pkgs/native_assets_cli/test/example/native_add_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void main() async {
'-Dout_dir=${tempUri.toFilePath()}',
'-Dpackage_root=${testPackageUri.toFilePath()}',
'-Dtarget=${Target.current}',
'-Dbuild_mode=debug',
'-Dlink_mode_preference=dynamic',
if (cc != null) '-Dcc=${cc!.toFilePath()}',
if (envScript != null)
Expand Down
Loading