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

chore(ffi): avoid hardcoding clang version #4405

Merged
merged 1 commit into from
Dec 12, 2024
Merged

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Dec 11, 2024

Update the workaround for rust-lang/rust#109717 to avoid hardcoding the clang version; instead, run clang -dumpversion to figure it out.

While we're there, use the CC_x86_64-linux-android env var, which should point to clang, rather than relying on ANDROID_NDK_HOME to be set.

@richvdh richvdh requested a review from a team as a code owner December 11, 2024 17:21
@richvdh richvdh requested review from bnjbvr and removed request for a team December 11, 2024 17:21
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.28%. Comparing base (0264e49) to head (909fa56).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4405   +/-   ##
=======================================
  Coverage   85.27%   85.28%           
=======================================
  Files         282      282           
  Lines       31209    31209           
=======================================
+ Hits        26615    26617    +2     
+ Misses       4594     4592    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bnjbvr bnjbvr requested a review from jmartinesp December 11, 2024 17:53
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Sure 😅 I'll ask someone who has more Android knowledge to review this, as I'm not sure why the change is needed (supposedly NDK_CLANG_VERSION` is not always set anymore?).

If you wanted to go the extra mile, we could introduce a new tiny crate for building this, that would contain the code for the workaround setup. It seemed fine to duplicate when it was one function-long, but now that it seems to become a bit more involved, sharing code would make sense IMO.

Comment on lines 22 to 29
let toolchain_path = cc
.ancestors()
.nth(2)
Copy link
Member

Choose a reason for hiding this comment

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

Can you include an example of what cc would look like, so we understand a bit better why nth(2) is the right thing to do?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup. 923fe51

// Configure rust to statically link against the `libclang_rt.builtins` supplied with clang.

// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the Android NDK.
let cc = PathBuf::from(env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"));
Copy link
Member

Choose a reason for hiding this comment

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

Can this variable be called clang_path?

Copy link
Member Author

Choose a reason for hiding this comment

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

println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
}
}

/// Run the clang binary at `clang_path`, and return its major version number
fn get_clang_major_version(clang_path: impl AsRef<OsStr>) -> String {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is called only once, I don't think we need a generic impl AsRef<...> parameter here 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

@richvdh
Copy link
Member Author

richvdh commented Dec 11, 2024

supposedly NDK_CLANG_VERSION is not always set anymore?

I don't think it was ever set, unless you knew about it and set it manually.

@richvdh
Copy link
Member Author

richvdh commented Dec 11, 2024

If you wanted to go the extra mile, we could introduce a new tiny crate for building this, that would contain the code for the workaround setup. It seemed fine to duplicate when it was one function-long, but now that it seems to become a bit more involved, sharing code would make sense IMO.

It would, but ugh my life is running short right now. I could do with just shipping this :/

@jmartinesp
Copy link
Contributor

jmartinesp commented Dec 12, 2024

Sorry, I wasn't able to take a good look at this PR until now: I'm not really fond of this solution, since we went from something that would work most of the time if you had the ANDROID_NDK_HOME and the latest LTS NDK version to something that now would only work if you provide the new CC_x86_64-linux-android env var, which must point to a non-easily discoverable path.

If we do this change it'll break the setups of everyone building the SDK Kotlin bindings up until now if I'm not mistaken, including the https://github.com/matrix-org/matrix-rust-components-kotlin repo, so I think it should be well documented in the bindings crate README file, with some example (same for the crypto-ffi crate)... as should have been the workaround already implemented by me, so I apologise for that 😓 .

Maybe a 'Breaking' changelog entry in the commit message would be good too.

@richvdh
Copy link
Member Author

richvdh commented Dec 12, 2024

something that now would only work if you provide the new CC_x86_64-linux-android env var, which must point to a non-easily discoverable path.

No, the whole point is that CC_x86_64-linux-android is automatically provided by cargo ndk. The intention of this change is very much something that just works most of the time.

@jmartinesp: would you mind giving it a go and confirming that it does, in fact, work for you?

@jmartinesp
Copy link
Contributor

No, the whole point is that CC_x86_64-linux-android is automatically provided by cargo ndk. The intention of this change is very much something that just works most of the time.

Oh, TIL! Then I retract everything I said. I already started a build in the bindings repo to test this out: https://github.com/matrix-org/matrix-rust-components-kotlin/actions/runs/12295378552/job/34312142906

Copy link
Contributor

@jmartinesp jmartinesp left a comment

Choose a reason for hiding this comment

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

Approving, since this works as expected. Thanks!

Comment on lines +18 to +19
// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the
// Android NDK.
Copy link
Contributor

Choose a reason for hiding this comment

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

I completely missed this comment 🤦 .

Update the workaround for rust-lang/rust#109717 to
avoid hardcoding the clang version; instead, run `clang -dumpversion` to figure
it out.

While we're there, use the `CC_x86_64-linux-android` env var, which should
point to clang, rather than relying on `ANDROID_NDK_HOME` to be set.
@richvdh richvdh force-pushed the rav/no_hardcode_clang branch from 3f15bca to 909fa56 Compare December 12, 2024 12:25
@richvdh richvdh merged commit 780a463 into main Dec 12, 2024
40 checks passed
@richvdh richvdh deleted the rav/no_hardcode_clang branch December 12, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants