diff --git a/include/aws/common/platform.h b/include/aws/common/platform.h index 9742860fb..d40b36bc5 100644 --- a/include/aws/common/platform.h +++ b/include/aws/common/platform.h @@ -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 diff --git a/include/aws/common/system_info.h b/include/aws/common/system_info.h index ebe8bd67d..0c65a3629 100644 --- a/include/aws/common/system_info.h +++ b/include/aws/common/system_info.h @@ -15,18 +15,21 @@ 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_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_ANDROID, + AWS_PLATFORM_OS_BSD, AWS_PLATFORM_OS_UNIX, }; @@ -87,7 +90,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 - + * + * 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); diff --git a/source/posix/system_info.c b/source/posix/system_info.c index 2d957f734..eefde298b 100644 --- a/source/posix/system_info.c +++ b/source/posix/system_info.c @@ -468,11 +468,14 @@ 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_ANDROID) return AWS_PLATFORM_OS_ANDROID; +#elif defined(AWS_OS_BSD) + return AWS_PLATFORM_OS_BSD; #else return AWS_PLATFORM_OS_UNIX; #endif diff --git a/source/system_info.c b/source/system_info.c index 8c1dceada..3e328cd92 100644 --- a/source/system_info.c +++ b/source/system_info.c @@ -79,38 +79,64 @@ 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_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); } diff --git a/tests/system_info_tests.c b/tests/system_info_tests.c index a87e93468..d15cd869f 100644 --- a/tests/system_info_tests.c +++ b/tests/system_info_tests.c @@ -118,12 +118,16 @@ 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_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 @@ -141,16 +145,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;