Compiling libyuv of Android Platform in Windows

Keywords: Android cmake Windows Gradle

1. description

As for libyuv [1] The use is not to be overlooked. Following is an official note

Scale YUV to prepare content for compression, with point, bilinear or box filter.
Convert to YUV from webcam formats for compression.
Convert to RGB formats for rendering/effects.
Rotate by 90/180/270 degrees to adjust for mobile devices in portrait mode.
Optimized for SSSE3/AVX2 on x86/x64.
Optimized for Neon on Arm.
Optimized for MSA on Mips

2. Compilation process

Of course, you can also compile it on Android Studio. This is compiled separately from Android Studio development environment, compiled dynamic and static libraries and header files directly, and can be copied to other libraries.

2.1 download

Download source code
: https://chromium.googlesource.com/libyuv/libyuv/+archive/refs/heads/master.tar.gz

The latest version is 1917, which can be downloaded from the source code include/libyuv/version.h to see the version number.

2.2 Modify the CMakeLists.txt script slightly

_Originally, we did not need to change the CMakeLists.txt script, just to store the directory, put the dynamic and static libraries in the lib directory, and the header files in the include directory. If you don't change the script, you can copy it manually after compiling.
The most important thing is that Android studio integrates cmake compilation and strips out the symbol table when it is packaged. We leave the Android Studio development environment and compile directly using Cmake. Whether Release version or Debug version, Cmake does not have Strip dropping symbol tables. The library will be larger, so we customize a command in the script and strip the symbol tables directly. Normally we don't debug open source libraries

# CMakeLists for libyuv
# Originally created for "roxlu build system" to compile libyuv on windows
# Run with -DTEST=ON to build unit tests

PROJECT ( YUV C CXX )   # "C" is required even for C++ projects
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
OPTION( TEST "Built unit tests" OFF )

SET ( ly_base_dir   ${PROJECT_SOURCE_DIR} )
SET ( ly_src_dir    ${ly_base_dir}/source )
SET ( ly_inc_dir    ${ly_base_dir}/include )
SET ( ly_tst_dir    ${ly_base_dir}/unit_test )
SET ( ly_lib_name   yuv )
SET ( ly_lib_static ${ly_lib_name} )
SET ( ly_lib_shared ${ly_lib_name}_shared )

FILE ( GLOB_RECURSE ly_source_files ${ly_src_dir}/*.cc )
LIST ( SORT         ly_source_files )

FILE ( GLOB_RECURSE ly_unittest_sources ${ly_tst_dir}/*.cc )
LIST ( SORT         ly_unittest_sources )

INCLUDE_DIRECTORIES( BEFORE ${ly_inc_dir} )

# this creates the static library (.a)
ADD_LIBRARY             ( ${ly_lib_static} STATIC ${ly_source_files} )

# this creates the shared library (.so)
ADD_LIBRARY             ( ${ly_lib_shared} SHARED ${ly_source_files} )
SET_TARGET_PROPERTIES   ( ${ly_lib_shared} PROPERTIES OUTPUT_NAME "${ly_lib_name}" )
SET_TARGET_PROPERTIES   ( ${ly_lib_shared} PROPERTIES PREFIX "lib" )

# this creates the conversion tool
ADD_EXECUTABLE          ( yuvconvert ${ly_base_dir}/util/yuvconvert.cc )
TARGET_LINK_LIBRARIES   ( yuvconvert ${ly_lib_static} )


INCLUDE ( FindJPEG )
if (JPEG_FOUND)
  include_directories( ${JPEG_INCLUDE_DIR} )
  target_link_libraries( yuvconvert ${JPEG_LIBRARY} )
  add_definitions( -DHAVE_JPEG )
endif()

if(TEST)
  find_library(GTEST_LIBRARY gtest)
  if(GTEST_LIBRARY STREQUAL "GTEST_LIBRARY-NOTFOUND")
    set(GTEST_SRC_DIR /usr/src/gtest CACHE STRING "Location of gtest sources")
    if(EXISTS ${GTEST_SRC_DIR}/src/gtest-all.cc)
      message(STATUS "building gtest from sources in ${GTEST_SRC_DIR}")
      set(gtest_sources ${GTEST_SRC_DIR}/src/gtest-all.cc)
      add_library(gtest STATIC ${gtest_sources})
      include_directories(${GTEST_SRC_DIR})
      include_directories(${GTEST_SRC_DIR}/include)
      set(GTEST_LIBRARY gtest)
    else()
      message(FATAL_ERROR "TEST is set but unable to find gtest library")
    endif()
  endif()

  add_executable(libyuv_unittest ${ly_unittest_sources})
  target_link_libraries(libyuv_unittest ${ly_lib_name} ${GTEST_LIBRARY})
  find_library(PTHREAD_LIBRARY pthread)
  if(NOT PTHREAD_LIBRARY STREQUAL "PTHREAD_LIBRARY-NOTFOUND")
    target_link_libraries(libyuv_unittest pthread)
  endif()
  if (JPEG_FOUND)
    target_link_libraries(libyuv_unittest ${JPEG_LIBRARY})
  endif()

  if(NACL AND NACL_LIBC STREQUAL "newlib")
    target_link_libraries(libyuv_unittest glibc-compat)
  endif()

  find_library(GFLAGS_LIBRARY gflags)
  if(NOT GFLAGS_LIBRARY STREQUAL "GFLAGS_LIBRARY-NOTFOUND")
    target_link_libraries(libyuv_unittest gflags)
    add_definitions(-DLIBYUV_USE_GFLAGS)
  endif()
endif()

# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# To make the directory clearer, create the directory yourself
if(NOT EXISTS CMAKE_INSTALL_PREFIX)
    message(WARNING "Catalog ${CMAKE_INSTALL_PREFIX}-----not exists---------------")
    file(MAKE_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin ${CMAKE_INSTALL_PREFIX}/lib ${CMAKE_INSTALL_PREFIX}/include)
endif()

#copy header folder, dynamic library and static library files are placed in lib directory, and header files are placed in include directory.
add_custom_command( TARGET ${ly_lib_shared} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy_directory ${ly_inc_dir} ${CMAKE_INSTALL_PREFIX}/include)

# Symbol table of strip dynamic library
add_custom_command( TARGET ${ly_lib_shared} POST_BUILD
                    COMMAND "${ANDROID_TOOLCHAIN_PREFIX}strip" "--strip-unneeded" "-o" 
                "${CMAKE_INSTALL_PREFIX}/lib/lib${ly_lib_name}-strip.so" "${CMAKE_INSTALL_PREFIX}/lib/lib${ly_lib_name}.so"
                    COMMENT "strip Symbol table")
# ---------------------------------------------------------------------------------

# install the conversion tool, .so, .a, and all the header files
INSTALL ( PROGRAMS ${CMAKE_BINARY_DIR}/yuvconvert           DESTINATION ${CMAKE_INSTALL_PREFIX}/bin )
INSTALL ( TARGETS ${ly_lib_static}                  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib )
INSTALL ( TARGETS ${ly_lib_shared} LIBRARY              DESTINATION lib RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin )
INSTALL ( DIRECTORY ${PROJECT_SOURCE_DIR}/include/      DESTINATION ${CMAKE_INSTALL_PREFIX}/include )

# create the .deb and .rpm packages using cpack
INCLUDE ( CM_linux_packages.cmake )

2.3 Windows environment compiler script bat

@echo off
cls
::==========================================================================
::This script is used in the Windows Environmental use Cmake Cross compilation Android Used libyuv library
::==========================================================================

title cmake build libyuv

chcp 65001>nul

echo.
echo.
echo.--------This script is used in the Windows Environmental use Cmake Cross compilation Android Library used------
echo.

echo CMD Path:%cd%
echo current dir:%~dp0

rem Switch to the current directory
cd /d %~dp0

:: --------------------------------------------------------------------------
rem Temporary variable
set ANDROID_NDK=F:/Android/android-ndk-r16b
set CMAKE_PATH=D:/Android/android-sdk/cmake/3.6.4111459
set NASM_PATH=D:/Progra~1/Android/nasm-2.13.03
set ANDROID_VERSION=24
set SOURCE_DIR=./libyuv-refs_heads_master
set NATIVE_BUILD_DIR=.
set INSTALL_DIR=%~dp0

:: --------------------------------------------------------------------------
echo.
echo.--------------------------start build--------------------------------------
echo.

rem --make armeabi
call:buildLibTurboJpeg armeabi

rem --make armeabi-v7a
call:buildLibTurboJpeg armeabi-v7a

rem --make arm64-v8a
call:buildLibTurboJpeg arm64-v8a

rem --make x86
call:buildLibTurboJpeg x86

rem --make x86_64
call:buildLibTurboJpeg x86_64

set timeout=5
echo.Count down %timeout% Exit automatically in seconds
for /L %%a in (%timeout% -1 1) do ( ^
    set /p=%%a . <nul&ping -n 2 127.0.0.1>nul
)

echo.
echo.&pause&goto:eof

:: --------------------------------------------------------------------------
rem Defined function (%1:arch,%2:abi)
:buildLibTurboJpeg
setlocal

    echo.[func]-----------------start make [%1]---------------------------
    echo.
    
    set ABI=%1
    set BINARY_DIR="%NATIVE_BUILD_DIR%/bin/%ABI%"
    set PLATFORM_VERSION=%ANDROID_VERSION%
    set LIB_OUTPUT_DIR="%INSTALL_DIR%/dist/%ABI%"
    

    echo [func]----PLATFORM_VERSION==%ANDROID_VERSION%
    echo [func]----ABI==%ABI%
    echo [func]----SOURCE_DIR==%SOURCE_DIR%
    echo [func]----BINARY_DIR==%BINARY_DIR%
    echo [func]----LIB_OUTPUT_DIR==%LIB_OUTPUT_DIR%
    echo.
    echo.
    
    if not exist %BINARY_DIR% md %BINARY_DIR%
    
    cd /d %BINARY_DIR%
    
    ::  -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../../%LIB_OUTPUT_DIR% ^
    ::  -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=../../%LIB_OUTPUT_DIR% ^
    ::-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=%LIB_OUTPUT_DIR% ^
    ::-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=%LIB_OUTPUT_DIR% ^
    
    rem Open source libraries are available cmake         Android Gradle - Ninja ^ 
    cmake ^
        -DANDROID_ABI=%ABI% ^
        -DANDROID_PLATFORM=android-%PLATFORM_VERSION% ^
        -DCMAKE_INSTALL_PREFIX=%LIB_OUTPUT_DIR% ^
        -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=%LIB_OUTPUT_DIR%/lib ^
        -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=%LIB_OUTPUT_DIR%/lib ^
        -DCMAKE_BUILD_TYPE=Release ^
        -DANDROID_NDK=%ANDROID_NDK% ^
        -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%/build/cmake/android.toolchain.cmake ^
        -DCMAKE_MAKE_PROGRAM=%CMAKE_PATH%/bin/ninja.exe ^
        -G"Android Gradle - Ninja" ^
        -DANDROID_ARM_NEON=TRUE ^
        -DANDROID_TOOLCHAIN=clang ^
        -DANDROID_STL=c++_static ^
        -DCMAKE_C_FLAGS=" -Os -Wall -pipe -fPIC" ^
        -DANDROID_CPP_FEATURES=rtti exceptions ^
        ../../%SOURCE_DIR%
        
    cmake --build ./
    rem ninja -C .
    
    cd ../../
            
    echo.[func]-----------------[%1] make end---------------------------
    echo.

endlocal
goto:eof

2.4 Compile static and dynamic libraries

_Double-click the bat file saved by executing the above script and wait for compilation to complete.


Compile and install directory

ABIS

arm64-v8a

libs
  1. https://chromium.googlesource.com/libyuv/libyuv/+/refs/heads/master

Posted by river001 on Sat, 02 Feb 2019 15:06:15 -0800