Hacker News new | past | comments | ask | show | jobs | submit login

I've found these guidelines for Makefiles make for a pretty good experience using make: https://tech.davis-hansson.com/p/make/

The advice on output sentinel files for rules creating multiple files helps keep rebuilding dependencies reliable. Avoiding most of the cryptic make variables also helps Makefiles to remain easily understandable when you're not frequently working on them. And using .ONESHELL to allow multi-line statements (e.g. loops, conditional etc) is great. No need to contort things into one line. or escape line breaks.

Seems like you could even use a more serious programming language instead of sh/bash by setting SHELL to Python or similar. That may be a road to madness though...




> Seems like you could even use a more serious programming language instead of sh/bash by setting SHELL to Python or similar. That may be a road to madness though...

TIL.

    SHELL=/usr/bin/python
    .ONESHELL:

    all:
      @from plumbum.cmd import ls
      print(ls["-a"]())

It totally works... Mwoooo ha ha ha haaaa!


The problem with .ONESHELL is, that it is for the whole file. I so wish it was per target. That would be really useful. But for the whole file? Maybe I need each line to be a separate shell anywhere in the file and that will make it impossible to use .ONESHELL for the entire file.


It is per target, that's how it works when I've used it. For example:

  # Makefile
  SHELL := bash
  .ONESHELL:
  .RECIPEPREFIX = >
  
  thing1:
  > FOO=bar
  > echo "$${FOO:?}"
  .PHONY: thing1
  
  thing2:
  > echo "$${FOO:?}"
  .PHONY: thing2
Results in:

  $ make thing1 thing2
  FOO=bar
  echo "${FOO:?}"
  bar
  echo "${FOO:?}"
  bash: line 1: FOO: parameter null or not set
Note how the bash error for unset FOO is line 1 for the second target.

Edit: Maybe I misinterpreted, do you mean you'd want to choose whether a given target is ONESHELL or not?


> [...] do you mean you'd want to choose whether a given target is ONESHELL or not?

Yep, exactly! I would like to have some targets use ONESHELL and others in the same Makefile not use ONESHELL. So that I can choose the most appropriate for each target.

So far I have managed by avoiding ONESHELL and doing the typical "backslash, next line continues" thingy. But it puts some people off.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: