Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Dec 6, 2024
1 parent f599be3 commit 1a11e7d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ void main(List<String> args) async {
sources: [
'src/math.c',
],
dynamicallyLinkTo: ['debug'],
libraries: ['debug'],
),
CBuilder.library(
name: 'add',
assetName: 'add.dart',
sources: [
'src/add.c',
],
dynamicallyLinkTo: ['math'],
libraries: ['math'],
)
];

Expand Down
47 changes: 12 additions & 35 deletions pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,14 @@ class CBuilder extends CTool implements Builder {
/// Defaults to `true`.
final bool ndebugDefine;

/// Libraries to dynamically link to.
///
/// The libraries are expected to be at the root of the output directory of
/// the build hook invocation.
///
/// When using this option ensure that the builders producing the libraries
/// to link to are run before this builder.
final List<String> dynamicallyLinkTo;

CBuilder.library({
required super.name,
super.assetName,
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
super.libraries = const [],
super.libraryPaths = const [],
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
Expand All @@ -72,7 +65,6 @@ class CBuilder extends CTool implements Builder {
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
this.dynamicallyLinkTo = const [],
super.pic = true,
super.std,
super.language = Language.c,
Expand All @@ -86,6 +78,8 @@ class CBuilder extends CTool implements Builder {
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
super.libraries = const [],
super.libraryPaths = const [],
@Deprecated(
'Newer Dart and Flutter SDKs automatically add the Dart hook '
'sources as dependencies.',
Expand All @@ -95,7 +89,6 @@ class CBuilder extends CTool implements Builder {
super.defines = const {},
this.buildModeDefine = true,
this.ndebugDefine = true,
this.dynamicallyLinkTo = const [],
bool? pie = false,
super.std,
super.language = Language.c,
Expand Down Expand Up @@ -143,6 +136,11 @@ class CBuilder extends CTool implements Builder {
// ignore: deprecated_member_use_from_same_package
for (final source in this.dartBuildFiles) packageRoot.resolve(source),
];
final libraryPaths = [
outDir,
for (final path in this.libraryPaths)
outDir.resolveUri(Uri.file(path)),
];
// ignore: deprecated_member_use
if (!config.dryRun) {
final task = RunCBuilder(
Expand All @@ -152,6 +150,8 @@ class CBuilder extends CTool implements Builder {
sources: sources,
includes: includes,
frameworks: frameworks,
libraries: libraries,
libraryPaths: libraryPaths,
dynamicLibrary:
type == OutputType.library && linkMode == DynamicLoadingBundled()
? libUri
Expand All @@ -162,10 +162,7 @@ class CBuilder extends CTool implements Builder {
executable: type == OutputType.executable ? exeUri : null,
// ignore: invalid_use_of_visible_for_testing_member
installName: installName,
flags: [
...flags,
...config.dynamicLinkingFlags(dynamicallyLinkTo),
],
flags: flags,
defines: {
...defines,
if (buildModeDefine) config.buildMode.name.toUpperCase(): null,
Expand Down Expand Up @@ -216,23 +213,3 @@ class CBuilder extends CTool implements Builder {
}
}
}

extension on BuildConfig {
List<String> dynamicLinkingFlags(List<String> libraries) =>
switch (targetOS) {
OS.macOS || OS.iOS => [
'-L${outputDirectory.toFilePath()}',
for (final library in libraries) '-l$library',
],
OS.linux || OS.android => [
'-Wl,-rpath=\$ORIGIN/.',
'-L${outputDirectory.toFilePath()}',
for (final library in libraries) '-l$library',
],
OS.windows => [
for (final library in libraries)
outputDirectory.resolve('$library.lib').toFilePath(),
],
_ => throw UnimplementedError('Unsupported OS: $targetOS'),
};
}
2 changes: 2 additions & 0 deletions pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CLinker extends CTool implements Linker {
super.sources = const [],
super.includes = const [],
super.frameworks = CTool.defaultFrameworks,
super.libraries = const [],
super.libraryPaths = const [],
@visibleForTesting super.installName,
super.flags = const [],
super.defines = const {},
Expand Down
8 changes: 7 additions & 1 deletion pkgs/native_toolchain_c/lib/src/cbuilder/ctool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ abstract class CTool {

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

final List<String> libraries;

final List<String> libraryPaths;

/// TODO(https://github.com/dart-lang/native/issues/54): Move to [LinkConfig]
/// or hide in public API.
@visibleForTesting
final Uri? installName;

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

/// Definitions of preprocessor macros.
Expand Down Expand Up @@ -130,6 +134,8 @@ abstract class CTool {
required this.sources,
required this.includes,
required this.frameworks,
required this.libraries,
required this.libraryPaths,
required this.installName,
required this.flags,
required this.defines,
Expand Down
28 changes: 22 additions & 6 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class RunCBuilder {
final List<Uri> sources;
final List<Uri> includes;
final List<String> frameworks;
final List<String> libraries;
final List<Uri> libraryPaths;
final Uri? executable;
final Uri? dynamicLibrary;
final Uri? staticLibrary;
Expand Down Expand Up @@ -56,6 +58,8 @@ class RunCBuilder {
this.sources = const [],
this.includes = const [],
required this.frameworks,
this.libraries = const [],
this.libraryPaths = const [],
this.executable,
this.dynamicLibrary,
this.staticLibrary,
Expand Down Expand Up @@ -298,8 +302,7 @@ class RunCBuilder {
if (executable != null) ...[
'-o',
outDir.resolveUri(executable!).toFilePath(),
],
if (dynamicLibrary != null) ...[
] else if (dynamicLibrary != null) ...[
'--shared',
'-o',
outFile!.toFilePath(),
Expand All @@ -310,6 +313,16 @@ class RunCBuilder {
],
...linkerOptions?.postSourcesFlags(toolInstance.tool, sourceFiles) ??
[],
if (executable != null || dynamicLibrary != null) ...[
if (config.targetOS case OS.android || OS.linux)
if (linkerOptions != null)
'-rpath=\$ORIGIN'
else
'-Wl,-rpath=\$ORIGIN',
for (final libraryPath in libraryPaths)
'-L${libraryPath.toFilePath()}',
for (final library in libraries) '-l$library',
],
],
logger: logger,
captureOutput: false,
Expand Down Expand Up @@ -344,17 +357,20 @@ class RunCBuilder {
...sources.map((e) => e.toFilePath()),
'/link',
'/out:${outDir.resolveUri(executable!).toFilePath()}',
],
if (dynamicLibrary != null) ...[
] else if (dynamicLibrary != null) ...[
...sources.map((e) => e.toFilePath()),
'/link',
'/DLL',
'/out:${outDir.resolveUri(dynamicLibrary!).toFilePath()}',
],
if (staticLibrary != null) ...[
] else if (staticLibrary != null) ...[
'/c',
...sources.map((e) => e.toFilePath()),
],
if (executable != null || dynamicLibrary != null) ...[
for (final libraryPath in libraryPaths)
'/LIBPATH:${libraryPath.toFilePath()}',
for (final library in libraries) '$library.lib',
],
],
workingDirectory: outDir,
environment: environment,
Expand Down
10 changes: 3 additions & 7 deletions pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ void main() {
}
});

test('CBuilder dynamicallyLinkTo', () async {
test('CBuilder libraries', () async {
final tempUri = await tempDirForTest();
final dynamicallyLinkedCUri = packageUri.resolve(
'test/cbuilder/testfiles/dynamically_linked/src/dynamically_linked.c');
Expand All @@ -579,11 +579,7 @@ void main() {
buildMode: BuildMode.release,
// Ignored by executables.
linkModePreference: LinkModePreference.dynamic,
cCompiler: CCompilerConfig(
compiler: cc,
envScript: envScript,
envScriptArgs: envScriptArgs,
),
cCompiler: cCompiler,
linkingEnabled: false,
);
final buildOutput = BuildOutput();
Expand All @@ -598,7 +594,7 @@ void main() {
name: name,
includes: [addSrcUri.toFilePath()],
sources: [dynamicallyLinkedCUri.toFilePath()],
dynamicallyLinkTo: ['add'],
libraries: ['add'],
)
];
for (final builder in builders) {
Expand Down

0 comments on commit 1a11e7d

Please sign in to comment.