Using makefiles to package addons

Tutorials Nov 11, 2015

Ahh finally putting your addon in a zip file and posting it, the most relaxing part of development. Except when you have to do it often!

Generating the documentation and including it as well as icons, py files, extra assets, etc. all while making sure I don’t include __pycache__ by mistake is a chore on the level of doing dishes after christmas. It’s also very error prone, specially when you’re rushing embarrassing bugfix updates. Not that I ever had to do that **cough**

The solution, of course, is automating it.

There’s a tool developers know well and is included with Linux and OS X: Make. Make is a command line utility used to simplify the compiling process, however it can be used like a shell script to automate other things (for instance, zipping files). Make uses a special format file to set up the commands it has to run, these are files are called Makefiles.

A Makefile consists of one or more targets. A target is a group of commands identified by a name. You can be make individual targets by running make [target name].

Knowing this, we can now figure out what commands to use and write a Makefile to take care of them for us. Ok so now that we know how it works, how can we use make to get addons packaged?

Meet the addon Makefile

You can see the actual file below. Simply save it in your addon folder as “Makefile” (no extension). To build (or “deploy”) open a terminal in your addon directory (where the makefile is) and simply run make. A file browser will show you the folder containing the zip file when it’s done.

Let’s look at the settings available. You can change the FILE_BROWSER setting to whatever you use to manage files (finder, ranger, thunar, etc.), or comment the last line if you don’t care about seeing the zip file.

The FILES variable is passed as it is to the zip command. Each file or directory is separated by a space, and you can use wildcards like I did in the example below.

BUILD_DIR refers to the directory to put the zip file, it can be either a relative or absolute path. The rest of the variables are self-explanatory.

After the settings, I have set the targets. You can adapt the makefile to any project by tweaking the variables, but you can also adapt targets and commands to your needs.

I’ve written three targets in the file:

**docs** (re)Generate the documentation
**clean** Delete the zip
build Run docs target and create zip

The default target is build. If you simply run make with no arguments, it will use that target.

Also as you can read below, this Makefile requires Sphinx to build the docs (since that’s what I use) and the zip command. If you’re not using Sphinx you can comment out that line, or replace it with a command of your own.

# ---------------------------------------------------------------
# Makefile for My_addon
# Diego Gangl - <diego@sinestesia.co>
# ---------------------------------------------------------------


# /./././././././././././././././././././././././././././././././
# SETTINGS
# /./././././././././././././././././././././././././././././././

NAME            = My_addon
VERSION         = 1_0
BUILD_DIR       = ../release/
BUILD_FILE      = $(BUILD_DIR)/$(NAME)_$(VERSION).zip
FILES           = *.py icons/* docs/*
FILE_BROWSER    = nautilus

.DEFAULT_GOAL 	:= build


# /./././././././././././././././././././././././././././././././
# TARGETS
# /./././././././././././././././././././././././././././././././

.PHONY: docs
docs:
	sphinx-build -b html docs_src docs


clean:
	rm -rf $(BUILD_FILE)
	@echo "Release zip deleted - " $(BUILD_FILE)


build: 	docs

	mkdir -p $(BUILD_DIR)

	zip -r9T $(BUILD_FILE) $(FILES)

	@echo
	@echo $(NAME)" "$(VERSION) " is ready"
	$(FILE_BROWSER) $(BUILD_DIR) &

Hope this you found this useful. Feel free to use this code for anything, tweak it or even change the ASCII art.

Tweet CC-BY-NC-SADiego Gangl

Subscribe now and be the first to read the new articles!

Hosting by XMundo Networks
comments powered by Disqus