Python code example for generating a simple Makefile file,

Source: Internet
Author: User

Python code example for generating a simple Makefile file,

Write a few test programs in linux, and write a line of input g ++ commands for compilation. When the test code is often changed, repeat (or click the online arrow again and again) it's not as easy as making. The advantage of using Makefile is unnecessary. Here I wrote a script to automatically search for the. c file under the current directory (excluding subdirectories) to generate the Makefile.

The code here has limited functions (applicable when a single file is an independent test code). You can make some modifications to your needs.

Copy codeThe Code is 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 \ 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)
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. o \ n') % (str2, f, f)
Str2 = ('% s \ t $ (CC)-o % 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 \ 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 running effect is as follows (example ):

Copy codeThe Code is 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 $ <

Run the script and run make.

Appendix:

The above script is inconvenient to use, and then the modification code is as follows:
Copy codeThe 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' + '\ n \ 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)
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. o \ n') % (str2, f, f)
Str2 = ('% s \ t $ (CC)-o % 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 \ 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 genMakefile. py to genMakefile and copy it to/usr/local/bin. Then, execute the following command in the required directory:

GenMakefile.

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.