TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Self-Documented Makefile

249 pointsby fzaninottoabout 9 years ago

15 comments

ArnaudRinquinabout 9 years ago
As I used `make` more and more for our node projects, I missed the clean outputs `grunt` or `gulp` provide.<p>To fix that, I created `make2tap`: <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;make2tap" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;make2tap</a><p>This small utility takes a `make` output and generate a `TAP` one that you can pipe to any `TAP reporter`.<p>Our current `make build | make2tap | tap-format-spec` looks like: <a href="http:&#x2F;&#x2F;i.imgur.com&#x2F;chs0Jf3.png" rel="nofollow">http:&#x2F;&#x2F;i.imgur.com&#x2F;chs0Jf3.png</a>
评论 #11203511 未加载
chubotabout 9 years ago
They should really be using bash for this, not make. There is nothing wrong with bash scripts calling Make -- for building with DEPENDENCIES. But when you aren&#x27;t doing that, just use bash (because Make is actually Make + bash to begin with).<p>This is dumb:<p><pre><code> restart-frontend: ## Restart the frontend and admin apps in dev @make stop-frontend-dev &amp;&amp; make run-frontend-dev @echo &quot;Frontend app restarted&quot; </code></pre> Write it with a shell script like this:<p><pre><code> stop-frontend-dev() { ... } run-frontend-dev() { ... } restart-front-end() { stop-frontend-dev run-frontend-dev echo &quot;Frontend app restarted&quot; } build() { make # this actually does stuff you can&#x27;t do in bash. } &quot;$@&quot; # call function $1 with args $2... Can also print help here. </code></pre> This is a lot cleaner. The PID stuff can be done with bash too.
评论 #11202544 未加载
评论 #11202746 未加载
评论 #11201288 未加载
评论 #11202849 未加载
评论 #11201163 未加载
jdpabout 9 years ago
I like the ideas here, but for long-running processes like file watching, dev servers, hot reloading, etc. a better format is Procfile (<a href="https:&#x2F;&#x2F;devcenter.heroku.com&#x2F;articles&#x2F;procfile" rel="nofollow">https:&#x2F;&#x2F;devcenter.heroku.com&#x2F;articles&#x2F;procfile</a>). The ideas from this article could be nicely applied to it.<p>Procfil is a format that declares a named list of processes to be run that can be controlled by tools like Foreman (<a href="http:&#x2F;&#x2F;ddollar.github.io&#x2F;foreman&#x2F;" rel="nofollow">http:&#x2F;&#x2F;ddollar.github.io&#x2F;foreman&#x2F;</a>) and Honcho (<a href="https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;honcho" rel="nofollow">https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;honcho</a>). The advantage is being able to start and stop them concurrently as a group, useful for things that otherwise take a tmux session or multiple windows&#x2F;tabs, like dev server + file watching + live reload: they become a simple `foreman start`. Processes can also be started individually. Procfiles can also be exported to other formats, like systemd, upstart, inittab, etc.<p>Here&#x27;s an example Procfile from a web project I&#x27;ve been working on. Since it uses node I went with node tools like http-server and watch, but it could just as easily use any other web server or file watcher. The way it works is it starts a web server serving public&#x2F;; starts a live reload server for public&#x2F;; and watches the src&#x2F; directory for changes and re-runs make. The makefile has a few rules for compiling JS and CSS from src&#x2F; to public&#x2F;.<p><pre><code> web: .&#x2F;node_modules&#x2F;.bin&#x2F;http-server livereload: .&#x2F;node_modules&#x2F;.bin&#x2F;livereload public watch: .&#x2F;node_modules&#x2F;.bin&#x2F;watch make src</code></pre>
评论 #11201362 未加载
评论 #11200483 未加载
yxlxabout 9 years ago
&gt;Wouldn’t it better if we could just type make, and get a list of available commands, together with their desciption?<p>No, Jesus Christ, please don&#x27;t. Preserve default action as being to build.<p>Good user interface and good user experience relies on meeting expectations. This behavior breaks those expectations. What if your expectations are different, you ask? In environments where there is an established tradition I think it is rude to break with the norm unless there is a compelling reason to do so. The commandline is popular among developers, other IT professionals and power users because of how efficient it is. It is efficient because there is not all the noise, handholding and other bullsh*t. Please let us keep it that way.<p>Use a specific target to list other targets. I&#x27;ve seen some people use &quot;make targets&quot;.
评论 #11200613 未加载
评论 #11201063 未加载
评论 #11200476 未加载
评论 #11200484 未加载
评论 #11202041 未加载
评论 #11201079 未加载
jgrahamcabout 9 years ago
An alternative way that just uses GNU make functions: <a href="http:&#x2F;&#x2F;www.cmcrossroads.com&#x2F;article&#x2F;self-documenting-makefiles" rel="nofollow">http:&#x2F;&#x2F;www.cmcrossroads.com&#x2F;article&#x2F;self-documenting-makefil...</a>
saikiabout 9 years ago
Thanks for sharing! This is a great way to document and see documentation for main targets in Makefile.<p>We use Makefile in same way to execute project related tasks like deployments and run development environments. This will even further help to show main targets from a Makefile easily and pretty standard way. Will be taken into use.<p>You can achieve similar by writing bash scripts, but it will be mostly custom and others need to learn how to use it and extend it. Makefile gives you a standard way of writing small utilities related to all your project, and almost everybody knows how Makefile works and if not, they can learn from existing documentation.
kluckabout 9 years ago
Good idea but target names might contain numbers as well, so you should adjust the regular expression used:<p>@grep -P &#x27;^[a-zA-Z0-9_-]+:.<i>?## .</i>$$&#x27; $(MAKEFILE_LIST) | ...
m6w6about 9 years ago
Shorter, more fail-proof help target:<p><pre><code> help: @awk -F &#x27;:|##&#x27; \ &#x27;&#x2F;^[^\t].+?:.*?##&#x2F; {\ printf &quot;\033[36m%-30s\033[0m %s\n&quot;, $$1, $$NF \ }&#x27; $(MAKEFILE_LIST) </code></pre> ... but I agree, breaking expectations is somewhat bad. Also, many shells have completion for Makefiles nowadays, though, that won&#x27;t get you an additional help text.
to3mabout 9 years ago
You probably want to be using phony targets if your Makefile consists of stuff like this. See: <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;make&#x2F;manual&#x2F;html_node&#x2F;Phony-Targets.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;make&#x2F;manual&#x2F;html_node&#x2F;Phony-Tar...</a>
spoilerabout 9 years ago
I&#x27;ve been using a similar self-documenting technique myself for a while now, too. Although, my version preserved the traditional part where just calling `make` starts building the program and also supported short one line descriptions and longer ones.<p>Slightly OT: I like how Rake handles this, which is what gave me the idea in the first place
rekadoabout 9 years ago
Instead of using `awk` to break lines you could just use `fmt`, which is part of the GNU coreutils.
tempodoxabout 9 years ago
Change `grep -P` to `grep -E` (or simply `egrep`) and it also works on OS X.
评论 #11201506 未加载
beaufilsabout 9 years ago
Here is a version with no dependencies to grep or awk but just sed. I did not tested it on OS X yet.<p><pre><code> help: @eval $$(sed -r -n &#x27;s&#x2F;^([a-zA-Z0-9_-]+):.*?## (.*)$$&#x2F;printf &quot;\\033[36m%-30s\\033[0m %s\\n&quot; &quot;\1&quot; &quot;\2&quot; ;&#x2F;; ta; b; :a p&#x27; $(MAKEFILE_LIST) | sort)</code></pre>
评论 #11205730 未加载
ehartsuykerabout 9 years ago
I do this in my bash scripts, if anyone wants to see how.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ehartsuyker&#x2F;node-deb&#x2F;blob&#x2F;ede596b2c8a07f1f1925e4f8e51df5ba9ef15be3&#x2F;node-deb#L44" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ehartsuyker&#x2F;node-deb&#x2F;blob&#x2F;ede596b2c8a07f1...</a>
Gratsbyabout 9 years ago
But then you have lost the default target of make and instead of make &amp;&amp; make install you end up with make build &amp;&amp; make install. That&#x27;s going to break a brain or two when people try to figure out why their default MO doesn&#x27;t work.