Skip to content

Commit

Permalink
Merge pull request #1 from nsoranzo/improve_for_muscle
Browse files Browse the repository at this point in the history
Improve Python code and generated Makefiles
  • Loading branch information
rcedgar committed Nov 17, 2021
2 parents f905eb6 + e9b7a6f commit 2efd85e
Showing 1 changed file with 92 additions and 103 deletions.
195 changes: 92 additions & 103 deletions vcxproj2makefile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/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)
print("***ERROR***", sys.argv[0], s, file=sys.stderr)
sys.exit(1)


LRT = False
DEBUG = False
Expand All @@ -16,12 +18,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 ProjFileName == None:
print("Project file not found in current directory", file=sys.stderr)
if FileName.endswith(".vcxproj"):
ProjFileName = FileName
elif FileName.endswith(".h"):
HdrNames.append(FileName)
if ProjFileName is None:
print("Project file not found in current directory", file=sys.stderr)

Fields = ProjFileName.split("/")
n = len(Fields)
Expand All @@ -30,120 +32,107 @@ def Die(s):
progname = Fields[0]

f = open("Makefile", "w")
fo = open("Makefile_osx", "w")

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)

CPPNames = []


def Out(s):
print(s, file=f)


CXXNames = []
CNames = []
File = open(ProjFileName)
while 1:
Line = File.readline()
if len(Line) == 0:
break
Line = Line.strip()
Line = Line.replace('"', '')
Line = Line.replace(' ', '')
# <ClCompile Include="betadiv.cpp" />
if Line.startswith("<ClCompileInclude"):
Fields = Line.split("=")
if len(Fields) != 2:
continue
FileName = Fields[1]
FileName = FileName.replace("/>", "")
if FileName.endswith(".cpp"):
FileName = FileName.replace(".cpp", "")
CPPNames.append(FileName)
elif FileName.endswith(".c"):
FileName = FileName.replace(".c", "")
CNames.append(FileName)

# <ProjectName>usearch</ProjectName>
elif Line.startswith("<ProjectName"):
Line = Line.replace("<ProjectName>", "")
Line = Line.replace("</ProjectName>", "")
progname = Line

assert len(CPPNames) > 0 or len(CNames) > 0
with open(ProjFileName) as File:
for Line in File:
Line = Line.strip()
Line = Line.replace('"', '')
Line = Line.replace(' ', '')
# <ClCompile Include="betadiv.cpp" />
if Line.startswith("<ClCompileInclude"):
Fields = Line.split("=")
if len(Fields) != 2:
continue
FileName = Fields[1]
FileName = FileName.replace("/>", "")
if FileName.endswith(".cpp"):
FileName = FileName.replace(".cpp", "")
CXXNames.append(FileName)
elif FileName.endswith(".c"):
FileName = FileName.replace(".c", "")
CNames.append(FileName)
# <ProjectName>usearch</ProjectName>
elif Line.startswith("<ProjectName"):
Line = Line.replace("<ProjectName>", "")
Line = Line.replace("</ProjectName>", "")
progname = Line

assert len(CXXNames) > 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]
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
Out("UNAME_S := $(shell uname -s)")

Out("CPP = ccache g++", Linux)
Out("CPP = g++", OSX)
Out("CPPOPTS = -fopenmp -ffast-math -msse -mfpmath=sse -O3 -DNDEBUG -c")
if CNames:
Out("")
Out("CC = gcc")
Out("CFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse")

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("")
Out("CXX = g++")
Out("CXXFLAGS = -O3 -DNDEBUG -fopenmp -ffast-math -msse -mfpmath=sse")

Out("LNK = g++")
Out("LNKOPTS = -O3 -fopenmp -pthread -lpthread -static", Linux)
Out("LNKOPTS = -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 = \\")
n = len(HdrNames)
for i in range(0, n):
Name = HdrNames[i]
Out(" %s \\" % Name)
for Name in sorted(HdrNames):
Out(" %s \\" % Name)

Out("")
Out("OBJS = \\")
n = len(CPPNames)
for i in range(0, n):
Name = CPPNames[i]
Out(" o/%s.o \\" % Name)
for Name in CXXNames:
Out(" o/%s.o \\" % Name)

n = len(CNames)
for i in range(0, n):
Name = CNames[i]
Out(" o/%s.o \\" % Name)
for Name in CNames:
Out(" o/%s.o \\" % Name)

Out("")
Out(".PHONY: clean")

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:
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))
if CNames:
Out("")
Out("o/%.o : %.c $(HDRS)")
Out(" $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<")

if CXXNames:
Out("")
Out("o/%.o : %.cpp $(HDRS)")
Out(" $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<")

Out("")
Out("clean:")
Out(" -rm -rf o/")

f.close()

0 comments on commit 2efd85e

Please sign in to comment.