Skip to content

Commit 452cf0c

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 f60c037 commit 452cf0c

14 files changed

Lines changed: 172 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.7.0-alpha.1] - Unreleased
8+
9+
### Added
10+
### Changed
11+
### Fixed
12+
- `erlang:system_info(system_architecture)` now reports normalized `arch-vendor-os` strings
13+
714
## [0.7.0-alpha.0] - 2026-03-20
815

916
### Added
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
@@ -320,6 +320,26 @@ else()
320320
set(ATOMVM_VERSION ${ATOMVM_BASE_VERSION})
321321
endif()
322322

323+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
324+
if (AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH STREQUAL "")
325+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "unknown")
326+
endif()
327+
string(REPLACE "-" "_" AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH "${AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH}")
328+
329+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "${CMAKE_SYSTEM_NAME}")
330+
if (AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS STREQUAL "")
331+
set(AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "unknown")
332+
endif()
333+
if (NOT CMAKE_SYSTEM_VERSION STREQUAL "")
334+
string(APPEND AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "_${CMAKE_SYSTEM_VERSION}")
335+
endif()
336+
string(REPLACE "-" "_" AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS "${AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS}")
337+
338+
if (NOT DEFINED AVM_SYSTEM_ARCHITECTURE_STRING OR AVM_SYSTEM_ARCHITECTURE_STRING STREQUAL "")
339+
set(AVM_SYSTEM_ARCHITECTURE_STRING
340+
"${AVM_SYSTEM_ARCHITECTURE_FALLBACK_ARCH}-unknown-${AVM_SYSTEM_ARCHITECTURE_FALLBACK_OS}")
341+
endif()
342+
323343
# Add include to directory where avm_version.h is generated so targets linking
324344
# libAtomVM can access it
325345
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
@@ -3173,13 +3173,11 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
31733173
return term_from_int11(sizeof(avm_float_t));
31743174
}
31753175
if (key == SYSTEM_ARCHITECTURE_ATOM) {
3176-
char buf[128];
3177-
snprintf(buf, 128, "%s-%s-%s", SYSTEM_NAME, SYSTEM_VERSION, SYSTEM_ARCHITECTURE);
3178-
size_t len = strnlen(buf, 128);
3176+
size_t len = sizeof(SYSTEM_ARCHITECTURE_STRING) - 1;
31793177
if (memory_ensure_free_opt(ctx, term_binary_heap_size(len), MEMORY_CAN_SHRINK) != MEMORY_GC_OK) {
31803178
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
31813179
}
3182-
return term_from_literal_binary((const uint8_t *) buf, len, &ctx->heap, ctx->global);
3180+
return term_from_literal_binary((const uint8_t *) SYSTEM_ARCHITECTURE_STRING, len, &ctx->heap, ctx->global);
31833181
}
31843182
if (key == ATOMVM_VERSION_ATOM) {
31853183
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
@@ -29,6 +29,9 @@ idf_component_register(INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../../lib
2929
# "pedantic" flag should be disabled rather poluting diagnostics with warnings caused from 3rd party
3030
option(AVM_PEDANTIC_WARNINGS "Pedantic compiler warnings" OFF)
3131

32+
include(SystemArchitecture)
33+
avm_get_system_architecture_string(AVM_SYSTEM_ARCHITECTURE_STRING PLATFORM_OS esp_idf)
34+
3235
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../libAtomVM" "libAtomVM")
3336

3437
# 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
@@ -502,6 +502,12 @@ TEST_CASE("test_time_and_processes", "[test_run]")
502502
TEST_ASSERT(term_to_int(ret_value) == 6);
503503
}
504504

505+
TEST_CASE("test_system_architecture", "[test_run]")
506+
{
507+
term ret_value = avm_test_case("test_system_architecture.beam");
508+
TEST_ASSERT(ret_value == OK_ATOM);
509+
}
510+
505511
TEST_CASE("test_tz", "[test_run]")
506512
{
507513
term ret_value = avm_test_case("test_tz.beam");

0 commit comments

Comments
 (0)