News for hackers. Stuff that matters.
Re:A simple makefile is a unicorn by __david__ on Thursday January 11, 2018 @04:26AM (#16122494)
The Makefile shown in the article is simultaneously not complicated and also over-complicated. I feel like most Make users don't understand Make very well (especially the ones that complain really loudly about it).

Here's a couple examples:

1. The "%.o: %.c" line is a huge code smell for Makefiles. You shouldn't ever write that line—it's built in!

2. Mentioning .h files in the Makefile. Don't. Go look up the `-MMD` flag in your C compiler. Clang, gcc and many other C compilers support it. Add that and a "-include *.d" line in the Makefile and you'll have great dependency support.

Everything else in that makefile is just setting variables. It's made overly complicated by the directory junk, but I'm failing to see the problem.

Re:A simple makefile is a unicorn by endymi0n on Thursday January 11, 2018 @05:00AM (#16122633)
> I feel like most Make users don't understand Make very well (especially the ones that complain really loudly about it).

This is the very same narrative that vim users proclaim: "It's totally not complex if you took some years to learn your tools" — while simply not realizing that the whole Make syntax is a pile of clunks and hacks and stopgaps and bandaids that has a super steep learning curve and looks like incomprehensive gibberish to the uninitated eye.

And most of that needlessly so IMHO. It shouldn't be hard to find a better, well designed syntax with sane defaults, stricter rules with typing and linting, less surprises (tabs, I'm looking at you) that's overall much less scary for newcomers — as well as less taxing for "expert casual" users like me. (I still go for my coworkers and Stack Overflow regularly if I have to fix one and I'm a fucking CTO with 10 years of dev experience)

Make's only reason for survival is its ubiquity. No matter which terminal you boot up, it's just there, that's why we use it too, and I hate it every day.

I think none of the alternatives (cmake, Jam, Gradle, Maven, grunt etc. etc.) has really succeeded as a cross-platform, cross-ecosystem solution that's both an approachable, simple and usable version of a DAG execution engine that's usable for lots of situation.

So the situation will probably stay like with vim and the QWERTY keyboard, the status quo pretty bad, but everyone's too used to it to go for something better.

Re:A simple makefile is a unicorn by dwc on Thursday January 11, 2018 @07:49AM (#16123355)
> This is the very same narrative that vim users proclaim: "It's totally not complex if you took some years to learn your tools"

Something interesting I see more often than not when someone I know decides to try out vim:

They know I'm a vim user so they ask me for good tutorials and I tell them there are lots out there but they should start by running vimtutor (included with vim). Takes around 15-20 minutes to work through the interactive lessons and gives you all the basics of movement and editing.

A day or so later I ask how it's going and the answer is usually that they're struggling with vim, so I ask if they ran vimtutor. Usually nope, or they started it but quit after a minute or two.

Why would one switch to a tool with a different paradigm, ask for and get a tutorial recommendation, and then ignore the advice of spending less than 1/2 hour on a tutorial that ships with the tool? sigh

Re:A simple makefile is a unicorn by wolfgke on Thursday January 11, 2018 @08:24AM (#16123593)
> They know I'm a vim user so they ask me for good tutorials and I tell them there are lots out there but they should start by running vimtutor (included with vim). Takes around 15-20 minutes to work through the interactive lessons and gives you all the basics of movement and editing.

I think that I am rather good at memorizing lots of obscure raw data - but in my experience it takes a lot longer to remember all the "obscure" commands of vim. In this sense I would not say it takes 15-20 minutes, but at least a day if at the end you really want to have the commands in your brain.

In this sense the mistake in your argumentation is the timescale that you are talking about.

Re:A simple makefile is a unicorn by dwc on Thursday January 11, 2018 @08:54AM (#16123813)
I never meant to imply that 15-20 minutes in vimtutor would make you a vim wizard, or that you'd remember everything that vimtutor went through. There's no quick and easy answer to switching to something that does things in a fundamentally different way.

Also, I hold some fairly controversial opinions: you don't need dozens of vim plugins; you don't need to compete with vim golf champions; you don't need an extensive configuration.

What you do need is to fully grok the idea of normal mode as an editing language with verbs, objects, and modifiers. Once you "get it" then you have a working knowledge of how to construct "sentences", and learning a new text object allows you to apply pretty much all verbs to it. Same for learning a new verb; you can apply it to lots of things.

Over the years I've learned a lot of interesting vim things and I've forgotten most of them. Because I don't need them every day or even every week. That's ok. The things I remember are things I use often, because they're useful in everyday editing.

Re:A simple makefile is a unicorn by WillReplyfFood on Thursday January 11, 2018 @08:03AM (#16123429)
Because in the windows world- every ui-interface holds its contract, everything explains itself and does what it supossed to do. There is no disk-icon that doesent save. There is no set of shortkeys ignoring controll s. Design is meeting the users expectations without additional instructions with functionality. VIM is not designed very well.
Re:A simple makefile is a unicorn by alexeiz on Thursday January 11, 2018 @10:07AM (#16124269)
Just because Vim was not designed for the mouse clicking UI environment does not mean that it's not designed well. If it weren't designed well, Vim would not be in widespread use 40 years after its inception.
Re:A simple makefile is a unicorn by Spivak on Thursday January 11, 2018 @10:07AM (#16124272)
Sure, but if you're trying to carry over Windows expectations over to Linux you're sure to be disappointed.

In Vim there are modes that describe sets of keybindings and there are commands that are bound to them. The commands themselves are are straightforward for anyone who's fluent in Linux vocabulary.

'Saving' is a Windows word. We :write to files or :w for short.

Re:A simple makefile is a unicorn by xfer on Thursday January 11, 2018 @06:19AM (#16122937)
Maybe you should point to a better editor(or design one if you can) in vim case than simply claiming everyone is using it because they are used to it.
Re:A simple makefile is a unicorn by stuaxo on Thursday January 11, 2018 @07:51AM (#16123361)
Meson seems to be taking over open source projects, let's see how it goes.
Re:A simple makefile is a unicorn by seba_dos1 on Thursday January 11, 2018 @09:25AM (#16124000)
Doesn't seem like it brings much over CMake aside of having yet another build system to support/know how to use.
Re:A simple makefile is a unicorn by monob on Thursday January 11, 2018 @07:43AM (#16123316)
>This is the very same narrative that vim users proclaim

Sounds like a solid reason why makefiles are great to me then.

Vim is so good it finally is letting emacs-OS have a real editor.

Re:A simple makefile is a unicorn by skohan on Thursday January 11, 2018 @04:53AM (#16122604)
> I feel like most Make users don't understand Make very well (especially the ones that complain really loudly about it).

But the question is whether this is Make's fault for being incomprehensible, or the User's fault for being lazy.

The thing is virtually any system or tool can be understood with enough practice, but just because there is someone out there who can use it well does not mean it's not poorly designed.

Both C and Make are decades-old technologies, but most people can read a C file without much experience and intuitively understand what is going on. The same is not true for Makefiles.

Re:A simple makefile is a unicorn by mschaef on Thursday January 11, 2018 @05:49AM (#16122824)
> Both C and Make are decades-old technologies, but most people can read a C file without much experience and intuitively understand what is going on.

While they might think they know what's going on, I'm not convinced they actually do. C can be an incredibly subtle language to get right...

http://blog.llvm.org/2011/05/what-every-c-programmer-should-...

Regarding make, the thing to realize there is that it's just a DSL for specifying a dependency graph (and the way for satisfying dependencies). It's not a hard concept, but unless you come to the field with a bit of computer science background, it's not likely something you've been directly exposed to at the level required to understand make. But I don't know that this means make's choice of abstraction is necessarily a bad thing for its intended use cases. I also think this tends to put the lie to the notion that the difficulty is mainly around tabs... tabs may obscure the details, but switching to a clearer syntax won't solve the more fundamental gaps in understanding the mechanics of make.

I'll also argue that this is a potential problem with DSL's in general... because DSL's often don't use traditional imperative language semantics, there's always a bit of a learning curve that needs to be ascended. However, for make at least, there are a useful debugging tools to help understand what it's actually doing.

For me, at least, 'make -n' is great in that it instructs make to resolve the dependency graph based on the current state, and then just print the commands it'll run without executing them. If you know what the commands do, and know what needs to run, this can be a huge help understanding what's going on. (And there are other more powerful debugging capabilities as well for dumping variables, etc.)

Re:A simple makefile is a unicorn by crdoconnor on Thursday January 11, 2018 @06:28AM (#16122969)
DSLs are most suited to constrained execution spaces - where you know that there are relatively hard limits on what it is that the resultant program will have to do. The problem is that build systems are not really a constrained execution space - there's too often a lot more required than just specifying a dependency tree and just saying 'make it happen'.

As with many 'simplified' DSLs that are often unsuited to the task at hand (e.g. C++ templating), make (or versions of it, anyway), evolved to accommodate these use cases and then morphed into something that accidentally became turing complete: https://stackoverflow.com/questions/3480950/are-makefiles-tu...

Re:A simple makefile is a unicorn by mschaef on Thursday January 11, 2018 @08:15AM (#16123530)
> As with many 'simplified' DSLs that are often unsuited to the task at hand (e.g. C++ templating)

Isn't a C++ template the exact opposite of 'Domain Specific'?

Re:A simple makefile is a unicorn by Annatar on Thursday January 11, 2018 @07:34AM (#16123266)
...Nor does it mean that just because it has a steep learning curve it’s badly designed.

Make is not a needlessly complex tool (unlike most garbage tools these days); it’s complex because it needs to be flexible enough to build any programmer’s software. Since ways to program and build software are infinite, the tool itself cannot be simple, otherwise trade-offs have to be made.

Re:A simple makefile is a unicorn by sureaboutthis on Thursday January 11, 2018 @05:41AM (#16122788)
You can also create a point and click gui mechanism and brag about how wonderful it is, too, but it wouldn't be 'make' or be as capable, well-rounded, flexible, encompassing, etc.

'make' exists for what it's capable of doing and it must be so as it's used in every piece of software and operating sytem everywhere.

Re:A simple makefile is a unicorn by Qwertious on Thursday January 11, 2018 @06:36AM (#16123004)
>'make' exists for what it's capable of doing and it must be so as it's used in every piece of software and operating sytem everywhere.

So it's extremely popular/widespread, therefore it must be good, and therefore you should use it (thereby making it even more popular/widespread)?

I really hope that you poorly worded that/I'm grossly misunderstanding you because otherwise, your logic is batshit insane.

Re:A simple makefile is a unicorn by mschaef on Thursday January 11, 2018 @07:28AM (#16123235)
> So it's extremely popular/widespread, therefore it must be good, and therefore you should use it (thereby making it even more popular/widespread)?

My experience has been that that people that truly understand and appreciate technology for its own sake are also likely to underestimate the value of a tool being popular and widespread. What 'popular' really means is that the tool is more likely to be where you need it to be and the people you need to use it will already know how. Those things outweigh a 'perfectly' designed tool that you don't have or know how to use.

Re:A simple makefile is a unicorn by skohan on Thursday January 11, 2018 @08:23AM (#16123591)
> What 'popular' really means is that the tool is more likely to be where you need it to be and the people you need to use it will already know how.

There's a balance point, for sure. For instance, the pace of "innovation" in front-end javascript libraries throws out a lot of babies with the bathwater, and we might be well-served by accepting a paradigm for 10 minutes and building on it rather than always chasing after the next hot thing.

But at the other extreme, there was a time when PHP was ubiquitous for back-end development, and it stuck around largely because of inertia: the obvious flaws were accepted just because it was so accepted as the tool for that job. But I don't think many people are lamenting the fact that we've moved on to better tools for back-end development. In that case inertia was a bad thing because it delayed innovation.

For me an example of a "good enough" tool is GIT. It's not perfect, it's got a non-trivial learning curve, and surely some innovations could be made to improve version control. Some would already argue that Mercurial is the Betamax to GIT's VHS - but it does the job well enough, and everybody uses it, so it's good enough.

I'd argue Make is more like PHP than it is GIT.

Re:A simple makefile is a unicorn by mschaef on Thursday January 11, 2018 @09:02AM (#16123861)
> we might be well-served by accepting a paradigm for 10 minutes and building on it rather than always chasing after the next hot thing.

Exactly this.

> I'd argue Make is more like PHP than it is GIT.

What would you want a make replacement to look like?

Make is pretty 'roll your own' in places, but I've found it to have a nice balance between expressiveness and terseness. For simple projects, there isn't much to do in make, and for the more complex projects I've been involved in (embedded work, a self-hosting compiler, etc.) make can still generally express the build process within the confines of Makefile syntax. (In contrast to more modern tools that might require plugins in a different language, etc.)

For the compiler project I mention, the build process includes building several executables in C, running them against a pre-built VM image to compile another VM image, translating that image into C code that is then compiled and linked into the VM to produce another executable, after which there's a unit test suite. Root rebuild command is 'make clean && make tested', and there are only a couple hundred lines of Makefile code to get there. (There's also cross-compile support, etc.)

I'll admit that there was a certain amount of effort involved in writing those Makefiles, but I've also implemented the same build process using a modern GUI tool and found it to be much more difficult.

Re:A simple makefile is a unicorn by skohan on Friday January 12, 2018 @05:34AM (#16131882)
> What would you want a make replacement to look like?

Admittedly I haven't given a ton of thought to this specific problem, but here goes:

I actually like Make from a conceptual standpoint. I think it's a good idea to codify build orchestration in code rather than non-human-readable output from a GUI application. My main gripe is that it seems to be very much a 1.0 tool: it was build to solve a specific problem, and then features were added as needed. I would love to have an iteration on that concept which benefits from what we've learned from Make while rounding off the rough edges of what feels like the result of a largely ad-hoc design process.

As far as what that would look like: I would like to see a concise, low-level API for doing the kinds of things Makefiles do (i.e. defining a dependency graph, setting up build parameters etc), and better tools for composition.

Also I would sacrifice a bit of terseness for the benefit of readability. For instance an arbitrary keyword like `Phony` could be replaced by a more extensible name-spacing syntax which lets you know exactly what's going on.

Re:A simple makefile is a unicorn by sureaboutthis on Thursday January 11, 2018 @06:55AM (#16123074)
You are then suggesting that make is not popular and widespread because it's "good". I am saying that, when given the choice, most operating systems and software use 'make' as evidence.
Re:A simple makefile is a unicorn by WillReplyfFood on Thursday January 11, 2018 @08:04AM (#16123438)
Lets blame the users, it lost us the desktop wars- what more damage can this paradigm do?
Re:A simple makefile is a unicorn by avip on Thursday January 11, 2018 @05:18AM (#16122697)
Sure but if all things were kept intuitive, well-specified and simple, no one would have paid us six-figs to maintain it. It's part of the guild protection.
Re:A simple makefile is a unicorn by qznc on Thursday January 11, 2018 @05:30AM (#16122750)
The Make problems I know [0] are:

* Recursive Make Considered Harmful

* Changes to the Makefile do not implicitly trigger rebuilds

* Dependencies on directories is not really possible

* Lots of output by default

* Rules with multiple output files are not really possible

* can't handle spaces in pathnames

[0] https://github.com/qznc/annoying-build-systems#make-alone

Re:A simple makefile is a unicorn by snarfy on Thursday January 11, 2018 @06:20AM (#16122939)
Some more:

* Change tracking entirely depends on modified timestamps instead of something more robust like checksums.

* Makefiles are always platform specific. Make executes commands based on a dependency graph, but it has no commands of its own. You either write multiple makefiles or you build everything in a bash environment.

Re:A simple makefile is a unicorn by qznc on Thursday January 11, 2018 @06:45AM (#16123036)
Do you have a good link/example, why timestamps are bad?

I think in the context of Make it is ok. If you rely on checksums, you must implicitly assume that all commands you execute are idempotent. That assumption easily breaks.

Re:A simple makefile is a unicorn by oriolid on Thursday January 11, 2018 @03:12PM (#16127370)
When I worked with Clearcase (which is terrible in many ways, this is just one), one of its nice features was that it changed file timestamp to the timestamp of commit, not the timestamp it was changed on developer's machine. The commit timestamp may be after previous commit but also before the compiled object file. Because of this, the only way to do a reliable build was to start from scratch. Which took 45 minutes.

Ceterum censeo, Clearcase must be destroyed.

Re:A simple makefile is a unicorn by snarfy on Thursday January 11, 2018 @07:10AM (#16123147)
I don't have a link, but one reason to prefer checksums is if you have intermediary steps that produce artifacts, possibly binary input to another step, that may or may not need to actually be built.
Re:A simple makefile is a unicorn by hzhou321 on Thursday January 11, 2018 @09:31AM (#16124049)
Very nice. It appears to be high time to create my own simple make.
Re:A simple makefile is a unicorn by yodsanklai on Thursday January 11, 2018 @05:05AM (#16122653)
"The "%.o: %.c" line is a huge code smell for Makefiles. You shouldn't ever write that line—it's built in!"

Does it hurt to write it though? It makes the makefile more readable for people who don't know it's built-in. Besides, it's only built-in for ".c". I prefer to keep it in order to have a generic Makefile I can adapt to other programming languages.

Re:A simple makefile is a unicorn by exDM69 on Thursday January 11, 2018 @06:40AM (#16123015)
Either you should not write it, or disable all built-in rules of Make with a flag. Doing both is asking for trouble.

Make has lots of built-in rules for lots of languages, C, C++, Fortran, Lex, Yacc, etc.

Re:A simple makefile is a unicorn by _wp_ on Thursday January 11, 2018 @04:47AM (#16122578)
Could you provide an equivalent Makefile to the one written in the article to demonstrate best practices?
Re:A simple makefile is a unicorn by danaliv on Thursday January 11, 2018 @05:18AM (#16122698)
I am by no means a make expert but this is how I would write it:

  CC = clang
  CFLAGS += -Wall -Wextra
  
  libs = -lm
  objs = hellomake.o
  
  hellomake: $(objs)
  	$(CC) $(CFLAGS) -o $@ $(objs) $(libs)
  
  .PHONY: clean
  
  clean:
  	rm -f hellomake *.o *.d
  
  deps := $(objs:.o=.d)
  
  %.d: %.c
  	$(CC) $(CFLAGS) -MM -MF $@ $<
  
  -include $(deps)
The first two lines tell us which compiler we're using and set up some extra compiler warnings. Then we specify which libraries we use (it's a matter of taste whether this is a variable or just spelled out later) and the names of our object files.

The first target, hellomake, is our executable. This is the default target, so it's the one that'll be built if we run make with no arguments. It runs the C compiler with the flags we specified earlier. Its dependencies are the object files, which means they'll be built automatically and rebuilt when their dependencies change. As noted, there is a built-in rule to build the object files from the C source using our CC and CFLAGS. We don't need to tell make how to do that.

The next target, clean, is a "phony" target—it doesn't refer to a file. Normally a make target tells make how to build a file. A phony target, in contrast, is really just a shell script that doesn't produce any file output. This one deletes all the build products.

The remaining lines set up the dependency tracking for headers. This makes it so that a change in a header file will trigger a rebuild of any source that includes it. We begin by making a list of files by copying the list of object files and swapping in ".d" for the ".o" file extension. Next is a rule that tells how to build a .d file from a .c file. This rule runs the C compiler and tells it to output a dependency tracking file. The last line says to include each of the .d files in the Makefile. (The .d files contain Makefile syntax to specify the header dependencies.)

I omitted the IDIR and ODIR and such because in a project of this size I wouldn't bother putting includes and object files in separate directories from the source.

I hope that helps. I know make can seem outdated and weird but I think it really can be simple once you learn a few things about how it works. Hopefully this will get you started in that direction!

Re:A simple makefile is a unicorn by exDM69 on Thursday January 11, 2018 @06:41AM (#16123023)
Put -MMD to CFLAGS and you can drop the rules to build dependency files. The compiler will then generate the deps files when compiling.

Don't use wildcards in clean target. Use $(RM) $(OBJS) $(DEPS) instead.

Re:A simple makefile is a unicorn by jwilk on Thursday January 11, 2018 @06:23AM (#16122950)
> $(CC) $(CFLAGS) -o $@ $(objs) $(libs)

Please honour LDFLAGS and LDLIBS here.

> $(CC) $(CFLAGS) -MM -MF $@ $<

Please honour CPPFLAGS here.

Re:A simple makefile is a unicorn by beefhash on Thursday January 11, 2018 @04:45AM (#16122573)
> Clang, gcc and many other C compilers support it. Add that and a "-include *.d" line in the Makefile and you'll have great dependency support.

Doesn't that sacrifice portability, though? Is this method portable between GNU make and FreeBSD make and OpenBSD make?

Re:A simple makefile is a unicorn by Tomte on Thursday January 11, 2018 @05:21AM (#16122712)
Why wouldn't it? the *.d files are file snippets in regular make syntax.

The special handling is in the compilers: they emit make file parts!

Re:A simple makefile is a unicorn by beefhash on Thursday January 11, 2018 @06:07AM (#16122902)
Because "-include" is a non-standard[1,2] extension. "include" is standard POSIX behavior[1] but (1) does not seem to support wildcards and (2) errors if the file does not exist, which it never will on initial build. From a glance at the man pages, OpenBSD make does support -include[2], but FreeBSD make does not[3].

[1] cf. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ma...

[2] https://man.openbsd.org/make

[3] https://www.freebsd.org/cgi/man.cgi?make

Re:A simple makefile is a unicorn by pja on Thursday January 11, 2018 @06:16AM (#16122929)
Frankly at this point in time, when people say "Make" they mean GNU Make. Unix distributions that wish to remain stuck in the 1980s with their original BSD Make are welcome to do so, but no sane developer is going to use BSD Make for a build system written any time in the last 25 years unless they have to.
Re:A simple makefile is a unicorn by mannykannot on Thursday January 11, 2018 @08:04AM (#16123436)
Re -MMD: without automated dependency discovery, make was half a tool: if a dependency was omitted, it might do a less-than minimal job of rebuilding, introducing regressions into the build output that did not exist in the source. For this reason, make was quite often used in an unconditional manner, and if you are doing that, you can get the same result more simply and clearly (and without surprises) with a procedural shell script. It is surprising how long it took for that gap in capabilities to be closed.
Re:A simple makefile is a unicorn by JdeBP on Thursday January 11, 2018 @09:52AM (#16124174)
It is not completely closed.

* https://news.ycombinator.com/item?id=15060146

Re:A simple makefile is a unicorn by falsedan on Thursday January 11, 2018 @05:51AM (#16122827)
> I feel like most Make users don't understand Make very well

This indicates that make is hard to understand. I've been using it for a while and I still struggle to grok concepts like order-only prerequisites.