openexr icon indicating copy to clipboard operation
openexr copied to clipboard

CMake issue using Android NDK

Open cristian1980 opened this issue 3 years ago • 1 comments

Hello,

I am having issues using OpenEXR as android C++ library subproject, what is strange is that sometimes it compiles and works with no issues. I consistently reproduced the error, if I do a clean project before building, the second build seemed to work.

But sometimes, I am getting the following error.

Launching lib/main.dart on SM T510 in debug mode...
Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:configureCMakeDebug[arm64-v8a]'.
> [CXX1405] error when building with cmake using /Users/cristi/Desktop/work/BlocksDesign/common/CMakeLists.txt: Build command failed.
  Error while executing process /Users/cristi/Library/Android/sdk/cmake/3.22.1/bin/cmake with arguments {-H/Users/cristi/Desktop/work/BlocksDesign/common -DCMAKE_SYSTEM_NAME=Android -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_SYSTEM_VERSION=29 -DANDROID_PLATFORM=android-29 -DANDROID_ABI=arm64-v8a -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a -DANDROID_NDK=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393 -DCMAKE_ANDROID_NDK=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393 -DCMAKE_TOOLCHAIN_FILE=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=/Users/cristi/Library/Android/sdk/cmake/3.22.1/bin/ninja -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/cristi/Desktop/work/BlocksDesign/build/app/intermediates/cxx/Debug/2i702x5f/obj/arm64-v8a -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/cristi/Desktop/work/BlocksDesign/build/app/intermediates/cxx/Debug/2i702x5f/obj/arm64-v8a -DCMAKE_BUILD_TYPE=Debug -B/Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/Debug/2i702x5f/arm64-v8a -GNinja}
  -- CMAKE_BUILD_TYPE = Debug
  -- VERSION = 2.1.4, BUILD = 20221005
  -- 64-bit build (arm64)
  -- CMAKE_INSTALL_PREFIX = /opt/blocksdesign
  -- CMAKE_INSTALL_BINDIR = bin (/opt/blocksdesign/bin)
  -- CMAKE_INSTALL_DATAROOTDIR =  (/opt/blocksdesign)
  -- CMAKE_INSTALL_DOCDIR = doc (/opt/blocksdesign/doc)
  -- CMAKE_INSTALL_INCLUDEDIR = include (/opt/blocksdesign/include)
  -- CMAKE_INSTALL_LIBDIR = lib64 (/opt/blocksdesign/lib64)
  -- CMAKE_INSTALL_MANDIR = man (/opt/blocksdesign/man)
  -- Shared libraries enabled (ENABLE_SHARED = 1)
  -- Static libraries enabled (ENABLE_STATIC = 1)
  -- 12-bit JPEG support disabled (WITH_12BIT = 0)
  -- Arithmetic decoding support enabled (WITH_ARITH_DEC = 1)
  -- Arithmetic encoding support enabled (WITH_ARITH_ENC = 1)
  -- TurboJPEG API library enabled (WITH_TURBOJPEG = 1)
  -- TurboJPEG Java wrapper disabled (WITH_JAVA = 0)
  -- In-memory source/destination managers enabled (WITH_MEM_SRCDST = 1)
  -- Emulating libjpeg API/ABI v6.2 (WITH_JPEG7 = 0, WITH_JPEG8 = 0)
  -- libjpeg API shared library version = 62.3.0
  -- Compiler flags = -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -fno-limit-debug-info 
  -- Linker flags = -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--gc-sections -Wl,--no-undefined -Qunused-arguments -Wl,--gc-sections  
  -- INLINE = __inline__ __attribute__((always_inline)) (FORCE_INLINE = 1)
  -- THREAD_LOCAL = __thread
  -- Linker supports GNU-style version scripts
  -- CMAKE_EXECUTABLE_SUFFIX = 
  -- Use full Neon SIMD intrinsics implementation (NEON_INTRINSICS = ON)
  -- SIMD extensions: arm64 (WITH_SIMD = 1)
  -- FLOATTEST = no-fp-contract
  -- Symbol prefix: 
  -- OpenEXR is configuring as a cmake subproject
  -- Using Imath from /Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/Debug/2i702x5f/arm64-v8a/_deps/imath-build/config
  -- Configure OpenEXR 3.2.0-dev, library API version: 29.0.0
  -- Could NOT find OpenEXR (missing: OpenEXR_DIR)
  -- Configuring done
  -- Generating done

  CMake Error at CMakeLists.txt:66 (add_library):
    Target "blocksdesign" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at CMakeLists.txt:66 (add_library):
    Target "blocksdesign" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/cmake/LibraryDefine.cmake:16 (add_library):
    Target "OpenEXR" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?
  Call Stack (most recent call first):
    openexr/src/lib/OpenEXR/CMakeLists.txt:4 (openexr_define_library)


  CMake Error at openexr/cmake/LibraryDefine.cmake:16 (add_library):
    Target "OpenEXRUtil" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?
  Call Stack (most recent call first):
    openexr/src/lib/OpenEXRUtil/CMakeLists.txt:4 (openexr_define_library)


  CMake Error at openexr/src/bin/exr2aces/CMakeLists.txt:4 (add_executable):
    Target "exr2aces" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrheader/CMakeLists.txt:4 (add_executable):
    Target "exrheader" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrmaketiled/CMakeLists.txt:4 (add_executable):
    Target "exrmaketiled" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrstdattr/CMakeLists.txt:4 (add_executable):
    Target "exrstdattr" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrmakepreview/CMakeLists.txt:4 (add_executable):
    Target "exrmakepreview" links to target "Imath::Imath" but the target was
    not found.  Perhaps a find_package() call is missing for an IMPORTED
    target, or an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrenvmap/CMakeLists.txt:4 (add_executable):
    Target "exrenvmap" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrmultiview/CMakeLists.txt:4 (add_executable):
    Target "exrmultiview" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrmultipart/CMakeLists.txt:4 (add_executable):
    Target "exrmultipart" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Error at openexr/src/bin/exrcheck/CMakeLists.txt:4 (add_executable):
    Target "exrcheck" links to target "Imath::Imath" but the target was not
    found.  Perhaps a find_package() call is missing for an IMPORTED target, or
    an ALIAS target is missing?


  CMake Generate step failed.  Build files cannot be regenerated correctly.


* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
Exception: Gradle task assembleDebug failed with exit code 1

My directory structure is the following:

> CMakeLists.txt
> libpng/
> libjpeg-turbo/
> openexr/
> cppfiles.cpp

In the log above I can see the message "Using Imath from /Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/Debug/2i702x5f/arm64-v8a/_deps/imath-build/config" and I can confirm that folder contains Imath sources. Then I can see the message: -- Configure OpenEXR 3.2.0-dev, library API version: 29.0.0 -- Could NOT find OpenEXR (missing: OpenEXR_DIR)

I don't understand what is happening, I am pretty new to CMake and I don't know if this is a bug or I am doing it wrong. I have no issues with libpng and libjpeg-turbo.

I attached my CMakeLists.txt.

Thank you for any help.

CMakeLists.txt

cristian1980 avatar Oct 04 '22 21:10 cristian1980

Your CMakeLists.txt file references OpenEXR first and Imath second, could you try reversing them, since OpenEXR requires Imath?

i.e.

add_subdirectory("imath")
add_subdirectory("openexr")

In theory, all the include_directories you are using for Imath and OpenEXR shouldn't be necessary, but I'm not familiar with how the NDK works.

meshula avatar Oct 05 '22 20:10 meshula

Basically gradle seems to call cmake with this command line:

/Users/cristi/Library/Android/sdk/cmake/3.22.1/bin/cmake -H/Users/cristi/Desktop/work/BlocksDesign/common -DCMAKE_SYSTEM_NAME=Android -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_SYSTEM_VERSION=29 -DANDROID_PLATFORM=android-29 -DANDROID_ABI=x86 -DCMAKE_ANDROID_ARCH_ABI=x86 -DANDROID_NDK=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393 -DCMAKE_ANDROID_NDK=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393 -DCMAKE_TOOLCHAIN_FILE=/Users/cristi/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=/Users/cristi/Library/Android/sdk/cmake/3.22.1/bin/ninja -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/cristi/Desktop/work/BlocksDesign/build/app/intermediates/cxx/RelWithDebInfo/453r2463/obj/x86 -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/cristi/Desktop/work/BlocksDesign/build/app/intermediates/cxx/RelWithDebInfo/453r2463/obj/x86 -DCMAKE_BUILD_TYPE=RelWithDebInfo -B/Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/RelWithDebInfo/453r2463/x86 -GNinja

Ninja seems to be the build system as seen from the command above. Calling this sometimes shows the error mentioned in this post.

I changed the CMakeLists.txt in favor of FetchContent. I don't have Imath in my project and let OpenEXR to download the version it needs.

include(FetchContent)

FetchContent_Declare(openexrgit
        GIT_REPOSITORY https://github.com/cristian1980/openexr
        GIT_TAG ded0d62ceb2a0a0fc27498630898bd8a886acf7b
        )
FetchContent_GetProperties(openexrgit)
if(NOT openexrgit_POPULATED)
    message("openexr not populated... downloading")
    FetchContent_Populate(openexrgit)
    add_subdirectory(${openexrgit_SOURCE_DIR} ${openexrgit_BINARY_DIR})
    message("openexr downloaded")
else()
    message("openexr already populated")
endif()

Note I used my own fork because I had to change a single line of code in a cpp file for the code to compile with NDK.

I only reference Imath in the link command:

target_link_libraries( # Specifies the target library.
        blocksdesign
        PRIVATE
        OpenEXR::OpenEXR
        Imath::Imath
        ..........

This also shows the same behaviour, sometimes gives the error in the first post and other times everything works.

It looks like a synchronisation issue in OpenEXR build, maybe the OpenEXR finishes compilation before Imath? Is that possible?

cristian1980 avatar Oct 06 '22 17:10 cristian1980

Another thing I should mention is that gradle may build for more than 1 CPU architecture simultaneously but CMake will have different build directories path for each cmake command. For example:

-B/Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/RelWithDebInfo/453r2463/x86 -B/Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/RelWithDebInfo/453r2463/arm64-v8a -B/Users/cristi/Desktop/work/BlocksDesign/android/app/.cxx/RelWithDebInfo/453r2463/armeabi-v7a

Maybe there is a fixed path somewhere that is used during build? I changed gradle config to only build for 1 arch and it seems to work for now. Will let you know if the issue comes back.

cristian1980 avatar Oct 06 '22 18:10 cristian1980

Hi @cristian1980 Did you succeed in building this for android? I'm trying as well but having different troubles. In my case it seems that binary executable (ELF) files eLut and toFloat are generated, which are then executed to generate eLut.h and toFloat.h. Obviously the machine on which I'm building can't execute these and so the build fails. How did you get around this?

max-krichenbauer avatar Mar 11 '23 06:03 max-krichenbauer

@max-krichenbauer, that would seem to indicate you're working with an old version of the library, since the runtime-generation of eLut.h and toFloat.h was retired in v2.5 several years ago. Can you confirm you're working with a recent release?

cary-ilm avatar Mar 11 '23 15:03 cary-ilm

@cary-ilm You're right. I got OpenEXR as a submodule of com.unity.formats.alembic and didn't notice how old the referenced version was. Updating to v2.5 solves that issue. Thank you!

max-krichenbauer avatar Mar 12 '23 04:03 max-krichenbauer

Glad it's solved :) Please feel free to reopen if there's further issues.

meshula avatar Mar 12 '23 22:03 meshula

Glad it's solved :) Please feel free to reopen if there's further issues.

I didn't open the issue, I just asked a (related) question. You should ask @cristian1980 whether the issue is solved. ;-)

max-krichenbauer avatar Mar 12 '23 22:03 max-krichenbauer