Skip to content

Commit 07597c6

Browse files
committed
Adds detection of libffi and sdl2 on macos using arm processor
1 parent cade7b4 commit 07597c6

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

builder/macOS.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
mpy_cross as _mpy_cross
1313
)
1414

15-
1615
from . import unix
16+
from . import spawn
1717

1818

1919
unix.REAL_PORT = 'macOS'
@@ -58,7 +58,81 @@ def build_sdl(_):
5858
unix.build_sdl = build_sdl
5959

6060

61+
def is_homebrew_arm(cmd):
62+
ret_val, output = spawn(cmd, out_to_screen=False)
63+
if ret_val:
64+
if cmd[0][0] == '/opt/homebrew/bin/brew':
65+
raise RuntimeError('Unable to locate homebrew installation')
66+
67+
return is_homebrew_arm([['/opt/homebrew/bin/brew', 'config']])
68+
69+
data = {line.split(':', 1)[0].strip(): line.split(':', 1)[1].strip() for
70+
line in output.split('\n')}
71+
72+
if 'macOS' not in data:
73+
raise RuntimeError('Unable to determine Homebrew CPU type')
74+
75+
if 'arm64' in data['macOS']:
76+
if 'Rosetta 2' not in data:
77+
if cmd[0][0] != '/opt/homebrew/bin/brew':
78+
return is_homebrew_arm([['/opt/homebrew/bin/brew', 'config']])
79+
80+
raise RuntimeError('Unable to determine Homebrew platform')
81+
82+
if data['Rosetta 2'] == 'true':
83+
if cmd[0][0] != '/opt/homebrew/bin/brew':
84+
return is_homebrew_arm([['/opt/homebrew/bin/brew', 'config']])
85+
86+
raise RuntimeError('Unable to locate Homebrew for Arm processors.')
87+
88+
return True, cmd[0][0]
89+
90+
return False, cmd[0][0]
91+
92+
93+
6194
def submodules():
95+
is_arm, brew_path = is_homebrew_arm([['brew', 'config']])
96+
97+
if is_arm:
98+
ret, out = spawn([[brew_path, 'info', 'libffi']], out_to_screen=False)
99+
if ret:
100+
print(out)
101+
sys.exit(ret)
102+
103+
if 'Installed\n' not in out:
104+
raise RuntimeError('libffi is not installed')
105+
106+
out = out.split('Installed\n', 1)[-1]
107+
alt_path = out.split('(', 1)[0].strip().split('Cellar', 1)[0]
108+
109+
if 'export LDFLAGS=' in out:
110+
out = out.split('export LDFLAGS="', 1)[-1]
111+
ldflags = out.split('"', 1)[0]
112+
else:
113+
ldflags = f'-L{alt_path}opt/libffi/lib'
114+
115+
if 'export CPPFLAGS=' in out:
116+
out = out.split('export CPPFLAGS="', 1)[-1]
117+
cflags = out.split('"', 1)[0]
118+
else:
119+
cflags = f'-I{alt_path}opt/libffi/include'
120+
121+
ret, out = spawn([[brew_path, 'info', 'sdl2']], out_to_screen=False)
122+
123+
if ret:
124+
print(out)
125+
sys.exit(ret)
126+
127+
if 'Installed\n' not in out:
128+
raise RuntimeError('sdl2 is not installed')
129+
130+
ldflags += f' -L{alt_path}lib'
131+
cflags += f' -I{alt_path}include'
132+
133+
os.environ['LDFLAGS'] = f'"{ldflags}"'
134+
os.environ['CFLAGS'] = f'"{cflags}"'
135+
62136
berkeley_db = os.path.abspath('lib/micropython/lib/berkeley-db-1.xx/README')
63137

64138
if not os.path.exists(berkeley_db):

0 commit comments

Comments
 (0)