The Python implementation generates a simple code sample for the Makefile file

  • 2020-04-02 14:37:38
  • OfStack

Write a few test programs under Linux and type g++ command line by line to compile. When you frequently change the test code, the repeated typing (or on-line arrow selection again and again) is not as good as the speed of make. The advantage of using a Makefile goes without saying. Here I've written a script that automatically searches for ".c "files in the current directory (not including subdirectories) to generate makefiles.

In this case, the code has limited functionality (in cases where a single file is a separate test code), and those who need it can make minor modifications to meet the requirements.


#! /usr/bin/python
'''
 File      : genMakefile.py
 Author    : Mike
 E-Mail    : Mike_Zhang@live.com
'''
import os def genMakefileStr(dir,surfix = '.c'):
    msg = ''
    msg = msg + 'CC = gcc' + 'n'
    msg = msg +  'CFLAGS = -g -O2 -Wall' + 'nn'
   
    fList = []
    for dirPath,dirNames,fileNames in os.walk(dir):
        for file in fileNames:
            name,extension = os.path.splitext(file)
            if extension == surfix:
                fList.append(name)
        break # only search the current directory
    str1 = 'all:n'
    str2 = ''
    str3 = 'clean:n'
    for f in fList:
        str1 = str1 + 'tmake ' + f + 'n'
        str2 = ('%s%s:%s.on') % (str2,f,f)
        str2 = ('%st$(CC) -o %s %s.onn') % (str2,f,f)
        str3 = ('%strm -f %sn') % (str3,f)
    str3 = str3 + 'trm -f *.on'
    strClean = '.c.o:nt$(CC) $(CFLAGS) -c -o $*.o $<n'
    msg = ('%s%sn%sn%sn%s') % (msg,str1,str2,str3,strClean)
    #print 'msg : n'
    #print msg
    return msg if __name__ == '__main__':
    str = genMakefileStr('.','.c')
    file = open("Makefile","w")
    file.write(str)
    file.close()
    print str

The operation effect is as follows (example) :


# ./genMakefile.py         
CC = gcc
CFLAGS = -g -O2 -Wall all:
        make pfun1
        make pfun2 pfun1:pfun1.o
        $(CC) -o pfun1 pfun1.o pfun2:pfun2.o
        $(CC) -o pfun2 pfun2.o
clean:
        rm -f pfun1
        rm -f pfun2
        rm -f *.o .c.o:
        $(CC) $(CFLAGS) -c -o $*.o $<

Just make after you run the script.

The attached:

I feel that the above script is not convenient to use, and then I modify it. The code is as follows:


#! /usr/bin/python
'''
  File      : genMakefile.py
  Author    : Mike
  E-Mail    : Mike_Zhang@live.com
'''
import os,sys
 
surfix = ['.c','.cpp'] def genMakefileStr(dir):
    msg = ''
    msg = msg + 'CC = g++ ' + 'n'
    msg = msg +  'CFLAGS = -g -O2 -Wall' + 'nn'
   
    fList = []
    for dirPath,dirNames,fileNames in os.walk(dir):
        for file in fileNames:
            name,extension = os.path.splitext(file)
            if surfix.count(extension) > 0:
                fList.append(name)
        break # only search the current directory
    str1 = 'all:n'
    str2 = ''
    str3 = 'clean:n'
    for f in fList:
        str1 = str1 + 'tmake ' + f + 'n'
        str2 = ('%s%s:%s.on') % (str2,f,f)
        str2 = ('%st$(CC) -o %s %s.onn') % (str2,f,f)
        str3 = ('%strm -f %sn') % (str3,f)
    str3 = str3 + 'trm -f *.on'
    strClean = '.c.cpp.o:nt$(CC) $(CFLAGS) -c -o $*.o $<n'
    msg = ('%s%sn%sn%sn%s') % (msg,str1,str2,str3,strClean)
    #print 'msg : n'
    #print msg
    return msg
 
if __name__ == '__main__':
    for arg in sys.argv[1:]:
        print arg
    str = genMakefileStr(arg)
    if arg[-1] == '/':arg = arg[:-1]
    file = open(arg+"/Makefile","w")
    file.write(str)
    file.close()
    print str

Change the file genmakefile.py to genMakefile, copy it to /usr/local/bin, and then execute the following command in the required directory:

GenMakefile.


Related articles: