@@ -1315,7 +1315,7 @@ def add_lto_flags(self) -> bool:
13151315 Add LTO flags to pioarduino-build.py.
13161316
13171317 Adds -flto=auto to CCFLAGS, CFLAGS, CXXFLAGS and -flto to LINKFLAGS
1318- in the Arduino build script.
1318+ in the Arduino build script. Flags are inserted right after the opening bracket.
13191319
13201320 Returns:
13211321 bool: True if successful, False otherwise
@@ -1333,68 +1333,41 @@ def add_lto_flags(self) -> bool:
13331333 original_content = content
13341334 modified = False
13351335
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 :]
1336+ # Add -flto=auto to CCFLAGS right after the opening bracket
1337+ if 'CCFLAGS=[' in content :
1338+ ccflags_start = content .find ('CCFLAGS=[' )
1339+ ccflags_section_start = ccflags_start + len ('CCFLAGS=[' )
1340+ content = (content [:ccflags_section_start ] +
1341+ '\n "-flto=auto",' +
1342+ content [ccflags_section_start :])
13461343 modified = True
13471344
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 :]
1345+ # Add -flto=auto to CFLAGS right after the opening bracket
1346+ if 'CFLAGS=[' in content :
1347+ cflags_start = content .find ('CFLAGS=[' )
1348+ cflags_section_start = cflags_start + len ('CFLAGS=[' )
1349+ content = (content [:cflags_section_start ] +
1350+ '\n "-flto=auto",' +
1351+ content [cflags_section_start :])
13561352 modified = True
13571353
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 :]
1354+ # Add -flto=auto to CXXFLAGS right after the opening bracket
1355+ if 'CXXFLAGS=[' in content :
1356+ cxxflags_start = content .find ('CXXFLAGS=[' )
1357+ cxxflags_section_start = cxxflags_start + len ('CXXFLAGS=[' )
1358+ content = (content [:cxxflags_section_start ] +
1359+ '\n "-flto=auto",' +
1360+ content [cxxflags_section_start :])
13661361 modified = True
13671362
1368- # Pattern to find LINKFLAGS list and add -flto if not present
1369- # Note: remove_no_lto_flags() needs to be called before
1363+ # Add -flto to LINKFLAGS right after the opening bracket
13701364 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"'
1393- else :
1394- new_linkflags = new_linkflags [:- 1 ] + ',\n "-flto"\n ]'
1395-
1396- content = content [:start ] + new_linkflags + content [end :]
1397- modified = True
1365+ linkflags_start = content .find ('LINKFLAGS=[' )
1366+ linkflags_section_start = linkflags_start + len ('LINKFLAGS=[' )
1367+ content = (content [:linkflags_section_start ] +
1368+ '\n "-flto",' +
1369+ content [linkflags_section_start :])
1370+ modified = True
13981371
13991372 if modified :
14001373 with open (build_py_path , 'w' , encoding = 'utf-8' ) as f :
0 commit comments