Skip to content

Commit dec9f8f

Browse files
committed
fix add lto function
1 parent 888eba5 commit dec9f8f

File tree

1 file changed

+62
-43
lines changed

1 file changed

+62
-43
lines changed

builder/frameworks/component_manager.py

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,58 +1328,77 @@ def add_lto_flags(self) -> bool:
13281328

13291329
try:
13301330
with open(build_py_path, 'r', encoding='utf-8') as f:
1331-
lines = f.readlines()
1331+
content = f.read()
13321332

1333+
original_content = content
13331334
modified = False
1334-
new_lines = []
13351335

1336-
for line in lines:
1337-
# Add LTO flags to compiler flags
1338-
if 'CCFLAGS=' in line and '-flto' not in line:
1339-
line = line.rstrip()
1340-
if line.endswith(','):
1341-
line = line[:-1] + ', "-flto=auto",\n'
1342-
elif line.endswith(']'):
1343-
line = line[:-1] + ', "-flto=auto"]\n'
1344-
else:
1345-
line = line + '\n'
1346-
modified = True
1347-
1348-
elif 'CFLAGS=' in line and 'CCFLAGS' not in line and '-flto' not in line:
1349-
line = line.rstrip()
1350-
if line.endswith(','):
1351-
line = line[:-1] + ', "-flto=auto",\n'
1352-
elif line.endswith(']'):
1353-
line = line[:-1] + ', "-flto=auto"]\n'
1354-
else:
1355-
line = line + '\n'
1356-
modified = True
1357-
1358-
elif 'CXXFLAGS=' in line and '-flto' not in line:
1359-
line = line.rstrip()
1360-
if line.endswith(','):
1361-
line = line[:-1] + ', "-flto=auto",\n'
1362-
elif line.endswith(']'):
1363-
line = line[:-1] + ', "-flto=auto"]\n'
1336+
# Pattern to find CCFLAGS list and add -flto=auto if not present
1337+
if 'CCFLAGS=[' in content and '"-flto' not in content[content.find('CCFLAGS=['):content.find('],', content.find('CCFLAGS=['))]:
1338+
# Find the closing bracket for CCFLAGS
1339+
start = content.find('CCFLAGS=[')
1340+
end = content.find('],', start) + 1
1341+
ccflags_section = content[start:end]
1342+
1343+
# Add -flto=auto before the closing bracket
1344+
new_ccflags = ccflags_section.replace('],', ',\n "-flto=auto"\n ],')
1345+
content = content[:start] + new_ccflags + content[end:]
1346+
modified = True
1347+
1348+
# Pattern to find CFLAGS list and add -flto=auto if not present
1349+
if 'CFLAGS=[' in content and '"-flto' not in content[content.find('CFLAGS=['):content.find('],', content.find('CFLAGS=['))]:
1350+
start = content.find('CFLAGS=[')
1351+
end = content.find('],', start) + 1
1352+
cflags_section = content[start:end]
1353+
1354+
new_cflags = cflags_section.replace('],', ',\n "-flto=auto"\n ],')
1355+
content = content[:start] + new_cflags + content[end:]
1356+
modified = True
1357+
1358+
# Pattern to find CXXFLAGS list and add -flto=auto if not present
1359+
if 'CXXFLAGS=[' in content and '"-flto' not in content[content.find('CXXFLAGS=['):content.find('],', content.find('CXXFLAGS=['))]:
1360+
start = content.find('CXXFLAGS=[')
1361+
end = content.find('],', start) + 1
1362+
cxxflags_section = content[start:end]
1363+
1364+
new_cxxflags = cxxflags_section.replace('],', ',\n "-flto=auto"\n ],')
1365+
content = content[:start] + new_cxxflags + content[end:]
1366+
modified = True
1367+
1368+
# Pattern to find LINKFLAGS list and add -flto if not present
1369+
# Note: remove_no_lto_flags() needs to be called before
1370+
if 'LINKFLAGS=[' in content:
1371+
start = content.find('LINKFLAGS=[')
1372+
# Find the end of LINKFLAGS - look for the matching bracket
1373+
bracket_count = 0
1374+
pos = start + len('LINKFLAGS=[') - 1
1375+
while pos < len(content):
1376+
if content[pos] == '[':
1377+
bracket_count += 1
1378+
elif content[pos] == ']':
1379+
bracket_count -= 1
1380+
if bracket_count == 0:
1381+
end = pos + 1
1382+
break
1383+
pos += 1
1384+
1385+
linkflags_section = content[start:end]
1386+
1387+
# Check if -flto is already present
1388+
if '"-flto"' not in linkflags_section:
1389+
# Add -flto before the closing bracket
1390+
new_linkflags = linkflags_section.rstrip()
1391+
if new_linkflags.endswith(','):
1392+
new_linkflags = new_linkflags + '\n "-flto"'
13641393
else:
1365-
line = line + '\n'
1366-
modified = True
1394+
new_linkflags = new_linkflags[:-1] + ',\n "-flto"\n ]'
13671395

1368-
elif 'LINKFLAGS=' in line and '-flto' not in line:
1369-
line = line.rstrip()
1370-
if line.endswith(','):
1371-
line = line[:-1] + ', "-flto",\n'
1372-
elif line.endswith(']'):
1373-
line = line[:-1] + ', "-flto"]\n'
1374-
else:
1375-
line = line + '\n'
1396+
content = content[:start] + new_linkflags + content[end:]
13761397
modified = True
1377-
1378-
new_lines.append(line)
13791398

13801399
if modified:
13811400
with open(build_py_path, 'w', encoding='utf-8') as f:
1382-
f.writelines(new_lines)
1401+
f.write(content)
13831402

13841403
print("*** Added LTO flags to pioarduino-build.py ***")
13851404
print(" CCFLAGS: -flto=auto")

0 commit comments

Comments
 (0)