Skip to content

Commit

Permalink
Changes as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Jul 23, 2024
1 parent b1e4592 commit 3bfe024
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 366 deletions.
3 changes: 2 additions & 1 deletion pkgs/native_toolchain_c/lib/native_toolchain_c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// A library to invoke the native C compiler installed on the host machine.
library;

export 'src/cbuilder/cbuilder.dart' show CBuilder, Language;
export 'src/cbuilder/cbuilder.dart' show CBuilder;
export 'src/cbuilder/clinker.dart' show CLinker, LinkerOptions;
export 'src/cbuilder/language.dart' show Language;
export 'src/utils/env_from_bat.dart';
224 changes: 41 additions & 183 deletions pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,14 @@ import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

import 'ctool.dart';
import 'language.dart';
import 'linkmode.dart';
import 'output_type.dart';
import 'run_cbuilder.dart';

/// A programming language that can be selected for compilation of source files.
///
/// See [CBuilder.language] for more information.
class Language {
/// The name of the language.
final String name;

const Language._(this.name);

static const Language c = Language._('c');

static const Language cpp = Language._('c++');

static const Language objectiveC = Language._('objective c');

/// Known values for [Language].
static const List<Language> values = [
c,
cpp,
objectiveC,
];

@override
String toString() => name;
}

/// Specification for building an artifact with a C compiler.
class CBuilder implements Builder {
/// What kind of artifact to build.
final CBuilderType _type;

/// Name of the library or executable to build.
///
/// The filename will be decided by [BuildConfig.targetOS] and
/// [OS.libraryFileName] or [OS.executableFileName].
///
/// File will be placed in [BuildConfig.outputDirectory].
final String name;

/// Asset identifier.
///
/// Used to output the [BuildOutput.assets].
///
/// If omitted, no asset will be added to the build output.
final String? assetName;

/// Sources to build the library or executable.
///
/// Resolved against [BuildConfig.packageRoot].
///
/// 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;

/// Frameworks to link.
///
/// Only effective if [language] is [Language.objectiveC].
///
/// Defaults to `['Foundation']`.
///
/// Not used to output the [BuildOutput.dependencies], frameworks can be
/// mentioned by name if they are available on the system, so the file path
/// is not known. If you're depending on your own frameworks add them to
/// [BuildOutput.dependencies] manually.
final List<String> frameworks;

static const List<String> _defaultFrameworks = ['Foundation'];

class CBuilder extends CTool implements Builder {
/// The dart files involved in building this artifact.
///
/// Resolved against [BuildConfig.packageRoot].
Expand All @@ -95,19 +27,6 @@ class CBuilder implements Builder {
)
final List<String> dartBuildFiles;

/// TODO(https://github.com/dart-lang/native/issues/54): Move to [BuildConfig]
/// or hide in public API.
@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.
final Map<String, String?> defines;

/// Whether to define a macro for the current [BuildMode].
///
/// The macro name is the uppercase name of the build mode and does not have a
Expand All @@ -127,100 +46,54 @@ class CBuilder implements Builder {
/// Defaults to `true`.
final bool ndebugDefine;

/// Whether the compiler will emit position independent code.
///
/// When set to `true`, libraries will be compiled with `-fPIC` and
/// executables with `-fPIE`. Accordingly the corresponding parameter of the
/// [CBuilder.executable] constructor is named `pie`.
///
/// When set to `null`, the default behavior of the compiler will be used.
///
/// This option has no effect when building for Windows, where generation of
/// position independent code is not configurable.
///
/// 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;

/// The language to compile [sources] as.
///
/// [cppLinkStdLib] only has an effect when this option is set to
/// [Language.cpp].
final Language language;

/// The C++ standard library to link against.
///
/// This option has no effect when [language] is not set to [Language.cpp] 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;

/// If the code asset should be a dynamic or static library.
///
/// This determines whether to produce a dynamic or static library. If null,
/// the value is instead retrieved from the [BuildConfig].
final LinkModePreference? linkModePreference;

CBuilder.library({
required this.name,
required this.assetName,
this.sources = const [],
this.includes = const [],
this.frameworks = _defaultFrameworks,
required super.name,
super.assetName,
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
)
this.dartBuildFiles = const [],
@visibleForTesting this.installName,
this.flags = const [],
this.defines = const {},
@visibleForTesting super.installName,
super.flags = const [],
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
this.pic = true,
this.std,
this.language = Language.c,
this.cppLinkStdLib,
this.linkModePreference,
}) : _type = CBuilderType.library;
super.pic = true,
super.std,
super.language = Language.c,
super.cppLinkStdLib,
super.linkModePreference,
}) : super(type: OutputType.library);

CBuilder.executable({
required this.name,
this.sources = const [],
this.includes = const [],
this.frameworks = _defaultFrameworks,
required super.name,
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
)
this.dartBuildFiles = const [],
this.flags = const [],
this.defines = const {},
super.flags = const [],
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
bool? pie = false,
this.std,
this.language = Language.c,
this.cppLinkStdLib,
}) : _type = CBuilderType.executable,
assetName = null,
installName = null,
pic = pie,
linkModePreference = null;
super.std,
super.language = Language.c,
super.cppLinkStdLib,
}) : super(
type: OutputType.executable,
assetName: null,
installName: null,
pic: pie,
linkModePreference: null,
);

/// Runs the C Compiler with on this C build spec.
///
Expand Down Expand Up @@ -264,14 +137,14 @@ class CBuilder implements Builder {
includes: includes,
frameworks: frameworks,
dynamicLibrary:
_type == CBuilderType.library && linkMode == DynamicLoadingBundled()
type == OutputType.library && linkMode == DynamicLoadingBundled()
? libUri
: null,
staticLibrary:
_type == CBuilderType.library && linkMode == StaticLinking()
? libUri
: null,
executable: _type == CBuilderType.executable ? exeUri : null,
staticLibrary: type == OutputType.library && linkMode == StaticLinking()
? libUri
: null,
executable: type == OutputType.executable ? exeUri : null,
// ignore: invalid_use_of_visible_for_testing_member
installName: installName,
flags: flags,
defines: {
Expand Down Expand Up @@ -322,18 +195,3 @@ class CBuilder implements Builder {
}
}
}

enum CBuilderType {
executable,
library,
}

LinkMode getLinkMode(LinkModePreference preference) {
if (preference == LinkModePreference.dynamic ||
preference == LinkModePreference.preferDynamic) {
return DynamicLoadingBundled();
}
assert(preference == LinkModePreference.static ||
preference == LinkModePreference.preferStatic);
return StaticLinking();
}
Loading

0 comments on commit 3bfe024

Please sign in to comment.