Skip to content

Instantly share code, notes, and snippets.

@jiganomegsdfdf
Last active May 15, 2024 21:12
Show Gist options
  • Save jiganomegsdfdf/c1e9f6079d5de1f12696d53fd53d2565 to your computer and use it in GitHub Desktop.
Save jiganomegsdfdf/c1e9f6079d5de1f12696d53fd53d2565 to your computer and use it in GitHub Desktop.
Help adapting anything like "techpack/camera" for successful building using gcc 6 (PostmarketOS), if any error came up, that not script fault, that means OEM provide not so good sources, but it will be easy to fix. Usage: python3 AbsoluteToRelative.py "PATH where to replace all includes to relative one". THAT SCRIPT ALSO GOES IN SUBDIRECTORIES!!!
import os
import subprocess
import sys
def find(name, path):
for root, dirs, files in os.walk(path):
if name in files:
return os.path.join(root, name)
arr = []
for root, dirs, files in os.walk(sys.argv[1]):
path = root.split(os.sep)
for file in files:
with open(root + os.sep + file, "r") as f:
arr = f.readlines()
for i in range(0,len(arr)):
line = arr[i]
# find include lines and check if already relative
if(('#include "' in line and '.h"' in line) and "/" not in line):
toreplaceline = line.split('#include "')[1].split('"')[0]
elif(('#include <' in line and '.h>' in line) and "/" not in line):
toreplaceline = line.split('#include <')[1].split('>')[0]
else:
continue
#find file to be replaced(need no same names in any subdirectories to work properly)
toreplace = find(toreplaceline, sys.argv[1])
#find relative path
try:
rel = os.path.relpath(toreplace,root + os.sep + file).replace("../", "", 1)
#check if it for some reason including himself
if(rel == "."): rel = file
except:
continue
arr[i] = '#include "' + rel + '"\n'
with open(root + os.sep + file,'w') as f:
f.writelines(arr)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment