通过 Gradle 和 Android NDK 在详细模式下强制 CMake [英] Force CMake in verbose mode via Gradle and the Android NDK
问题描述
我正在使用 Gradle 和 CMake 从命令行编译 Android NDK 项目.以前,我使用 Ant 和 ndk-build
,但我正在尝试将项目完全迁移到 Gradle 和 CMake.
I'm using Gradle and CMake to compile an Android NDK project from the command line. Previously, I was using Ant and ndk-build
but I'm trying to migrate the project completely to Gradle and CMake.
在我的 build.gradle
中,我有以下几行来调用 CMake:
In my build.gradle
I have the following lines to invoke CMake:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
现在如何强制 CMake 在生成所有编译器调用之前将它们打印到控制台?具体来说,我想看看 CMake 如何运行编译器和链接器.
Now how can I force CMake to print all compiler calls to the console before it makes them? Specifically, I want to see just how CMake runs the compiler and linker.
我已经尝试了以下方法,但都无济于事:
I've already tried the following, all to no avail:
1) 在我的 CMakeLists.txt
中,我添加了以下行:
1) In my CMakeLists.txt
I have put the following line:
set(CMAKE_VERBOSE_MAKEFILE on)
没有任何影响.
2) 我已经开始构建这样的:
2) I have started the build like this:
./gradlew build --info
Gradle 打印了一些东西,但没有编译器调用.
Gradle printed some stuff, but no compiler calls.
3) 就像这样:
./gradlew build --debug
Gradle 打印了很多东西,但没有编译器调用.
Gradle printed lots of stuff, but no compiler calls.
所以这三个尝试都没有达到我想要的效果,这让我想知道如何才能看到 CMake 如何在我的单个源文件上运行 clang?
So none of those three attempts did what I wanted which makes me wonder how can I see how CMake runs clang on my individual source files?
推荐答案
免责声明: 以下说明适用于 Android Gradle Plugin (AGP)这是我更新此答案时的最新消息(21 年 8 月).如果您好奇,请查看编辑历史.
Disclaimer: the following description applies to the version of Android Gradle Plugin (AGP) that was the latest at the time I updated this Answer (aug '21). If you are curious, have a look at the history of edits.
正如 @artyomd 所注意到的,对于 AGP 4.2.0 及更高版本,您可以将 android.native.buildOutput
gradle 属性设置为 verbose
强制 cmake 日志记录.
As @artyomd has noticed, for AGP 4.2.0 and higher, you can set android.native.buildOutput
gradle property to verbose
to force cmake logging.
在 Android Studio 中,gradle 在模块根目录下为每个具有 NDK 集成的模块创建目录 .cxx
,通过 CMake 或 ndk-build.
In Android Studio, gradle creates directory .cxx
under the module root, for each module that has NDK integration, via CMake or ndk-build.
对于 CMake,gradle 插件非常冗长.对于每个构建变体,它创建单独的子目录,例如.cxx/cmake/debug/x86
或 .cxx/cmake/release/armeabi-v7a
等
For CMake, the gradle plugin is quite verbose. For each build variant it creates separate subdirectory, e.g. .cxx/cmake/debug/x86
or .cxx/cmake/release/armeabi-v7a
, etc.
每个目录都包含一些有用的文件:cmake_build_command.txt 描述了传递给 CMake 的实际参数;android_gradle_build.json 显示 gradle 插件为您的二进制文件派生的参数;从 build.ninja,您可以推断出这些参数是如何应用于每个编译或链接步骤的.
Each directory contains some useful files: cmake_build_command.txt describes the actual parameters passed to CMake; android_gradle_build.json shows what parameters the gradle plugin derived for your binaries; from build.ninja you can deduce the how these parameters were applied for each compilation or linkage step.
对于ndk-build
,android_gradle_build.json
文件也很有用.ndkBuild_build_command.txt
列出传递给 ndk-build 命令的所有参数,ndkBuild_build_output.txt
是该命令的未删节输出.您可以轻松地将 V=1
添加到参数中,例如
For ndk-build
, the android_gradle_build.json
file is also quite useful. ndkBuild_build_command.txt
lists all parameters passed to ndk-build command, and ndkBuild_build_output.txt
is the unabridged output of that command. You can easily add V=1
to the arguments, e.g.
externalNativeBuild {
ndkBuild {
cppFlags "-std=c++11"
arguments "APP_STL=c++_static", "APP_OPTIM=release", "NDK_DEBUG=0", "V=1"
abiFilters "armeabi-v7a"
}
}
对于 CMake
,相关参数是 "-DCMAKE_VERBOSE_MAKEFILE=ON"
(请参阅说明和替代方案):
For CMake
, the relevant argument is "-DCMAKE_VERBOSE_MAKEFILE=ON"
(see explanation
and alternatives):
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DCMAKE_VERBOSE_MAKEFILE=ON"
abiFilters "armeabi-v7a"
}
}
但是,正如 @user7860670 所观察到的,AGP 的最新版本忽略了这个标志.
But, as @user7860670 has observed, the recent versions of AGP ignore this flag.
如果没有 CMAKE_VERBOSE_MAKEFILE
,Gradle 控制台会显示:
Without CMAKE_VERBOSE_MAKEFILE
, the Gradle Console displays:
:app:externalNativeBuildDebug
Build native-lib armeabi-v7a
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
使用 "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
,我过去常常获得 大量输出:
With "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
, I used to get tons of output:
:app:externalNativeBuildDebug
Build native-lib armeabi-v7a
[1/2] /Users/alex/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv5te-none-linux-androideabi --gcc-toolchain=/Users/alex/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/alex/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Dnative_lib_EXPORTS -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -isystem /Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c /Users/alex/test/egl/app/src/main/cpp/native-lib.cpp
[2/2] : && /Users/alex/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv5te-none-linux-androideabi --gcc-toolchain=/Users/alex/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/alex/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lEGL -lGLESv2 -lm "/Users/alex/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "-latomic" && :
没有了.据我所知,这个信息被Gradle Plugin过滤掉了,完全丢失了.我只能手动恢复:运行命令
Not anymore. As far as I know, this information is filtered by Gradle Plugin and is completely lost. I can recover it only manually: run the command
/Users/alex/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake --build app/.cxx/cmake/debug/armeabi-v7a
您可以使用 Android Studio 的终端窗口 (Alt-F12).当 -DCMAKE_VERBOSE_MAKEFILE=ON
用于将 C++ 与 Gradle 同步时,这将调用带有 -v
标志的 ninja.
You can use the Terminal window (Alt-F12) of Android Studio. This will invoke ninja with -v
flag when -DCMAKE_VERBOSE_MAKEFILE=ON
was used to sync C++ with Gradle.
请注意,预期文件 .cxx/cmake/debug/armeabi-v7a/cmake_build_output.txt
不包含有趣的信息(除非您对 CMake 配置本身有问题>).
Note that the expected file .cxx/cmake/debug/armeabi-v7a/cmake_build_output.txt
does not contain interesting information (unless you have problems with CMake configuration per se).
PS 使用这个 3.6.0 Gradle Plugin,如果你有编译错误,那么编译器的整个命令行都会显示在 Build Output 窗口中,无论是否您是否设置了 CMAKE_VERBOSE_MAKEFILE
.实际上,两次:一次,白底黑字(我不使用深色主题),第二次,白底棕色,之后
P.S. with this 3.6.0 Gradle Plugin, if you have a compilation error, then the whole command line for compiler is shown in Build Output window, no matter whether you set CMAKE_VERBOSE_MAKEFILE
or not. Actually, twice: once, in black on white (I don't use the dark theme), second time, in brown on white, after
FAILURE: Build failed with an exception.
* What went wrong:
这篇关于通过 Gradle 和 Android NDK 在详细模式下强制 CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!