Search code examples
cmakeandroid-ndkandroid.mk

Migrate from .mk to CMakeList


I have some .mk file (on Android) that looks like this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE := openal
SRC_FILES := $(wildcard $(LOCAL_PATH)/../../../common/openal/*.c)
SRC_FILES := $(SRC_FILES:$(LOCAL_PATH)/%=%) 
LOCAL_SRC_FILES = $(SRC_FILES)
LOCAL_LDLIBS = -llog
LOCAL_CFLAGS := -O3 -mno-thumb
LOCAL_CPPFLAGS := -O3 -mno-thumb
include $(BUILD_SHARED_LIBRARY)


include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE   := templateApp
LOCAL_CFLAGS   := -O3 -mno-thumb 
LOCAL_CPPFLAGS := $(LOCAL_CFLAGS)
LOCAL_C_INCLUDES = $(LOCAL_PATH)/../../../common/openal/
SRC_FILES := $(wildcard $(LOCAL_PATH)/../../*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/bullet/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/ttf/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/nvtristrip/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/png/*.c)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/zlib/*.c)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/detour/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/recast/*.cpp)
SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../common/vorbis/*.c)
SRC_FILES := $(SRC_FILES:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES := $(SRC_FILES)
LOCAL_SHARED_LIBRARIES := libopenal
LOCAL_LDLIBS := -L$(LOCAL_PATH)/../libs/armeabi 
LOCAL_LDLIBS += -lz -lm -ldl -lGLESv2 -lEGL -llog -lopenal
LOCAL_CFLAGS := -O3 -mno-thumb -Wno-write-strings
LOCAL_CPPFLAGS := -O3 -mno-thumb -Wno-write-strings
include $(BUILD_SHARED_LIBRARY)

I need to migrate that to modern CMake build tool, and this is how far i reached by now:

cmake_minimum_required(VERSION 3.4.1)    

include_directories(src/main/cpp)
include_directories(src/main/cpp/common)
include_directories(src/main/cpp/common/bullet)
include_directories(src/main/cpp/common/detour)
include_directories(src/main/cpp/common/GLES2)
include_directories(src/main/cpp/common/nvtristrip)
include_directories(src/main/cpp/common/openal)
include_directories(src/main/cpp/common/png)
include_directories(src/main/cpp/common/recast)
include_directories(src/main/cpp/common/ttf)
include_directories(src/main/cpp/common/vorbis)
include_directories(src/main/cpp/common/zlib)

file(GLOB SOURCES_CPP "src/main/cpp/common/*.cpp")
file(GLOB SOURCES_C "src/main/cpp/common/*.c")

file(GLOB OPENAL_SOURCES_C "src/main/cpp/common/openal/*.c")

add_library( openal-lib SHARED ${OPENAL_SOURCES_C})

find_library( log-lib log )

target_link_libraries( openal-lib ${log-lib} )



add_library( native-lib SHARED 

             src/main/cpp/native-lib.cpp
             ${SOURCES_CPP}
             ${SOURCES_C} )

find_library( GLESv2-lib GLESv2 )
find_library( EGL-lib EGL )
find_library( z-lib z )
find_library( m-lib m )
find_library( dl-lib dl )


target_link_libraries( native-lib

               openal-lib
                       ${z-lib}
                       ${m-lib}
                       ${dl-lib}
                       ${log-lib}
                       ${GLESv2-lib}
                       ${EGL-lib})

Definitely I'm doing something wrong, but i can't find any clear information that can help me to solve this problem.After build process a got this:

Build command failed.
Error while executing process C:\Users\Fervill\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Android\GLCTest\app\.externalNativeBuild\cmake\debug\armeabi-v7a --target native-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\Fervill\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/Users/Fervill/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Fervill/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -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 CMakeFiles/native-lib.dir/src/main/cpp/common/audio.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/font.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/gfx.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/light.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/matrix.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/md5.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/memory.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/navigation.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/obj.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/program.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/shader.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/sound.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/texture.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/thread.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/utils.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/common/vector.cpp.o  ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libopenal-lib.so C:/Users/Fervill/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/arm-linux-androideabi/libz.a C:/Users/Fervill/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/arm-linux-androideabi/libm.a C:/Users/Fervill/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/arm-linux-androideabi/libdl.a -llog -lGLESv2 -lEGL -latomic -lm && cd ."
D:\Android\GLCTest\app\src\main\cpp\common/font.cpp:115: error: undefined reference to 'stbtt_BakeFontBitmap'
D:\Android\GLCTest\app\src\main\cpp\common/md5.cpp:588: error: undefined reference to 'SetCacheSize(unsigned int)'
D:\Android\GLCTest\app\src\main\cpp\common/md5.cpp:596: error: undefined reference to 'GenerateStrips(unsigned short const*, unsigned int, PrimitiveGroup**, unsigned short*, bool)'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:78: error: undefined reference to 'unzOpen'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:85: error: undefined reference to 'unzGoToFirstFile'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:89: error: undefined reference to 'unzGetFilePos'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:91: error: undefined reference to 'unzLocateFile'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:93: error: undefined reference to 'unzGetCurrentFileInfo'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:100: error: undefined reference to 'unzOpenCurrentFilePassword'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:107: error: undefined reference to 'unzReadCurrentFile'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:109: error: undefined reference to 'unzCloseCurrentFile'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:111: error: undefined reference to 'unzClose'
D:\Android\GLCTest\app\src\main\cpp\common/memory.cpp:117: error: undefined reference to 'unzClose'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:45: error: undefined reference to 'dtFreeNavMesh(dtNavMesh*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:158: error: undefined reference to 'rcCalcBounds(float const*, int, float*, float*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:164: error: undefined reference to 'rcCalcGridSize(float const*, float const*, float, int*, int*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:171: error: undefined reference to 'rcAllocHeightfield()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:173: error: undefined reference to 'rcCreateHeightfield(rcHeightfield&, int, int, float const*, float const*, float, float)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:186: error: undefined reference to 'rcMarkWalkableTriangles(float, float const*, int, int const*, int, unsigned char*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:194: error: undefined reference to 'rcRasterizeTriangles(float const*, int, int const*, unsigned char const*, int, rcHeightfield&, int)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:210: error: undefined reference to 'rcFilterLowHangingWalkableObstacles(int, rcHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:214: error: undefined reference to 'rcFilterLedgeSpans(int, int, rcHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:219: error: undefined reference to 'rcFilterWalkableLowHeightSpans(int, rcHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:223: error: undefined reference to 'rcAllocCompactHeightfield()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:225: error: undefined reference to 'rcBuildCompactHeightfield(int, int, unsigned char, rcHeightfield&, rcCompactHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:231: error: undefined reference to 'rcFreeHeightField(rcHeightfield*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:234: error: undefined reference to 'rcErodeArea(unsigned char, int, rcCompactHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:239: error: undefined reference to 'rcBuildDistanceField(rcCompactHeightfield&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:242: error: undefined reference to 'rcBuildRegions(rcCompactHeightfield&, int, int, int)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:248: error: undefined reference to 'rcAllocContourSet()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:250: error: undefined reference to 'rcBuildContours(rcCompactHeightfield&, float, int, rcContourSet&, int)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:256: error: undefined reference to 'rcAllocPolyMesh()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:258: error: undefined reference to 'rcBuildPolyMesh(rcContourSet&, int, rcPolyMesh&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:263: error: undefined reference to 'rcAllocPolyMeshDetail()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:265: error: undefined reference to 'rcBuildPolyMeshDetail(rcPolyMesh const&, rcCompactHeightfield const&, float, float, rcPolyMeshDetail&)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:272: error: undefined reference to 'rcFreeCompactHeightfield(rcCompactHeightfield*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:275: error: undefined reference to 'rcFreeContourSet(rcContourSet*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:327: error: undefined reference to 'dtCreateNavMeshData(dtNavMeshCreateParams*, unsigned char**, int*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:333: error: undefined reference to 'dtAllocNavMesh()'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:335: error: undefined reference to 'dtNavMesh::init(unsigned char*, int, int, int)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:340: error: undefined reference to 'rcFreePolyMesh(rcPolyMesh*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:343: error: undefined reference to 'rcFreePolyMeshDetail(rcPolyMeshDetail*)'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:367: error: undefined reference to 'dtNavMesh::findNearestPoly(float const*, float const*, dtQueryFilter const*, float*) const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:373: error: undefined reference to 'dtNavMesh::findNearestPoly(float const*, float const*, dtQueryFilter const*, float*) const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:379: error: undefined reference to 'dtNavMesh::findPath(unsigned int, unsigned int, float const*, float const*, dtQueryFilter const*, unsigned int*, int) const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:393: error: undefined reference to 'dtNavMesh::closestPointOnPoly(unsigned int, float const*, float*) const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:401: error: undefined reference to 'dtNavMesh::findStraightPath(float const*, float const*, unsigned int const*, int, float*, unsigned char*, unsigned int*, int) const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:484: error: undefined reference to 'dtNavMesh::getMaxTiles() const'
D:\Android\GLCTest\app\src\main\cpp\common/navigation.cpp:486: error: undefined reference to 'dtNavMesh::getTile(int)'
D:\Android\GLCTest\app\src\main\cpp\common/obj.cpp:608: error: undefined reference to 'SetCacheSize(unsigned int)'
D:\Android\GLCTest\app\src\main\cpp\common/obj.cpp:614: error: undefined reference to 'GenerateStrips(unsigned short const*, unsigned int, PrimitiveGroup**, unsigned short*, bool)'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:43: error: undefined reference to 'ov_open_callbacks'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:49: error: undefined reference to 'ov_info'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:54: error: undefined reference to 'ov_pcm_total'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:63: error: undefined reference to 'ov_read'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:84: error: undefined reference to 'ov_clear'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:117: error: undefined reference to 'ov_open_callbacks'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:123: error: undefined reference to 'ov_info'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:149: error: undefined reference to 'ov_read'
D:\Android\GLCTest\app\src\main\cpp\common/sound.cpp:186: error: undefined reference to 'ov_clear'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:107: error: undefined reference to 'png_create_read_struct'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:112: error: undefined reference to 'png_create_info_struct'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:114: error: undefined reference to 'png_set_read_fn'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:116: error: undefined reference to 'png_read_info'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:118: error: undefined reference to 'png_get_bit_depth'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:120: error: undefined reference to 'png_get_color_type'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:123: error: undefined reference to 'png_set_expand'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:126: error: undefined reference to 'png_set_expand'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:128: error: undefined reference to 'png_get_valid'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:129: error: undefined reference to 'png_set_expand'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:132: error: undefined reference to 'png_set_strip_16'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:135: error: undefined reference to 'png_set_gray_to_rgb'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:137: error: undefined reference to 'png_read_update_info'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:139: error: undefined reference to 'png_get_IHDR'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:205: error: undefined reference to 'png_read_image'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:207: error: undefined reference to 'png_read_end'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:209: error: undefined reference to 'png_destroy_read_struct'
D:\Android\GLCTest\app\src\main\cpp\common/texture.cpp:87: error: undefined reference to 'png_get_io_ptr'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

What i'm doing wrong? How to get rid of that nasty error logs?


Solution

  • Actually, there was a lot of errors related to directory structure of source files and theirs corresponding reflection in CMakeLists.txt. I wrongly assumed that file(GLOB ..) will look for sources in a sub directories. And also i used *.cpp in places where it should look for *.c (didn't notice this). Here is final working solution:

    cmake_minimum_required(VERSION 3.4.1)
    
    include_directories(src/main/cpp)
    include_directories(src/main/cpp/common)
    include_directories(src/main/cpp/common/bullet)
    include_directories(src/main/cpp/common/detour)
    include_directories(src/main/cpp/common/GLES2)
    include_directories(src/main/cpp/common/nvtristrip)
    include_directories(src/main/cpp/common/openal)
    include_directories(src/main/cpp/common/png)
    include_directories(src/main/cpp/common/recast)
    include_directories(src/main/cpp/common/ttf)
    include_directories(src/main/cpp/common/vorbis)
    include_directories(src/main/cpp/common/zlib)
    
    
    file(GLOB ROOT_SOURCES_CPP "src/main/cpp/common/*.cpp")
    file(GLOB BULLET_SOURCES_CPP "src/main/cpp/common/bullet/*.cpp")
    file(GLOB DETOUR_SOURCES_CPP "src/main/cpp/common/detour/*.cpp")
    file(GLOB NVTRISTRIP_SOURCES_CPP "src/main/cpp/common/nvtristrip/*.cpp")
    file(GLOB PNG_SOURCES_C "src/main/cpp/common/png/*.c")
    file(GLOB RECAST_SOURCES_CPP "src/main/cpp/common/recast/*.cpp")
    file(GLOB TTF_SOURCES_CPP "src/main/cpp/common/ttf/*.cpp")
    file(GLOB VORBIS_SOURCES_C "src/main/cpp/common/vorbis/*.c")
    file(GLOB ZLIB_SOURCES_C "src/main/cpp/common/zlib/*.c")
    
    
    
    file(GLOB OPENAL_SOURCES_C "src/main/cpp/common/openal/*.c")
    
    add_library( openal-lib             
                 SHARED            
                 ${OPENAL_SOURCES_C})
    
    
    
    find_library( log-lib              
                  log )
    
    target_link_libraries( openal-lib 
    
                           ${log-lib})
    
    add_library( native-lib
                 SHARED
                 src/main/cpp/native-lib.cpp
                 ${ROOT_SOURCES_CPP}
                 ${BULLET_SOURCES_CPP}
                 ${DETOUR_SOURCES_CPP}
                 ${NVTRISTRIP_SOURCES_CPP}
                 ${PNG_SOURCES_C}
                 ${RECAST_SOURCES_CPP}
                 ${TTF_SOURCES_CPP}
                 ${VORBIS_SOURCES_C}
                 ${ZLIB_SOURCES_C})
    
    find_library( GLESv2-lib GLESv2 )
    find_library( EGL-lib EGL )
    find_library( z-lib z )
    find_library( m-lib m )
    find_library( dl-lib dl )
    
    target_link_libraries( native-lib 
    
                           openal-lib
                           ${z-lib}
                           ${m-lib}
                           ${dl-lib}
                           ${log-lib}
                           ${GLESv2-lib}
                           ${EGL-lib})