Skip to content

Commit 6d52dff

Browse files
committed
Add logging to the naot android runtime
Linking seems to work, not tested yet
1 parent 93cc258 commit 6d52dff

File tree

14 files changed

+118
-66
lines changed

14 files changed

+118
-66
lines changed

src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ static partial class JavaInteropRuntime
88
{
99
static JniRuntime? runtime;
1010

11+
[DllImport("xa-internal-api")]
12+
static extern int XA_Host_NativeAOT_JNI_OnLoad (IntPtr vm, IntPtr reserved);
13+
1114
[UnmanagedCallersOnly (EntryPoint="JNI_OnLoad")]
1215
static int JNI_OnLoad (IntPtr vm, IntPtr reserved)
1316
{
1417
try {
1518
AndroidLog.Print (AndroidLogLevel.Info, "JavaInteropRuntime", "JNI_OnLoad()");
19+
XA_Host_NativeAOT_JNI_OnLoad (vm, reserved);
1620
LogcatTextWriter.Init ();
1721
return (int) JniVersion.v1_6;
1822
}
@@ -64,4 +68,4 @@ static void init (IntPtr jnienv, IntPtr klass, IntPtr classLoader)
6468
}
6569
transition.Dispose ();
6670
}
67-
}
71+
}

src/native/CMakeLists.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ include("${XA_BUILD_DIR}/xa_build_configuration.cmake")
161161
#
162162
# Paths
163163
#
164+
macro(temp_configure_mono_runtime_dir)
165+
# TEMPORARY: for now JI needs to build with MonoVM embedding APIs
166+
set(TEMP_NETCORE_RUNTIME_DIR_ARM64 "${NETCORE_APP_RUNTIME_DIR_ARM64}")
167+
set(TEMP_NETCORE_RUNTIME_DIR_ARM "${NETCORE_APP_RUNTIME_DIR_ARM}")
168+
set(TEMP_NETCORE_RUNTIME_DIR_X86_64 "${NETCORE_APP_RUNTIME_DIR_X86_64}")
169+
set(TEMP_NETCORE_RUNTIME_DIR_X86 "${NETCORE_APP_RUNTIME_DIR_X86}")
170+
endmacro()
171+
164172
if(IS_CLR_RUNTIME)
165173
if(LOCAL_CORECLR_PATH)
166174
set(CLR_PACKAGE_NAME_STEM "microsoft.netcore.app.runtime.android")
@@ -179,11 +187,9 @@ if(IS_CLR_RUNTIME)
179187
set(RUNTIME_DIR_X86 "${CORECLR_APP_RUNTIME_DIR_X86}")
180188
endif()
181189

182-
# TEMPORARY: for now JI needs to build with MonoVM embedding APIs
183-
set(TEMP_NETCORE_RUNTIME_DIR_ARM64 "${NETCORE_APP_RUNTIME_DIR_ARM64}")
184-
set(TEMP_NETCORE_RUNTIME_DIR_ARM "${NETCORE_APP_RUNTIME_DIR_ARM}")
185-
set(TEMP_NETCORE_RUNTIME_DIR_X86_64 "${NETCORE_APP_RUNTIME_DIR_X86_64}")
186-
set(TEMP_NETCORE_RUNTIME_DIR_X86 "${NETCORE_APP_RUNTIME_DIR_X86}")
190+
temp_configure_mono_runtime_dir()
191+
elseif(IS_NAOT_RUNTIME)
192+
temp_configure_mono_runtime_dir()
187193
elseif(IS_MONO_RUNTIME)
188194
set(RUNTIME_DIR_ARM64 "${NETCORE_APP_RUNTIME_DIR_ARM64}")
189195
set(RUNTIME_DIR_ARM "${NETCORE_APP_RUNTIME_DIR_ARM}")
@@ -262,13 +268,15 @@ if(IS_CLR_RUNTIME)
262268
endmacro()
263269
elseif(IS_NAOT_RUNTIME)
264270
set(XA_RUNTIME_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nativeaot/include)
271+
set(CLR_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/clr/include)
265272

266273
macro(xa_add_include_directories TARGET)
267274
target_include_directories(
268275
${TARGET}
269276
PRIVATE
270277
${XA_RUNTIME_INCLUDE_DIR}
271278
${RUNTIME_INCLUDE_DIR}
279+
${CLR_INCLUDE_DIR}
272280
)
273281
endmacro()
274282
else()
@@ -314,7 +322,7 @@ macro(xa_add_compile_definitions TARGET)
314322
${TARGET}
315323
PRIVATE
316324
TARGET_ANDROID
317-
XA_HOST_NAOT
325+
XA_HOST_NATIVEAOT
318326
)
319327
else()
320328
target_compile_definitions(
@@ -637,8 +645,9 @@ endmacro()
637645
if(BUILD_ARCHIVE_DSO_STUB)
638646
add_subdirectory(common/archive-dso-stub)
639647
else()
648+
add_subdirectory(common/java-interop)
649+
640650
if (IS_MONO_RUNTIME OR IS_CLR_RUNTIME)
641-
add_subdirectory(common/java-interop)
642651
add_subdirectory(common/libunwind)
643652
add_subdirectory(common/lz4)
644653
add_subdirectory(common/runtime-base)

src/native/clr/include/runtime-base/util.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
#include "../constants.hh"
1515
#include <shared/helpers.hh>
16-
#include "archive-dso-stub-config.hh"
1716
#include <runtime-base/jni-wrappers.hh>
1817
#include "logger.hh"
1918
#include <runtime-base/strings.hh>
2019

20+
#if !defined(XA_HOST_NATIVEAOT)
21+
#include "archive-dso-stub-config.hh"
22+
#endif
23+
2124
namespace xamarin::android {
2225
namespace detail {
2326
template<typename T>
@@ -218,6 +221,7 @@ namespace xamarin::android {
218221
return file_info;
219222
}
220223

224+
#if !defined(XA_HOST_NATIVEAOT)
221225
[[gnu::always_inline]]
222226
static std::tuple<void*, size_t> get_wrapper_dso_payload_pointer_and_size (detail::mmap_info const& map_info, std::string_view const& file_name) noexcept
223227
{
@@ -248,6 +252,7 @@ namespace xamarin::android {
248252
payload_hdr.sh_size
249253
};
250254
}
255+
#endif // ndef XA_HOST_NATIVEAOT
251256

252257
static auto is_path_rooted (const char *path) noexcept -> bool
253258
{

src/native/clr/runtime-base/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(LIB_ALIAS xa::runtime-base)
5151

5252
set(XA_RUNTIME_BASE_SOURCES
5353
android-system.cc
54+
android-system-shared.cc
5455
cpu-arch-detect.cc
5556
jni-remapping.cc
5657
logger.cc
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <limits>
2+
#include <string_view>
3+
4+
#include <runtime-base/android-system.hh>
5+
6+
using namespace xamarin::android;
7+
8+
using std::operator""sv;
9+
10+
auto AndroidSystem::monodroid_get_system_property (std::string_view const& name, dynamic_local_property_string &value) noexcept -> int
11+
{
12+
int len = monodroid__system_property_get (name, value.get (), value.size ());
13+
if (len > 0) {
14+
// Clumsy, but if we want direct writes to be fast, this is the price we pay
15+
value.set_length_after_direct_write (static_cast<size_t>(len));
16+
return len;
17+
}
18+
19+
size_t plen;
20+
const char *v = lookup_system_property (name, plen);
21+
if (v == nullptr) {
22+
return len;
23+
}
24+
25+
value.assign (v, plen);
26+
return Helpers::add_with_overflow_check<int> (plen, 0);
27+
}
28+
29+
auto
30+
AndroidSystem::monodroid__system_property_get (std::string_view const& name, char *sp_value, size_t sp_value_len) noexcept -> int
31+
{
32+
if (name.empty () || sp_value == nullptr) {
33+
return -1;
34+
}
35+
36+
char *buf = nullptr;
37+
if (sp_value_len < Constants::PROPERTY_VALUE_BUFFER_LEN) {
38+
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (Constants::PROPERTY_VALUE_BUFFER_LEN, 1uz);
39+
log_warn (LOG_DEFAULT, "Buffer to store system property may be too small, will copy only {} bytes", sp_value_len);
40+
buf = new char [alloc_size];
41+
}
42+
43+
int len = __system_property_get (name.data (), buf ? buf : sp_value);
44+
if (buf != nullptr) {
45+
strncpy (sp_value, buf, sp_value_len);
46+
sp_value [sp_value_len] = '\0';
47+
delete[] buf;
48+
}
49+
50+
return len;
51+
}

src/native/clr/runtime-base/android-system.cc

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -312,49 +312,6 @@ AndroidSystem::lookup_system_property (std::string_view const& name, size_t &val
312312
return nullptr;
313313
}
314314

315-
auto
316-
AndroidSystem::monodroid__system_property_get (std::string_view const& name, char *sp_value, size_t sp_value_len) noexcept -> int
317-
{
318-
if (name.empty () || sp_value == nullptr) {
319-
return -1;
320-
}
321-
322-
char *buf = nullptr;
323-
if (sp_value_len < Constants::PROPERTY_VALUE_BUFFER_LEN) {
324-
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (Constants::PROPERTY_VALUE_BUFFER_LEN, 1uz);
325-
log_warn (LOG_DEFAULT, "Buffer to store system property may be too small, will copy only {} bytes", sp_value_len);
326-
buf = new char [alloc_size];
327-
}
328-
329-
int len = __system_property_get (name.data (), buf ? buf : sp_value);
330-
if (buf != nullptr) {
331-
strncpy (sp_value, buf, sp_value_len);
332-
sp_value [sp_value_len] = '\0';
333-
delete[] buf;
334-
}
335-
336-
return len;
337-
}
338-
339-
auto AndroidSystem::monodroid_get_system_property (std::string_view const& name, dynamic_local_property_string &value) noexcept -> int
340-
{
341-
int len = monodroid__system_property_get (name, value.get (), value.size ());
342-
if (len > 0) {
343-
// Clumsy, but if we want direct writes to be fast, this is the price we pay
344-
value.set_length_after_direct_write (static_cast<size_t>(len));
345-
return len;
346-
}
347-
348-
size_t plen;
349-
const char *v = lookup_system_property (name, plen);
350-
if (v == nullptr) {
351-
return len;
352-
}
353-
354-
value.assign (v, plen);
355-
return Helpers::add_with_overflow_check<int> (plen, 0);
356-
}
357-
358315
auto
359316
AndroidSystem::get_max_gref_count_from_system () noexcept -> long
360317
{

src/native/common/include/runtime-base/strings.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
#include <shared/helpers.hh>
1414

15-
#if defined(XA_HOST_CLR)
16-
#include <constants.hh>
17-
#else
15+
#if defined(XA_HOST_MONOVM)
1816
#include <runtime-base/shared-constants.hh>
1917

2018
using Constants = xamarin::android::internal::SharedConstants;
19+
#else
20+
#include <constants.hh>
2121
#endif
2222

2323
namespace xamarin::android {

src/native/common/include/runtime-base/timing-internal.hh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#include <thread>
1717
#include <vector>
1818

19-
#if defined(XA_HOST_CLR)
20-
#include <constants.hh>
21-
#else
19+
#if defined(XA_HOST_MONOVM)
2220
#include <runtime-base/shared-constants.hh>
2321

2422
using Constants = xamarin::android::internal::SharedConstants;
2523
using namespace xamarin::android::internal;
24+
#else
25+
#include <constants.hh>
2626
#endif
2727

2828
#include <runtime-base/logger.hh>
@@ -73,10 +73,10 @@ namespace xamarin::android {
7373

7474
class FastTiming final
7575
{
76-
#if defined(XA_HOST_CLR)
77-
using mutex = std::mutex;
78-
#else
76+
#if defined(XA_HOST_MONOVM)
7977
using mutex = xamarin::android::mutex;
78+
#else
79+
using mutex = std::mutex;
8080
#endif
8181
enum class SequenceError
8282
{

src/native/native.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
</Target>
9292

9393
<Target Name="_ConfigureAndBuildArchiveDSOStub"
94+
Condition=" '$(CMakeRuntimeFlavor)' == 'NativeAOT' "
9495
DependsOnTargets="_PrepareCommonProperties;_ConfigureArchiveDSOInputsAndOutputs"
9596
Inputs="@(_ArchiveDSOInput)"
9697
Outputs="@(_ArchiveDSOOutput)">

src/native/nativeaot/host/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ string(TOLOWER ${CMAKE_BUILD_TYPE} XAMARIN_NAOT_ANDROID_SUFFIX)
2424
set(XAMARIN_NAOT_ANDROID_LIB "naot-android${CHECKED_BUILD_INFIX}.${XAMARIN_NAOT_ANDROID_SUFFIX}")
2525
set(XAMARIN_NAOT_ANDROID_STATIC_LIB "${XAMARIN_NAOT_ANDROID_LIB}-static")
2626

27+
set(CLR_SOURCES_PATH "../../clr")
28+
2729
set(XAMARIN_MONODROID_SOURCES
2830
host.cc
2931
host-jni.cc
32+
33+
../runtime-base/android-system.cc
34+
35+
# Sources from CoreCLR host
36+
${CLR_SOURCES_PATH}/runtime-base/android-system-shared.cc
37+
${CLR_SOURCES_PATH}/runtime-base/logger.cc
3038
)
3139

3240
list(APPEND LOCAL_CLANG_CHECK_SOURCES
@@ -83,7 +91,6 @@ macro(lib_target_options TARGET_NAME)
8391
target_include_directories(
8492
${TARGET_NAME} BEFORE
8593
PRIVATE
86-
${CMAKE_CURRENT_BINARY_DIR}/include
8794
${EXTERNAL_DIR}
8895
)
8996

@@ -92,6 +99,7 @@ macro(lib_target_options TARGET_NAME)
9299
SYSTEM PRIVATE
93100
${SYSROOT_CXX_INCLUDE_DIR}
94101
${RUNTIME_INCLUDE_DIR}
102+
${TEMP_MONO_RUNTIME_INCLUDE_DIR}
95103
${LIBUNWIND_INCLUDE_DIRS}
96104
)
97105

@@ -111,6 +119,7 @@ macro(lib_target_options TARGET_NAME)
111119

112120
target_link_libraries(
113121
${TARGET_NAME}
122+
xa::java-interop
114123
-llog
115124
)
116125
endmacro ()

0 commit comments

Comments
 (0)