Skip to content

Commit

Permalink
Support for src and build directories, cleaned up and unified some Ma…
Browse files Browse the repository at this point in the history
…kefile generation code
  • Loading branch information
Melinysh committed Apr 16, 2016
1 parent 63394f1 commit 8168d06
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions pymake.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from ConfigParser import SafeConfigParser

## Global variables
VERSION = "0.4.4"
VERSION = "0.5"
flags = ""
outputFile = ""
directory = ""
compiler = ""
installPath = ""
builddir = ""
srcdir = ""
configFile = ""
fileType = ""
verbose = False
Expand All @@ -21,14 +23,16 @@ def debugPrint(*args):
print("")

def parseCommandline():
global flags, outputFile, directory, compiler, installPath, configFile, verbose, fileType
global flags, outputFile, directory, compiler, installPath, builddir, srcdir, configFile, verbose, fileType
# Get commandline arguments
parser = OptionParser(usage="Usage: pymake.py [ -cdfihotvx ]", version="PyMake Version " + VERSION)
parser.add_option("-c", "--compiler", dest="compiler", help="set compiler to use. Default: PyMake will look at your files and guess, if it can't then it will use gcc", default="")
parser.add_option("-d", "--directory", dest="directory", help="directory for pymake to create Makefile for. Default: ./", default="./")
parser = OptionParser(usage="Usage: pymake.py [ -bcdfihostvx ]", version="PyMake Version " + VERSION)
parser.add_option("-b", "--build-dir", dest="builddir", help="set build directory for project to use. Default: .", default=".")
parser.add_option("-c", "--compiler", dest="compiler", help="set compiler to use. Default: PyMake will look at your files and guess, if it can't then it will use gcc", default="")
parser.add_option("-d", "--directory", dest="directory", help="directory for pymake to create Makefile for. Default: .", default=".")
parser.add_option("-f", "--flags", dest="flags", help="flags for the compiler and typed within quotes", default="")
parser.add_option("-i", "--install-dir", dest="installPath", help="directory for 'make install'. Default: /usr/local/bin", default="/usr/local/bin")
parser.add_option("-o", "--output-target", dest="outputFile", help="output file name from compiler. Default: a.out", default="a.out")
parser.add_option("-o", "--output-target", dest="outputFile", help="output file name from compiler. Default: a.out", default="a.out")
parser.add_option("-s", "--source-dir", dest="srcdir", help="set source directory for project to use. Default: .", default=".")
parser.add_option("-t", "--file-type", dest="fileType", help="the file type of your source files (ex. c, cpp, go). Default: pymake will look at your files and guess", default = "")
parser.add_option("-v", dest="verbose", help="enable verbose output", action="store_true")
parser.add_option("-x", "--config-file", dest="configFile", help="path to pymake config file. Default: ~/.pymake.cfg", default="~/.pymake.cfg")
Expand All @@ -39,6 +43,8 @@ def parseCommandline():
directory = options.directory
compiler = options.compiler
installPath = options.installPath
builddir = options.builddir
srcdir = options.srcdir
configFile = options.configFile
verbose = options.verbose
fileType = options.fileType
Expand Down Expand Up @@ -74,7 +80,8 @@ def parseConfig(fileType):


def files():
allFiles = [f for f in os.listdir('.') if os.path.isfile(f)]
print(os.getcwd())
allFiles = [f for f in os.listdir(os.getcwd() + "/" + srcdir) if os.path.isfile(srcdir + "/" +f)]
sourceFiles = []
for f in allFiles:
if "." in f and isSourceFile(f):
Expand Down Expand Up @@ -137,53 +144,34 @@ def flagsForCompiler(compilerName):
return flagVars


def goMakefile():
fileContents = "\nall: $(TARGET)\n"

# Main compilation
fileContents += "$(TARGET): $(SOURCES)\n"
fileContents += "\t$(PYMAKE_COMPILER) $(PYMAKE_COMPILER_FLAGS) -o $(TARGET) $^\n\n"

## Clean
fileContents += "\n"
fileContents += "clean:" + "\n"
fileContents += "\t" + "-rm $(TARGET)\n"
def generateFileContents(fileType, compilerName):
fileContents = "# Generated by pymake version " + VERSION + "\n# PyMake was written by Stephen Melinyshyn | github.com/Melinysh/PyMake\n\n"

# Compiler specific variables
fileContents += flagsForCompiler(compilerName)
fileContents += "SRCEXT := " + fileType + "\n"
fileContents += "SRCDIR := " + srcdir + "\n"
fileContents += "BUILDDIR := " + builddir + "\n"
fileContents += "INSTALL_PATH := " + installPath + "\n"
fileContents += "TARGET := " + outputFile + "\n"
fileContents += "SOURCES := $(wildcard $(SRCDIR)/*.$(SRCEXT))\n"
fileContents += "OBJECTS := $(patsubst $(SRCDIR)/%.o,$(BUILDDIR)/%.o,$(SOURCES:.$(SRCEXT)=.o))\n"

return fileContents
fileContents += "\n\nall: $(TARGET)\n\n"

def cBasedMakefile():
fileContents = "OBJS := $(patsubst %." + fileType + ", %.o, $(SOURCES))\n"
fileContents += "all: $(TARGET)\n\n"

# Main compilation
fileContents += "$(TARGET): $(OBJS)\n\n"
fileContents += "\t$(PYMAKE_COMPILER) -o $(TARGET) $^\n\n"
# Main compilation
fileContents += "$(TARGET): " + ("\n" if fileType == "go" else "$(OBJECTS)\n")
fileContents += "\t$(PYMAKE_COMPILER) " + ("$(PYMAKE_COMPILER_FLAGS) " if fileType == "go" else "") + "-o $(TARGET) " + ("$(SOURCES)" if fileType == "go" else "$^") + "\n\n"

# Object files
fileContents += "%.o: %.c\n"
fileContents +="\t$(PYMAKE_COMPILER) $< $(PYMAKE_COMPILER_FLAGS) -o $@\n"
if fileType != "go":
fileContents += "$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)\n"
fileContents +="\t$(PYMAKE_COMPILER) $< $(PYMAKE_COMPILER_FLAGS) -c -o $@\n"

## Clean
fileContents += "\n"
fileContents += "clean:" + "\n"
fileContents += "\t" + "-rm $(TARGET) $(OBJS)\n"

return fileContents


def generateFileContents(fileType, compilerName):
fileContents = "# Generated by pymake version " + VERSION +"\n# PyMake was written by Stephen Melinyshyn | github.com/Melinysh/PyMake\n\n"

# Compiler specific variables
fileContents += flagsForCompiler(compilerName)
fileContents += "SOURCES := $(wildcard *." + fileType + ")\n"
fileContents += "INSTALL_PATH := " + installPath + "\n"
fileContents += "TARGET := " + outputFile + "\n"

if fileType == "go":
fileContents += goMakefile()
else:
fileContents += cBasedMakefile()
fileContents += "\t" + "-rm $(TARGET) " + ("" if fileType == "go" else "$(OBJECTS)") + "\n"

## Run
fileContents += "\n"
Expand Down Expand Up @@ -222,7 +210,7 @@ def start():
parseCommandline()
os.chdir(directory)
fileList = files()
debugPrint("Files found: ", fileList)
debugPrint("Files found in src directory ",srcdir," : ", fileList)
if fileType == "":
guessFileType(fileList)
debugPrint("Pymake believes the filetype is " + fileType)
Expand Down

0 comments on commit 8168d06

Please sign in to comment.