Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] CXX flags not applied with Android toolchain #13374

Closed
1 task
cocoza4 opened this issue Mar 8, 2023 · 12 comments · Fixed by #13459
Closed
1 task

[question] CXX flags not applied with Android toolchain #13374

cocoza4 opened this issue Mar 8, 2023 · 12 comments · Fixed by #13459
Assignees
Milestone

Comments

@cocoza4
Copy link

cocoza4 commented Mar 8, 2023

What is your question?

Hi there,

I'm crossing compiling a simple hello package for aarch64-linux-android with android-ndk-r21. In my profile, I specified tools.build:cxxflags to pass additional flags, even though these flags appear in conan_toolchain.cmake, it actually didn't get applied during the build.

Steps to reproduce

  1. download Android NDK from https://dl.google.com/android/repository/android-ndk-r21-linux-x86_64.zip
  2. create a test hello package with conan new hello/0.1 -m=cmake_lib
  3. create an aarch64-linux-android profile, content below
  4. run VERBOSE=1 conan create . -pr:h /workspace/conan/profiles/aarch64-linux-android -pr:b default -tf=None

Some logs

/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot  -I/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O2 -DNDEBUG  -fPIC -MD -MT CMakeFiles/hello.dir/src/hello.cpp.o -MF CMakeFiles/hello.dir/src/hello.cpp.o.d -o CMakeFiles/hello.dir/src/hello.cpp.o -c /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/src/hello.cpp

conan_toolchain.cmake

# Conan automatically generated toolchain file
# DO NOT EDIT MANUALLY, it will be overwritten

# Avoid including toolchain file several times (bad if appending to variables like
#   CMAKE_CXX_FLAGS. See https://github.com/android/ndk/issues/323
include_guard()

message(STATUS "Using Conan toolchain: ${CMAKE_CURRENT_LIST_FILE}")

if(${CMAKE_VERSION} VERSION_LESS "3.15")
    message(FATAL_ERROR "The 'CMakeToolchain' generator only works with CMake >= 3.15")
endif()

# New toolchain things
set(ANDROID_PLATFORM android-21)
set(ANDROID_STL c++_shared)
set(ANDROID_ABI arm64-v8a)
include(/root/android-ndk-r21/build/cmake/android.toolchain.cmake)

message(STATUS "Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)")
set(CMAKE_POSITION_INDEPENDENT_CODE ON CACHE BOOL "Position independent code")

# Extra c, cxx, linkflags and defines
string(APPEND CONAN_CXX_FLAGS " -fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer")
string(APPEND CONAN_C_FLAGS " -fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden")

if(DEFINED CONAN_CXX_FLAGS)
  string(APPEND CMAKE_CXX_FLAGS_INIT " ${CONAN_CXX_FLAGS}")
endif()
if(DEFINED CONAN_C_FLAGS)
  string(APPEND CMAKE_C_FLAGS_INIT " ${CONAN_C_FLAGS}")
endif()
if(DEFINED CONAN_SHARED_LINKER_FLAGS)
  string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${CONAN_SHARED_LINKER_FLAGS}")
endif()
if(DEFINED CONAN_EXE_LINKER_FLAGS)
  string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${CONAN_EXE_LINKER_FLAGS}")
endif()

get_property( _CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE )
if(_CMAKE_IN_TRY_COMPILE)
    message(STATUS "Running toolchain IN_TRY_COMPILE")
    return()
endif()

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)

# Definition of CMAKE_MODULE_PATH
# the generators folder (where conan generates files, like this toolchain)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

# Definition of CMAKE_PREFIX_PATH, CMAKE_XXXXX_PATH
# The Conan local "generators" folder, where this toolchain is saved.
list(PREPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR} )

if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE OR CMAKE_FIND_ROOT_PATH_MODE_PACKAGE STREQUAL "ONLY")
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE "BOTH")
endif()
if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM OR CMAKE_FIND_ROOT_PATH_MODE_PROGRAM STREQUAL "ONLY")
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM "BOTH")
endif()
if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY OR CMAKE_FIND_ROOT_PATH_MODE_LIBRARY STREQUAL "ONLY")
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "BOTH")
endif()
if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE OR CMAKE_FIND_ROOT_PATH_MODE_INCLUDE STREQUAL "ONLY")
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE "BOTH")
endif()

if (DEFINED ENV{PKG_CONFIG_PATH})
set(ENV{PKG_CONFIG_PATH} "/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/generators:$ENV{PKG_CONFIG_PATH}")
else()
set(ENV{PKG_CONFIG_PATH} "/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/generators:")
endif()

message(STATUS "Conan toolchain: Setting BUILD_SHARED_LIBS = OFF")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries")

set(CMAKE_INSTALL_PREFIX "/root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f")
set(CMAKE_INSTALL_BINDIR "bin")
set(CMAKE_INSTALL_SBINDIR "bin")
set(CMAKE_INSTALL_LIBEXECDIR "bin")
set(CMAKE_INSTALL_LIBDIR "lib")
set(CMAKE_INSTALL_INCLUDEDIR "include")
set(CMAKE_INSTALL_OLDINCLUDEDIR "include")

As you can see, -fvisibility-inlines-hidden -fno-omit-frame-pointer appear in the toolchain but it didn't get applied when running cmake. How can I make these flags get applied?

aarch64-linux-android

target_host=aarch64-linux-android
android_ndk=/root/android-ndk-r21
toolchain=$android_ndk/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/aarch64-linux-android/bin
api_level=21

clang_cflags="-fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden"
clang_cxxflags="$clang_cflags -fvisibility-inlines-hidden -fno-omit-frame-pointer"

[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.libcxx=c++_shared
compiler.version=9
os=Android
os.api_level=$api_level

[tool_requires]
[options]

[conf]
tools.env.virtualenv:auto_use=True
tools.android:ndk_path=$android_ndk
tools.build:cflags=["$clang_cflags"]
tools.build:cxxflags=["$clang_cxxflags"]

[buildenv]
CHOST=$target_host
AR=$toolchain/ar
CC=$target_host$api_level-clang
CXX=$target_host$api_level-clang++
AS=$CC
LD=$toolchain/ld
RANLIB=$toolchain/ranlib
STRIP=$toolchain/strip

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@cocoza4
Copy link
Author

cocoza4 commented Mar 10, 2023

@memsharded any idea about the fix? thanks

@memsharded memsharded self-assigned this Mar 10, 2023
@memsharded
Copy link
Member

Hi @cocoza4

I am trying this (on Windows), and it seems to be working fine here:

C:\ws\android\android-ndk-r23b\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android21 --sysroot=C:/ws/android/android-ndk-r23b/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -IC:\Users\Diego\.conan\data\hello\0.1\_\_\build\a2ccebaee30b07c93935ff3643d57c8239e9ee4f\include -fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer   -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++ -O3 -DNDEBUG -fPIC -MD -MT CMakeFiles/hello.dir/src/hello.cpp.o -MF CMakeFiles\hello.dir\src\hello.cpp.o.d -o CMakeFiles\hello.dir\src\hello.cpp.o -c C:\Users\Diego\.conan\data\hello\0.1\_\_\build\a2ccebaee30b07c93935ff3643d57c8239e9ee4f\src\hello.cpp

The lines

-fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer 

Are all there, not sure why it wouldn't be working on your side.

Also, it is not necessary to detail the full environment [buildenv] as you do, I think the Android toolchain is able to manage it. This profile works fine for me:

target_host=aarch64-linux-android
android_ndk=C:\ws\android\android-ndk-r23b
api_level=21

clang_cflags="-fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden"
clang_cxxflags="$clang_cflags -fvisibility-inlines-hidden -fno-omit-frame-pointer"

[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.libcxx=c++_shared
compiler.version=9
os=Android
os.api_level=$api_level

[tool_requires]
[options]

[conf]
tools.env.virtualenv:auto_use=True
tools.android:ndk_path=$android_ndk
tools.build:cflags=["$clang_cflags"]
tools.build:cxxflags=["$clang_cxxflags"]

[buildenv]
PATH+=(path)C:\ws\msys64\mingw64\bin

@memsharded
Copy link
Member

Maybe you want to report the Conan version, and also the CMake version (recommended >3.19 at least for Android), just in case.

@cocoza4
Copy link
Author

cocoza4 commented Mar 10, 2023

@memsharded I'm on Conan 1.58, CMake 3.22.1

@memsharded
Copy link
Member

If you can please share your full log/output, that might have some extra hint.

@cocoza4
Copy link
Author

cocoza4 commented Mar 10, 2023

If you can please share your full log/output, that might have some extra hint.

root@peeranat:/workspace/3rdparty/hello# VERBOSE=1 conan create . -pr:h /workspace/conan/profiles/aarch64-linux-android -pr:b default -tf=None
Exporting package recipe
hello/0.1 exports_sources: Copied 1 '.txt' file: CMakeLists.txt
hello/0.1 exports_sources: Copied 1 '.cpp' file: hello.cpp
hello/0.1 exports_sources: Copied 1 '.h' file: hello.h
hello/0.1: The stored package has not changed
hello/0.1: Exported revision: bb4d07e0897a612e65c02400af2276aa
Configuration (profile_host):
[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.libcxx=c++_shared
compiler.version=9
os=Android
os.api_level=21
[options]
[build_requires]
[env]
[conf]
tools.env.virtualenv:auto_use=True
tools.android:ndk_path=/root/android-ndk-r21
tools.build:cflags=['-fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden']
tools.build:cxxflags=['-fPIC -pthread -fstrict-aliasing -ffast-math -O3 -fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer']
[buildenv]
CHOST=aarch64-linux-android
AR=/root/android-ndk-r21/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/aarch64-linux-android/bin/ar
CC=aarch64-linux-android21-clang
CXX=aarch64-linux-android21-clang++
AS=$CC
LD=/root/android-ndk-r21/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/aarch64-linux-android/bin/ld
RANLIB=/root/android-ndk-r21/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/aarch64-linux-android/bin/ranlib
STRIP=/root/android-ndk-r21/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/aarch64-linux-android/bin/strip

Configuration (profile_build):
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=9
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

hello/0.1: Forced build from source
Installing package: hello/0.1
Requirements
    hello/0.1 from local cache - Cache
Packages
    hello/0.1:a2ccebaee30b07c93935ff3643d57c8239e9ee4f - Build

Cross-build from 'Linux:x86_64' to 'Android:armv8'
Installing (downloading, building) binaries...
hello/0.1: Copying sources to build folder
hello/0.1: Building your package in /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f
hello/0.1: Generator txt created conanbuildinfo.txt
hello/0.1: Calling generate()
hello/0.1: Preset 'release' added to CMakePresets.json. Invoke it manually using 'cmake --preset release'
hello/0.1: If your CMake version is not compatible with CMakePresets (<3.19) call cmake like: 'cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/generators/conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release'
hello/0.1: Aggregating env generators
hello/0.1: Calling build()
hello/0.1: CMake command: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/."
Re-run cmake no build system arguments
-- Using Conan toolchain: /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/generators/conan_toolchain.cmake
-- Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release
hello/0.1: CMake command: cmake --build "/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release" '--' '-j12'
/usr/local/bin/cmake -S/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f -B/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/CMakeFiles /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release//CMakeFiles/progress.marks
/usr/bin/make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
/usr/bin/make  -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/depend
make[2]: Entering directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
cd /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/CMakeFiles/hello.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
/usr/bin/make  -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/build
make[2]: Entering directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
[ 50%] Building CXX object CMakeFiles/hello.dir/src/hello.cpp.o
/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot  -I/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O2 -DNDEBUG  -fPIC -MD -MT CMakeFiles/hello.dir/src/hello.cpp.o -MF CMakeFiles/hello.dir/src/hello.cpp.o.d -o CMakeFiles/hello.dir/src/hello.cpp.o -c /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/src/hello.cpp
[100%] Linking CXX static library libhello.a
/usr/local/bin/cmake -P CMakeFiles/hello.dir/cmake_clean_target.cmake
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/hello.dir/link.txt --verbose=1
/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar qc libhello.a CMakeFiles/hello.dir/src/hello.cpp.o
/root/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ranlib libhello.a
make[2]: Leaving directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
[100%] Built target hello
make[1]: Leaving directory '/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release'
/usr/local/bin/cmake -E cmake_progress_start /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release/CMakeFiles 0
hello/0.1: Package 'a2ccebaee30b07c93935ff3643d57c8239e9ee4f' built
hello/0.1: Build folder /root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release
hello/0.1: Generated conaninfo.txt
hello/0.1: Generated conanbuildinfo.txt
hello/0.1: Generating the package
hello/0.1: Package folder /root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f
hello/0.1: Calling package()
hello/0.1: CMake command: cmake --install "/root/.conan/data/hello/0.1/_/_/build/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/build/Release" --prefix "/root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f"
-- Install configuration: "Release"
-- Installing: /root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/lib/libhello.a
-- Installing: /root/.conan/data/hello/0.1/_/_/package/a2ccebaee30b07c93935ff3643d57c8239e9ee4f/include/hello.h
hello/0.1 package(): Packaged 1 '.h' file: hello.h
hello/0.1 package(): Packaged 1 '.a' file: libhello.a
hello/0.1: Package 'a2ccebaee30b07c93935ff3643d57c8239e9ee4f' created
hello/0.1: Created package revision 661594dbd45e7ccf59fb1d6d12e29822

@memsharded
Copy link
Member

I see these lines in my output that I miss in yours:

-- Android: Targeting API '21' with architecture 'arm64', ABI 'arm64-v8a', and processor 'aarch64'
-- Android: Selected unified Clang toolchain
-- The CXX compiler identification is Clang 12.0.8

Not sure why. Next step is to test in Linux, but I would need to setup that I don't have that installed, it can be a bit more challenging.

@cocoza4
Copy link
Author

cocoza4 commented Mar 10, 2023

@memsharded thanks for your response, let me know if you need additional information.

@jcar87
Copy link
Contributor

jcar87 commented Mar 10, 2023

Hi @cocoza4 - I have been able to reproduce this issue.

The cmake toolchain included in the Android NDK (/root/android-ndk-r21/build/cmake/android.toolchain.cmake), which is in turn included by the conan-generated conan_toolchain.cmake, seems to have some unfortunate logic that overrides the values of CMAKE_CXX_FLAGS, so they are not propagated.

We follow the advice by the CMake maintainers (link here) of setting CMAKE_CXX_FLAGS_INIT in the toolchain file, rather than meddle with CMAKE_CXX_FLAGS directly.

The android-ndk provided toolchain on the other had modifies CMAKE_CXX_FLAGS directly in a way that does away with our values. This has been fixed in newer versions of the Android NDK, however I believe even on recent versions it uses the legacy behaviour due to developers relying on the ability to pass CMAKE_CXX_FLAGS to cmake directly.

I have had luck with:

  • using the Android r25c NDK
  • explicitly passing ANDROID_USE_LEGACY_TOOLCHAIN_FILE to OFF (in the recipe's generate() method, pass it as a cache_variable to the cmaketoolchain object).

Then I see the behaviour you expect. Do you have the ability to use a newer Android NDK while still targeting Android 21?

@cocoza4
Copy link
Author

cocoza4 commented Mar 12, 2023

@jcar87 @memsharded thank you very much, I've fixed the issue as you suggested.

@cocoza4 cocoza4 closed this as completed Mar 12, 2023
@memsharded
Copy link
Member

@jcar87 I am re-opening, to discuss if we should add ANDROID_USE_LEGACY_TOOLCHAIN_FILE to OFF automatically in our toolchain, it is possible that this will also impact other users?

@memsharded
Copy link
Member

Implemented in #13459, for next 2.0.3

Adding a new conf to opt-in into this Android CMake toolchain behavior is the best we can do, it is a bit of a mess there with the injection of flags, there is not much Conan can do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants