Add (and require) version scripts for everything.

This commit is contained in:
Dan Albert
2025-09-18 17:11:43 -07:00
committed by Dan Albert
parent 77557fb73e
commit 59791f25df
29 changed files with 187 additions and 13 deletions

View File

@@ -86,6 +86,26 @@ above:
5. Android Studio can warn you and auto-fix common mistakes in class names and 5. Android Studio can warn you and auto-fix common mistakes in class names and
function signatures. function signatures.
### Version scripts
All of the app libraries shown here are built using a version script. This is a
file that explicitly lists which symbols should be exported from the library,
and hides all the others. Version scripts function similarly to
`-fvisibility=hidden`, but can go a step further and are capable of hiding
symbols in static libraries that are used by your app. Hiding as many symbols as
possible results in smaller binaries that load faster, as there are fewer
relocations required and LTO can do a better job. They also run faster as
same-library function calls do not need to be made through the PLT. There are no
good reasons to not use a version script for your NDK code. See the NDK
documentation on [controlling symbol visibility] for more information.
You can find these in each sample as the `lib<name>.map.txt` file (where
`<name>` is the name of the library passed to `add_app_library()` in the
`CMakeLists.txt` file). The build plumbing that uses the version scripts is in
the definition of `add_app_library()` in `cmake/AppLibrary.cmake`.
[controlling symbol visibility]: https://developer.android.com/ndk/guides/symbol-visibility
## Additional documentation ## Additional documentation
- [Add Native Code to Your Project](https://developer.android.com/studio/projects/add-native-code.html) - [Add Native Code to Your Project](https://developer.android.com/studio/projects/add-native-code.html)

View File

@@ -0,0 +1,6 @@
LIBECHO {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBPLASMA {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,7 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBCAMERA_TEXTUREVIEW {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -3,7 +3,7 @@ include_guard()
# Wrapper for add_library which enables common options we want for all # Wrapper for add_library which enables common options we want for all
# libraries. # libraries.
function(add_app_library name) function(add_app_library name)
cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "") cmake_parse_arguments(PARSE_ARGV 1 arg "NO_VERSION_SCRIPT" "" "")
add_library(${name} ${arg_UNPARSED_ARGUMENTS}) add_library(${name} ${arg_UNPARSED_ARGUMENTS})
target_compile_options(${name} target_compile_options(${name}
@@ -12,4 +12,17 @@ function(add_app_library name)
-Wextra -Wextra
-Werror -Werror
) )
if(NOT arg_NO_VERSION_SCRIPT)
target_link_options(${name}
PRIVATE
-Wl,--no-undefined-version
-Wl,--version-script,${CMAKE_SOURCE_DIR}/lib${name}.map.txt
)
set_target_properties(${name}
PROPERTIES
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/lib${name}.map.txt
)
endif()
endfunction() endfunction()

View File

@@ -0,0 +1,6 @@
LIBGAME {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBEXCEPTIONS {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBGLES3JNI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBGL2JNI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBHELLOJNI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBJNICALLBACK {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBHELLOOBOE {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBHELLOVKJNI {
global:
Java_com_google_androidgamesdk_GameActivity_initializeNativeCode;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNATIVEACTIVITY {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNATIVEAUDIOJNI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNATIVECODECJNI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNATIVE_MIDI {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBORDERFILEDEMO {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBSANITIZERS {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBACCELEROMETERGRAPH {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,7 @@
LIBCHOREOGRAPHERNATIVEACTIVITY {
global:
ANativeActivity_onCreate;
JNI_OnLoad;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};

View File

@@ -28,7 +28,7 @@ target_link_libraries( # Specifies the target library.
log log
) )
add_app_library(app_tests SHARED adder_test.cpp) add_app_library(app_tests NO_VERSION_SCRIPT SHARED adder_test.cpp)
target_link_libraries(app_tests target_link_libraries(app_tests
PRIVATE PRIVATE
$<TARGET_OBJECTS:adder> $<TARGET_OBJECTS:adder>

View File

@@ -0,0 +1,6 @@
LIBUNITTEST {
global:
JNI_OnLoad;
local:
*;
};

View File

@@ -18,14 +18,3 @@ target_link_libraries(app
base::base base::base
log log
) )
target_link_options(app
PRIVATE
-flto
-Wl,--version-script,${CMAKE_SOURCE_DIR}/libapp.map.txt
)
set_target_properties(app
PROPERTIES
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/libapp.map.txt
)