From 0828eaeca7c50c888d1222eedd5d57c0a9950e00 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 15 Jul 2021 22:34:34 +0100 Subject: [PATCH 1/5] Indent with spaces instead of tabs as recommended by PEP-8. --- vcxproj2makefile.py | 123 ++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 62 deletions(-) diff --git a/vcxproj2makefile.py b/vcxproj2makefile.py index 948d345..115d3a2 100644 --- a/vcxproj2makefile.py +++ b/vcxproj2makefile.py @@ -6,8 +6,8 @@ import re def Die(s): - print("***ERROR***", sys.argv[0], s, file=sys.stderr) - sys.exit(1) + print("***ERROR***", sys.argv[0], s, file=sys.stderr) + sys.exit(1) LRT = False DEBUG = False @@ -16,12 +16,12 @@ def Die(s): ProjFileName = None HdrNames = [] for FileName in os.listdir("."): - if FileName.endswith(".vcxproj"): - ProjFileName = FileName - elif FileName.endswith(".h"): - HdrNames.append(FileName) + if FileName.endswith(".vcxproj"): + ProjFileName = FileName + elif FileName.endswith(".h"): + HdrNames.append(FileName) if ProjFileName == None: - print("Project file not found in current directory", file=sys.stderr) + print("Project file not found in current directory", file=sys.stderr) Fields = ProjFileName.split("/") n = len(Fields) @@ -35,61 +35,60 @@ def Die(s): Linux = "Linux" OSX = "OSX" def Out(s, OS = "ALL"): - global f, fo - if OS == "ALL": - print(s, file=f) - print(s, file=fo) - elif OS == "Linux": - print(s, file=f) - elif OS == "OSX": - print(s, file=fo) - else: - Die("OS %s", OS) + global f, fo + if OS == "ALL": + print(s, file=f) + print(s, file=fo) + elif OS == "Linux": + print(s, file=f) + elif OS == "OSX": + print(s, file=fo) + else: + Die("OS %s", OS) CPPNames = [] CNames = [] File = open(ProjFileName) while 1: - Line = File.readline() - if len(Line) == 0: - break - Line = Line.strip() - Line = Line.replace('"', '') - Line = Line.replace(' ', '') + Line = File.readline() + if len(Line) == 0: + break + Line = Line.strip() + Line = Line.replace('"', '') + Line = Line.replace(' ', '') # - if Line.startswith("", "") - if FileName.endswith(".cpp"): - FileName = FileName.replace(".cpp", "") - CPPNames.append(FileName) - elif FileName.endswith(".c"): - FileName = FileName.replace(".c", "") - CNames.append(FileName) - - # usearch - elif Line.startswith("", "") - Line = Line.replace("", "") - progname = Line + if Line.startswith("", "") + if FileName.endswith(".cpp"): + FileName = FileName.replace(".cpp", "") + CPPNames.append(FileName) + elif FileName.endswith(".c"): + FileName = FileName.replace(".c", "") + CNames.append(FileName) + # usearch + elif Line.startswith("", "") + Line = Line.replace("", "") + progname = Line assert len(CPPNames) > 0 or len(CNames) > 0 binname = progname if DEBUG: - binname += "d" + binname += "d" if binname.find("/") > 0: - Fields = binname.split("/") - N = len(Fields) - binname = Fields[N-1] + Fields = binname.split("/") + N = len(Fields) + binname = Fields[N-1] if binname.find("\\") > 0: - Fields = binname.split("\\") - N = len(Fields) - binname = Fields[N-1] + Fields = binname.split("\\") + N = len(Fields) + binname = Fields[N-1] # CNames CPPNames @@ -111,27 +110,27 @@ def Out(s, OS = "ALL"): Out("HDRS = \\") n = len(HdrNames) for i in range(0, n): - Name = HdrNames[i] - Out(" %s \\" % Name) + Name = HdrNames[i] + Out(" %s \\" % Name) Out("") Out("OBJS = \\") n = len(CPPNames) for i in range(0, n): - Name = CPPNames[i] - Out(" o/%s.o \\" % Name) + Name = CPPNames[i] + Out(" o/%s.o \\" % Name) n = len(CNames) for i in range(0, n): - Name = CNames[i] - Out(" o/%s.o \\" % Name) + Name = CNames[i] + Out(" o/%s.o \\" % Name) Out("") Out("%s : o/ $(OBJS)" % progname) if LRT: - Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s -lrt" % progname) + Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s -lrt" % progname) else: - Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s" % progname) + Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s" % progname) Out(" strip -d o/%s" % progname) Out("") @@ -139,11 +138,11 @@ def Out(s, OS = "ALL"): Out(" mkdir -p o/") for Name in CNames: - Out("") - Out("o/%s.o : %s.c $(HDRS)" % (Name, Name)) - Out(" $(CC) $(CCOPTS) -o o/%s.o %s.c" % (Name, Name)) + Out("") + Out("o/%s.o : %s.c $(HDRS)" % (Name, Name)) + Out(" $(CC) $(CCOPTS) -o o/%s.o %s.c" % (Name, Name)) for Name in CPPNames: - Out("") - Out("o/%s.o : %s.cpp $(HDRS)" % (Name, Name)) - Out(" $(CPP) $(CPPOPTS) -o o/%s.o %s.cpp" % (Name, Name)) + Out("") + Out("o/%s.o : %s.cpp $(HDRS)" % (Name, Name)) + Out(" $(CPP) $(CPPOPTS) -o o/%s.o %s.cpp" % (Name, Name)) From 14f750604dc59bbf460efa7825f034f002c49ea3 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 15 Jul 2021 22:42:49 +0100 Subject: [PATCH 2/5] Code cleanups --- vcxproj2makefile.py | 94 +++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/vcxproj2makefile.py b/vcxproj2makefile.py index 115d3a2..0761b4d 100644 --- a/vcxproj2makefile.py +++ b/vcxproj2makefile.py @@ -1,14 +1,16 @@ #!/usr/bin/python2 from __future__ import print_function -import sys + import os -import re +import sys + def Die(s): print("***ERROR***", sys.argv[0], s, file=sys.stderr) sys.exit(1) + LRT = False DEBUG = False SVNVER = False @@ -20,7 +22,7 @@ def Die(s): ProjFileName = FileName elif FileName.endswith(".h"): HdrNames.append(FileName) -if ProjFileName == None: +if ProjFileName is None: print("Project file not found in current directory", file=sys.stderr) Fields = ProjFileName.split("/") @@ -34,46 +36,45 @@ def Die(s): Linux = "Linux" OSX = "OSX" -def Out(s, OS = "ALL"): - global f, fo + + +def Out(s, OS="ALL"): if OS == "ALL": print(s, file=f) print(s, file=fo) - elif OS == "Linux": + elif OS == Linux: print(s, file=f) - elif OS == "OSX": + elif OS == OSX: print(s, file=fo) else: Die("OS %s", OS) + CPPNames = [] CNames = [] -File = open(ProjFileName) -while 1: - Line = File.readline() - if len(Line) == 0: - break - Line = Line.strip() - Line = Line.replace('"', '') - Line = Line.replace(' ', '') - # - if Line.startswith("", "") - if FileName.endswith(".cpp"): - FileName = FileName.replace(".cpp", "") - CPPNames.append(FileName) - elif FileName.endswith(".c"): - FileName = FileName.replace(".c", "") - CNames.append(FileName) - # usearch - elif Line.startswith("", "") - Line = Line.replace("", "") - progname = Line +with open(ProjFileName) as File: + for Line in File: + Line = Line.strip() + Line = Line.replace('"', '') + Line = Line.replace(' ', '') + # + if Line.startswith("", "") + if FileName.endswith(".cpp"): + FileName = FileName.replace(".cpp", "") + CPPNames.append(FileName) + elif FileName.endswith(".c"): + FileName = FileName.replace(".c", "") + CNames.append(FileName) + # usearch + elif Line.startswith("", "") + Line = Line.replace("", "") + progname = Line assert len(CPPNames) > 0 or len(CNames) > 0 @@ -81,14 +82,10 @@ def Out(s, OS = "ALL"): if DEBUG: binname += "d" -if binname.find("/") > 0: - Fields = binname.split("/") - N = len(Fields) - binname = Fields[N-1] -if binname.find("\\") > 0: - Fields = binname.split("\\") - N = len(Fields) - binname = Fields[N-1] +if "/" in binname: + binname = binname.split("/")[-1] +if "\\" in binname: + binname = binname.split("\\")[-1] # CNames CPPNames @@ -108,21 +105,15 @@ def Out(s, OS = "ALL"): Out("") Out("HDRS = \\") -n = len(HdrNames) -for i in range(0, n): - Name = HdrNames[i] +for Name in sorted(HdrNames): Out(" %s \\" % Name) Out("") Out("OBJS = \\") -n = len(CPPNames) -for i in range(0, n): - Name = CPPNames[i] +for Name in CPPNames: Out(" o/%s.o \\" % Name) -n = len(CNames) -for i in range(0, n): - Name = CNames[i] +for Name in CNames: Out(" o/%s.o \\" % Name) Out("") @@ -146,3 +137,6 @@ def Out(s, OS = "ALL"): Out("") Out("o/%s.o : %s.cpp $(HDRS)" % (Name, Name)) Out(" $(CPP) $(CPPOPTS) -o o/%s.o %s.cpp" % (Name, Name)) + +f.close() +fo.close() From da4c79b200dc7a14ae6d0095f6b156c03592236b Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 15 Jul 2021 22:46:20 +0100 Subject: [PATCH 3/5] Improve generated Makefiles - Simplify by using pattern rules and automatic variables - Use standard variable names - Fix default target to include 'o/' - Drop ccache --- vcxproj2makefile.py | 55 ++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/vcxproj2makefile.py b/vcxproj2makefile.py index 0761b4d..89c63c7 100644 --- a/vcxproj2makefile.py +++ b/vcxproj2makefile.py @@ -50,7 +50,7 @@ def Out(s, OS="ALL"): Die("OS %s", OS) -CPPNames = [] +CXXNames = [] CNames = [] with open(ProjFileName) as File: for Line in File: @@ -66,7 +66,7 @@ def Out(s, OS="ALL"): FileName = FileName.replace("/>", "") if FileName.endswith(".cpp"): FileName = FileName.replace(".cpp", "") - CPPNames.append(FileName) + CXXNames.append(FileName) elif FileName.endswith(".c"): FileName = FileName.replace(".c", "") CNames.append(FileName) @@ -76,7 +76,7 @@ def Out(s, OS="ALL"): Line = Line.replace("", "") progname = Line -assert len(CPPNames) > 0 or len(CNames) > 0 +assert len(CXXNames) > 0 or len(CNames) > 0 binname = progname if DEBUG: @@ -87,21 +87,20 @@ def Out(s, OS="ALL"): if "\\" in binname: binname = binname.split("\\")[-1] -# CNames CPPNames - -Out("CPP = ccache g++", Linux) -Out("CPP = g++", OSX) -Out("CPPOPTS = -fopenmp -ffast-math -msse -mfpmath=sse -O3 -DNDEBUG -c") +if CNames: + Out("CC = gcc", Linux) + Out("CC = gcc", OSX) + Out("CFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") + Out("") -Out("") -Out("CC = ccache gcc", Linux) -Out("CC = gcc", OSX) -Out("CCOPTS = -fopenmp -ffast-math -msse -mfpmath=sse -O3 -DNDEBUG -c") -Out("") +if CXXNames: + Out("CXX = g++", Linux) + Out("CXX = g++", OSX) + Out("CXXFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") + Out("") -Out("LNK = g++") -Out("LNKOPTS = -O3 -fopenmp -pthread -lpthread -static", Linux) -Out("LNKOPTS = -O3 -fopenmp -pthread -lpthread", OSX) +Out("LDFLAGS = -O3 -fopenmp -pthread -lpthread -static", Linux) +Out("LDFLAGS = -O3 -fopenmp -pthread -lpthread", OSX) Out("") Out("HDRS = \\") @@ -110,33 +109,33 @@ def Out(s, OS="ALL"): Out("") Out("OBJS = \\") -for Name in CPPNames: +for Name in CXXNames: Out(" o/%s.o \\" % Name) for Name in CNames: Out(" o/%s.o \\" % Name) Out("") -Out("%s : o/ $(OBJS)" % progname) -if LRT: - Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s -lrt" % progname) -else: - Out(" $(LNK) $(LNKOPTS) $(OBJS) -o o/%s" % progname) +Out("o/%s : o/ $(OBJS)" % progname) +Out(" %s $(LDFLAGS) $(OBJS) -o $@%s" % ( + "$(CXX)" if CXXNames else "$(CC)", + " -lrt" if LRT else "", +)) Out(" strip -d o/%s" % progname) Out("") Out("o/ :") Out(" mkdir -p o/") -for Name in CNames: +if CNames: Out("") - Out("o/%s.o : %s.c $(HDRS)" % (Name, Name)) - Out(" $(CC) $(CCOPTS) -o o/%s.o %s.c" % (Name, Name)) + Out("o/%.o : %.c $(HDRS)") + Out(" $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<") -for Name in CPPNames: +if CXXNames: Out("") - Out("o/%s.o : %s.cpp $(HDRS)" % (Name, Name)) - Out(" $(CPP) $(CPPOPTS) -o o/%s.o %s.cpp" % (Name, Name)) + Out("o/%.o : %.cpp $(HDRS)") + Out(" $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<") f.close() fo.close() From 8960729612959213e30b0350984fab32e884cd9b Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 15 Jul 2021 23:29:49 +0100 Subject: [PATCH 4/5] Add `clean` target --- vcxproj2makefile.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vcxproj2makefile.py b/vcxproj2makefile.py index 89c63c7..5252d20 100644 --- a/vcxproj2makefile.py +++ b/vcxproj2makefile.py @@ -115,6 +115,9 @@ def Out(s, OS="ALL"): for Name in CNames: Out(" o/%s.o \\" % Name) +Out("") +Out(".PHONY: clean") + Out("") Out("o/%s : o/ $(OBJS)" % progname) Out(" %s $(LDFLAGS) $(OBJS) -o $@%s" % ( @@ -137,5 +140,9 @@ def Out(s, OS="ALL"): Out("o/%.o : %.cpp $(HDRS)") Out(" $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<") +Out("") +Out("clean:") +Out(" -rm -rf o/") + f.close() fo.close() From e9b7a6f1938b26cd456bcab4e1b02f8ba793fa4f Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Fri, 16 Jul 2021 00:52:55 +0100 Subject: [PATCH 5/5] Generate a single Makefile for both Linux and macOS. --- vcxproj2makefile.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/vcxproj2makefile.py b/vcxproj2makefile.py index 5252d20..45db431 100644 --- a/vcxproj2makefile.py +++ b/vcxproj2makefile.py @@ -32,22 +32,10 @@ def Die(s): progname = Fields[0] f = open("Makefile", "w") -fo = open("Makefile_osx", "w") -Linux = "Linux" -OSX = "OSX" - -def Out(s, OS="ALL"): - if OS == "ALL": - print(s, file=f) - print(s, file=fo) - elif OS == Linux: - print(s, file=f) - elif OS == OSX: - print(s, file=fo) - else: - Die("OS %s", OS) +def Out(s): + print(s, file=f) CXXNames = [] @@ -87,20 +75,23 @@ def Out(s, OS="ALL"): if "\\" in binname: binname = binname.split("\\")[-1] +Out("UNAME_S := $(shell uname -s)") + if CNames: - Out("CC = gcc", Linux) - Out("CC = gcc", OSX) - Out("CFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") Out("") + Out("CC = gcc") + Out("CFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") if CXXNames: - Out("CXX = g++", Linux) - Out("CXX = g++", OSX) - Out("CXXFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") Out("") + Out("CXX = g++") + Out("CXXFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse") -Out("LDFLAGS = -O3 -fopenmp -pthread -lpthread -static", Linux) -Out("LDFLAGS = -O3 -fopenmp -pthread -lpthread", OSX) +Out("") +Out("LDFLAGS = -O3 -fopenmp -pthread -lpthread") +Out("ifeq ($(UNAME_S),Linux)") +Out(" LDFLAGS += -static") +Out("endif") Out("") Out("HDRS = \\") @@ -145,4 +136,3 @@ def Out(s, OS="ALL"): Out(" -rm -rf o/") f.close() -fo.close()