Python implementation generates a simple Makefile file code example _python

Source: Internet
Author: User

In Linux to write a few test procedures, but also a line of input g++ command to compile, when often changed the test code, that time and again the knock (or time and again on the line arrow election) also feel bad, not as fast as make. With the benefits of makefile, I have written a script that automatically searches the ". C" file in the current directory (excluding subdirectories) to generate makefile files.

The code here, limited in functionality (for a single file is a stand-alone test code case), requires a friend to modify to meet the requirements.

Copy Code code as follows:

#! /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 ' + ' \ n '

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)
# Search the current directory
str1 = ' all:\n '
str2 = ' '
STR3 = ' clean:\n '
For F in Flist:
str1 = str1 + ' \tmake ' + f + ' \ n '
str2 = ('%s%s:%s.o\n ')% (str2,f,f)
str2 = ('%s\t$ (CC)-O%s%s.o\n\n ')% (str2,f,f)
STR3 = ('%s\trm-f%s\n ')% (str3,f)
STR3 = Str3 + ' \trm-f *.o\n '
Strclean = '. c.o:\n\t$ (CC) $ (cflags)-c-o $*.o $<\n '
msg = ('%s%s\n%s\n%s\n%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 results of the operation are as follows (example):

Copy Code code as follows:

#./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 $<

After you run the script, make it.

Report:

Feel the script above is inconvenient, then modify the code as follows:

Copy Code code 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 ' + ' \ n '

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)
# Search the current directory
str1 = ' all:\n '
str2 = ' '
STR3 = ' clean:\n '
For F in Flist:
str1 = str1 + ' \tmake ' + f + ' \ n '
str2 = ('%s%s:%s.o\n ')% (str2,f,f)
str2 = ('%s\t$ (CC)-O%s%s.o\n\n ')% (str2,f,f)
STR3 = ('%s\trm-f%s\n ')% (str3,f)
STR3 = Str3 + ' \trm-f *.o\n '
Strclean = '. c.cpp.o:\n\t$ (CC) $ (cflags)-c-o $*.o $<\n '
msg = ('%s%s\n%s\n%s\n%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

Rename the file genmakefile.py to Genmakefile, copy it to/usr/local/bin, and then execute the following command in the desired directory:

Genmakefile.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.