How do you get the full set of compiler and linker flags used by ndk-build?

Is there an easy way to get the full set of compilation and linker flags used by NDK assemblies? We are hacking things right now by looking at the cmake files generated when adding C ++ to an Android Studio project.

The problem is that most of the time we create many existing side projects, each with their own build systems. Overwriting build systems to use Android tools (cmake and ndk-build systems) would be insane; we need to use existing make systems and in most cases this means that we need to provide scripts like CFLAGS, CXX_FLAGS, CPPFLAGS, LDFLAGS, etc. And these sets of flags are not documented anywhere I have found other than watching native Android ndk builds release.

UPDATE: I think it is not entirely clear what exactly I wanted.

We've had some nasty scripts that managed to suck the right set of flags from an existing build using cmake. (One of those projects where you say it will take you twenty minutes and you are ridiculously wrong). These scripts broke with the r16 beta. I wouldn't have to write nasty unstable scripts to pull the flags, as they might still break with the next version of ndk.

(Here's the last output from our scripts if you want to see the flags I'm talking about: https://gist.github.com/banshee/15f1a5fbce0c71af6498656bb02ebc67 )

0


source to share


3 answers


Try to explore

{SDK_PATH}/ndk-bundle/build/core/toolchains/arm-linux-androideabi-4.9/setup.mk

      

or another toolchain (depending on what you are building). There are flags required to define cross-compilation.

EDIT

I did something.

If you are editing the file:



{SDK_PATH}/ndk-bundle/build/core/definitions.mk

      

and find the line define ev-compile-cpp-source

. Or maybe better define ev-build-file

.

You can add there

$(info $_FLAGS is [${_FLAGS}])

      

before $$eval

.

Now if you create something, you will see flags.

+1


source


For custom build systems, you must use the standalone toolchain . This will get all the major flags for Android targeting. It will not handle any decisions most appropriate for the build system like optimization level, debug flags, etc.

EDIT:



Unfortunately, your current solution is as good as it is now. Eventually, the toolchain configuration will be migrated to JSON files $NDK/meta

, as are a few (trivial) things that are already there, so we can share this information between ndk-build and CMake, but this is not happening anytime soon.

+1


source


Use cmake to manage configuration or to write values ​​in whatever format you need.

For example, here's a CMakeLists.txt file that configures and compiles the expat project. It expects the source for expat to already be on the system. Probably most of what you want is in the standard_configure_env macro; these are the modified CFLAGS etc you want.

cmake_minimum_required(VERSION 3.6)

project (expat)

macro(standard_configure_env output)
set(${output}
    CC=${ANDROID_C_COMPILER}
    CXX=${ANDROID_CXX_COMPILER}
    CFLAGS=--target=${CMAKE_C_COMPILER_TARGET}\ --sysroot=${ANDROID_SYSROOT}\ \ --gcc-toolchain=${ANDROID_TOOLCHAIN_ROOT}\ ${ANDROID_COMPILER_FLAGS}
    CXXFLAGS=${ANDROID_COMPILER_FLAGS_CXX}
    AR=${ANDROID_AR} 
    RANLIB=${ANDROID_RANLIB}  
    LD=${CMAKE_LINKER}  
    LDFLAGS=${ANDROID_LINKER_FLAGS}
)
endmacro()

include(ProcessorCount)
ProcessorCount(nCpus)

set(expat_output_dir ${CMAKE_CURRENT_BINARY_DIR}/expat)
set(expat_build_dir ${expat_output_dir}/expat_bin)
set(expat_install_dir ${expat_output_dir}/install)
set(expat_src_dir /Users/james/yourpathgoeshere/cmake/expat/src/expat-2.1.0)

set(markers_dir ${expat_output_dir}/markers)
file(MAKE_DIRECTORY ${markers_dir})

set(expat_copied ${markers_dir}/copied)
set(expat_configured ${markers_dir}/configured)

set(expat_final_lib ${expat_install_dir}/lib/libexpat.a)

file(MAKE_DIRECTORY ${expat_install_dir}/include)

standard_configure_env(configure_env)

set(configure_host ${ANDROID_HEADER_TRIPLE})
# expat doesn't understand these Android architectures, so set them by hand to what expat can
# use
if(${configure_host} STREQUAL aarch64-linux-android)
set(configure_host arm-linux-android64v8a)
elseif(${configure_host} STREQUAL armv7-linux-androideabi)
set(configure_host arm-linux-androideabiv7a)
endif()

set(configure_args
    --host=${configure_host}
    --disable-shared
    --prefix=${expat_output_dir}/install
    --exec-prefix=${expat_output_dir}/install
    )

add_custom_command(
    OUTPUT ${expat_configured}
    COMMAND ${configure_env} ./configure ${configure_args} && touch ${expat_configured}
    DEPENDS ${expat_copied}
    WORKING_DIRECTORY ${expat_build_dir}
    )

add_custom_command(
    OUTPUT ${expat_copied}
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${expat_src_dir} ${expat_build_dir}
    COMMAND touch ${expat_copied}
    )

add_custom_command(
    OUTPUT ${expat_final_lib}
    COMMAND make -j${nCpus} installlib && touch ${expat_final_lib}
    WORKING_DIRECTORY ${expat_build_dir}
    DEPENDS ${expat_configured}
    )

add_custom_target(expat_build DEPENDS ${expat_final_lib})
add_library(expat STATIC IMPORTED GLOBAL)
add_dependencies(expat expat_build)

set_target_properties(expat
  PROPERTIES
  LINKER_LANGUAGE CXX
  IMPORTED_LOCATION ${expat_install_dir}/lib/libexpat.a
  INTERFACE_INCLUDE_DIRECTORIES ${expat_install_dir}/include)

      

Thanks @DanAlbert and @JakubPiskorz for really wanting to get this from cmake.

0


source







All Articles