Native App Glue requires a C compiler
Description
Our project only enables the CXX compiler by default.
project(VVL LANGUAGES CXX) # <- Are project is pure C++
However, for the Android build we have some awkward CMake code:
enable_language(C) # NOTE: We need to enable the C language for android_native_app_glue.c
set(native_app_glue_dir "${CMAKE_ANDROID_NDK}/sources/android/native_app_glue")
if (NOT EXISTS ${native_app_glue_dir})
message(FATAL_ERROR "Couldn't find Android Native Glue directory!")
endif()
add_library(android_glue STATIC)
target_include_directories(android_glue PUBLIC ${native_app_glue_dir})
target_sources(android_glue PRIVATE
${native_app_glue_dir}/android_native_app_glue.c
${native_app_glue_dir}/android_native_app_glue.h
)
As you can see we need to enable a C compiler in order to use native app glue. This CMake isn't ideal and is confusing to look at unless you know what is going on. Ideally this process would be more streamlined.
Thoughts?
What do you have in mind? This looks like the expected CMake behavior to me. You need to enable C support to build the C code.
What do you have in mind?
Providing a CMakeLists.txt in the native_app_glue directory. It would look something like this:
project(ANDROID_NATIVE_APP_GLUE LANGUAGES C)
add_library(android_native_app_glue STATIC)
add_library(Android::NativeAppGlue ALIAS android_native_app_glue)
target_include_directories(android_native_app_glue PUBLIC .)
target_sources(android_native_app_glue PRIVATE
android_native_app_glue.c
android_native_app_glue.h
)
Then I could just call add_subdirectory and link against the static library.
add_subdirectory(${CMAKE_ANDROID_NDK}/sources/android/native_app_glue)
target_link_libraries(foobar PRIVATE Android::NativeAppGlue)
Thoughts?
Seems plausible. It might actually be more idiomatic to do something find_package() shaped. Either way, worth investigating. I can't say when we'll get to this though.
In case others see this thread. I'd advise using an object library.
See this PR: https://github.com/KhronosGroup/Vulkan-ValidationLayers/pull/6343/files
add_library(android_glue OBJECT)
target_include_directories(android_glue PUBLIC ${native_app_glue_dir})
target_sources(android_glue PRIVATE
${native_app_glue_dir}/android_native_app_glue.c
${native_app_glue_dir}/android_native_app_glue.h
)
This fixed an issue with our symbol exporting logic allowing us to use a version-script.