Last active
December 3, 2018 19:45
-
-
Save leeonix/042222cdfc479465051a to your computer and use it in GitHub Desktop.
generate Makefile template using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, string | |
template = """ | |
CC = gcc | |
CXX = g++ | |
AR = ar | |
DEFINES += | |
CFLAGS = $(DEFINES) -c -O2 -Wall | |
INCLUDES += | |
LIBS += | |
${kind} | |
.PHONY: clean test | |
TARGETDIR = bin | |
OBJDIR = obj | |
${target} | |
all: $(TARGETDIR) $(OBJDIR) $(TARGET) | |
@: | |
clean: | |
rm -f $(TARGET) | |
rm -rf $(OBJDIR) | |
test: | |
$(TARGETDIR): | |
mkdir $(subst /,\\\\,$(TARGETDIR)) | |
$(OBJDIR): | |
mkdir $(subst /,\\\\,$(OBJDIR)) | |
OBJECTS := \\ | |
$(OBJDIR)/${name}.o \\ | |
$(TARGET): $(OBJECTS) | |
$(LINKCMD) | |
$(OBJDIR)/${name}.o: ${name}.c | |
$(CC) -o "$@" $(CFLAGS) $(INCLUDES) "$<" | |
""" | |
kind_table = { | |
'con' : 'LDFLAGS +=\nLINKCMD = $(CC) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(LIBS)', | |
'dll' : 'LDFLAGS += -shared\nLINKCMD = $(CC) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(LIBS)', | |
'lib' : 'LINKCMD = $(AR) -rcs $(TARGET) $(OBJECTS)', | |
} | |
target_fmt = 'TARGET = $(TARGETDIR)/%s%s.%s' | |
def main(): | |
if len(sys.argv) == 1: | |
sys.stderr.write("\nuseage:\n\tpython " + sys.argv[0] + " name language(c or c++) kind(con dll lib) >Makefile\n") | |
return 1 | |
content = {} | |
name = sys.argv[1] | |
if len(sys.argv) <= 2: | |
cc = 'c++' | |
else: | |
cc = sys.argv[2].lower() | |
if len(sys.argv) <= 3: | |
kind = 'con' | |
else: | |
kind = sys.argv[3].lower() | |
content['name'] = name | |
content['kind'] = kind_table[kind] | |
if kind == 'con': | |
content['target'] = target_fmt % ('', name, 'exe') | |
elif kind == 'dll': | |
content['target'] = target_fmt % ('', name, 'dll') | |
elif kind == 'lib': | |
content['target'] = target_fmt % ('lib', name, 'a') | |
result = string.Template(template).safe_substitute(content) | |
if cc == 'c++': | |
result = result.replace('$(CC)', '$(CXX)') | |
result = result.replace('.c', '.cpp') | |
print(result) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment