From 038cd5f96d33fe49759be49134f0ecf6c1c35f6b Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Fri, 24 Jun 2011 02:10:32 +0200 Subject: Got it to generate a very simple ebuild for uzbl --- cli.py | 4 +- filetypes/makefilecom.py | 32 +++++++++++---- filetypes/makefiles.py | 103 +++++++++++++++++++++++++++++++++++++++++------ linkdeps.py | 8 +++- scanfiles.py | 24 +++++++---- 5 files changed, 141 insertions(+), 30 deletions(-) diff --git a/cli.py b/cli.py index 6188020..a3eece3 100755 --- a/cli.py +++ b/cli.py @@ -52,11 +52,11 @@ else: srcdir = args.dir dltype = "www" -(inclst,binaries,targets) = scanfiles.scanproject(srcdir,"makefile") +(inclst,binaries,incpaths,targets) = scanfiles.scanproject(srcdir,"makefile") packages = set() print(binaries) for dep in inclst[0]: - packages.add(linkdeps.deptopackage(dep)[0]) + packages.add(linkdeps.deptopackage(dep,incpaths)[0]) ebuildgen.genebuild([],packages,dltype,args.dir,targets,binaries) diff --git a/filetypes/makefilecom.py b/filetypes/makefilecom.py index 555747a..a600c79 100644 --- a/filetypes/makefilecom.py +++ b/filetypes/makefilecom.py @@ -2,6 +2,7 @@ from ply import lex from ply import yacc import glob import os +from subprocess import getstatusoutput def expand(lst,variables): newlst = [] @@ -133,13 +134,19 @@ def com_interp(string,variables): """ p[0] = "" if len(p) == 4: - for item in expand(variables[p[2]],variables): - p[0] += item + " " - p[0] = p[0][:-1] + if p[2] in variables: + for item in expand(variables[p[2]],variables): + p[0] += item + " " + p[0] = p[0][:-1] + else: + p[0] = "" elif len(p) == 5: - for item in expand(variables[p[3]],variables): - p[1] += item + " " - p[0] = p[1][:-1] + if p[3] in variables: + for item in expand(variables[p[3]],variables): + p[1] += item + " " + p[0] = p[1][:-1] + else: + p[0] = "" elif len(p) == 3: p[0] = p[1] + p[2] else: @@ -330,7 +337,16 @@ def wildcard(inputlst,variables): return glob.glob(command[0]) def shell(inputlst,variables): - return ["dummy shell command"] + command = "" + retlst = [] + for item in expand(inputlst,variables): + command += item + " " + (status,returnstr) = getstatusoutput(command) + if status: + print("Error with command" + command) + for item in returnstr.split(): + retlst.append(item) + return retlst def notdir(inputlst,variables): #strip the dir from the file name if isinstance(inputlst[0],list): @@ -351,5 +367,5 @@ funcdict = { "notdir" : notdir, } -#print(com_interp("(foreach var,hej hopp,$(var))",{"x":["y","z"], "y":[".py"], "z":["u"]})) +#print(com_interp("(shell pkg-config --cflags libsoup-2.4 $(x))",{"x":["gtk+-2.0"], "y":[".py"], "z":["u"]})) diff --git a/filetypes/makefiles.py b/filetypes/makefiles.py index d451541..d2057fd 100644 --- a/filetypes/makefiles.py +++ b/filetypes/makefiles.py @@ -1,5 +1,6 @@ from ply import lex from ply import yacc +import glob from filetypes.makefilecom import expand def scanmakefile(makefile): @@ -15,7 +16,6 @@ def scanmakefile(makefile): "TEXT", "COMMAND", "ENDTAB", - "LIT", "SPACE", ) @@ -103,9 +103,10 @@ def scanmakefile(makefile): t.lexer.lineno += 1 pass - def t_LIT(t): + def t_litteral(t): r"\\." t.value = t.value[1] #take the literal char + t.type = "TEXT" return t def t_COL(t): @@ -188,11 +189,13 @@ def scanmakefile(makefile): if len(p) == 6: rulelst = convtargets(p[2],p[4],targets,variables) for rule in rulelst: + rule = findfiles(rule,variables) #Implicit rule (path search) rule.append(p[5]) targets.append(rule) else: rulelst = convtargets(p[2],[],targets,variables) for rule in rulelst: + rule = findfiles(rule,variables) #Implicit rule (path search) rule.append(p[4]) targets.append(rule) @@ -204,13 +207,17 @@ def scanmakefile(makefile): if len(p) == 5: rulelst = convtargets(p[2],p[4],targets,variables) for rule in rulelst: - rule.append([]) + rule,newtars = imprules(rule,targets,variables) targets.append(rule) + for tar in newtars: + targets.append(tar) else: rulelst = convtargets(p[2],[],targets,variables) for rule in rulelst: - rule.append([]) + rule,newtars = imprules(rule,targets,variables) targets.append(rule) + for tar in newtars: + targets.append(tar) def p_peq(p): #immediate if peq was defined as immediate before else deferred """ @@ -293,10 +300,8 @@ def scanmakefile(makefile): def p_textstr(p): """ - textstr : textstr LIT - | textstr TEXT + textstr : textstr TEXT | TEXT - | LIT """ if len(p) == 3: p[0] = p[1] + p[2] @@ -332,7 +337,7 @@ def scanmakefile(makefile): def p_error(p): - print("syntax error at '%s'" % p.type,p.lexpos) + print("syntax error at '%s'" % p.type,p.value) pass yacc.yacc() @@ -343,11 +348,8 @@ def scanmakefile(makefile): # print(target) #print(variables) - return targets - + return targets,variables -#immediate -#deferred def convtargets(tarlist,deplist,targets,variables): finaltars = [] @@ -372,6 +374,83 @@ def convtargets(tarlist,deplist,targets,variables): finaltars.append([target,deps]) return finaltars +def findfiles(rule,variables): #check if deps exists, if not look for them in VPATH. + newtarget = [] + newdeps = [] + if "VPATH" in variables: #if vpath isn't defined this it's useless to search + if glob.glob(rule[0]): #check target + newtarget.append(rule[0]) + else: #search for it + matches = [] + for path in variables["VPATH"]: + matches += glob.glob(path + "/" + rule[0]) + if matches: + newtarget.append(matches[0]) + else: + newtarget.append(rule[0]) + + for dep in rule[1]: + if glob.glob(dep): + newdeps.append(dep) + else: #search for it + matches = [] + for path in variables["VPATH"]: + matches += glob.glob(path + "/" + dep) + if matches: + newdeps.append(matches[0]) + else: + newdeps.append(dep) + + newtarget.append(newdeps) + return newtarget #newrule + else: + return rule + +def find(searchstr,paths): + matches = [] + for path in paths: + matches += glob.glob(path + "/" + searchstr) + + if len(matches) > 1: + matches = [matches[0]] + return matches + +def imprules(rule,targets,variables): #Implicit Rules + if len(rule[0].split(".")) == 1: #this is not a *.* file + deps_type = set() #.o for example + for dep in rule[1]: + if len(dep.split(".")) == 2: + deps_type.add(dep.split(".")[1]) + else: + deps_type.add("notype") + if len(deps_type) == 1 and "o" in deps_type: + searchpaths = ["./"] + if "VPATH" in variables: + searchpaths += variables["VPATH"] + matches = [] + matches = find(rule[0] + ".c",searchpaths) + if matches: + newtargets = [] + newdeps = [] + newtargets.append(rule[0] + ".o") + newdeps.append(matches[0]) + matches = [] + for dep in rule[1]: + matches += find(dep.split(".")[0] + ".c",searchpaths) + if len(matches) == len(rule[1]): + newtargets += rule[1] + newdeps += matches + newtars = [] + for index in range(len(newtargets)): + newtars.append([newtargets[index],[newdeps[index]],[["(CC)"], ["(CFLAGS)"], ["(CPPFLAGS)"], "-c"]]) + + rule.append([["(CC)"], ["(LDFLAGS)"], "n.o", ["(LOADLIBES)"], ["(LDLIBS)"]]) + return rule,newtars + + rule = findfiles(rule,variables) + rule.append([]) + return rule,[] + #file="Makefile2" #with open(file, encoding="utf-8", errors="replace") as inputfile: diff --git a/linkdeps.py b/linkdeps.py index 415d549..c31813e 100644 --- a/linkdeps.py +++ b/linkdeps.py @@ -1,8 +1,10 @@ import os from subprocess import getstatusoutput -def deptopackage(dep): +def deptopackage(dep,addpaths): + print(dep) incpaths = ["/usr/include", "/usr/local/include"] + incpaths += addpaths depname = os.path.split(dep)[1] (statuscode,packagestr) = getstatusoutput("qfile -C " + depname) @@ -22,5 +24,9 @@ def deptopackage(dep): if len(package) > 1: print("more than one matching package where found!") + print(package) + if not package: + print("not matching package found withing the include paths!") + package = ["dummy"] return package diff --git a/scanfiles.py b/scanfiles.py index 03ac76b..6ed6a80 100644 --- a/scanfiles.py +++ b/scanfiles.py @@ -2,6 +2,7 @@ import os import glob from filetypes.ctypefiles import scanincludes from filetypes.makefiles import scanmakefile +from filetypes.makefilecom import expand def scandirfor(dir, filetypes): files = [] @@ -18,10 +19,11 @@ def scanmakefiledeps(makefile): olddir = os.getcwd() makefile = openfile(makefile) binaries = set() #the binaries that the .o file create - filestoscan = [] + filestoscan = set() impfiles = [] #look for these files + moptions = [] #make options scan these for -I... flags os.chdir(curdir) #so makefiles commands can execute in the correct dir - targets = scanmakefile(makefile) + targets,variables = scanmakefile(makefile) deps = targets[0][1] #Use first make target while deps != []: newdeps = [] @@ -31,16 +33,24 @@ def scanmakefiledeps(makefile): newdeps += target[1] if ".o" in dep or dep in impfiles: impfiles += target[1] + moptions += target[2] elif ".o" in target[1][0]: binaries.add(target[0]) + moptions += target[2] deps = newdeps - #impfiles.sort() + #print(impfiles) for impfile in impfiles: - filestoscan.append(curdir + impfile) + filestoscan.add(curdir + impfile) + + incflags = set() + for item in expand(moptions,variables): + if item[0:2] == "-I": + incflags.add(item[2:]) + #print(filestoscan) os.chdir(olddir) - return filestoscan,binaries,targets + return filestoscan,binaries,incflags,targets def scanfilelist(filelist): global_hfiles = set() @@ -62,8 +72,8 @@ def scanproject(dir,projecttype): mfile = scandirfor(dir, filestolookfor)[0] #use first file found print(mfile) - (scanlist,binaries,targets) = scanmakefiledeps(mfile) - return scanfilelist(scanlist),binaries,targets + (scanlist,binaries,incflags,targets) = scanmakefiledeps(mfile) + return scanfilelist(scanlist),binaries,incflags,targets def openfile(file): try: -- cgit v1.2.3-65-gdbad