Skip to content

Commit 493cbd8

Browse files
committed
Environment variables support complete
1 parent 5abb8de commit 493cbd8

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/native/nativeaot/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(CLR_SOURCES_PATH "../../clr")
2929
set(XAMARIN_MONODROID_SOURCES
3030
bridge-processing.cc
3131
host.cc
32+
host-environment.cc
3233
host-jni.cc
3334
internal-pinvoke-stubs.cc
3435

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <cstdint>
2+
3+
#include <host/host-environment.hh>
4+
#include <runtime-base/logger.hh>
5+
6+
using namespace xamarin::android;
7+
8+
struct AppEnvironmentVariable {
9+
uint32_t name_index;
10+
uint32_t value_index;
11+
};
12+
13+
extern "C" {
14+
extern const uint32_t __naot_android_app_environment_variable_count;
15+
extern const AppEnvironmentVariable __naot_android_app_environment_variables[];
16+
extern const char __naot_android_app_environment_variable_contents[];
17+
}
18+
19+
void HostEnvironment::init () noexcept
20+
{
21+
if (__naot_android_app_environment_variable_count == 0) {
22+
return;
23+
}
24+
25+
log_debug (LOG_DEFAULT, "Setting {} environment variables", __naot_android_app_environment_variable_count);
26+
for (size_t i = 0; i < __naot_android_app_environment_variable_count; i++) {
27+
AppEnvironmentVariable const& env_var = __naot_android_app_environment_variables[i];
28+
const char *var_name = &__naot_android_app_environment_variable_contents[env_var.name_index];
29+
const char *var_value = &__naot_android_app_environment_variable_contents[env_var.value_index];
30+
31+
set_variable (var_name, var_value);
32+
}
33+
}

src/native/nativeaot/host/host.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <host/bridge-processing.hh>
22
#include <host/gc-bridge.hh>
3+
#include <host/host-environment.hh>
34
#include <host/host-nativeaot.hh>
45
#include <host/os-bridge.hh>
56
#include <runtime-base/logger.hh>
@@ -20,6 +21,7 @@ extern "C" {
2021
auto HostCommon::Java_JNI_OnLoad (JavaVM *vm, void *reserved) noexcept -> jint
2122
{
2223
Logger::init_logging_categories ();
24+
HostEnvironment::init ();
2325

2426
log_warn (LOG_ASSEMBLY, "{}", __PRETTY_FUNCTION__);
2527

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <cerrno>
4+
#include <cstdlib>
5+
#include <cstring>
6+
#include <string_view>
7+
8+
#include <runtime-base/logger.hh>
9+
10+
namespace xamarin::android {
11+
class HostEnvironment
12+
{
13+
public:
14+
static void init () noexcept;
15+
16+
static void set_variable (const char *name, const char *value) noexcept
17+
{
18+
log_debug (LOG_DEFAULT, " Variable {} = '{}'", name, value);
19+
if (::setenv (name, value, 1) < 0) {
20+
log_warn (LOG_DEFAULT, "Failed to set environment variable '{}': {}", name, ::strerror (errno));
21+
}
22+
}
23+
24+
static void set_variable (std::string_view const& name, std::string_view const& value) noexcept
25+
{
26+
set_variable (name.data (), value.data ());
27+
}
28+
};
29+
}

0 commit comments

Comments
 (0)