Skip to content

Commit 15b3e59

Browse files
committed
Detect executable with file(1)
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 6fc8beb commit 15b3e59

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

.github/workflows/build-and-test.yaml

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ on: [push, pull_request]
1010

1111
jobs:
1212
build-and-test:
13-
runs-on: "ubuntu-24.04"
13+
runs-on: ${{ matrix.os }}
1414
strategy:
15+
fail-fast: false
1516
matrix:
1617
otp: ["25", "26", "27", "28"]
18+
os: ["ubuntu-24.04", "ubuntu-24.04-arm", "macos-26", "macos-15-intel"]
1719

1820
include:
1921
- otp: "25"
2022
make_jobs: "compile etest"
21-
rebar3_version: "3.24.0 "
23+
rebar3_version: "3.24.0"
2224
- otp: "26"
2325
make_jobs: "all"
2426
rebar3_version: "3.25.1"
@@ -31,20 +33,46 @@ jobs:
3133

3234
steps:
3335
# Builder info
34-
- name: "System info"
36+
- name: "Install deps"
37+
if: startsWith(matrix.os, 'macos-') && matrix.otp != '25'
3538
run: |
36-
echo "**uname:**"
37-
uname -a
38-
echo "**OTP version:**"
39-
cat $(dirname $(which erlc))/../releases/RELEASES || true
39+
brew update && HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install gperf ninja llvm erlang@${{ matrix.otp }} rebar3
40+
echo PATH="/usr/local/opt/llvm/bin:/opt/homebrew/opt/llvm/bin:$PATH" >> $GITHUB_ENV
41+
42+
- name: "Install deps"
43+
if: startsWith(matrix.os, 'macos-') && matrix.otp == '25'
44+
run: |
45+
brew update
46+
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install gperf ninja llvm erlang@${{ matrix.otp }}
47+
wget https://github.com/erlang/rebar3/releases/download/${{ matrix.rebar3_version }}/rebar3
48+
chmod +x rebar3
49+
for bin_dir in {/usr/local,/opt/homebrew}/opt/erlang@25/bin/ ; do
50+
if [ -e ${bin_dir} ]; then
51+
sudo cp rebar3 ${bin_dir}
52+
fi
53+
done
54+
echo PATH="/usr/local/opt/llvm/bin:/opt/homebrew/opt/llvm/bin:/usr/local/opt/erlang@${{ matrix.otp }}/bin:/opt/homebrew/opt/erlang@${{ matrix.otp }}/bin:$PATH" >> $GITHUB_ENV
55+
56+
- name: "Install deps"
57+
if: startsWith(matrix.os, 'ubuntu-')
58+
run: |
59+
sudo apt install -y make git gcc-14 g++-14 cmake gperf zlib1g-dev libmbedtls-dev ninja-build
4060
4161
# Setup OTP
4262
- name: "Setup OTP"
63+
if: startsWith(matrix.os, 'ubuntu-')
4364
uses: erlef/setup-beam@v1
4465
with:
4566
otp-version: ${{ matrix.otp }}
4667
rebar3-version: ${{ matrix.rebar3_version }}
4768

69+
- name: "System info"
70+
run: |
71+
echo "**uname:**"
72+
uname -a
73+
echo "**OTP version:**"
74+
cat $(dirname $(which erlc))/../releases/RELEASES || true
75+
4876
# Checkout AtomVM
4977
- name: "Checkout AtomVM"
5078
uses: actions/checkout@v5
@@ -53,18 +81,14 @@ jobs:
5381
path: 'atomvm'
5482
submodules: 'recursive'
5583

56-
- name: "Install deps"
57-
run: |
58-
sudo apt install -y make git gcc-14 g++-14 cmake gperf zlib1g-dev libmbedtls-dev ninja-build
59-
6084
- name: "Install AtomVM"
6185
working-directory: 'atomvm'
6286
run: |
6387
mkdir build
6488
cd build
6589
cmake -DAVM_BUILD_RUNTIME_ONLY=ON -G Ninja ..
66-
ninja
67-
sudo ninja install
90+
cmake --build .
91+
sudo cmake --build . -t install
6892
atomvm -v
6993
7094
# Setup

src/atomvm_escriptize_provider.erl

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -235,32 +235,24 @@ find_atomvm_binary() ->
235235

236236
%% @private
237237
resolve_atomvm_binary(Path) ->
238-
% Try to read the file to see if it's a shell script
239-
case file:read_file(Path) of
240-
{ok, Content} ->
241-
case binary:part(Content, 0, 9) of
242-
<<"#!/bin/sh">> ->
243-
% It's a shell script, parse it to find the actual binary
244-
% The standard wrapper is at /prefix/bin/atomvm
245-
% The actual binary is at /prefix/lib/atomvm/AtomVM
246-
Dir = filename:dirname(Path),
247-
Prefix = filename:dirname(Dir),
248-
ActualBinary = filename:join([Prefix, "lib", "atomvm", "AtomVM"]),
249-
case filelib:is_file(ActualBinary) of
250-
true -> {ok, ActualBinary};
251-
false -> {error, not_found}
252-
end;
253-
<<_:8, $e, $l, $f, _:40>> ->
254-
% It's the actual binary
255-
case filelib:is_file(Path) of
256-
true -> {ok, Path};
257-
false -> {error, not_found}
258-
end;
259-
_ ->
260-
{error, not_a_script}
238+
case string:trim(os:cmd("file -b --mime-type " ++ Path)) of
239+
"text/x-shellscript" ->
240+
% It's a shell script, suppose
241+
% The standard wrapper is at /prefix/bin/atomvm
242+
% The actual binary is at /prefix/lib/atomvm/AtomVM
243+
Dir = filename:dirname(Path),
244+
Prefix = filename:dirname(Dir),
245+
ActualBinary = filename:join([Prefix, "lib", "atomvm", "AtomVM"]),
246+
case filelib:is_file(ActualBinary) of
247+
true -> {ok, ActualBinary};
248+
false -> {error, not_found}
261249
end;
262-
{error, Reason} ->
263-
{error, Reason}
250+
"application/x-mach-binary" ->
251+
{ok, Path};
252+
"application/x-elf" ->
253+
{ok, Path};
254+
Other ->
255+
{error, {unexpected, Other}}
264256
end.
265257

266258
%% @private
@@ -281,7 +273,7 @@ find_objcopy() ->
281273
Tools =
282274
case os:type() of
283275
{unix, darwin} ->
284-
% On macOS, prefer llvm-objcopy and try MacPorts variants
276+
% On macOS, prefer llvm-objcopy (Homebrew) and try MacPorts variants
285277
[
286278
"llvm-objcopy",
287279
"llvm-objcopy-mp-21",
@@ -296,8 +288,11 @@ find_objcopy() ->
296288
["objcopy", "llvm-objcopy"]
297289
end,
298290
case find_first_executable(Tools) of
299-
{ok, Path} -> {ok, Path};
300-
error -> {error, "No objcopy tool found. Please install llvm or binutils"}
291+
{ok, Path} ->
292+
{ok, Path};
293+
error ->
294+
{error,
295+
"No objcopy tool found. Please install llvm or binutils and ensure objcopy or llvm-objcopy is on the PATH"}
301296
end.
302297

303298
%% @private

0 commit comments

Comments
 (0)