Skip to content

Commit 4b16aba

Browse files
committed
Normalize system_architecture strings
Normalize system_architecture to arch-vendor-os form. Use compiler target strings where available and normalize them to exactly three components across generic Unix, Emscripten, ESP32, RP2, and STM32. Add a shared CMake helper for normalization, keep platform-specific vendor tags for RP2 and STM32, and extend system_architecture tests for generic Unix and ESP32. Signed-off-by: Peter M <petermm@gmail.com>
1 parent e2dda4e commit 4b16aba

File tree

14 files changed

+166
-5
lines changed

14 files changed

+166
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ table.
120120
### Fixed
121121

122122
- ESP32: improved sntp sync speed from a cold boot.
123+
- `erlang:system_info(system_architecture)` now reports normalized `arch-vendor-os` strings
123124
- Fixed `gen_server` internal messages to match OTP so it works across erlang distribution
124125
- Utilize reserved `phy_init` partition on ESP32 to store wifi calibration for faster connections.
125126
- Support for zero count in `lists:duplicate/2`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#
2+
# This file is part of AtomVM.
3+
#
4+
# Copyright 2026 Peter M. <petermm@gmail.com>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
13+
#
14+
15+
function(avm_get_system_architecture_string out_var)
16+
set(options)
17+
set(one_value_args PLATFORM_VENDOR PLATFORM_OS)
18+
cmake_parse_arguments(PARSE_ARGV 1 AVM "${options}" "${one_value_args}" "")
19+
20+
execute_process(
21+
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
22+
OUTPUT_VARIABLE avm_raw_system_architecture
23+
OUTPUT_STRIP_TRAILING_WHITESPACE
24+
ERROR_QUIET
25+
)
26+
27+
if (avm_raw_system_architecture STREQUAL "")
28+
unset(${out_var} PARENT_SCOPE)
29+
return()
30+
endif()
31+
32+
string(REPLACE "-" ";" avm_system_architecture_parts "${avm_raw_system_architecture}")
33+
list(LENGTH avm_system_architecture_parts avm_system_architecture_length)
34+
35+
if (avm_system_architecture_length EQUAL 1)
36+
list(GET avm_system_architecture_parts 0 avm_architecture)
37+
if (DEFINED AVM_PLATFORM_VENDOR)
38+
set(avm_vendor "${AVM_PLATFORM_VENDOR}")
39+
else()
40+
set(avm_vendor "unknown")
41+
endif()
42+
set(avm_os "unknown")
43+
elseif (avm_system_architecture_length EQUAL 2)
44+
list(GET avm_system_architecture_parts 0 avm_architecture)
45+
if (DEFINED AVM_PLATFORM_VENDOR)
46+
set(avm_vendor "${AVM_PLATFORM_VENDOR}")
47+
else()
48+
set(avm_vendor "unknown")
49+
endif()
50+
list(GET avm_system_architecture_parts 1 avm_os)
51+
else()
52+
list(GET avm_system_architecture_parts 0 avm_architecture)
53+
list(GET avm_system_architecture_parts 1 avm_vendor)
54+
if (DEFINED AVM_PLATFORM_VENDOR AND (avm_vendor STREQUAL "none" OR avm_vendor STREQUAL "unknown"))
55+
set(avm_vendor "${AVM_PLATFORM_VENDOR}")
56+
endif()
57+
58+
list(REMOVE_AT avm_system_architecture_parts 0 1)
59+
string(REPLACE ";" "_" avm_os "${avm_system_architecture_parts}")
60+
endif()
61+
62+
if (DEFINED AVM_PLATFORM_OS)
63+
set(avm_os "${AVM_PLATFORM_OS}")
64+
endif()
65+
66+
string(REPLACE "-" "_" avm_architecture "${avm_architecture}")
67+
string(REPLACE "-" "_" avm_vendor "${avm_vendor}")
68+
string(REPLACE "-" "_" avm_os "${avm_os}")
69+
70+
set(${out_var} "${avm_architecture}-${avm_vendor}-${avm_os}" PARENT_SCOPE)
71+
endfunction()

src/libAtomVM/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ else()
310310
set(ATOMVM_VERSION ${ATOMVM_BASE_VERSION})
311311
endif()
312312

313+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
314+
if (AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH STREQUAL "")
315+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "unknown")
316+
endif()
317+
string(REPLACE "-" "_" AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "${AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH}")
318+
319+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "${CMAKE_SYSTEM_NAME}")
320+
if (AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS STREQUAL "")
321+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "unknown")
322+
endif()
323+
if (NOT CMAKE_SYSTEM_VERSION STREQUAL "")
324+
string(APPEND AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "_${CMAKE_SYSTEM_VERSION}")
325+
endif()
326+
string(REPLACE "-" "_" AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "${AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS}")
327+
328+
if (NOT DEFINED AVM_SYSTEM_ARCHITECTURE_STRING OR AVM_SYSTEM_ARCHITECTURE_STRING STREQUAL "")
329+
set(AVM_SYSTEM_ARCHITECTURE_STRING
330+
"${AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH}-unknown-${AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS}")
331+
endif()
332+
313333
# Add include to directory where avm_version.h is generated so targets linking
314334
# libAtomVM can access it
315335
target_include_directories(libAtomVM PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

src/libAtomVM/nifs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,13 +3073,11 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
30733073
return term_from_int11(sizeof(avm_float_t));
30743074
}
30753075
if (key == SYSTEM_ARCHITECTURE_ATOM) {
3076-
char buf[128];
3077-
snprintf(buf, 128, "%s-%s-%s", SYSTEM_NAME, SYSTEM_VERSION, SYSTEM_ARCHITECTURE);
3078-
size_t len = strnlen(buf, 128);
3076+
size_t len = sizeof(SYSTEM_ARCHITECTURE_STRING) - 1;
30793077
if (memory_ensure_free_opt(ctx, term_binary_heap_size(len), MEMORY_CAN_SHRINK) != MEMORY_GC_OK) {
30803078
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
30813079
}
3082-
return term_from_literal_binary((const uint8_t *) buf, len, &ctx->heap, ctx->global);
3080+
return term_from_literal_binary((const uint8_t *) SYSTEM_ARCHITECTURE_STRING, len, &ctx->heap, ctx->global);
30833081
}
30843082
if (key == ATOMVM_VERSION_ATOM) {
30853083
size_t len = strlen(ATOMVM_VERSION);

src/libAtomVM/version.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
#define SYSTEM_NAME "${CMAKE_SYSTEM_NAME}"
2222
#define SYSTEM_VERSION "${CMAKE_SYSTEM_VERSION}"
2323
#define SYSTEM_ARCHITECTURE "${CMAKE_SYSTEM_PROCESSOR}"
24+
#define SYSTEM_ARCHITECTURE_STRING "${AVM_SYSTEM_ARCHITECTURE_STRING}"
2425
#define ATOMVM_VERSION "${ATOMVM_VERSION}"

src/platforms/emscripten/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ add_executable(AtomVM main.c)
2424

2525
target_compile_features(AtomVM PUBLIC c_std_11)
2626

27+
include(SystemArchitecture)
28+
avm_get_system_architecture_string(AVM_SYSTEM_ARCHITECTURE_STRING)
29+
2730
add_subdirectory(../../../libAtomVM libAtomVM)
2831
target_link_libraries(AtomVM PUBLIC libAtomVM)
2932
target_compile_options(libAtomVM PUBLIC -O3 -fno-exceptions -fno-rtti -pthread -sINLINING_LIMIT -sUSE_ZLIB=1)

src/platforms/esp32/components/libatomvm/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ idf_component_register(INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../../lib
2424
# "pedantic" flag should be disabled rather poluting diagnostics with warnings caused from 3rd party
2525
option(AVM_PEDANTIC_WARNINGS "Pedantic compiler warnings" OFF)
2626

27+
include(SystemArchitecture)
28+
avm_get_system_architecture_string(AVM_SYSTEM_ARCHITECTURE_STRING PLATFORM_OS esp_idf)
29+
2730
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../libAtomVM" "libAtomVM")
2831

2932
# Add directory with platform_atomic.h if we mean to use it

src/platforms/esp32/test/main/test_erl_sources/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ compile_erlang(test_rtc_slow)
7474
compile_erlang(test_select)
7575
compile_erlang(test_socket)
7676
compile_erlang(test_ssl)
77+
compile_erlang(test_system_architecture)
7778
compile_erlang(test_time_and_processes)
7879
compile_erlang(test_twdt)
7980
compile_erlang(test_tz)
@@ -96,6 +97,7 @@ set(erlang_test_beams
9697
test_select.beam
9798
test_socket.beam
9899
test_ssl.beam
100+
test_system_architecture.beam
99101
test_time_and_processes.beam
100102
test_twdt.beam
101103
test_tz.beam
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2026 Peter M. <petermm@gmail.com>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_system_architecture).
22+
-export([start/0]).
23+
24+
start() ->
25+
SystemArchitecture = erlang:system_info(system_architecture),
26+
ok =
27+
case SystemArchitecture of
28+
<<"xtensa-esp-esp_idf">> ->
29+
ok;
30+
<<"riscv32-esp-esp_idf">> ->
31+
ok
32+
end,
33+
nomatch = binary:match(SystemArchitecture, <<"esp_idf-">>),
34+
ok.

src/platforms/esp32/test/main/test_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ TEST_CASE("test_time_and_processes", "[test_run]")
506506
TEST_ASSERT(term_to_int(ret_value) == 6);
507507
}
508508

509+
TEST_CASE("test_system_architecture", "[test_run]")
510+
{
511+
term ret_value = avm_test_case("test_system_architecture.beam");
512+
TEST_ASSERT(ret_value == OK_ATOM);
513+
}
514+
509515
TEST_CASE("test_tz", "[test_run]")
510516
{
511517
term ret_value = avm_test_case("test_tz.beam");

0 commit comments

Comments
 (0)