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 linking for native_toolchain_c and linux #987

Open
wants to merge 222 commits into
base: main
Choose a base branch
from

Conversation

mosuem
Copy link
Member

@mosuem mosuem commented Mar 11, 2024

Requires #827 to be merged.

Extend API by providing a CLinker, supporting development only on linux for now. Other platforms to be added in future PRs.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

@mosuem mosuem marked this pull request as ready for review April 12, 2024 11:59
@mosuem mosuem requested a review from dcharkes April 15, 2024 11:48
commit c24e422
Author: Moritz <mosum@google.com>
Date:   Tue Apr 16 10:46:50 2024 +0200

    Unify `configFile`

commit 53ab489
Author: Moritz <mosum@google.com>
Date:   Tue Apr 16 10:16:26 2024 +0200

    Changes as per review

commit 95a6479
Author: Moritz <mosum@google.com>
Date:   Mon Apr 15 16:48:49 2024 +0200

    Copy over more files

commit c33d5dd
Author: Moritz <mosum@google.com>
Date:   Mon Apr 15 15:02:10 2024 +0200

    Fix linking id check

commit b88c152
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:55:56 2024 +0200

    Fix dep

commit 3d8ed4a
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:17:52 2024 +0200

    Fix import

commit 3955ea9
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:09:59 2024 +0200

    Export ResourceIdentifiers
commit 725bdd7
Author: Moritz <mosum@google.com>
Date:   Tue Apr 16 15:58:21 2024 +0200

    Make resources nullable

commit c24e422
Author: Moritz <mosum@google.com>
Date:   Tue Apr 16 10:46:50 2024 +0200

    Unify `configFile`

commit 53ab489
Author: Moritz <mosum@google.com>
Date:   Tue Apr 16 10:16:26 2024 +0200

    Changes as per review

commit 95a6479
Author: Moritz <mosum@google.com>
Date:   Mon Apr 15 16:48:49 2024 +0200

    Copy over more files

commit c33d5dd
Author: Moritz <mosum@google.com>
Date:   Mon Apr 15 15:02:10 2024 +0200

    Fix linking id check

commit b88c152
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:55:56 2024 +0200

    Fix dep

commit 3d8ed4a
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:17:52 2024 +0200

    Fix import

commit 3955ea9
Author: Moritz <mosum@google.com>
Date:   Fri Apr 12 15:09:59 2024 +0200

    Export ResourceIdentifiers
Copy link
Collaborator

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add linking for native_toolchain_c and linux

[native_toolchain_c] Linker support for Linux

@@ -19,7 +17,10 @@ void main(List<String> arguments) async {
'src/native_add.c',
'src/native_multiply.c',
],
dartBuildFiles: ['hook/build.dart'],
dartBuildFiles: [
'hook/build.dart',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to rerun the build hook if the link hook changed?

os: config.targetOS,
),
linkInPackage: packageName,
NativeCodeAsset placeholderAsset(HookConfig config) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed anymore.

Am I correct in thinking that:

  1. The unit tests for the package here can land without SDK changes.
  2. Then we land the SDK CL in which this test_data is used in dartdev unit tests and we actually add a new asset in link.
  3. Then we land another PR in the dart-lang/native that enables the examples.

If I am not mistaken, we should not need to land the placeholder workaround.

/// Get the linker flags for the specified [linker].
///
/// Throws if the [linker] is not supported.
Iterable<String> preflags(Tool linker) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document pre-what.

/// Get the linker flags for the specified [linker].
///
/// Throws if the [linker] is not supported.
Iterable<String> flags(Tool linker) => _toLinkerSyntax(linker, [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be postFlags? And post-what?

linkerTool == clang ||
linkerTool == gcc ||
linkerTool == gnuLinker) {
await runClangLike(compiler: linker_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compiler: linker_ looks odd, maybe rename the param to tool so it's tool: linker_.

tool = gnuLinker;

//TODO: Make this logic more correct
if (filePath.endsWith(os.executableFileName('clang'))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we recognize a linker with a compiler executable name?

@@ -0,0 +1,6 @@
#include <stdio.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit binary files (test.a, test1.o, test2.o).

Add git ignores.

sources: [
'test/clinker/testfiles/linker/test1.o',
'test/clinker/testfiles/linker/test2.o',
].map((e) => packageUri.resolve(e).toFilePath()).toList())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

].map((e) => packageUri.resolve(e).toFilePath()).toList(),) for better formatting

class LinkerRecognizer implements ToolResolver {
final Uri uri;
final OS os;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is not used, resolve has a variable os that shadows it.

@@ -1,7 +1,7 @@
name: native_toolchain_c
description: >-
A library to invoke the native C compiler installed on the host machine.
version: 0.4.1
version: 0.4.2-wip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I can't leave comments on binary files.)

Don't commit test.so, add a git ignore.

//TODO: expose name from `placeholderAsset`
assetName: 'src/${packageName}_bindings.dart',
linkerOptions: LinkerOptions.treeshake(
symbols: config.resources
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 😄 🚀

Base automatically changed from addLinkingScript to main May 8, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants