Settings

Theme

Show HN: One makefile to rule them all

gist.github.com

57 points by LiquidityC 3 years ago · 18 comments

Reader

blueflow 3 years 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.

LiquidityCOP 3 years ago

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

  • b5n 3 years ago

    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
    • oso2k 3 years ago

      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=''
  • oso2k 3 years ago
tom_ 3 years ago

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

  • gary_0 3 years ago

    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.
    • oso2k 3 years ago

      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)
gcr 3 years ago

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.

__mharrison__ 3 years ago

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

streakfix 3 years ago

Can it do cross compilation?

  • oso2k 3 years ago

    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.

withinboredom 3 years ago

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.

  • oso2k 3 years ago

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

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection