#! /usr/bin/env python # dymmakefile.py --- generate a Makefile from Dymore.dsp to use on Linux # author :Jinwei Shen # this script mainly does two things # 1. extract C source files used in Dymore.dsp. Not all C files in the # pgrm subdirectories are used in Dymore.dsp. # 2. correct the file and directory name case in Dymore.dsp because # file name in Dymore.dsp under MS windows is case insensitive. # run this script in "pgrm" directory without argument such as: # dymmakefile.py >|Makefile import string, sys, sre, os def readdata(filename): """ return a list of file lines """ f=open(filename, 'r') data = f.read() f.close() data = data.split('\n') return data def matchfile(filename): """ find a existing file which may not have the same case as filename BUG: just use the first exist file, ignore rest """ (dirname, basename) = os.path.split(filename) # find the correct dirname cmd = "find -iname '" + dirname + "'" dirname = os.popen(cmd).read().strip() if len(dirname) == 0: sys.stderr.write("matchfile: did not find dirname\n") # find the correct filename cmd = "find " + dirname + " -iname '" + basename + "'" basename = os.popen(cmd).read().strip() if len(basename) == 0: sys.stderr.write("matchfile: did not find basename \n") else: return basename.strip("./") class TabFile: def __init__(self, filename): """ init """ self.filename = filename self.headstr() self.tailstr() self.srcstr() self.procsrcstr() print self.HEADSTR self.printsrcstr() print self.TAILSTR def srcstr(self): """ get Makefile source files str """ data = readdata(self.filename) self.src = []; for L in data: L = L.strip('\r') # dos line end n = sre.match(r"^SOURCE=\.\\(.*\.c)$", L) if n >=0: self.src.append(n.group(1).replace('\\', '/')) def procsrcstr(self): """ process Makefile source files """ self.src.sort() self.foundsrc = [] for filename in self.src: if os.access(filename, os.R_OK): self.foundsrc.append(filename) else: self.foundsrc.append(matchfile(filename)) self.foundsrc.sort() def printsrcstr(self): """ print Makefile source files """ SRCSTR = "dymore_exe_C_SRCS = \\" HEADSRCSTR = " " print SRCSTR for filename in self.foundsrc: print HEADSRCSTR + filename + " \\" def headstr(self): """ assign Makefile head str """ self.HEADSTR = r""" ### Generated by Winemaker EXES = dymore2.exe ### Common settings # CEXTRA = -fast -mp -prec_div # this is for debugging floatingpoint problem # CEXTRA = -O3 -funroll-loops -malign-double # best gcc CEXTRA = -march=pentium4 -O3 -ffast-math -funroll-loops -malign-double -mfpmath=sse -msse2 -mmmx CEXTRA = -O3 -funroll-loops -malign-double # CEXTRA = -g # icc CEXTRA = -O3 -tpp7 -xW -axW -parallel -ipo -unroll -openmp INCLUDE_PATH = -I./ -I./HEAD ### dymore.exe sources and settings """ def tailstr(self): """ assign Makefile tail str """ self.TAILSTR = r""" dymore_exe_LDFLAGS = -lm dymore_exe_OBJS = $(dymore_exe_C_SRCS:.c=.o) ### Global source lists C_SRCS = $(dymore_exe_C_SRCS) CXX_SRCS = $(dymore_exe_CXX_SRCS) ### Tools CC = gcc CXX = gcc ### Generic targets all: $(EXES:%.exe=%) ### Build rules .PHONY: all clean # Implicit rules .SUFFIXES: .cpp DEFINCL = $(INCLUDE_PATH) $(DEFINES) $(OPTIONS) .c.o: $(CC) -c $(CFLAGS) $(CEXTRA) $(DEFINCL) -o $@ $< .cpp.o: $(CXX) -c $(CXXFLAGS) $(CXXEXTRA) $(DEFINCL) -o $@ $< .cxx.o: $(CXX) -c $(CXXFLAGS) $(CXXEXTRA) $(DEFINCL) -o $@ $< # Rules for cleaning CLEAN_FILES = core *.il \ \\\#*\\\# *~ *% .\\\#* clean:: $(RM) $(CLEAN_FILES) $(C_SRCS:.c=.o) $(C_SRCS:.c=.il) $(CXX_SRCS:.cpp=.o) # $(RM) $(EXES:%.exe=%) ### Target specific build rules $(EXES:%.exe=%): $(dymore_exe_OBJS) $(CC) $(dymore_exe_LDFLAGS) $(CEXTRA) -o $@ $(dymore_exe_OBJS) """ def main(): """ main proc """ filename = "Dymore.dsp" # main call to the class file = TabFile(filename) if __name__=='__main__': main()