Skip to content

Commit c42b48d

Browse files
authored
Native runtime reorganization (#9113)
Context: #9006 This commit prepares ground for dynamic native runtime linking at application build time by moving code around and making it more modular, thus allowing the on-build linking of the runtime to work properly. In particular, this applies to the `p/invoke` handling code which is going to be different between the pre-linked (and shipped) runtime and the one linked on demand during application build. The difference lies in the fact that on-demand build will scan the application for p/invoke usage (after trimming) and will only include functions that are actually called from the managed land. Results of the scan will be used to generate LLVM IR code at build time that references the required functions, which will allow the native linker to remove the remaining ones. Changes implemented here can be committed to `main` and, possibly, released with .NET9 without changing how things currently work.
1 parent 688245d commit c42b48d

38 files changed

+2359
-2743
lines changed

build-tools/scripts/generate-pinvoke-tables.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MY_DIR="$(dirname $0)"
33
HOST="$(uname | tr A-Z a-z)"
44

55
NATIVE_DIR="${MY_DIR}/../../src/native"
6-
MONODROID_SOURCE_DIR="${NATIVE_DIR}/monodroid"
6+
MONODROID_SOURCE_DIR="${NATIVE_DIR}/pinvoke-override"
77
GENERATOR_SOURCE="${MONODROID_SOURCE_DIR}/generate-pinvoke-tables.cc"
88
GENERATOR_BINARY="${MONODROID_SOURCE_DIR}/generate-pinvoke-tables"
99
TARGET_FILE="${MONODROID_SOURCE_DIR}/pinvoke-tables.include"

src/native/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ file(REAL_PATH "../../" REPO_ROOT_DIR)
145145
set(EXTERNAL_DIR "${REPO_ROOT_DIR}/external")
146146
set(JAVA_INTEROP_SRC_PATH "${EXTERNAL_DIR}/Java.Interop/src/java-interop")
147147
set(LIBUNWIND_SOURCE_DIR "${EXTERNAL_DIR}/libunwind")
148-
148+
set(ROBIN_MAP_DIR "${EXTERNAL_DIR}/robin-map")
149149

150150
#
151151
# Include directories
@@ -449,6 +449,7 @@ add_subdirectory(java-interop)
449449
add_subdirectory(xamarin-app-stub)
450450
add_subdirectory(runtime-base)
451451
add_subdirectory(tracing)
452+
add_subdirectory(pinvoke-override)
452453

453454
if(DEBUG_BUILD)
454455
add_subdirectory(xamarin-debug-app-helper)

src/native/java-interop/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ target_compile_options(
3737
${XA_COMMON_CXX_ARGS}
3838
)
3939

40-
set_target_properties(
41-
${LIB_NAME}
42-
PROPERTIES
43-
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
44-
)
40+
if(DEBUG_BUILD)
41+
set_target_properties(
42+
${LIB_NAME}
43+
PROPERTIES
44+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
45+
)
46+
endif()
4547

4648
xa_add_compile_definitions(${LIB_NAME})

src/native/libstub/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ endmacro()
3838

3939
xa_add_stub_library(c)
4040
xa_add_stub_library(m)
41+
xa_add_stub_library(z)
4142

4243
# These two are used by the marshal methods tracing library when linking libxamarin-app.so
4344
xa_add_stub_library(log)

src/native/lz4/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ target_include_directories(
3030
"$<BUILD_INTERFACE:${LZ4_INCLUDE_DIR}>"
3131
)
3232

33-
set_target_properties(
34-
${LIB_NAME}
35-
PROPERTIES
36-
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
37-
)
33+
if(DEBUG_BUILD)
34+
set_target_properties(
35+
${LIB_NAME}
36+
PROPERTIES
37+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
38+
)
39+
endif()
3840

3941
xa_add_compile_definitions(${LIB_NAME})

src/native/monodroid/CMakeLists.txt

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ include(CheckCXXSymbolExists)
88
# Paths
99

1010
set(BIONIC_SOURCES_DIR "${REPO_ROOT_DIR}/src-ThirdParty/bionic")
11-
set(ROBIN_MAP_DIR "${EXTERNAL_DIR}/robin-map")
11+
set(BUILD_STATIC_LIBRARY OFF)
1212

1313
if(DEBUG_BUILD)
1414
# Convince NDK to really optimize our Debug builds. Without this, NDK's cmake toolchain definition
@@ -17,6 +17,8 @@ if(DEBUG_BUILD)
1717
set(XA_COMPILER_FLAGS_DEBUG "-fno-limit-debug-info -O2")
1818
set(CMAKE_C_FLAGS_DEBUG ${XA_COMPILER_FLAGS_DEBUG})
1919
set(CMAKE_CXX_FLAGS_DEBUG ${XA_COMPILER_FLAGS_DEBUG})
20+
elseif(NOT ANALYZERS_ENABLED)
21+
set(BUILD_STATIC_LIBRARY ON)
2022
endif()
2123

2224
# Library directories
@@ -42,13 +44,15 @@ endif()
4244
# Sources
4345
string(TOLOWER ${CMAKE_BUILD_TYPE} XAMARIN_MONO_ANDROID_SUFFIX)
4446
set(XAMARIN_MONO_ANDROID_LIB "mono-android${CHECKED_BUILD_INFIX}.${XAMARIN_MONO_ANDROID_SUFFIX}")
47+
set(XAMARIN_MONO_ANDROID_STATIC_LIB "${XAMARIN_MONO_ANDROID_LIB}-static")
4548

4649
set(XAMARIN_MONODROID_SOURCES
4750
debug-constants.cc
4851
debug.cc
4952
embedded-assemblies-zip.cc
5053
embedded-assemblies.cc
5154
globals.cc
55+
internal-pinvokes.cc
5256
jni-remapping.cc
5357
mono-log-adapter.cc
5458
monodroid-glue.cc
@@ -58,9 +62,7 @@ set(XAMARIN_MONODROID_SOURCES
5862
osbridge.cc
5963
pinvoke-override-api.cc
6064
runtime-util.cc
61-
timing.cc
6265
timezones.cc
63-
timing-internal.cc
6466
xamarin_getifaddrs.cc
6567
)
6668

@@ -93,90 +95,107 @@ add_library(
9395
SHARED ${XAMARIN_MONODROID_SOURCES}
9496
)
9597

96-
target_compile_definitions(
97-
${XAMARIN_MONO_ANDROID_LIB}
98-
PRIVATE
99-
HAVE_CONFIG_H
100-
HAVE_LZ4
101-
JI_DLL_EXPORT
102-
JI_NO_VISIBILITY
103-
MONO_DLL_EXPORT
104-
NET
105-
TSL_NO_EXCEPTIONS
106-
)
107-
108-
if(DONT_INLINE)
109-
target_compile_definitions(
110-
${XAMARIN_MONO_ANDROID_LIB}
111-
PRIVATE
112-
NO_INLINE
98+
if(BUILD_STATIC_LIBRARY)
99+
add_library(
100+
${XAMARIN_MONO_ANDROID_STATIC_LIB}
101+
STATIC
102+
${XAMARIN_MONODROID_SOURCES}
113103
)
114104
endif()
115105

116-
if(DEBUG_BUILD AND NOT DISABLE_DEBUG)
106+
macro(lib_target_options TARGET_NAME)
117107
target_compile_definitions(
118-
${XAMARIN_MONO_ANDROID_LIB}
108+
${TARGET_NAME}
119109
PRIVATE
120-
DEBUG
110+
HAVE_CONFIG_H
111+
HAVE_LZ4
112+
JI_DLL_EXPORT
113+
JI_NO_VISIBILITY
114+
MONO_DLL_EXPORT
115+
NET
116+
TSL_NO_EXCEPTIONS
121117
)
122-
endif()
123118

124-
if (ENABLE_TIMING)
125-
target_compile_definitions(
126-
${XAMARIN_MONO_ANDROID_LIB}
119+
if(DONT_INLINE)
120+
target_compile_definitions(
121+
${TARGET_NAME}
122+
PRIVATE
123+
NO_INLINE
124+
)
125+
endif()
126+
127+
if(DEBUG_BUILD AND NOT DISABLE_DEBUG)
128+
target_compile_definitions(
129+
${TARGET_NAME}
130+
PRIVATE
131+
DEBUG
132+
)
133+
endif()
134+
135+
if (ENABLE_TIMING)
136+
target_compile_definitions(
137+
${TARGET_NAME}
138+
PRIVATE
139+
MONODROID_TIMING
140+
)
141+
endif()
142+
143+
target_compile_options(
144+
${TARGET_NAME}
127145
PRIVATE
128-
MONODROID_TIMING
146+
${XA_DEFAULT_SYMBOL_VISIBILITY}
147+
${XA_COMMON_CXX_ARGS}
129148
)
130-
endif()
131149

132-
target_compile_options(
133-
${XAMARIN_MONO_ANDROID_LIB}
134-
PRIVATE
135-
${XA_DEFAULT_SYMBOL_VISIBILITY}
136-
${XA_COMMON_CXX_ARGS}
137-
)
138-
139-
target_include_directories(
140-
${XAMARIN_MONO_ANDROID_LIB} BEFORE
141-
PRIVATE
142-
${CMAKE_CURRENT_BINARY_DIR}/include
143-
${EXTERNAL_DIR}
144-
${ROBIN_MAP_DIR}/include
145-
)
150+
target_include_directories(
151+
${TARGET_NAME} BEFORE
152+
PRIVATE
153+
${CMAKE_CURRENT_BINARY_DIR}/include
154+
${EXTERNAL_DIR}
155+
${ROBIN_MAP_DIR}/include
156+
)
146157

147-
target_include_directories(
148-
${XAMARIN_MONO_ANDROID_LIB}
149-
SYSTEM PRIVATE
150-
${SYSROOT_CXX_INCLUDE_DIR}
151-
${MONO_RUNTIME_INCLUDE_DIR}
152-
${NATIVE_TRACING_INCLUDE_DIRS}
153-
${LIBUNWIND_INCLUDE_DIRS}
154-
)
158+
target_include_directories(
159+
${TARGET_NAME}
160+
SYSTEM PRIVATE
161+
${SYSROOT_CXX_INCLUDE_DIR}
162+
${MONO_RUNTIME_INCLUDE_DIR}
163+
${NATIVE_TRACING_INCLUDE_DIRS}
164+
${LIBUNWIND_INCLUDE_DIRS}
165+
)
155166

156-
target_link_directories(
157-
${XAMARIN_MONO_ANDROID_LIB}
158-
PRIVATE
159-
${NET_RUNTIME_DIR}/native
160-
)
167+
target_link_directories(
168+
${TARGET_NAME}
169+
PRIVATE
170+
${NET_RUNTIME_DIR}/native
171+
)
161172

162-
target_link_options(
163-
${XAMARIN_MONO_ANDROID_LIB}
164-
PRIVATE
165-
${XA_DEFAULT_SYMBOL_VISIBILITY}
166-
${XA_COMMON_CXX_LINKER_ARGS}
167-
${XA_CXX_DSO_LINKER_ARGS}
168-
)
173+
target_link_options(
174+
${TARGET_NAME}
175+
PRIVATE
176+
${XA_DEFAULT_SYMBOL_VISIBILITY}
177+
${XA_COMMON_CXX_LINKER_ARGS}
178+
${XA_CXX_DSO_LINKER_ARGS}
179+
)
169180

170-
target_link_libraries(
171-
${XAMARIN_MONO_ANDROID_LIB}
172-
${LINK_LIBS}
173-
xa::xamarin-app
174-
${SHARED_LIB_NAME}
175-
xa::runtime-base
176-
xa::java-interop
177-
xa::lz4
178-
-lmonosgen-2.0
179-
-llog
180-
)
181+
target_link_libraries(
182+
${TARGET_NAME}
183+
${LINK_LIBS}
184+
xa::xamarin-app
185+
${SHARED_LIB_NAME}
186+
xa::runtime-base
187+
xa::java-interop
188+
xa::pinvoke-override-precompiled
189+
xa::lz4
190+
-lmonosgen-2.0
191+
-llog
192+
)
193+
endmacro ()
181194

195+
lib_target_options(${XAMARIN_MONO_ANDROID_LIB})
182196
xa_add_compile_definitions(${XAMARIN_MONO_ANDROID_LIB})
197+
198+
if(BUILD_STATIC_LIBRARY)
199+
lib_target_options(${XAMARIN_MONO_ANDROID_STATIC_LIB})
200+
xa_add_compile_definitions(${XAMARIN_MONO_ANDROID_STATIC_LIB})
201+
endif()

src/native/monodroid/embedded-assemblies.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "xamarin-app.hh"
3434
#include "cpp-util.hh"
3535
#include "monodroid-glue-internal.hh"
36+
#include "monodroid-state.hh"
3637
#include "startup-aware-lock.hh"
3738
#include "timing-internal.hh"
3839
#include "search.hh"
@@ -179,7 +180,7 @@ EmbeddedAssemblies::map_runtime_file (XamarinAndroidBundledAssembly& file) noexc
179180
close (fd);
180181
}
181182

182-
if (MonodroidRuntime::is_startup_in_progress ()) {
183+
if (MonodroidState::is_startup_in_progress ()) {
183184
file.data = static_cast<uint8_t*>(map_info.area);
184185
} else {
185186
uint8_t *expected_null = nullptr;

0 commit comments

Comments
 (0)