通过Gradle和Android NDK以详细模式强制CMake [英] Force CMake in verbose mode via Gradle and the Android NDK

查看:1328
本文介绍了通过Gradle和Android NDK以详细模式强制CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gradle和CMake从命令行编译Android NDK项目。以前,我使用Ant和 ndk-build ,但我试图将该项目完全迁移到Gradle和CMake。



在我的 build.gradle 中,我有以下几行来调用CMake:

  externalNativeBuild {
cmake {
pathsrc / main / cpp / CMakeLists.txt
}
}

现在,如何强制CMake将所有编译器调用打印到控制台之前?具体来说,我想看看CMake如何运行编译器和链接器。



我已经尝试了以下内容,但都无济于事:



<1>在我的 CMakeLists.txt 中,我放了下面这行:

 set(CMAKE_VERBOSE_MAKEFILE)

没有任何效果。



2)我已经开始构建:

 。 / gradlew build --info 

Gradle打印了一些内容,但没有编译器调用。



3)像这样:

  ./ gradlew build --debug 

Gradle列印了很多东西,但没有编译器调用。



<因此,这三次尝试都没有做到我想要的,这让我想知道我怎么能看到CMake如何在我的单个源文件上运行clang?

在Android Studio中, gradle 创建目录 .externalNat对于每个具有NDK集成的模块,通过CMake或ndk-build,在模块根目录下的iveBuild


$ b

对于CMake,gradle插件是非常详细。对于每个构建变体,它创建单独的子目录,例如, .externalNativeBuild / cmake / debug / x86 .externalNativeBuild / cmake / release / armeabi 等。



每个目录都包含一些有用的文件: cmake_build_command.txt 描述传递给CMake的实际参数; android_gradle_build.json 显示了gradle插件为二进制文件派生的参数;从 build.ninja 中,您可以推导出这些参数是如何应用于每个编译或链接步骤的。

对于 ndk -build android_gradle_build.json 文件也非常有用。 ndkBuild_build_command.txt 列出传递给 ndk-build 命令的所有参数, ndkBuild_build_output.txt 是该命令的未删节输出。您可以轻松地将 V = 1 添加到参数中,例如

  externalNativeBuild {
ndkBuild {
cppFlags-std = c ++ 11
argumentsAPP_STL = gnustl_static,NDK_TOOLCHAIN_VERSION = 4.9,APP_OPTIM = release ,NDK_DEBUG = 0,V = 1
abiFiltersarmeabi
}
}

对于 CMake ,相关参数是 - DCMAKE_VERBOSE_MAKEFILE:BOOL = ON (请参阅 解释
和替代方法
):

  externalNativeBuild {
cmake {
cppFlags-std = c ++ 11
arguments-DCMAKE_VERBOSE_MAKEFILE:BOOL = ON
abiFiltersarmeabi

}

code> CMAKE_VERBOSE_MAKEFILE ,Gradle控制台显示:

 :app:externalNativeBuildDebug 
构建native-lib armeabi
[1/2]构建CXX对象CMak eFiles / native-lib.dir / src / main / cpp / native-lib.cpp.o
[2/2]链接CXX共享库../../../../build/intermediates/ cmake / debug / obj / armeabi / libnative-lib.so

使用

 :app:externalNativeBuildDebug $ -DCMAKE_VERBOSE_MAKEFILE:BOOL = ON  b $ b build native-lib armeabi 

[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/include -isystem / Users / alex / Library / Android / SDK / NDK束/源/ 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 -integrate-as -mthumb -Wa, - noexecstack -Wformat -Werror =格式安全性-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.od -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 =格式安全性-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, - 致命警告-Wl, - 无定义-Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl, - build-id -Wl, - 战争n-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/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/gnu-libstdc++/4.9/libs/armeabi /libgnustl_static.a-latomic&& :

显而易见的文件 .externalNativeBuild / cmake / debug / armeabi / cmake_build_output。 txt 不包含有趣的信息(除非你有CMake配置本身的问题)。 Gradle不会将此输出写入文件,但可以使用命令行重定向,请参阅 Gradle日志在哪里?


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.

In my build.gradle I have the following lines to invoke CMake:

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
    }
}

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) In my CMakeLists.txt I have put the following line:

set(CMAKE_VERBOSE_MAKEFILE on)

Didn't have any effect.

2) I have started the build like this:

./gradlew build --info

Gradle printed some stuff, but no compiler calls.

3) And like this:

./gradlew build --debug

Gradle printed lots of stuff, but no compiler calls.

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?

解决方案

In Android Studio, gradle creates directory .externalNativeBuild under the module root, for each module that has NDK integration, via CMake or ndk-build.

For CMake, the gradle plugin is quite verbose. For each build variant it creates separate subdirectory, e.g. .externalNativeBuild/cmake/debug/x86 or .externalNativeBuild/cmake/release/armeabi, etc.

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.

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=gnustl_static", "NDK_TOOLCHAIN_VERSION=4.9", "APP_OPTIM=release", "NDK_DEBUG=0", "V=1"
    abiFilters "armeabi"
  }
}

For CMake, the relevant argument is "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON" (see explanation and alternatives):

externalNativeBuild {
  cmake {
    cppFlags "-std=c++11"
    arguments "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
    abiFilters "armeabi"
  }
}

Without CMAKE_VERBOSE_MAKEFILE, the Gradle Console displays:

:app:externalNativeBuildDebug
Build native-lib armeabi
[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/libnative-lib.so

With "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON", I get tons of output:

:app:externalNativeBuildDebug
Build native-lib armeabi

[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/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/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/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a" "-latomic" && :

The obvious file .externalNativeBuild/cmake/debug/armeabi/cmake_build_output.txt does not contain interesting information (unless you have problems with CMake configuration per se). Gradle does not write this output to file, but you can use command line redirection, see Where are Gradle logs?.

这篇关于通过Gradle和Android NDK以详细模式强制CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆