从ndk-build迁移到CMake - build.ninja

时间:2018-04-08 06:36:13

标签: android c++ cmake android-ndk ndk-build

我一直在尝试从我的ndk-build配置转移到CMake,目的是在我的android库中构建不同的共享库。我的Android.mk文件是:

    LOCAL_PATH := $(call my-dir)

######################################################
####   Building internal evaluation and dsp library
######################################################
include $(CLEAR_VARS)

LOCAL_MODULE    := pitchYIN
LOCAL_SRC_FILES := yin.c yinOld.c DSPAlgorithms.c singingEvaluation.c myutils.c
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DVERBOSE=0

include $(BUILD_SHARED_LIBRARY)

######################################################
####   Building soundtouch library
######################################################
include $(CLEAR_VARS)

### Update this local path wrt system ###
MY_SOUNDTOUCH_SRC_DIR := <path-to-the-src-dir>

LOCAL_MODULE    := soundtouch
LOCAL_SRC_FILES := soundtouch-jni.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/AAFilter.cpp  $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/FIFOSampleBuffer.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/FIRFilter.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/cpu_detect_x86.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/sse_optimized.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundStretch/WavFile.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/RateTransposer.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/SoundTouch.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateCubic.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateLinear.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateShannon.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/TDStretch.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/BPMDetect.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/PeakFinder.cpp

# Including the header files for sound touch ..
LOCAL_C_INCLUDES := $(MY_SOUNDTOUCH_SRC_DIR)
LOCAL_C_INCLUDES += $(MY_SOUNDTOUCH_SRC_DIR)/../includes

# for native audio
LOCAL_LDLIBS += -lgcc
# --whole-archive -lgcc
# for logging
LOCAL_LDLIBS += -llog
# for native asset manager
#LOCAL_LDLIBS += -landroid

# Custom Flags:
# -fvisibility=hidden : don't export all symbols
LOCAL_CFLAGS += -fvisibility=hidden -I $(MY_SOUNDTOUCH_SRC_DIR)/../include -fdata-sections -ffunction-sections

# OpenMP mode : enable these flags to enable using OpenMP for parallel computation
#LOCAL_CFLAGS += -fopenmp
#LOCAL_LDFLAGS += -fopenmp


# Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs
LOCAL_ARM_MODE := arm

include $(BUILD_SHARED_LIBRARY)

在这里,我尝试构建两个共享库pitchYINsoundtouch。我现在正试图采用更加独立于平台的方法来使用CMake。这是我尝试写的等效CMakeLists.txt

    # Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

################Loading other libraries############################
find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              gcc-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              gcc )
######################################################################################
######### Adding pitchYIN library to the module
######################################################################################
set( jni_src_DIR ./src/main/jni )

add_library( # Specifies the name of the library.
             pitchYIN

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${jni_src_DIR}/yin.c
             ${jni_src_DIR}/yinOld.c
             ${jni_src_DIR}/DSPAlgorithms.c
             ${jni_src_DIR}/singingEvaluation.c
             ${jni_src_DIR}/myutils.c )

# Specifies a path to native header files.
include_directories(${jni_src_DIR})

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       pitchYIN

                       # Links the log library to the target library.
                       ${log-lib} )


######################################################################################
######### Adding soundtouch library to the module
######################################################################################

set( soundtouch_src_DIR <path-to-src> )

add_library( # Specifies the name of the library.
             soundtouch

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${jni_src_DIR}/soundtouch-jni.cpp
             ${soundtouch_src_DIR}/SoundTouch/AAFilter.cpp
             ${soundtouch_src_DIR}/SoundTouch/FIFOSampleBuffer.cpp
             ${soundtouch_src_DIR}/SoundTouch/FIRFilter.cpp
             ${soundtouch_src_DIR}/SoundTouch/cpu_detect_x86.cpp
             ${soundtouch_src_DIR}/SoundTouch/sse_optimized.cpp
             ${soundtouch_src_DIR}/SoundStretch/WavFile.cpp
             ${soundtouch_src_DIR}/SoundTouch/RateTransposer.cpp
             ${soundtouch_src_DIR}/SoundTouch/SoundTouch.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateCubic.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateLinear.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateShannon.cpp
             ${soundtouch_src_DIR}/SoundTouch/TDStretch.cpp
             ${soundtouch_src_DIR}/SoundTouch/BPMDetect.cpp
             ${soundtouch_src_DIR}/SoundTouch/PeakFinder.cpp
             )

# Specifies a path to native header files.
include_directories(${soundtouch_src_DIR}
                    ${soundtouch_src_DIR}/../includes)

# Custom Flags:
# -fvisibility=hidden : don't export all symbols
set(gcc_coverage_compile_flags "-fvisibility=hidden -I ${soundtouch_src_DIR}/../include -fdata-sections -ffunction-sections")
add_definitions($(gcc_coverage_compile_flags))

# OpenMP mode : enable these flags to enable using OpenMP for parallel computation
add_definitions("-fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

# Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs
# Equivalent to LOCAL_ARM_MODE := arm in Android.mk
# set(CMAKE_ANDROID_ARM_MODE ON)

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       soundtouch

                       # Links the log library to the target library.
                       ${log-lib} )

另外,我的gradle文件中有一些cmake参数,如:

.........
defaultConfig {
    /*sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
        jniLibs.srcDir 'src/main/libs' //set .so files location to libs
    }*/
    externalNativeBuild {
        cmake {
            arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm"
        }
    }
}

....
externalNativeBuild {
    /*ndkBuild {
        path 'src/main/jni/Android.mk'
    }*/

    cmake {
        // Provides a relative path to your CMake build script.
        path "CMakeLists.txt"
    }
}
.........

添加arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm"是因为LOCAL_ARM_MODE := arm文件的Android.mk文件中的soundtouch。当我现在尝试构建它时,我收到以下错误:

Build command failed.
Error while executing process /Users/swapnilgupta/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/.externalNativeBuild/cmake/debug/armeabi-v7a --target clean}
ninja: error: build.ninja:57: bad $-escape (literal $ must be written as $$)

build.ninja上的第57行是:

FLAGS = -isystem /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security   -fopenmp -O2 -DNDEBUG  -fPIC   $(gcc_coverage_compile_flags) -fopenmp

我试图通过网络阅读一些有关此内容的内容,但在我的案例中无法弄清楚是什么导致了这一点。请帮助:)

编辑1

我已经尝试过亚历克斯在答案中提到的事情。它确实帮助我解决了一些问题,但我仍然得到以下链接错误:

Build command failed.
Error while executing process /Users/swapnilgupta/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/.externalNativeBuild/cmake/debug/arm64-v8a --target soundtouch}
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libsoundtouch.so
FAILED: : && /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  --target=aarch64-none-linux-android --gcc-toolchain=/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64 -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,libsoundtouch.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libsoundtouch.so CMakeFiles/soundtouch.dir/src/main/jni/soundtouch-jni.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/AAFilter.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/FIRFilter.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/cpu_detect_x86.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/sse_optimized.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundStretch/WavFile.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/RateTransposer.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/SoundTouch.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateCubic.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateLinear.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateShannon.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/TDStretch.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/BPMDetect.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/PeakFinder.cpp.o  -llog -lomp -latomic -lm "/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a" && :
CMakeFiles/soundtouch.dir/src/main/jni/soundtouch-jni.cpp.o: In function `_init_threading(bool)':
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:69: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:69: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:74: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:74: undefined reference to `gomp_tls_key'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

该错误与变量gomp_tls_key有关,该变量是上述文件中的extern变量。

1 个答案:

答案 0 :(得分:0)

add_definitions("-fvisibility=hidden -I ${soundtouch_src_DIR}/../include -fdata-sections -ffunction-sections")

实际上,我不知道为什么你需要这个 set();如果你直接

,你什么都不会失去
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

另外,使用 -fopenmp ,您不需要两次添加此标记;这条线

target_link_libraries( # Specifies the target library.
                   soundtouch
                   log omp)

可以安全删除。

最后,请注意,您不需要查找 liblog

{"address": {
    "street": "street 123",
    "information": {
        "province": "province 123",
        "regency": "regency 123",
        "district": "district 123"
    }
},
"mobileNumber": "123"}
由于NDK CMake工具链脚本,

可以正常工作。