Skip to content

Commit fdb98cc

Browse files
committed
Add (and require) version scripts for everything.
1 parent 1ee099f commit fdb98cc

File tree

29 files changed

+186
-13
lines changed

29 files changed

+186
-13
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ above:
8686
5. Android Studio can warn you and auto-fix common mistakes in class names and
8787
function signatures.
8888

89+
### Version scripts
90+
91+
All of the app libraries shown here are built using a version script. This is a
92+
file that explicitly lists which symbols should be exported from the library,
93+
and hides all the others. Version scripts function similarly to
94+
`-fvisibility=hidden`, but can go a step further and are capable of hiding
95+
symbols in static libraries that are used by your app. Hiding as many symbols as
96+
possible results in smaller binaries that load faster, as there are fewer
97+
relocations required and LTO can do a better job. There are no good reasons to
98+
not use a version script for your NDK code. See the NDK documentation on
99+
[controlling symbol visibility] for more information.
100+
101+
You can find these in each sample as the `lib<name>.map.txt` file (where
102+
`<name>` is the name of the library passed to `add_app_library()` in the
103+
`CMakeLists.txt` file). The build plumbing that uses the version scripts is in
104+
the definition of `add_app_library()` in `cmake/AppLibrary.cmake`.
105+
106+
[controlling symbol visibility]: https://developer.android.com/ndk/guides/symbol-visibility
107+
89108
## Additional documentation
90109

91110
- [Add Native Code to Your Project](https://developer.android.com/studio/projects/add-native-code.html)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBECHO {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBPLASMA {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
LIBNDK_CAMERA {
2+
global:
3+
ANativeActivity_onCreate;
4+
JNI_OnLoad;
5+
local:
6+
*;
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBCAMERA_TEXTUREVIEW {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};

cmake/AppLibrary.cmake

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include_guard()
33
# Wrapper for add_library which enables common options we want for all
44
# libraries.
55
function(add_app_library name)
6-
cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "")
6+
cmake_parse_arguments(PARSE_ARGV 1 arg "NO_VERSION_SCRIPT" "" "")
77
add_library(${name} ${arg_UNPARSED_ARGUMENTS})
88

99
target_compile_options(${name}
@@ -12,4 +12,17 @@ function(add_app_library name)
1212
-Wextra
1313
-Werror
1414
)
15+
16+
if(NOT arg_NO_VERSION_SCRIPT)
17+
target_link_options(${name}
18+
PRIVATE
19+
-Wl,--no-undefined-version
20+
-Wl,--version-script,${CMAKE_SOURCE_DIR}/lib${name}.map.txt
21+
)
22+
23+
set_target_properties(${name}
24+
PROPERTIES
25+
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/lib${name}.map.txt
26+
)
27+
endif()
1528
endfunction()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBGAME {
2+
global:
3+
ANativeActivity_onCreate;
4+
local:
5+
*;
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBEXCEPTIONS {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBGLES3JNI {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LIBGL2JNI {
2+
global:
3+
JNI_OnLoad;
4+
local:
5+
*;
6+
};

0 commit comments

Comments
 (0)