简体   繁体   中英

Migrating from ndk-build to CMake - error with build.ninja

I have been trying to move from my ndk-build configuration to CMake for the purpose of building building to different shared libraries in my android library. My Android.mk file is:

    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)

Here I am trying to build two shared libraries pitchYIN and soundtouch . I am now trying to move to a more platform independent method of using CMake . This is the equivalent CMakeLists.txt that I have tried to write:

    # 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} )

Also, I have some cmake argument in my gradle file like:

.........
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"
    }
}
.........

Adding of arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm" was because of LOCAL_ARM_MODE := arm in my Android.mk file for the soundtouch library. When I now try to build this, I get the following error:

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 $$)

The 57th line on build.ninja is:

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

I have tried to read a bit about this over the web but could not figure out what is causing this in my case. Please help :)

EDIT 1

I have tried things mentioned in the answer by Alex. It did help me to resolve some issues but I still get the following linking error:

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.

The error is related to a variable gomp_tls_key which is an extern variable in the mentioned file.

add_definitions($(gcc_coverage_compile_flags)) → add_definitions(${gcc_coverage_compile_flags}) 

Actually, I don't know why you need this set() ; you lose nothing if you directly

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

also, with -fopenmp you don't need to add this flag twice; the line

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

can be safely removed.

Finally, note that you don't need to find liblog .

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

will work fine, thanks to the NDK CMake toolchain script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM