Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/aws/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
# define AWS_OS_LINUX
#endif

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
# define AWS_OS_BSD
#endif

#if defined(__ANDROID__)
# define AWS_OS_ANDROID
#endif
Expand Down
39 changes: 32 additions & 7 deletions include/aws/common/system_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ AWS_PUSH_SANE_WARNING_LEVEL
* Platform OS enumeration and their corresponding string representations.
*
* String mappings:
* - AWS_PLATFORM_OS_WINDOWS → "Windows" (Microsoft Windows family)
* - AWS_PLATFORM_OS_MAC → "macOS" (Apple desktop/laptop)
* - AWS_PLATFORM_OS_IOS → "iOS" (Apple mobile platforms, covers iOS, watchOS, tvOS,
* and other non-macOS Apple platforms)
* - AWS_PLATFORM_OS_ANDROID → "Android" (Google Android)
* - AWS_PLATFORM_OS_UNIX → "Unix" (Linux, BSD, other Unix-like)
* - AWS_PLATFORM_OS_WINDOWS → "Windows" (Microsoft Windows family)
* - AWS_PLATFORM_OS_MAC → "macOS" (Apple desktop/laptop)
* - AWS_PLATFORM_OS_IOS → "iOS" (Apple mobile devices)
* - AWS_PLATFORM_OS_TVOS → "tvOS" (Apple TV devices)
* - AWS_PLATFORM_OS_WATCHOS → "watchOS" (Apple Watch devices)
* - AWS_PLATFORM_OS_ANDROID → "Android" (Android)
* - AWS_PLATFORM_OS_BSD → "BSD" (FreeBSD, NetBSD, openBSD)
* - AWS_PLATFORM_OS_UNIX → "Unix" (Linux, other Unix-like)
*/
enum aws_platform_os {
AWS_PLATFORM_OS_WINDOWS,
AWS_PLATFORM_OS_MAC,
AWS_PLATFORM_OS_IOS,
AWS_PLATFORM_OS_TVOS,
AWS_PLATFORM_OS_WATCHOS,
AWS_PLATFORM_OS_ANDROID,
AWS_PLATFORM_OS_BSD,
AWS_PLATFORM_OS_UNIX,
};

Expand Down Expand Up @@ -87,7 +92,27 @@ size_t aws_system_environment_get_cpu_group_count(const struct aws_system_enviro
AWS_COMMON_API
enum aws_platform_os aws_get_platform_build_os(void);

/* Returns the OS this was built under as a string */
/* Returns the OS and architecture this was built under as a string, with format <OS>-<ARCH>
*
* Platform OS string constants - these are the string representations for each supported platform.
* String choices follow common industry conventions:
* - "Windows" - Microsoft Windows family
* - "macOS" - Apple macOS
* - "iOS" - Apple iOS
* - "tvOS" - Apple tvOS
* - "watchOS" - Apple watchOS
* - "Android" - Google Android mobile OS
* - "BSD" - BSD family (FreeBSD, NetBSD, OpenBSD)
* - "Unix" - Unix-like systems (Linux, unix-like etc.)
* - "Unknown" - Fallback for unrecognized platforms
*
* Architecture string constants:
* - "x86_32" - 32-bit Intel x86 architecture
* - "x86_64" - 64-bit Intel x86_64 architecture
* - "arm32" - 32-bit ARM architecture
* - "arm64" - 64-bit ARM architecture
* - "unknown" - Fallback for unrecognized architectures
*/
AWS_COMMON_API
struct aws_byte_cursor aws_get_platform_build_os_string(void);

Expand Down
9 changes: 7 additions & 2 deletions source/posix/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,16 @@ void aws_backtrace_log(int log_level) {
enum aws_platform_os aws_get_platform_build_os(void) {
#if defined(AWS_OS_MACOS)
return AWS_PLATFORM_OS_MAC;
// Other Apple platforms will be reported as iOS
#elif defined(AWS_OS_APPLE)
#elif defined(AWS_OS_IOS)
return AWS_PLATFORM_OS_IOS;
#elif defined(AWS_OS_TVOS)
return AWS_PLATFORM_OS_TVOS;
#elif defined(AWS_OS_WATCHOS)
return AWS_PLATFORM_OS_WATCHOS;
#elif defined(AWS_OS_ANDROID)
return AWS_PLATFORM_OS_ANDROID;
#elif defined(AWS_OS_BSD)
return AWS_PLATFORM_OS_BSD;
#else
return AWS_PLATFORM_OS_UNIX;
#endif
Expand Down
77 changes: 53 additions & 24 deletions source/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,67 @@ size_t aws_system_environment_get_cpu_group_count(const struct aws_system_enviro
return env->cpu_group_count;
}

/*
* Platform OS string constants - these are the string representations for each supported platform. String choices
* follow common industry conventions:
* - "Windows" - Microsoft Windows family
* - "macOS" - Apple macOS
* - "iOS" - Apple iOS (or other unknown Apple platform)
* - "Android" - Google Android mobile OS
* - "Unix" - Unix-like systems (Linux, BSD, etc.)
* - "Unknown" - Fallback for unrecognized platforms
*/
AWS_STATIC_STRING_FROM_LITERAL(s_windows_str, "Windows");
AWS_STATIC_STRING_FROM_LITERAL(s_macos_str, "macOS");
AWS_STATIC_STRING_FROM_LITERAL(s_ios_str, "iOS");
AWS_STATIC_STRING_FROM_LITERAL(s_android_str, "Android");
AWS_STATIC_STRING_FROM_LITERAL(s_unix_str, "Unix");
AWS_STATIC_STRING_FROM_LITERAL(s_unknown_str, "Unknown");
static uint8_t s_platform_string_buffer[32];
static struct aws_byte_buf s_platform_buf =
{.buffer = s_platform_string_buffer, .capacity = sizeof(s_platform_string_buffer), .len = 0, .allocator = NULL};

struct aws_byte_cursor aws_get_platform_build_os_string(void) {
if (s_platform_buf.len != 0) {
return aws_byte_cursor_from_buf(&s_platform_buf);
}

enum aws_platform_os os = aws_get_platform_build_os();
struct aws_byte_cursor os_str;
struct aws_byte_cursor arch_str;

switch (os) {
case AWS_PLATFORM_OS_WINDOWS:
return aws_byte_cursor_from_string(s_windows_str);
os_str = aws_byte_cursor_from_c_str("Windows");
break;
case AWS_PLATFORM_OS_MAC:
return aws_byte_cursor_from_string(s_macos_str);
os_str = aws_byte_cursor_from_c_str("macOS");
break;
case AWS_PLATFORM_OS_IOS:
return aws_byte_cursor_from_string(s_ios_str);
os_str = aws_byte_cursor_from_c_str("iOS");
break;
case AWS_PLATFORM_OS_TVOS:
os_str = aws_byte_cursor_from_c_str("tvOS");
break;
case AWS_PLATFORM_OS_WATCHOS:
os_str = aws_byte_cursor_from_c_str("watchOS");
break;
case AWS_PLATFORM_OS_ANDROID:
return aws_byte_cursor_from_string(s_android_str);
os_str = aws_byte_cursor_from_c_str("Android");
break;
case AWS_PLATFORM_OS_BSD:
os_str = aws_byte_cursor_from_c_str("BSD");
break;
case AWS_PLATFORM_OS_UNIX:
return aws_byte_cursor_from_string(s_unix_str);
os_str = aws_byte_cursor_from_c_str("Unix");
break;
default:
os_str = aws_byte_cursor_from_c_str("unknown");
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "Unknown platform OS enum value: %d", (int)os);
}
/* Handle invalid enum values */
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "Unknown platform OS enum value: %d", (int)os);
return aws_byte_cursor_from_string(s_unknown_str);

#ifdef AWS_ARCH_INTEL
arch_str = aws_byte_cursor_from_c_str("x86_32");
#elif defined(AWS_ARCH_INTEL_64)
arch_str = aws_byte_cursor_from_c_str("x86_64");
#elif defined(AWS_ARCH_ARM64)
arch_str = aws_byte_cursor_from_c_str("arm64");
#elif defined(AWS_ARCH_ARM32)
arch_str = aws_byte_cursor_from_c_str("arm32");
#else
arch_str = aws_byte_cursor_from_c_str("unknown");
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "Unknown platform architecture.");
#endif

aws_byte_buf_reset(&s_platform_buf, false);
aws_byte_buf_append(&s_platform_buf, &os_str);
const struct aws_byte_cursor s_dash = aws_byte_cursor_from_c_str("-");
aws_byte_buf_append(&s_platform_buf, &s_dash);
aws_byte_buf_append(&s_platform_buf, &arch_str);

return aws_byte_cursor_from_buf(&s_platform_buf);
}
53 changes: 45 additions & 8 deletions tests/system_info_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ static int s_test_platform_build_os_fn(struct aws_allocator *allocator, void *ct

#if defined(AWS_OS_MACOS)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_MAC);
#elif defined(AWS_OS_APPLE)
#elif defined(AWS_OS_IOS)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_IOS);
#elif defined(AWS_OS_TVOS)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_TVOS);
#elif defined(AWS_OS_WATCHOS)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_WATCHOS);
#elif defined(AWS_OS_ANDROID)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_ANDROID);
#elif defined(_WIN32)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_WINDOWS);
#elif defined(AWS_OS_BSD)
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_BSD);
#else
ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_UNIX);
#endif
Expand All @@ -141,16 +147,47 @@ static int s_test_platform_build_os_string_fn(struct aws_allocator *allocator, v
ASSERT_TRUE(aws_byte_cursor_is_valid(&os_string));
ASSERT_TRUE(os_string.len > 0);

/* Verify OS-ARCH format */
struct aws_byte_cursor dash = aws_byte_cursor_from_c_str("-");
struct aws_byte_cursor found_dash;
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&os_string, &dash, &found_dash));

struct aws_byte_cursor expected_os = aws_byte_cursor_from_c_str("Unknown-");

/* Verify OS part */
#if defined(AWS_OS_MACOS)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "macOS"));
#elif defined(AWS_OS_APPLE)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "iOS"));
expected_os = aws_byte_cursor_from_c_str("macOS-");
#elif defined(AWS_OS_IOS)
expected_os = aws_byte_cursor_from_c_str("iOS-");
#elif defined(AWS_OS_TVOS)
expected_os = aws_byte_cursor_from_c_str("tvOS-");
#elif defined(AWS_OS_WATCHOS)
expected_os = aws_byte_cursor_from_c_str("watchOS-");
#elif defined(AWS_OS_ANDROID)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Android"));
#elif defined(_WIN32)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Windows"));
expected_os = aws_byte_cursor_from_c_str("Android-");
#elif defined(AWS_OS_WINDOWS)
expected_os = aws_byte_cursor_from_c_str("Windows-");
#elif defined(AWS_OS_BSD)
expected_os = aws_byte_cursor_from_c_str("BSD-");
#else
expected_os = aws_byte_cursor_from_c_str("Unix-");
#endif
ASSERT_TRUE(aws_byte_cursor_starts_with(&os_string, &expected_os));

/* Verify architecture part exists after dash */
size_t dash_pos = found_dash.ptr - os_string.ptr;
aws_byte_cursor_advance(&os_string, dash_pos + 1);

#if defined(AWS_ARCH_INTEL)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "x86_32"));
#elif defined(AWS_ARCH_INTEL_64)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "x86_64"));
#elif defined(AWS_ARCH_ARM64)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "arm64"));
#elif defined(AWS_ARCH_ARM32)
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "arm32"));
#else
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Unix"));
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "unknown"));
#endif

return 0;
Expand Down