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

Improve user experience with ldc2, incl. cross-compilation #1755

Merged
merged 8 commits into from
Aug 25, 2019

Conversation

kinke
Copy link
Contributor

@kinke kinke commented Aug 17, 2019

No description provided.

Don't perform the does-default-target-triple-contain-msvc check on
non-Windows hosts (which would need a hacked LLVM to return true on
other hosts).
@dlang-bot
Copy link
Collaborator

Thanks for your pull request and interest in making D better, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

@kinke
Copy link
Contributor Author

kinke commented Aug 17, 2019

@skoppe: Could you give this a try and see if it'd be a viable solution for #1749?

@kinke
Copy link
Contributor Author

kinke commented Aug 17, 2019

Other related issues: #809, #1431, #1523, #1701.

@@ -42,6 +42,7 @@ enum string platformCheck = q{
version(Android) ret ~= "android";
version(Cygwin) ret ~= "cygwin";
version(MinGW) ret ~= "mingw";
version(WebAssembly) ret ~= "wasm";
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good for completeness sake, but it won't do much, since it only ever runs when compiling dub itself to WebAssembly.

Copy link
Contributor Author

@kinke kinke Aug 17, 2019

Choose a reason for hiding this comment

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

Nope, this is also mixed into a platform probe file. The compiler compiles that one, using dub's --arch as LDC -mtriple, so that the BuildPlatform (parsed from compiler output) will feature a wasm platform. It's what makes dub 'target-aware'.

@skoppe
Copy link
Contributor

skoppe commented Aug 17, 2019

@skoppe: Could you give this a try and see if it'd be a viable solution for #1749?

So I just compile with dub -arch=wasm32-unknown-unknown-wasm?

@kinke
Copy link
Contributor Author

kinke commented Aug 17, 2019

Yep, and remove the -mtriple option in the dub config files.

@skoppe
Copy link
Contributor

skoppe commented Aug 17, 2019

I can see that dub passes the triple to ldc, but it also looks the dflags in the dub.sdl are stripped, including the betterC I need to pass compilation.

This is basically dlang#1548 with an augmented set of compiler flags being
included in the linking command lines.
By treating all 'architectures' containing a `-` as `-mtriple` values.
E.g., `dub --arch=wasm32-unknown-unknown-wasm` to cross-compile to
WebAssembly. This dub switch is already used when probing the compiler,
so the build platform detection works as expected.

Also add `wasm` as detected platform and use the .wasm executable file
extension (for ldc2).

This fixes dlang#1749 when moving the `-mtriple` option from the dub config
file to the dub command line.
@kinke
Copy link
Contributor Author

kinke commented Aug 18, 2019

I don't see how previous dflags wouldn't be applied anymore (after the amended fix wrt. a -betterC dflag making it into link cmdlines too when linking separately). dub configs + resulting cmdlines would be helpful. [My attempts at building a spasm dummy project fail due to the optional package dependency not compiling for wasm (missing wchar_t alias in druntime - only defined for version (Windows) and version (Posix) - etc).]

How is that supposed to work anyway? I mean, each (transitive) dependency of spasm would ideally be compiled with -betterC -fvisibility=hidden (-mtriple already is via --arch). Is there a way to enforce some base flags for the whole package tree?

With --arch, the build (target) platform might now diverge from the host
platform. The compiler's default triple doesn't matter when
cross-compiling, so use the <name>.lib MSVC format when targeting Windows
but not MinGW.

This fixes one aspect of dlang#1599.
@skoppe
Copy link
Contributor

skoppe commented Aug 18, 2019

How is that supposed to work anyway? I mean, each (transitive) dependency of spasm would ideally be compiled with -betterC -fvisibility=hidden (-mtriple already is via --arch). Is there a way to enforce some base flags for the whole package tree?

No there is not. That is a short coming of dub. Properties are only propagated upwards.

With regards to optional, I made a pull request for it to be betterC compatible (and merged). That has worked in the past. But with this pr I get similar errors to you.

I looked at the compiler invocations and with the current dub, the optional package isn't actually build for wasm, no triple, no betterC.

With this pr the wasm triple gets propagates downwards and causes issues. Well, rightfully so. I made a pr for the optional package to fix its configuration see pull#38

The reason it works is that there are only templates in the package.

@kinke
Copy link
Contributor Author

kinke commented Aug 18, 2019

That is a short coming of dub. Properties are only propagated upwards.

Oh wow, a serious shortcoming indeed. One could apparently work something out with the DFLAGS env variable, but I would have expected dub support for some global --dflags.

But with this pr I get similar errors to you.

This is off-topic, but DMD only supports either version (Windows) or version (Posix), while LDC predefines none of those 2 for Android, WebAssembly and unknown OS. Predefining Posix (currently disallowed in cmdline) for WebAssembly would get around these compile errors.

@skoppe
Copy link
Contributor

skoppe commented Aug 18, 2019

This is off-topic, but DMD only supports either version (Windows) or version (Posix), while LDC predefines none of those 2 for Android, WebAssembly and unknown OS. Predefining Posix (currently disallowed in cmdline) for WebAssembly would get around these compile errors.

also off-topic:

I have a wip PR in druntime where I define all those things for WebAssembly. Actually I took the WASI headers so it isnt strictly wasm. My reasoning is that is if you are using libc/druntime in wasm, you are actually either targeting WASI or emscripten. And if you don't, you simply dont call them and they will never end up in the binary.

Regardless, I don't think setting the Posix version for WebAssembly is the right thing to do. I remember trying it briefly just to get things compiling without betterC, but I abandoned that in favor of wasi.

@skoppe
Copy link
Contributor

skoppe commented Aug 18, 2019

@skoppe: Could you give this a try and see if it'd be a viable solution for #1749?

So I just compile with dub -arch=wasm32-unknown-unknown-wasm?

If I fix the above mentioned issues with optional (and mir-core as well), I can confirm that dub now sets the file extensions correctly.

Then I get this error:

lld: error: unknown argument: --no-as-needed

with

LDC - the LLVM D compiler (1.17.0-beta1):
  based on DMD v2.087.1 and LLVM 8.0.1
  built with LDC - the LLVM D compiler (1.17.0-beta1)

@kinke
Copy link
Contributor Author

kinke commented Aug 18, 2019

I have a wip PR in druntime where I define all those things for WebAssembly.

Ah nice, yeah that's a proper solution. I assume it cannot be tested for regressions though?

@kinke
Copy link
Contributor Author

kinke commented Aug 18, 2019

Then I get this error

Thx, should be fixed now.

@skoppe
Copy link
Contributor

skoppe commented Aug 18, 2019

Oh wow, a serious shortcoming indeed. One could apparently work something out with the DFLAGS env variable, but I would have expected dub support for some global --dflags.

I have hit that issue before with the dip1000/dip25 compiler flag. The issue was that a dependency had a template which I used in a project where dip1000/dip25 was active. It caused mangling issues because the template called a regular function in the dependency, which had different function attributes inferred on it (IIRC it was the return attribute). The workaround was to use the sourceLibrary targetType.

The obvious solution would be to propagate all dips (or other future compiler feature flags) to all dependencies. But I don't think that will work, as it is perfectly valid for dependencies to not use various compiler feature flags. Although with something as determent as an arch, it seems more than reasonable to propagate it.

@skoppe
Copy link
Contributor

skoppe commented Aug 18, 2019

Thx, should be fixed now.

Works!

@kinke
Copy link
Contributor Author

kinke commented Aug 18, 2019

Although with something as determent as an arch, it seems more than reasonable to propagate it.

Yep, I think there's a class of flags which make perfect sense as base flags, incl. -betterC, -linker, -L, -Xcc, -fvisibility, -flto (yes, that should suffice for LTO across all packages) etc. I'd leave it up to the user to choose sane ones.

…latform runtime checks)

Also fixes dlang#1249. For LDC as implicit cross compiler, the file
extensions for libraries and object files etc. don't depend on the
compiler, but solely on the target. Targets selected with `--arch` are
correctly probed by dub, so that the parsed `BuildPlatform` represents
the target platform.
@andre2007
Copy link
Contributor

@kinke the dub argument for just printing what would be done is --annotate.

Copy link
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

Not a big deal if the test isn't here IMO, especially given it's currently pretty hard to test this kind of things.

@Geod24
Copy link
Member

Geod24 commented Aug 25, 2019

Ping @wilzbach @thewilsonator , it would be great to have this in the next release.

@MartinNowak
Copy link
Member

A new feature right before the release candidate?
This change could use a test case and a changelog entry for the cross-compilation.

@Geod24
Copy link
Member

Geod24 commented Aug 29, 2019

A new feature right before the release candidate?

A lot of bug fixes, that only affect LDC, and were PRed by an LDC maintainer.
Basic things like coverage and profiling didn't even work with LDC and didn't give any feedback about not working.

kinke added a commit to kinke/ldc that referenced this pull request Sep 7, 2019
This includes dlang/dub#1755 which improves LDC
support in general and enables specifying arbitrary LLVM target triples
as dub `--arch` option.
kinke added a commit to ldc-developers/ldc that referenced this pull request Sep 7, 2019
This includes dlang/dub#1755 which improves LDC
support in general and enables specifying arbitrary LLVM target triples
as dub `--arch` option.
kinke added a commit to kinke/dub that referenced this pull request Sep 7, 2019
dlang-bot added a commit that referenced this pull request Sep 7, 2019
Changelog follow-up to #1755
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
@jondegenhardt
Copy link

Yep, I think there's a class of flags which make perfect sense as base flags, incl. -betterC, -linker, -L, -Xcc, -fvisibility, -flto (yes, that should suffice for LTO across all packages) etc. I'd leave it up to the user to choose sane ones.

@kinke - Building with LTO such that it included dependent packages would be very useful. Is this what you have in mind?

@kinke
Copy link
Contributor Author

kinke commented Oct 30, 2019

Yes; should be doable today via DFLAGS='-flto=thin' dub ....

@skoppe
Copy link
Contributor

skoppe commented Oct 30, 2019

Yep, I think there's a class of flags which make perfect sense as base flags, incl. -betterC, -linker, -L, -Xcc, -fvisibility, -flto (yes, that should suffice for LTO across all packages) etc. I'd leave it up to the user to choose sane ones.

I would also include -conf to that list. I am currently porting druntime to wasm and my setup includes having a base package that defines a custom ldc.conf. It works well for dependent packages (they inherit the conf), but falls short when using dependencies.

@jondegenhardt
Copy link

Yes; should be doable today via DFLAGS='-flto=thin' dub ....

Hm... I thought there was more to it. I'm partly thinking of having dependent packages also pull in phobos/druntime via -defaultlib=phobos2-ldc-lto,druntime-ldc-lto, but maybe that'd happen automatically as well.

I'll look into this further and see if it basically takes care of itself or there are other details to be worked through.

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

8 participants