Skip to content

[ClangImporter] Make sure the -resource-dir is checked before the -sdk, as done everywhere else in the compiler #74814

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

Merged
merged 1 commit into from
Aug 26, 2024

Conversation

finagolfin
Copy link
Member

Otherwise, these module maps can be pulled from a system SDK instead when building a fresh Swift stdlib, fixes #74696.

I saw no regressions when I built the latest June 13 trunk source snapshot natively on Android with this change and it fixed not picking up an older 5.10.1 module map that I had installed in the Termux SDK.

@egorzhdan, if you would run the CI on this, let's make sure it doesn't break anything on other platforms first.

@finagolfin finagolfin changed the title [ClangImporter] Make sure the -resource-dir is checked before the sdk, as done everywhere else in the compiler [ClangImporter] Make sure the -resource-dir is checked before the -sdk, as done everywhere else in the compiler Jun 28, 2024
@egorzhdan
Copy link
Contributor

@swift-ci please test

@compnerd
Copy link
Member

This causes a breakage locally for me due to the modulemaps now not being found.

@finagolfin
Copy link
Member Author

This causes a breakage locally for me due to the modulemaps now not being found.

Some more info, this is on Windows? Maybe you can paste the failing command.

@compnerd
Copy link
Member

Yes, on Windows, with an Android SDK being used to build; I'll try to get this soon, I have a different state now to fix a few issues with swift-foundation for android.

@finagolfin
Copy link
Member Author

Yes, on Windows, with an Android SDK being used to build

OK, I don't think that breakage is related to this patch, as this patch simply changes the order in which two paths are checked for the module maps, so it won't cause "the modulemaps now not being found," as it's still checking the same two paths.

What might be happening is that it's using a different module map with this patch, which might break something depending on your setup. Let us know what you find.

Since this passed CI, I will go ahead and add a test, to make sure it doesn't regress.

@tachoknight
Copy link
Contributor

@finagolfin I applied the patch to my Fedora build and I still get /home/rolson/rpmbuild/BUILD/swift-source/swift/stdlib/public/Cxx/std/Chrono.swift:19:12: error: cannot find '__swift_interopMakeChronoSeconds' in scope .

@finagolfin
Copy link
Member Author

Thanks for trying it out, @tachoknight, but I'm unclear how you're doing so. I gave a Fedora repro scenario in the last comment of the linked issue where a 5.10.1 system toolchain package is installed and interferes with building a 6.1 stdlib with a prebuilt Swift 6.1 compiler. Since this is a compiler patch, you would have to change that repro command to apply it, and it is not clear how you are doing so.

Let me know how you applied this patch and changed the Fedora repro command I gave to use it, and we can figure out if it works or not.

@tachoknight
Copy link
Contributor

@finagolfin I build Swift for Fedora using my repo here. I applied it to the master branch as that's always the "latest" version of Swift (e.g. 6 currently). Your patch is in the repo as finagolfin.patch.

If you'd like to give it a go in a container, all you have to do is create a new container (e.g. fedora:40), dnf install -y git, then checkout that repo, run setup-container.sh, then just-build.sh.

@finagolfin
Copy link
Member Author

@tachoknight, thanks for the link to the detailed setup. Three questions:

  1. You said you "still get" that error, implying this Fedora build is failing for you even before applying my patch, correct?
  2. I notice you are building the latest 6.0 snapshot there, not the latest 6.1 trunk snapshot as I am. I don't think that'll make a big difference, but maybe you could try trunk instead?
  3. Finally, I notice you had some issues typing in that patch: can you examine the source after the build error and make sure it applied cleanly? I've seen too many issues with git apply and the like silently failing to apply patches to trust that it works anymore.

I'd rather not do a full hours-long compiler build on my Fedora VPS to test this, maybe you can look into these issues and try rebuilding if you have faster hardware than me. If you're still seeing the error, I can then kick off a compiler build myself.

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

This LGTM in principle, and I agree that we should give -resource-dir priority over the SDK in this case, but let's make sure the regular build process isn't broken.

@finagolfin
Copy link
Member Author

let's make sure the regular build process isn't broken.

Unsure what you mean here? It passed CI.

I will add a test though, so this isn't ready yet.

@egorzhdan
Copy link
Contributor

Unsure what you mean here? It passed CI.

There are comments above from a couple of people saying that this patch is breaking their build process – let's make sure that doesn't happen before merging this patch.

I will add a test though, so this isn't ready yet.

Sounds good!

@finagolfin
Copy link
Member Author

Alright, I built the latest July 8 trunk source snapshot on Fedora 40 x86_64 using the system 5.10.1, and it failed exactly as shown in the linked issue with a prebuilt trunk compiler, but with a freshly-built trunk compiler now by running ./swift/utils/build-script -RA --skip-early-swift-driver. I then applied this patch and it succeeded, just as I had seen before natively on Android in the Termux app.

@tachoknight, I suspect this patch was not applied cleanly in your build, as it should get the Fedora build working again for you too. Let me know what you find.

@compnerd, find anything yet? As I said, since this pull only switches the order in which paths are checked, it's not going to cause "the modulemaps now not being found," though it might pick up a different modulemap, depending on your setup.

@finagolfin
Copy link
Member Author

Rebuilt this on Fedora without the --skip-early-swift-driver flag shown above, made no difference.

I dumped the libc mappings as I discussed in the linked issue, and found that glibc.modulemap and SwiftGlibc.h were also wrongly used from the Fedora system 5.10.1 SDK without this pull, which is a more serious bug. Thankfully, those two files have not been updated in years, but if they are changed some day, say to modularize that overlay, we would start seeing errors with those too.

I also tried cross-compiling the stdlib for Android and running the compiler validation suite in host test mode for Android with this pull, and most tests passed.

@hyp, you've been running the compiler validation suite in host test mode for Android: can you try it with this pull and see if any more tests fail?

@compnerd
Copy link
Member

Okay, rebuilt with this change and got the error logs from what previously would build fine:

S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:10: note: while building module 'std' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:
  25 |
  26 | #include <stdarg.h>
  27 | #include <stdint.h>
     |          `- note: while building module 'std' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:
  28 |
  29 | /* Primitive types that match up with Java equivalents. */

S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include/c++/v1/inttypes.h:252:17: note: while building module '_Builtin_inttypes' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include/c++/v1/inttypes.h:252:
250 |
251 | #if __has_include_next(<inttypes.h>)
252 | #  include_next <inttypes.h>
    |                 `- note: while building module '_Builtin_inttypes' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include/c++/v1/inttypes.h:252:
253 | #endif
254 |

<module-includes>:1:10: note: in file included from <module-includes>:1:
1 | #include "inttypes.h"
  |          `- note: in file included from <module-includes>:1:
2 |

S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\lib\swift\clang\include/inttypes.h:21:15: note: in file included from S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\lib\swift\clang\include/inttypes.h:21:
19 | #endif

20 |

21 | #include_next <inttypes.h>

   |               `- note: in file included from S:\Program Files\Swift\Toolchains\0.0.0+Asserts\usr\lib\swift\clang\include/inttypes.h:21:
22 |

23 | #if defined(_MSC_VER) && _MSC_VER < 1900


S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\inttypes.h:22:10: error: cyclic dependency in module 'std': std -> _Builtin_inttypes -> std
 20 | #define   _INTTYPES_H_
 21 |
 22 | #include <stdint.h>
    |          `- error: cyclic dependency in module 'std': std -> _Builtin_inttypes -> std
 23 | #include <sys/cdefs.h>
 24 |

S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:10: note: while building module 'std' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:
  25 |
  26 | #include <stdarg.h>
  27 | #include <stdint.h>
     |          `- note: while building module 'std' imported from S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:
  28 |
  29 | /* Primitive types that match up with Java equivalents. */

<module-includes>:11:10: note: in file included from <module-includes>:11:
  9 | }
 10 | extern "C" {
 11 | #include "inttypes.h"
    |          `- note: in file included from <module-includes>:11:
 12 | }
 13 | extern "C" {

S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include/c++/v1/inttypes.h:252:17: error: could not build module '_Builtin_inttypes'
250 |
251 | #if __has_include_next(<inttypes.h>)
252 | #  include_next <inttypes.h>
    |                 `- error: could not build module '_Builtin_inttypes'
253 | #endif
254 |

<module-includes>:1:10: note: in file included from <module-includes>:1:
1 | #include "FirebaseApp.hh"
  |          `- note: in file included from <module-includes>:1:
2 | #include "FirebaseAuth.hh"
3 | #include "FirebaseCore.hh"

S:\SourceCache\thebrowsercompany\swift-firebase\Sources\firebase\include/FirebaseApp.hh:6:10: note: in file included from S:\SourceCache\thebrowsercompany\swift-firebase\Sources\firebase\include/FirebaseApp.hh:6:
4 | #define firebase_include_FirebaseApp_hh
5 |
6 | #include <firebase/app.h>
  |          `- note: in file included from S:\SourceCache\thebrowsercompany\swift-firebase\Sources\firebase\include/FirebaseApp.hh:6:
7 |
8 | #endif

S:\SourceCache\thebrowsercompany\swift-firebase\third_party\firebase-development\usr\include\firebase/app.h:23:10: note: in file included from S:\SourceCache\thebrowsercompany\swift-firebase\third_party\firebase-development\usr\include\firebase/app.h:23:
 21 |

 22 | #if FIREBASE_PLATFORM_ANDROID

 23 | #include <jni.h>

    |          `- note: in file included from S:\SourceCache\thebrowsercompany\swift-firebase\third_party\firebase-development\usr\include\firebase/app.h:23:
 24 | #endif  // FIREBASE_PLATFORM_ANDROID

 25 |


S:\Applications\Android\ndk\r26d\toolchains\llvm\prebuilt\windows-x86_64\sysroot/usr/include\jni.h:27:10: error: could not build module 'std'
  25 |
  26 | #include <stdarg.h>
  27 | #include <stdint.h>
     |          `- error: could not build module 'std'
  28 |
  29 | /* Primitive types that match up with Java equivalents. */

S:\SourceCache\thebrowsercompany\swift-firebase\Sources\FirebaseCore\FirebaseApp+Swift.swift:4:8: error: could not build C module 'firebase'
 2 |
 3 | @_exported
 4 | import firebase
   |        `- error: could not build C module 'firebase'
 5 |
 6 | #if os(Android)
ninja: build stopped: subcommand failed.

tachoknight added a commit to tachoknight/swift-lang-packaging-fedora that referenced this pull request Jul 10, 2024
@finagolfin
Copy link
Member Author

That's pretty much useless: you'll want to extract the failing command, then run it manually with -v -Xcc -v -Xfrontend -dump-clang-diagnostics appended, as I showed in the linked issue when diagnosing this problem in the first place. Even that may not be enough, but it's a start.

@tachoknight
Copy link
Contributor

@finagolfin Yes when the patch is applied properly (I got it directly from this PR), it works. Now I'm onto errors further along the build chain, unrelated to your patch. :)

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

This change LGTM. Given that it fixes a build issue a lot of people are reliably hitting on Linux, I don't think we necessarily need a compiler test for this change right now. Let's land this patch this week.

@compnerd are you still seeing a build error with this patch? If you could provide steps to reproduce, I can try reproducing this on my end to get the verbose logs.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Aug 5, 2024
@finagolfin
Copy link
Member Author

Fine by me, I was going to try and add a test for this in the coming weeks, but I can always add that after this pull is in.

@compnerd
Copy link
Member

compnerd commented Aug 5, 2024

IIRC, it was simply using the Android SDK on Windows and cross-compiling swift-firebase for Android. There has been a series of regressions in the toolchain which is currently occupying my attention, so I would appreciate it if you could look into it @egorzhdan!

@egorzhdan
Copy link
Contributor

Ah, I missed that this build is targeting Android, I don't have the setup to test it unfortunately.

However, from the error message is looks like the libc++ version that's being picked up is somewhat old, and the module std is not yet split into many top-level modules in that version. That modulemap is essentially incorrect, it was fixed by @ian-twilightcoder in the upstream LLVM repo last year.

@compnerd is it possible for you to switch to a newer libc++ that doesn't suffer from this issue?

Alternatively, we could temporarily preserve the old search paths ordering for builds targeting Android.

@finagolfin
Copy link
Member Author

I don't think we should stress about that Android SDK for Windows. They are experimenting with a new layout and compilation flags, unusual changes which are not done on all other Unix platforms currently. If this pull breaks that experimental SDK, it should be fine, as long as it fixes much more stable platforms like linux.

@compnerd
Copy link
Member

compnerd commented Aug 7, 2024

@egorzhdan unfortunately, that is the version of the libc++ that is shipped in the latest Android SDK so we cannot update that. @finagolfin I strongly disagree with that - this was working before this change and breaking it because "its different" is not really acceptable. This currently does work and is not "experimental". It is actively being used by The Browser Company as we are developing Android libraries.

@finagolfin
Copy link
Member Author

This currently does work and is not "experimental". It is actively being used by The Browser Company as we are developing Android libraries.

It works because you added new paths and flags to your local toolchain, that are not used on other Unix platforms, as I detailed on that other pull. I am not against making those changes, as I noted in that linked comment that I actually submitted pulls to change the runtime library path for Unix platforms before that (which was unfortunately rejected by @etcwilde), but you can't expect us to break stable platforms like linux because you want to make breaking changes just for your new Android SDK on Windows.

As I said before, we can try to accomodate you, but you will have to give us some info on what you're doing. Have @hyp or somebody track down the failing compilation command with this pull, add -v -Xcc -v -Xfrontend -dump-clang-diagnostics to that command, and show us the full command and its output.

As long as you guys don't bother giving us that log and layout info, I don't see why we should support your experimental SDK layout, which is not used on linux.

@compnerd
Copy link
Member

compnerd commented Aug 7, 2024

I am willing to do that, but am currently dealing with a number of recent regressions in the build and trying to finish some of the changes for 6.0 regarding the macros builds for foundation. I can circle back to this once that is complete.

@egorzhdan
Copy link
Contributor

In the meantime, could we have a special case for builds targeting Android to preserve the old ordering, and apply the new ordering in all other cases? That would allow landing this patch sooner, and would unblock several usage scenarios on Linux.

@finagolfin
Copy link
Member Author

The issue is not Android. The issue is likely the new cross-compilation model laid out here, which they are now implementing only for their Android SDK that they're building on Windows.

egorzhdan added a commit that referenced this pull request Aug 22, 2024
This applies #74814 for all targets except Android.

This fixes a common source of build failures on Linux, where shim functions from CxxStdlibShim would not be found by the hosttools Swift compiler when building the CxxStdlib overlay.

This change is keeping Android intact to keep the existing Android SDK working.
result.append(SDKPath.begin(), SDKPath.end());
llvm::sys::path::append(result, "usr", "lib", "swift");
if (!Opts.RuntimeResourcePath.empty()) {
result.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

This line needs to be in the second if-condition below. Currently the SDK check would fail because the path would be incorrectly constructed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I simply copy-pasted this from below, maybe that's why it was failing for them on Windows? I will update this and maybe they can try again.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe that's why it was failing for them on Windows

Ah, I'm pretty sure that's indeed the reason! 😃

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, rebased and made this change.

@egorzhdan, another CI run would be good.

@compnerd or @hyp, please try this updated pull with this change pointed out by Egor and let us know if it works now.

Copy link
Member Author

Choose a reason for hiding this comment

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

@hjyamauchi, maybe you can trigger a Windows build with this pull, if your TBC colleagues are too busy.

Copy link
Contributor

Choose a reason for hiding this comment

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

@finagolfin if you mean the TBC CIs, they are still not in a good state and we're working on fixing them, which may need a bit more time :)

Copy link
Member Author

Choose a reason for hiding this comment

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

@hjyamauchi, I'm talking about whatever was used to produce the TBC error output above, which we don't see on this CI, could be CI or a local run. If your CI is broken now, maybe you can try a local run?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not actually sure what he ran there but I'll see if I can do some build

Copy link
Contributor

Choose a reason for hiding this comment

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

I ran a local build at tip of tree with this PR and it was successful. But I didn't build (or know how to build) it with the Android SDK and this was probably not helpful. Unless @hyp knows how, this may need to wait for @compnerd to come back.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @hjyamauchi, that was still a useful signal.

@egorzhdan
Copy link
Contributor

I put up another patch #76036 to unblock folks who are seeing compiler errors when building the Swift toolchain on Linux.

…-sdk`, as done everywhere else in the compiler

Otherwise, these module maps can be pulled from a system SDK instead when
building a fresh Swift stdlib, fixes swiftlang#74696.
@egorzhdan
Copy link
Contributor

@swift-ci please test

@egorzhdan
Copy link
Contributor

I think we'll have to land this patch now to unblock hosttools builds on Linux.
I believe this shouldn't break the Android builds anymore, but if it does, please feel free to revert this patch!

@egorzhdan egorzhdan merged commit 5080628 into swiftlang:main Aug 26, 2024
5 checks passed
@finagolfin finagolfin deleted the map branch August 27, 2024 06:34
finagolfin added a commit to finagolfin/swift that referenced this pull request Aug 28, 2024
…dir` before `-sdk`, as changed in swiftlang#74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests.
finagolfin added a commit to finagolfin/swift that referenced this pull request Aug 28, 2024
…dir` before `-sdk`, as changed in swiftlang#74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests.
finagolfin added a commit to finagolfin/swift that referenced this pull request Aug 28, 2024
…dir` before `-sdk`, as changed in swiftlang#74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests, and fix
another Frontend test to be more specific now that this other output trips it up.
finagolfin added a commit to finagolfin/swift that referenced this pull request Aug 28, 2024
…dir` before `-sdk`, as changed in swiftlang#74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests, and fix
another Frontend test to be more specific now that this other output trips it up.
finagolfin added a commit to finagolfin/swift that referenced this pull request Aug 29, 2024
…dir` before `-sdk`, as changed in swiftlang#74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests, and fix
another Frontend test to be more specific now that this other output trips it up.
DougGregor pushed a commit that referenced this pull request Dec 2, 2024
…dir` before `-sdk`, as changed in #74814

Also, dump the module map paths when `-Xfrontend -dump-clang-diagnostics` is
passed in, both so we can check that manually and in these tests, and fix
another Frontend test to be more specific now that this other output trips it up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClangImporter looks in the wrong path for the libc and new C++ module maps when -sdk is specified
5 participants