Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: One makefile to rule them all (gist.github.com)
57 points by LiquidityC 6 months ago | hide | past | favorite | 17 comments



- 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.


My goto makefile for any new c/c++ project.


I'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


Lots 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=''



It looks like it doesn't deal with C/C++/etc. header files?


Yeah, 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.


Alternative 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)


interesting! 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.


Was crossing my fingers this would be for Python projects...


Here 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


Thanks for sharing


Here's one that you write in Python to configure for your Python! https://www.bitecode.dev/p/doit-the-goodest-python-task-runn...


Can it do cross compilation?


If 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.


I 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.


If you could represent the docker state as a file with an updating (incrementing) timestamp, then you might be able to do something.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: