5 ms·
Show HN: One makefile to rule them all
- LiquidityC 3y agoMy goto makefile for any new c/c++ project.
- b5n 3y agoI've found this to come in handy, and it is easy to modify to immediate needs. .DEFAULT_GOAL = help .PHONY: help ##@ HELP help: ## display this help @awk 'BEGIN {FS = ":.*##"; \ printf "\033[1mUSAGE\n\033[0m make \033[36m<TARGET>\033[0m\n"} \ /^[a-zA-Z_-]+\.*[a-zA-Z]*:.*?##/ {printf " \033[36m%-12s\033[0m %s\n", $$1, $$2} \ /^##@/ {printf "\n\033[1m%s\033[0m\n", substr($$0, 5)} \ /^###/ {printf "\033[0m\t\t %s\033[0m\n", substr($$0, 4)}' $(MAKEFILE_LIST) .PHONY: foo bar baz ##@ DEMO foo: bar ## foo target ### additional foo target details @echo foo bar: ## bar target @echo bar baz: ## baz target @echo baz
- oso2k 3y agoLots of other good Makefile `help` target suggestions here. https://gist.github.com/prwhite/8168133 My favorite is (since it only depends on a recent bash): SHELL:=/bin/bash .PHONY: help help_target-funky+names.0k and_with_2_targets_and_spaces_like_bison help_target-funky+names.0k and_with_2_targets_and_spaces_like_bison: ## Funky ones & bison dual target display ok echo "bad - why are you not displaying?" help: ## bash help help: ## moar bash help @RE='^[a-zA-Z0-9 ._+-]*:[a-zA-Z0-9 ._+-]*##' ; while read line ; do [[ "$$line" =~ $$RE ]] && echo "$$line" ; done <$(MAKEFILE_LIST) ; RE=''
- oso2k 3y agoI like suckless style Makefiles https://git.suckless.org/dmenu/file/Makefile.html https://git.suckless.org/dmenu/file/Makefile.html https://git.suckless.org/sprop/file/Makefile.html https://git.suckless.org/sprop/file/Makefile.html with file rule variables (my name for them, `OBJ = ${SRC:.c=.o}`). https://github.com/lpsantil/rt0/blob/master/Makefile https://github.com/lpsantil/rt0/blob/master/Makefile https://github.com/lpsantil/oop0/blob/master/Makefile https://github.com/lpsantil/oop0/blob/master/Makefile
- tom_ 3y agoIt looks like it doesn't deal with C/C++/etc. header files?
- gary_0 3y agoYeah, you have to run GCC with `-MMD -MP` so you get .d files along with your .o files. Then in my own Makefile I have: include $(shell find $(BUILD_DIR) -type f -name '\*.d') so that make will check for header file changes.
- oso2k 3y agoAlternative way without shelling out: SRC = $(wildcard src/\*.c) OBJ = $(SRC:.c=.o) SDEPS = $(SRC:.c=.d) EXE = myapp INC = include CFLAGS ?= -Os -MMD -MP LDFLAGS ?= %.o: %.c $(INC) Makefile $(CC) $(CFLAGS) -c $< -o $@ $(EXE): $(OBJ) $(LD) $^ $(LDFLAGS) -o $@ # near the bottom of your Makefile -include $(SDEPS)
- gcr 3y agointeresting! how much of this is already implemented with implicit rules? is there any documentation about how to adapt this to one's own projects? how does a user trigger the verbosity on and off for example, is it `-DV=1 -Dsilent=0` to get all the nice debugging? Some things seem hardcoded and some things are not, e.g. $LANG specifies the file extension (not language) of the input files, but the assumption is that each of these files winds up as a .o, so there's some tweaking required for your projects.
- deleted 3y ago[deleted]
- blueflow 3y ago- No idea whats up with the QUIET_* variables as most of them dont seem to get used - $(RM), $(CP), $(MKDIR) and $(ECHO) are unnecessary - these variables are for programs that are not portable. These 4 are some of the few portable ones. - It only builds and installs a single executable (without extension), no library, no headers - The install target does not honour DESTDIR, but distributors can get away with prepending it to PREFIX. For programs that use the PREFIX at build time, thats not okay. Makefile portability is hard. Take this makefile and try to build on FreeBSD or cross-compiling for Windows (where executables have a suffix). Or cross-compiling in general.
- streakfix 3y agoCan it do cross compilation?
- oso2k 3y agoIf you set `$(CC)` (like `CC=i386-linux-musl make`) and can deal with `$(CC)` being your linker. Not all projects can but most will tolerate it.
- __mharrison__ 3y agoWas crossing my fingers this would be for Python projects...
- woodruffw 3y agoHere is my "one true" Makefile for Python projects[1]. The skeleton gets tweaked slightly each time, but it's served me well for 4+ years. [1]: https://github.com/pypa/pip-audit/blob/main/Makefile https://github.com/pypa/pip-audit/blob/main/Makefile
- __mharrison__ 3y agoThanks for sharing
- emptysongglass 3y agoHere's one that you write in Python to configure for your Python! https://www.bitecode.dev/p/doit-the-goodest-python-task-runner https://www.bitecode.dev/p/doit-the-goodest-python-task-runn...
- withinboredom 3y agoI haven’t looked at the gist yet, but the number of projects I’ve seen using make as a glorified script runner is far above zero. It makes me sad. In all seriousness, it would be amazing if make could somehow detect the docker state.
- oso2k 3y agoIf you could represent the docker state as a file with an updating (incrementing) timestamp, then you might be able to do something.