Live data from Hacker News

One line you should add to every makefile

blog.jgc.org

31–40 of 99 posts

Re: One line you should add to every makefile

#31

print-%: ; @echo $*=$($*) .PHONY: print-% This way the rule continues to work even if such a file suddenly exists in your repo.

No, implicit rules cannot be marked as .PHONY this way. You need to declare an explicit pseudo-target to achieve this.

  .PHONY: FORCE
  FORCE:
  print-%: FORCE ; @echo $*=$($*)

Re: One line you should add to every makefile

#32
post #21

I'd say that most people should not write makefiles by hand. You should use some higher level build system, that knows how to deal with source files in your language, track their dependencies, etc. A "hello world" makefile with one source file looks simple, but once the project gets more complicated, you quickly end up building such a high level build system yourself, with the additional disadvantage of restricting y…

It may be my inexperience speaking, but every time I come across code that was built not by hand I visibly cringe and search for something else. It's not that it's bad, it's just that if I don't understand it then there is nothing for me to fix or play around with.

I am old enough to remember when assembly programmers used to use that complaint as a reason not to program in C. And when C programmers would use that complaint as a reason not to program with templates in C++. And when C++ programmers used that complaint as a reason not to program in languages with garbage collection.

The truth is that no matter what level you program at, you are depending on a long toolchain that you don't understand the details of. You are explicitly aware of not understanding autogenerated stuff at your level. But are ignoring how little you understand of what is underneath the level you are used to programming at.

Get used to it. A few years back I remember an article that started by diving into what actually happens between pressing a key on the keyboard and a letter appearing on the screen. I wish I could find it for you. It was a long article. And repeatedly got into too much detail, then narrowed down the scope and got into more detail. Over and over again.

You don't actually understand how your computer or code works. Instead you create a useful working model and proceed with that. Said working models can include both lower levels than your usual, or you can build up higher levels. Get used to it, take advantage of good tools, and you will accomplish more. The alternative is to get stuck in what you know, refuse to go outside of that boundary, and be less productive. The choice is yours.

Re: One line you should add to every makefile

#33
post #3

I firmly believe that there is only one production-quality makefile that has ever been written from scratch. Every other makefile has been copied from an earlier makefile, dating back to the one Ur-makefile, and modified to suit the author's purpose.

Not exactly true. I tend to start with a new makefile for a good amount of projects. Know your tools, know your code.

Re: One line you should add to every makefile

#34
Ohh, that is a great trick. I've added it.

I go back and forth on Make. It was where I started so there is some bias there but generally it has always been possible to do what I want with it. The places where it bites me were things that gmake added which added, to me, features which could be cleverly exploited but made things much more complicated and error prone. I flirted with SCons and other build systems, I was amazed at how flexible Google's was (and had to be given the complexity embodied in it) but for small projects (where small is perhaps a couple of hundred source files, and a half dozen libraries) it still is my goto build tool of choice.

Re: One line you should add to every makefile

#35
post #30

Earlier quoted context omitted.

The problem is that C/C++ files have internal depenedencies which make needs to know about. Yes, for gcc you can use a simple trick to extract the dependencies to makefile rules, but in case you use a different compiler, you need to do it yourself. If you are compiling a different language, you need to do it yourself again. Using something like CMake or even automake, which can automatically track the dependencies, s…

How do they track the dependencies automatically?

[deleted]

Re: One line you should add to every makefile

#36
post #14
post #4

That's one huge advantage with Rake, just add `p VARIABLE_NAME` anywhere in your Rakefile, and you'll see the textual value of it at that point.

That misses the point; you can already echo a variable wherever you want in a makefile similarly. This gives you a way to do it without editing the makefile, without knowing before hand you want to see that variable.

Echo and p aren't exactly the same, there's a difference during debugging between:

    1 2 3 4 (echo)
and

    ["1", "2 3", "4"] (p)

Re: One line you should add to every makefile

#37
post #3

I firmly believe that there is only one production-quality makefile that has ever been written from scratch. Every other makefile has been copied from an earlier makefile, dating back to the one Ur-makefile, and modified to suit the author's purpose.

:)

In my EE/Physics department, lab reports from previous years were known as "newtons" (as in: "do you have a newton for the preservation-of-momentum experiment?"), the explanation being that Isaac Newton wrote a lab report from scratch, and everyone else has used an older lab report as reference to make sure they got the right result - the transitive closure of which having Newton as a boundary.

I guess the equivalent Makefile term would be a Feldman[0], as in: "Do you have a feldman that can combine ghc and dmd output? I need that for a project"

[0] http://en.wikipedia.org/wiki/Stuart_Feldman

Re: One line you should add to every makefile

#38
post #32
post #21

Earlier quoted context omitted.

It may be my inexperience speaking, but every time I come across code that was built not by hand I visibly cringe and search for something else. It's not that it's bad, it's just that if I don't understand it then there is nothing for me to fix or play around with.

I am old enough to remember when assembly programmers used to use that complaint as a reason not to program in C. And when C programmers would use that complaint as a reason not to program with templates in C++. And when C++ programmers used that complaint as a reason not to program in languages with garbage collection. The truth is that no matter what level you program at, you are depending on a long toolchain that…

I'm sure that, when those skilled engineers cringed at the next higher layer of the stack, that higher layer wasn't reliable yet, it probably had some stability issues and serious performance issues. When it was fully solid they moved on.

I have a degree in electrical engineering, though these days I work on server / infrastructure software. Yeah I have a pretty good idea how stuff works at the c/c++ level and below.

Re: One line you should add to every makefile

#39

Better article from years ago, that this blogger is probably rehashing anyway: http://blog.melski.net/2010/11/30/makefile-hacks-print-the-v...

that this blogger is probably rehashing anyway Nope. Eric (who wrote that blog you are referrring to is a friend), but I wrote this little trick up years before him: http://www.cmcrossroads.com/article/printing-value-makefile-... and now I'm rehashing my own writing 10 years later.

I shouldn't be surprised that you published that before I did -- to be fair, it's quite a challenge to find _any_ topic related to make that you _haven't_ written about. :)

Re: One line you should add to every makefile

#40

Better article from years ago, that this blogger is probably rehashing anyway: http://blog.melski.net/2010/11/30/makefile-hacks-print-the-v...

Your supposition about rehashing proved to be wrong, but the subsequent trick in the article you linked about injecting the extra line without modifying the original Makefile is an interesting twist that I had not seen.
Post reply on HN