Re: [cmake-developers] [RFC] FindAndroidNDK and UseAndroidNDK

2016-03-14 Thread Brad King
On 03/12/2016 04:05 AM, Xiaolei Yu wrote:
> my team decided to ditch ndk-build and migrate to cmake. And I would 
> like to upstream these two modules, which are currently used in my hobby 
> projects.

Thanks.  Yes, code to search for the Android NDK or a standalone toolchain
belongs in CMake upstream rather than in the toolchain file.  However, it
should not have to be a separate find_package(AndroidNDK) step.  The right
place is likely in a Modules/Platform/Android-Initialize.cmake file similar
to the Modules/Platform/Darwin-Initialize.cmake file already used to find
the OS X SDK.  The Android-Initialize file will be loaded automatically
when a toolchain file sets CMAKE_SYSTEM_NAME to Android.

I've long wished to see work along these lines done, but it is not trivial.
The selection of STL, ARCH, etc. all need to be presented cleanly as options
to the user as cache entries with reasonable defaults.  It all needs to be
done as part of the Android platform information modules and not activated
by any explicit code in the project.  I'm not sure how it should properly
mix with selection of the actual toolchain (compiler).

In particular, selecting the STL may need some additional changes on the
C++ side of the CMake implementation.  Instead of code like

>   include_directories ( SYSTEM
> ${_gnustl}/libs/${CMAKE_LIBRARY_ARCHITECTURE}/include
> ${_gnustl}/include
>   )

we need an equivalent to CMAKE_CXX_STANDARD_LIBRARIES for system include
directories that come from the SDK.

> 1. using CMAKE_LIBRARY_ARCHITECTURE to represent android 
> TARGET_ARCH_ABI.

CMAKE_LIBRARY_ARCHITECTURE affects the find_* logic and I do not think it
should be overloaded with additional meanings.  A simple CMAKE_ANDROID_ARCH
cache entry in the Android-Initialize module discussed above may be sufficient.

Thanks,
-Brad

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers


[cmake-developers] [RFC] FindAndroidNDK and UseAndroidNDK

2016-03-12 Thread Xiaolei Yu

Hello,
my team decided to ditch ndk-build and migrate to cmake. And I would 
like to upstream these two modules, which are currently used in my hobby 
projects.


They are intended to be used by custom toolchain files in place of 
android.cmake. The goal is to preserve as much information as possible 
as I am using a cmake super build to drive dozens of external projects, 
while android.cmake merges most info into linker and c/cxx flags.


I am aware of the code style problems and will fix them in the formal 
patch. In this mail I am seeking your feedbacks on several issues:


1. using CMAKE_LIBRARY_ARCHITECTURE to represent android 
TARGET_ARCH_ABI. The way android handles native libraries is similar to 
debian multiarch and I am reusing this property.
Android-GNU-{C, CXX}.cmake will setup compiler flags based on it. I can 
think of another use case when a super build decides where to pull 
prebuilt blobs.


2. clang support is lacking. I am not using clang for android but can 
add the support if deemed necessary.


3. the FindAndroidNDK module is saving many values and I am not sure if 
all of them are worth to keep.


Best Regards




macro ( android_use_cxx_gnustl variant version )
  set ( _gnustl ${ANDROID_NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/${version} )

  if ( "${variant}" STREQUAL "SHARED" )
set ( CMAKE_CXX_STANDARD_LIBRARIES_INIT
  ${_gnustl}/libs/${CMAKE_LIBRARY_ARCHITECTURE}/libgnustl_shared.so
)
  elseif ( "${variant}" STREQUAL "STATIC" )
set ( CMAKE_CXX_STANDARD_LIBRARIES_INIT
  ${_gnustl}/libs/${CMAKE_LIBRARY_ARCHITECTURE}/libgnustl_static.a
  ${_gnustl}/libs/${CMAKE_LIBRARY_ARCHITECTURE}/libsupc++.a
)
  else ()
message ( FATAL_ERROR "invalid gnustl variant: ${variant}")
  endif ()

  include_directories ( SYSTEM
${_gnustl}/libs/${CMAKE_LIBRARY_ARCHITECTURE}/include
${_gnustl}/include
  )
endmacro ()

macro ( android_use_toolchain version )
  set ( CMAKE_C_COMPILER${AndroidNDK_GNU_CROSSPREFIX_${version}}gcc )
  set ( CMAKE_CXX_COMPILER  ${AndroidNDK_GNU_CROSSPREFIX_${version}}g++ )
endmacro ()

cmake_minimum_required ( VERSION 3.4 )

set ( _target_arch_abi_list
  armeabi armeabi-v7a armeabi-v7a-hard arm64
  x86 x86_64 mips mips64
)

if ( NOT "${CMAKE_LIBRARY_ARCHITECTURE}" IN_LIST _target_arch_abi_list )
  message ( FATAL_ERROR "invalid value: 
CMAKE_LIBRARY_ARCHITECTURE=${CMAKE_LIBRARY_ARCHITECTURE}" )
endif ()

set ( _target_arch_list
  arm arm64
  x86 x86_64
  mips mips64
)

set ( _target_arch )
set ( _gnu_mach )
set ( _gnu_os )
set ( _gnu_toolchain_name )

if ( CMAKE_LIBRARY_ARCHITECTURE MATCHES "^armeabi" )
  set ( _target_archarm )
  set ( _gnu_mach   arm )
  set ( _gnu_os linux-androideabi )
elseif ( CMAKE_LIBRARY_ARCHITECTURE STREQUAL "arm64-v8a" )
  set ( _target_archarm64 )
  set ( _gnu_mach   aarch64 )
elseif ( CMAKE_LIBRARY_ARCHITECTURE STREQUAL "mips" )
  set ( _gnu_mach   mipsel )
elseif ( CMAKE_LIBRARY_ARCHITECTURE STREQUAL "mips64" )
  set ( _gnu_mach   mips64el )
elseif ( CMAKE_LIBRARY_ARCHITECTURE STREQUAL "x86" )
  set ( _gnu_mach   i686 )
  set ( _gnu_toolchain_name x86 )
elseif ( CMAKE_LIBRARY_ARCHITECTURE STREQUAL "x86_64" )
  set ( _gnu_toolchain_name x86_64 )
endif ()

if ( NOT _target_arch )
  set ( _target_arch ${CMAKE_LIBRARY_ARCHITECTURE} )
endif ()

if ( NOT _gnu_mach )
  set ( _gnu_mach${CMAKE_LIBRARY_ARCHITECTURE} )
endif ()
if ( NOT _gnu_os )
  set ( _gnu_os  linux-android )
endif ()
set ( _gnu_triplet   ${_gnu_mach}-${_gnu_os} )

if ( NOT _gnu_toolchain_name )
  set ( _gnu_toolchain_name ${_gnu_triplet} )
endif ()

macro ( _do_find_info )
  file ( READ ${ANDROID_NDK_ROOT}/RELEASE.TXT _release_txt_str )

  string ( REGEX MATCH "^r[^ ]+" AndroidNDK_VERSION_STRING ${_release_txt_str} )

  if ( ${_release_txt_str} MATCHES "64\\-bit" )
set ( AndroidNDK_64BIT TRUE )
  else ()
set ( AndroidNDK_64BIT FALSE )
  endif ()

  set ( AndroidNDK_64BIT ${AndroidNDK_64BIT} CACHE BOOL "64-bit" )
  
  find_path ( AndroidNDK_HOST_PREBUILT_DIR  bin
PATHS ${ANDROID_NDK_ROOT}/prebuilt/*
NO_DEFAULT_PATH
  )

  get_filename_component ( AndroidNDK_HOST
${AndroidNDK_HOST_PREBUILT_DIR} NAME CACHE 
  )
endmacro ()

macro ( _do_find_sysroot )

  file ( GLOB _sysroot_list RELATIVE "${ANDROID_NDK_ROOT}/"
"${ANDROID_NDK_ROOT}/platforms/android-*/arch-${_target_arch}"
  )

  foreach ( _sysroot ${_sysroot_list} )
string ( REGEX REPLACE ".*/android-(.*)/.*" "\\1" _sysroot_version 
"${_sysroot}" )
set ( AndroidNDK_SYSROOT_${_sysroot_version}
  ${ANDROID_NDK_ROOT}/${_sysroot} CACHE PATH
  "SYSROOT ${_sysroot_version}"
)
mark_as_advanced ( AndroidNDK_SYSROOT_${_sysroot_version} )
  endforeach ()
endmacro ()

macro ( _do_find_gnu_toolchain )

  file ( GLOB _gnu_toolchain_list RELATIVE "${ANDROID_NDK_ROOT}/"

"${ANDROID_NDK_ROOT}/toolchains/${_gnu_toolchain_name}-[0-9].[0-9]/prebuilt/${AndroidNDK_HOST}"
  )

  set ( AndroidNDK