Write makefile in python

Source: Internet
Author: User

Tip: it is best to read the makefile and python compiling rules. You can save your favorites for future viewing.

Reprinted please indicated with hyperlink: http://www.guimigame.com/thread-460-1-1.html

In fact, I have been very lazy before. I don't want to understand the makefile rules, because I have been using Qt creator for development in linux. (In many cases, the creativity of some "lazy" frees our hands. Obviously, we still need to write makefile with both hands ). Qt creator is a good IDE and can be developed across platforms. However, compared with VS, it is obviously not good enough. Therefore, many developers choose to develop C/C ++ programs in Windows and then deploy them in Linux for execution. Of course, I am no exception. So I spent the last few nights learning about the makefile compiling rules.

At the beginning, I wrote a new version of makefile by referring to some makefile examples on the Internet. However, this makefile reports an error when compiling my project.

Major errors are reflected in:

%. O: %. cpp

$ (CXX)-fpic-c $ (INCPATH) $ <-o $ @

Of course, it can be written

$ (Objdir)/%. o: $ (srcdir)/%. cpp

$ (CXX)-fpic-c $ (INCPATH) $ <-o $ @

The reason is:

1. The. o file and. cpp file are in different directories.

2. Different. o files or. cpp files are in different directories.

In this case, I found two solutions:

1. The special variable VPATH is used, but it is impossible for me to manually include all the directories to be included, so I gave up.

2. List all compilation rules.

I finally chose the second solution. Because of this problem, I specifically learned the makefile generated by Qt (in fact, this makefile is generated based on the. pro project file ). This makefile lists all the compilation rules. So there is the following python script. In fact, at the beginning, I wanted to use shell to do this step, but I saw sed and awk, and my head was dizzy, and I had been learning sed and awk before. Therefore, python is selected.

# Encoding: utf-8import osimport OS. pathimport sys # sys. exit (0 ); ######################################## ######################################## ######################################## ### the role of this script is: use python to generate makefile by configuring necessary information. (Technical Support: www.guimigame.com) # @ FILENAME execute the script to output the makefile file name # @ BIN generate the executable file name # @ SUFFIX source file SUFFIX # @ ROOTPATH "root" directory path (the top layer of the script working directory) # @ PWD current working directory # @ WD working directory, if the program has multiple working directories, add the # @ BINDIR executable file output directory # @ OBJDIR intermediate file output directory # @ INCROOTPATH header file contains the "root path" of the path ", facilitate the compilation of INCPATH # @ LIBROOTPATH contains the "root" Path of the library, convenient LIBS compilation # @ INCPATH header file inclusion path # @ SYSLIBS system library # @ LIBS library to be included in the compilation Program # @ CXX gcc/g ++ # @ FLAGSgcc/g ++ compilation flag ################################### ####################### ######################################## ######################## FILENAME = 'makefile '; BIN = "DatabaseServer"; SUFFIX = ". cpp "; ROOTPATH = OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _); PWD = OS. getcwd (); WD = []; WD. append (PWD); WD. append (ROOTPATH + "/common"); BINDIR = PWD + "/Bin/"; OBJDIR = BINDIR + "obj/"; INCROOTPATH = "-I" + ROOTPATH; LIBROOTPATH = "-L" + ROOTPATH; INCPATH = INCROOTPATH + "/ Common "+" "+ INCROOTPATH +"/lib/include "; SYSLIBS = "-lmysqlclient-lpthread" LIBS = LIBROOTPATH + "/lib/linux" + "-lTimeManager-lServerConfig-lGameSocket-lCommon-lTinyxml" + SYSLIBS; CXX = "g ++"; FLAGS = '-g-wall '; ######################################## ######################################## ######################################## ### no need to configure the following ################################ ################### ######################################## ############################## OBJFILE = ''; OBJ2SRC = []; SOURCES = ""; def SearchFiles (path): global OBJFILE; global OBJ2SRC; global SOURCES; global SUFFIX; listFile = OS. listdir (path); for file in listFile: if OS. path. isdir (OS. path. join (path, file): SearchFiles (OS. path. join (path, file); elif file. find (SUFFIX)> 0: if file. find (SUFFIX + "~ ")> 0: continue; OBJFILE = file; OBJFILE = OBJFILE. replace (SUFFIX ,'. o'); OBJ2SRC. append ([OBJDIR + OBJFILE, path + "/" + file]); SOURCES + = path + "/" + file + ""; for dir in WD: searchFiles (dir); if OS. path. exists (FILENAME): OS. remove (FILENAME); f = open (FILENAME, 'w'); f. write ("PWD =" + PWD + "\ n"); f. write ("CXX =" + CXX + "\ n"); f. write ("INCPATH =" + INCPATH + "\ n"); f. write ("LIBS =" + LIBS + "\ n"); f. write ("BINDIR =" + BINDIR + "\ n"); f. write ("OBJDIR =" + OBJDIR + "\ n"); f. write ("BIN =" + BIN + "\ n"); f. write ("SOURCES =" + SOURCES + "\ n"); f. write ("SOURCEFILES = $ (notdir $ (SOURCES) \ n"); f. write ("OBJECTS = $ (addprefix $ (OBJDIR), $ (patsubst %. cpp, %. o, $ (SOURCEFILES) \ n "); f. write ("FLAGS =" + FLAGS + "\ n"); f. write ("\ n"); f. write ("all: makedir $ (OBJECTS) \ n"); f. write ("$ (CXX) $ (FLAGS) $ (INCPATH)-o $ (BIN) $ (OBJECT S) $ (LIBS); \ n "); f. write ("\ n"); f. write ("makedir: \ n"); f. write ('$ (shell if [-n "$ (OBJDIR)"-! -E "$ (OBJDIR)"]; then mkdir-p $ (OBJDIR); fi) \ n'); f. write ('$ (shell if [-n "$ (BINDIR)"-! -E "$ (BINDIR)"]; then mkdir-p $ (BINDIR); fi) \ n'); f. write ("\ n"); for val in OBJ2SRC: f. write (val [0] + ":" + val [1] + "\ n"); f. write ("rm-f $ @ \ n"); f. write ("$ (CXX)-fpic-c $ (INCPATH) $ <-o $ @ \ n"); f. write ("\ n"); f. close (); OS. system ("make"); OS. system ("mv" + BIN + "" + BINDIR); OS. system ("cd" + OBJDIR + "; rm-f *. o ");



This section does not describe how to compile makefile and python. This article is not a makefile or python tutorial. The following describes the SearchFiles function.

By traversing the previously set project working directory, call SearchFiles to traverse all the source files under this directory (. cpp), and set the target file (. o), the absolute path is finally stored in the OBJ2SRC array in the form of tuple; some are stored in SOURCES. Of course, this process recursively traverses all subdirectories and finds all source files. Finally, in the for val in OBJ2SRC: traverse all the data, list all the source files (. cpp), generate the corresponding target file (. o), and write the compilation rules into the makefile.

 

This is the project to be compiled, of course, only part of it. In addition to DatabaseServer, the files to be included in this project must also be included in ../common (script code WD. append (ROOTPATH + "/common ");). I want to prove that this script is feasible. Some may say why not write a test example. As a matter of fact, it may be the best way to understand some technologies and do it yourself. If you have any questions, please feel free to discuss them with me!

 

 

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.