android / ndk

The Android Native Development Kit
1.96k stars 254 forks source link

[FR] NDK build supports C++ modules #1855

Open yujincheng08 opened 1 year ago

yujincheng08 commented 1 year ago

Description

CMake 3.26 with clang-16 provides preliminary support for c++ modules now, and for NDK we can specify the cmake version and CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS to enjoy c++ modules. But, for NDK build project, it does not look like an easy job to add c++ modules support from the user side.

kaizer1 commented 1 year ago

So in the end, is there support for modules or not? Can eat an example of successful implementation?

DanAlbert commented 1 year ago

It's not something we currently have the resources to investigate for ndk-build. If you use CMake that doesn't matter: https://www.kitware.com/import-cmake-c20-modules/

yujincheng08 commented 1 year ago

Sure we can use cmake. Since 3.26, cmake has added c++ module ninja build support for clang. And 3.27 fixes a bug that cmake failed to generate module dependencies for compile_command.json (see https://gitlab.kitware.com/cmake/cmake/-/issues/24618).

@DanAlbert Maybe we can cmake 3.27 to the SDK repository?

DanAlbert commented 1 year ago

You can use any CMake you like, it doesn't need to be in the SDK for you to use it: https://developer.android.com/studio/projects/install-ndk#vanilla_cmake

(We know it's more convenient when it's in the SDK repo, but the teams responsible for that are shorthanded)

yujincheng08 commented 1 year ago

Yes, convenience is what i want. Anyway, I noticed that #1853 has been fixed upstream, so a new version of cmake will be more friendly for devs that want to try c++ modules for the coming ndk 26.

kaizer1 commented 1 year ago

So 26 ndk came out - that is, my ideas can already be tried

yujincheng08 commented 1 year ago

@kaizer1 yes, ci version of ndk, cmake 3.27, and that is. (tho it may be a little tricky for the implementation of P2465R3 of libc++)

DanAlbert commented 1 year ago

(this bug is about ndk-build, if you want to continue discussing cmake, please take it to https://github.com/android/ndk/discussions)

nickbeth commented 7 months ago

I've recently tested C++20 modules support in the latest ndk release, and there seems to be a bug in the clang driver preventing it to work. I have correctly set up modules in CMake 3.28, but the clang driver is unable to find it during compilation and fails with the usual module not found error at the import site. After multiple tries, I replaced the clang.exe and clang++.exe executables inside the ndk bin dir with ones from the LLVM release 17.0.2, and that worked. I assume there must be some patch that was left out when bundling the ndk that fixed that issue for clang, so all we need to do is wait for LLVM upstream changes to be pulled in the next ndk release and try again.