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

Add includes, flags, std, language, cppLinkStdLib options #125

Merged
merged 18 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions pkgs/native_toolchain_c/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.2.3

- Added `includes` for specifying include directories.
- Added `flags` for specifying arbitrary compiler flags.
- Added `std` for specifying a language standard.
- Added `cpp` for enabling C++ compilation.
- Added `cppLinkStdLib` for specifying the C++ standard library to link against.

## 0.2.2

- Generate position independent code for libraries by default and add
Expand Down
71 changes: 69 additions & 2 deletions pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class CBuilder implements Builder {
/// Used to output the [BuildOutput.dependencies].
final List<String> sources;

/// Include directories to pass to the compiler.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// Used to output the [BuildOutput.dependencies].
final List<String> includes;

/// The dart files involved in building this artifact.
///
/// Resolved against [BuildConfig.packageRoot].
Expand All @@ -57,6 +64,9 @@ class CBuilder implements Builder {
@visibleForTesting
final Uri? installName;

/// Flags to pass to the compiler.
final List<String> flags;

/// Definitions of preprocessor macros.
///
/// When the value is `null`, the macro is defined without a value.
Expand Down Expand Up @@ -95,26 +105,63 @@ class CBuilder implements Builder {
/// Defaults to `true` for libraries and `false` for executables.
final bool? pic;

/// The language standard to use.
///
/// When set to `null`, the default behavior of the compiler will be used.
final String? std;

/// Whether to compile sources as C++.
///
/// [cppLinkStdLib] only has an effect when this is `true`.
final bool cpp;

/// The C++ standard library to link against.
///
/// This option has no effect when [cpp] is `false` or when compiling for
/// Windows.
///
/// When set to `null`, the following defaults will be used, based on the
/// target OS:
///
/// | OS | Library |
/// | :------ | :----------- |
/// | Android | `c++_shared` |
/// | iOS | `c++` |
/// | Linux | `stdc++` |
/// | macOS | `c++` |
/// | Fuchsia | `c++` |
final String? cppLinkStdLib;

CBuilder.library({
required this.name,
required this.assetId,
this.sources = const [],
this.includes = const [],
this.dartBuildFiles = const ['build.dart'],
@visibleForTesting this.installName,
this.flags = const [],
this.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
this.pic = true,
this.std,
this.cpp = false,
dcharkes marked this conversation as resolved.
Show resolved Hide resolved
this.cppLinkStdLib,
}) : _type = _CBuilderType.library;

CBuilder.executable({
required this.name,
this.sources = const [],
this.includes = const [],
this.dartBuildFiles = const ['build.dart'],
this.flags = const [],
this.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
bool? pie = false,
this.std,
this.cpp = false,
this.cppLinkStdLib,
}) : _type = _CBuilderType.executable,
assetId = null,
installName = null,
Expand All @@ -141,6 +188,10 @@ class CBuilder implements Builder {
for (final source in this.sources)
packageRoot.resolveUri(Uri.file(source)),
];
final includes = [
for (final directory in this.includes)
packageRoot.resolveUri(Uri.file(directory)),
];
final dartBuildFiles = [
for (final source in this.dartBuildFiles) packageRoot.resolve(source),
];
Expand All @@ -149,6 +200,7 @@ class CBuilder implements Builder {
buildConfig: buildConfig,
logger: logger,
sources: sources,
includes: includes,
dynamicLibrary:
_type == _CBuilderType.library && linkMode == LinkMode.dynamic
? libUri
Expand All @@ -159,13 +211,17 @@ class CBuilder implements Builder {
: null,
executable: _type == _CBuilderType.executable ? exeUri : null,
installName: installName,
flags: flags,
defines: {
...defines,
if (buildModeDefine) buildConfig.buildMode.name.toUpperCase(): null,
if (ndebugDefine && buildConfig.buildMode != BuildMode.debug)
'NDEBUG': null,
},
pic: pic,
std: std,
cpp: cpp,
cppLinkStdLib: cppLinkStdLib,
);
await task.run();
}
Expand All @@ -188,10 +244,21 @@ class CBuilder implements Builder {
}
}
if (!buildConfig.dryRun) {
buildOutput.dependencies.dependencies.addAll([
final includeFiles = await Stream.fromIterable(includes)
.asyncExpand(
(include) => Directory(include.toFilePath())
.list(recursive: true)
.where((entry) => entry is File)
.map((file) => file.uri),
)
.toList();

buildOutput.dependencies.dependencies.addAll({
// Note: We use a Set here to deduplicate the dependencies.
...sources,
...includeFiles,
...dartBuildFiles,
]);
});
}
}
}
Expand Down
59 changes: 45 additions & 14 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RunCBuilder {
final BuildConfig buildConfig;
final Logger? logger;
final List<Uri> sources;
final List<Uri> includes;
final Uri? executable;
final Uri? dynamicLibrary;
final Uri? staticLibrary;
Expand All @@ -32,19 +33,28 @@ class RunCBuilder {
/// Can be modified with `install_name_tool`.
final Uri? installName;

final List<String> flags;
final Map<String, String?> defines;
final bool? pic;
final String? std;
final bool cpp;
final String? cppLinkStdLib;

RunCBuilder({
required this.buildConfig,
this.logger,
this.sources = const [],
this.includes = const [],
this.executable,
this.dynamicLibrary,
this.staticLibrary,
this.installName,
this.flags = const [],
this.defines = const {},
this.pic,
this.std,
this.cpp = false,
this.cppLinkStdLib,
}) : outDir = buildConfig.outDir,
target = buildConfig.target,
assert([executable, dynamicLibrary, staticLibrary]
Expand Down Expand Up @@ -133,20 +143,6 @@ class RunCBuilder {
'-install_name',
installName!.toFilePath(),
],
...sources.map((e) => e.toFilePath()),
if (executable != null) ...[
'-o',
outDir.resolveUri(executable!).toFilePath(),
],
if (dynamicLibrary != null) ...[
'--shared',
'-o',
outDir.resolveUri(dynamicLibrary!).toFilePath(),
] else if (staticLibrary != null) ...[
'-c',
'-o',
outDir.resolve('out.o').toFilePath(),
],
if (pic != null)
if (pic!) ...[
if (dynamicLibrary != null) '-fPIC',
Expand All @@ -161,8 +157,31 @@ class RunCBuilder {
'-fno-PIC',
'-fno-PIE',
],
if (std != null) '-std=$std',
if (cpp) ...[
'-x',
'c++',
'-l',
cppLinkStdLib ?? defaultCppLinkStdLib[target.os]!
],
...flags,
for (final MapEntry(key: name, :value) in defines.entries)
if (value == null) '-D$name' else '-D$name=$value',
for (final include in includes) '-I${include.toFilePath()}',
...sources.map((e) => e.toFilePath()),
if (executable != null) ...[
'-o',
outDir.resolveUri(executable!).toFilePath(),
],
if (dynamicLibrary != null) ...[
'--shared',
'-o',
outDir.resolveUri(dynamicLibrary!).toFilePath(),
] else if (staticLibrary != null) ...[
'-c',
'-o',
outDir.resolve('out.o').toFilePath(),
dcharkes marked this conversation as resolved.
Show resolved Hide resolved
],
],
logger: logger,
captureOutput: false,
Expand Down Expand Up @@ -197,8 +216,12 @@ class RunCBuilder {
final result = await runProcess(
executable: compiler.uri,
arguments: [
if (std != null) '/std:$std',
if (cpp) '/TP',
...flags,
for (final MapEntry(key: name, :value) in defines.entries)
if (value == null) '/D$name' else '/D$name=$value',
for (final directory in includes) '/I${directory.toFilePath()}',
if (executable != null) ...[
...sources.map((e) => e.toFilePath()),
'/link',
Expand Down Expand Up @@ -261,4 +284,12 @@ class RunCBuilder {
IOSSdk.iPhoneSimulator: 'x86_64-apple-ios-simulator',
},
};

static const defaultCppLinkStdLib = {
OS.android: 'c++_shared',
OS.fuchsia: 'c++',
OS.iOS: 'c++',
OS.linux: 'stdc++',
OS.macOS: 'c++',
};
}
2 changes: 1 addition & 1 deletion 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.2.2
version: 0.2.3
dcharkes marked this conversation as resolved.
Show resolved Hide resolved
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_toolchain_c

topics:
Expand Down
Loading