@@ -1271,3 +1271,126 @@ def print_changes_summary(self) -> None:
12711271 session, useful for build reporting and debugging.
12721272 """
12731273 self .logger .print_changes_summary ()
1274+
1275+ def remove_lto_flags (self ) -> bool :
1276+ """
1277+ Remove all -fno-lto flags from pioarduino-build.py.
1278+
1279+ Removes all occurrences of -fno-lto from CCFLAGS, CFLAGS, CXXFLAGS,
1280+ and LINKFLAGS in the Arduino build script.
1281+
1282+ Returns:
1283+ bool: True if successful, False otherwise
1284+ """
1285+ build_py_path = str (Path (self .config .arduino_libs_mcu ) / "pioarduino-build.py" )
1286+
1287+ if not os .path .exists (build_py_path ):
1288+ print (f"Warning: pioarduino-build.py not found at { build_py_path } " )
1289+ return False
1290+
1291+ try :
1292+ with open (build_py_path , 'r' , encoding = 'utf-8' ) as f :
1293+ content = f .read ()
1294+
1295+ # Remove all -fno-lto flags
1296+ modified_content = re .sub (r'["\']?-fno-lto["\']?,?\s*' , '' , content )
1297+
1298+ # Clean up any resulting empty strings or double commas
1299+ modified_content = re .sub (r',\s*,' , ',' , modified_content )
1300+ modified_content = re .sub (r'\[\s*,' , '[' , modified_content )
1301+ modified_content = re .sub (r',\s*\]' , ']' , modified_content )
1302+
1303+ with open (build_py_path , 'w' , encoding = 'utf-8' ) as f :
1304+ f .write (modified_content )
1305+
1306+ print ("*** Removed -fno-lto flags from pioarduino-build.py ***" )
1307+ return True
1308+
1309+ except (IOError , OSError ) as e :
1310+ print (f"Error removing -fno-lto flags: { e } " )
1311+ return False
1312+
1313+ def add_lto_flags (self ) -> bool :
1314+ """
1315+ Add LTO flags to pioarduino-build.py.
1316+
1317+ Adds -flto=auto to CCFLAGS, CFLAGS, CXXFLAGS and -flto to LINKFLAGS
1318+ in the Arduino build script.
1319+
1320+ Returns:
1321+ bool: True if successful, False otherwise
1322+ """
1323+ build_py_path = str (Path (self .config .arduino_libs_mcu ) / "pioarduino-build.py" )
1324+
1325+ if not os .path .exists (build_py_path ):
1326+ print (f"Warning: pioarduino-build.py not found at { build_py_path } " )
1327+ return False
1328+
1329+ try :
1330+ with open (build_py_path , 'r' , encoding = 'utf-8' ) as f :
1331+ lines = f .readlines ()
1332+
1333+ modified = False
1334+ new_lines = []
1335+
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 '
1364+ else :
1365+ line = line + '\n '
1366+ modified = True
1367+
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 '
1376+ modified = True
1377+
1378+ new_lines .append (line )
1379+
1380+ if modified :
1381+ with open (build_py_path , 'w' , encoding = 'utf-8' ) as f :
1382+ f .writelines (new_lines )
1383+
1384+ print ("*** Added LTO flags to pioarduino-build.py ***" )
1385+ print (" CCFLAGS: -flto=auto" )
1386+ print (" CFLAGS: -flto=auto" )
1387+ print (" CXXFLAGS: -flto=auto" )
1388+ print (" LINKFLAGS: -flto" )
1389+ return True
1390+ else :
1391+ print ("*** LTO flags already present in pioarduino-build.py ***" )
1392+ return True
1393+
1394+ except (IOError , OSError ) as e :
1395+ print (f"Error adding LTO flags: { e } " )
1396+ return False
0 commit comments