views:

552

answers:

6

How I can make a Makefile, because it's the best way when you distribute a program by source code. Remember that this is for a C++ program and I'm starting in the C development world. But is it possible to make a Makefile for my Python programs?

+6  A: 

I use Makefiles for some Python projects, but this is highly dubious... I do things like:

SITE_ROOT=/var/www/apache/...

site_dist:
     cp -a assets/css build/$(SITE_ROOT)/css
     cp -a src/public/*.py build/$(SITE_ROOT)

and so on. Makefile are nothing but batch execution systems (and fairly complex ones at that). You can use your normal Python tools (to generate .pyc and others) the same way you would use GCC.

PY_COMPILE_TOOL=pycompiler

all: myfile.pyc
     cp myfile.pyc /usr/share/python/...wherever
myfile.pyc: <deps>
     $(PY_COMPILE_TOOL) myfile.py

Then

$ make all

And so on. Just treat your operations like any other. Your pycompiler might be something simple like:

#!/usr/bin/python
import py_compile
py_compile.compile(file_var)

or some variation on

$ python -mcompileall .

It is all the same. Makefiles are nothing special, just automated executions and the ability to check if files need updating.

Aiden Bell
Thanks for the Python part help!
Nathan Campos
That's a perfectly legitimate use of Makefiles, I don't think it's dubious at all.
dF
@dF Try telling that to my Makefile rules that delete and reset SQL tables on a staging server :P
Aiden Bell
+3  A: 

A simple Makefile usually consists of a set of targets, its dependencies, and the actions performed by each target:

all: output.out

output.out: dependency.o dependency2.o
    ld -o output.out dependency.o dependency2.o

dependency.o: dependency.c
    gcc -o dependency.o dependency.c

dependency2.o: dependency2.c
    gcc -o dependency2.o dependency2.c

The target all (which is the first in the example) and tries to build its dependencies in case they don't exist or are not up to date. will be run when no target argument is specified in the make command.

Mehrdad Afshari
...because it is the first, not because it's named 'all', right?
Stephan202
@Stephan202: Sure. That was just an example. But probably I should have mentioned that to reduce the chance of confusion.
Mehrdad Afshari
Also, the .PHONY rule is useful!
Aiden Bell
In GNU make, any target beginning with a dot will run automatically.
Mehrdad Afshari
+8  A: 

From your question it sounds like a tutorial or an overview of what Makefiles actually do might benefit you.

A good places to start is the GNU Make documentation.

It includes the following overview "The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them."

And its first three chapters covers:

  1. Overview of make
  2. An Introduction to Makefiles
  3. Writing Makefiles
Andre Miller
Thanks, this is a very good base.
Nathan Campos
+4  A: 

How i can make a MakeFile, because it's the best way when you distribuite a program by source code

It's not. For example, KDE uses CMake, and Wesnoth uses SCons. I would suggest one of these systems instead, they are easier and more powerful than make. CMake can generate makefiles. :-)

Bastien Léonard
Thanks very much!!!
Nathan Campos
Don't use SCons if you have any care about speed as your project grows: if you want a Python based build system, take a look at Waf.
jkp
+1  A: 

If you are asking about a portable form of creating Makefiles you can try to look at http://www.cmake.org/cmake/project/about.html

Aragorn
Thanks, this is good.
Nathan Campos
+2  A: 

For Python programs, they're usually distributed with a setup.py script which uses distutils in order to build the software. distutils has extensive documentation which should be a good starting point.

Paul Fisher