Skip to main content

A make equivalent in Python

Project description

make.py

PyPI version

make.py is like make but with its build configuration written in Python.

Example

For the following Makefile:

CC=gcc
OUTPUT=build

all: $(OUTPUT)/main

$(OUTPUT)/%.o: %.c
	mkdir -p $(dir $@)
	$(CC) -MMD -c $< -o $@

$(OUTPUT)/main: $(OUTPUT)/hello.o $(OUTPUT)/main.o
	$(CC) $^ -o $@

clean:
	rm -rf $(OUTPUT)

.PHONY: clean

-include $(OUTPUT)/*.d

The equivalent Makefile.py is:

from pathlib import Path
from shutil import rmtree
from subprocess import check_call

from make_py import task, rule, phony_task

CC = "gcc"
OUTPUT = "build"

phony_task("all", f"{OUTPUT}/main")


def collect_c_dependencies(target, target_regex_groups):
    dep_file = Path(target).with_suffix(".d")
    if dep_file.exists():
        return dep_file.read_text().split(":")[1].strip().split(" ")


# missing parent directories will be made automatically
@rule(f"{OUTPUT}/%.o", ["%.c", collect_c_dependencies])
def compile_c(ctx):
    check_call([CC, "-MMD", "-c", ctx.source, "-o", ctx.target])


@rule(f"{OUTPUT}/main", [f"{OUTPUT}/{o}" for o in ["hello.o", "main.o"]])
def link(ctx):
    check_call([CC, *ctx.sources, "-o", ctx.target])


@task()
def clean(ctx):
    rmtree(OUTPUT, ignore_errors=True)

The commands can be executed by make.py the same way as executing make

For more examples, refer to the examples folder in project root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

make_py-0.1.1.tar.gz (6.6 kB view hashes)

Uploaded Source

Built Distribution

make_py-0.1.1-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page