Skip to content

iberianpig/makef

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

makef

Override Makefile for developer local environment

  • Extend Makefile with overriding tasks
  • Behave like make -f make -f /path/to/git/ignored/Makefile
  • Override the project's Makefile tasks in local envirioment
  • Unify commands for developer's local between different projects
  • No more wrong selections from Ctrl-R's command line history
  • Support bash-completion

Installation

1. Install makef

register makef to ~/.bashrc

git clone https://github.com/iberianpig/makef
echo -e "# makef\nsource $(find `pwd` -name makef.sh)" >> ~/.bashrc
source ~/.bashrc

2. Install direnv

makef use direnv to set $MAKEF_PATH to envirionment variables

see: https://github.com/direnv/direnv

Usage

For example, You have a Makefile like following in your project.

./Makefile

task1: ## Sample task
	echo "this is Makefile"

task2: task1 ## task overridden in next step
	echo "from ./Makefile"

Export MAKEF_PATH in .envrc

Add export MAKEF_PATH=/path/to/hidden/Makefile to .envrc on your project

Edit .envrc, use .git/Makefile as MAKEF_PATH in this example.

export MAKEF_PATH=.git/Makefile

By setting the $MAKEF_PATH environment variable, you can specify a custom path for your override Makefile for each project. If you use direnv, simply add the following to your project's .envrc:

export MAKEF_PATH=.git/Makefile

Then allow the changes with:

direnv allow

(You can also set $MAKEF_PATH manually if you prefer not to use direnv.)

1. Initializing the Override Makefile

Generate a new override Makefile at the path specified by $MAKEF_PATH by running:

makefinit

An example override Makefile (e.g., .git/Makefile) generated by this command might look like:

.PHONY: all

all: help

help: ## show this messages
	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

NO_PHONY = /^:/
PHONY := $(shell cat $(MAKEFILE_LIST) | awk -F':' '/^[a-z0-9_.-]+:/ && !$(NO_PHONY) {print $$1}')
.PHONY: $(PHONY)

show_phony:
	@echo $(PHONY)

2. Editing and Overriding Tasks

After initialization, you can modify your override Makefile with:

makefedit

For example, you can redefine tasks as follows:

.PHONY: all

all: help

task2: task1 ## Overriding task
	echo "from .git/Makefile"

help: ## show this messages
	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

NO_PHONY = /^:/
PHONY := $(shell cat $(MAKEFILE_LIST) | awk -F':' '/^[a-z0-9_.-]+:/ && !$(NO_PHONY) {print $$1}')
.PHONY: $(PHONY)

show_phony:
	@echo $(PHONY)

3. Running Tasks with makef

Instead of running make directly, use makef. This command combines your original Makefile and the override Makefile, so tasks defined in both are executed together.

For example:

makef

Output might look like:

/tmp/tmp.u0vZq8mzkG:8: warning: overriding recipe for target 'task2'
/tmp/tmp.u0vZq8mzkG:5: warning: ignoring old recipe for target 'task2'
task1                          sample task
task2                          overriding task
help                           show help

$ makef task1
/tmp/tmp.cQARE0rtXT:8: warning: overriding recipe for target 'task2'
/tmp/tmp.cQARE0rtXT:5: warning: ignoring old recipe for target 'task2'
echo "this is Makefile"
this is Makefile

$ makef task2
/tmp/tmp.CI2hKcYZIH:8: warning: overriding recipe for target 'task2'
/tmp/tmp.CI2hKcYZIH:5: warning: ignoring old recipe for target 'task2'
echo "this is Makefile"
this is Makefile
echo "from .git/Makefile"
from .git/Makefile

To execute a specific task, run:

makef task1

or

makef task2

4. Bash Completion

When using makef, simply press the Tab key to auto-complete available task names from your override Makefile.

makef  # then press Tab to see: help   task1  task2

5. Changing the Source Makefile

By default, makef uses the Makefile in the current directory. If you want to use a different Makefile as your source, you can specify it by setting the $MAKEFILE environment variable in .envrc:

export MAKEF_PATH=.git/Makefile
export MAKEFILE=path/to/Makefile  # Specify the desired Makefile

Then reload your environment (for example, with direnv allow).


makef leverages the optional use of direnv to automatically manage the $MAKEF_PATH variable, simplifying the process of specifying a custom override Makefile for each project. Enjoy a smoother local development experience with makef!

About

Override Makefile

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages