TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Using Makefile(s) for Go

137 点作者 prakashdanish超过 5 年前

20 条评论

IHLayman超过 5 年前
I use Makefiles for Go projects all the time, but not in the way the article describes. First off, in a pre `go mod` world, if you had dependencies to check before running the build, then a Makefile was the easiest way to manage that. But even in a post `go mod` world, there are good reasons to use one that the article totally overlooks:<p>* Makefiles introduce a topological sort to build steps. This is the reason you use it instead of build shell scripts: it allows build steps to run in parallel, it guarantees order by dependency which is the best way to read build steps, and it makes file freshness an easy element to check for a build step, which is still needed for Go projects with multiple subpackages.<p>* Go projects usually have more than go files that are required in making an executable. If you run a web server and you are bundling static pages into your executable, Makefiles are the best way to handle that.<p>* If you are building for multiple architectures, or want to encode the git tag&#x2F;branch into the executable, it is better to have that Makefile bake in the necessary options on the build step and keep it uniform across the build.<p>* If you write a Go file and bake that into a Docker image, I find it best to drop the image and container hashes into files so that I can get to them easily for docker exec&#x2F;attach&#x2F;rm&#x2F;rmi commmands.<p>But there is one bigger reason Makefiles work for our entire team. We standardize on using Makefiles as the entrypoint for our builds. We have a polyglot environment at work so sometimes it gets confusing to figure out how to build a project. By standardizing on running &#x27;make&#x27; we are all on the same page. Have a Javascript project to webpack? Run make and have make call yarn. Have a python wheel to construct? Run make and have make call python setup.py. You have a Java project that requires a sequence of maven commands to build? Run make and have the makefile call maven.<p>Is that inefficient? You bet it is. Does it make it easier to sort out what to do to build a project for the first time? Yes it does. Does it make it 100% easier for our CI&#x2F;CD framework to work with multiple languages and scan for the necessary compilers and dependencies? Heck yeah.<p>[edited for lousy formatting]
评论 #21752794 未加载
评论 #21754476 未加载
评论 #21754323 未加载
echlebek超过 5 年前
I&#x27;ve seen a lot of developers, especially developers with C backgrounds, reach for Makefiles when approaching Go development, and I think it&#x27;s a harmful practice. So, I&#x27;d like to offer a respectful but firm rebuttal to this article. :)<p>I dislike using make(1) with Go for two reasons.<p>The first is that make was developed for building C projects, and therefore is oriented around that task. Building C projects is a lot different than building Go projects, and it involves stitching together a lot of pieces, with plenty of intermediate results.<p>make(1) has first class support for intermediate results, which are expressed as targets.<p>If you look at the article, the author has to use a workaround just to avoid this core feature of make(1).<p>The second reason I dislike using make(1) for Go projects is that it harms portability.<p>A Go project should only require the Go compiler to build successfully. Go projects that need make(1) to build will not work out of the box for Windows users, even though Go is fully supported on Windows. For me, this puts Makefiles into the &quot;nonstarter&quot; category, even though I do all of my own development work on Linux. There is just no reason to complicate things for people who don&#x27;t have make(1) installed.<p>For code generation and other ancillary tasks, Go includes the &#x27;go generate&#x27; facility. This feature was created specifically to free developers from depending on external build tools. (<a href="https:&#x2F;&#x2F;blog.golang.org&#x2F;generate" rel="nofollow">https:&#x2F;&#x2F;blog.golang.org&#x2F;generate</a>)<p>For producing several binaries for one project, use several different main packages in directories that are named what you want your binary to be.<p>Edit: corrected some terminology.
评论 #21752053 未加载
评论 #21752216 未加载
评论 #21752038 未加载
评论 #21752637 未加载
评论 #21752293 未加载
评论 #21744494 未加载
评论 #21752585 未加载
评论 #21753609 未加载
评论 #21753227 未加载
评论 #21752354 未加载
评论 #21752415 未加载
评论 #21753234 未加载
评论 #21752190 未加载
评论 #21753251 未加载
评论 #21752414 未加载
评论 #21754695 未加载
评论 #21751991 未加载
rraval超过 5 年前
This... isn&#x27;t even using the `make` part of Makefiles at all.<p>If you look at the final example, every [1] rule is marked as `.PHONY`. `make` bundles 2 capabilities: a dependency graph and an out-of-date check to rebuild files. This demonstration uses neither.<p>The author would be better served with a shell script and a `case` block. The advantages:<p>- Functions! The `check-environment` rule is really a function call in disguise.<p>- No 2 phase execution. The author talks about using variables like `APP`, but those are make variables with very different semantics than shell variables (which are also available inside the recipes).<p>[1] Yes, there&#x27;s a `check-environment` &quot;rule&quot; that isn&#x27;t marked, but it likely should be since it isn&#x27;t building a file target named `check-environment`.
评论 #21752700 未加载
评论 #21770011 未加载
mikegirouard超过 5 年前
It&#x27;s really frustrating seeing so many Makefiles that don&#x27;t _make_ anything.<p>Make syntax is really odd. I see so many folks go out of their way to deal w&#x2F;quirks of make when they really just need a shell script. You can see this anti-pattern very quickly when you see `.PHONY` targets for everything.<p>I think make is useful for some aspects of go. GOPATH is becoming less relevant now, but still helpful when you want to have build-time dependencies in $PATH<p><pre><code> $(GOPATH)&#x2F;bin&#x2F;some-dependency: go get -u ... </code></pre> I still use make when building artifacts, especially in CI. But as a default, I almost always try to talk folks out of using make for this sort of stuff.
评论 #21754331 未加载
boomlinde超过 5 年前
This Makefile could as well have been a shell script. It doesn&#x27;t track changes to dependencies even when it&#x27;s obvious how to do so. For example, the build rule has an obvious dependency (main.go) and an obvious target ($(APP)). Instead of tracking these which IMO is the primary advantage of using Make, it deliberately destroys the existing build. docked-build always necessarily rebuilds the binary as well<p>Presumably, Go has some kind of build cache making such dependency tracking relatively useless anyway, maybe Docker has too, but if you aren&#x27;t tracking dependencies and rebuilding only when necessary why use Make instead of a big switch in a shell script?<p>Personally I&#x27;d only use Make for Go if I introduce some task that takes significant time and isn&#x27;t already handled by the go toolchain.<p>Another couple of notes: there are two docker-push rules. The first seems like it was meant to be docker-build. The other is that the docker build rule will tag the build with the HEAD hash, regardless of whether it&#x27;s building from a clean checkout or a dirty repo.
peterwwillis超过 5 年前
<i>rm -rf ${APP}</i> is a code smell. If <i>${APP}</i> is not a directory, <i>-r</i> should not be in this command. At best it is possibly confusing, and at worst if somehow <i>${APP}</i> accidentally becomes a directory it will just remove it and you will have no idea that it was a directory, whereas just <i>rm -f ${APP}</i> will fail because it can&#x27;t unlink a directory. Build success is an important factor in a CI&#x2F;CD pipeline, therefore builds should fail immediately under unexpected behavior.<p>Also, on .PHONY on a single line:<p><pre><code> But for Makefiles which grow really big, this is not suggested as it could lead to ambiguity and unreadability, hence the preferred way is to explicitly set phony target right before the rule definition. </code></pre> If your Makefile grows really big, it&#x27;s going to become a nightmare to maintain. Either split up your codebase + builds into sub-directories, or figure out some other way to structure your builds so that it&#x27;s not super complicated to reason about or maintain them.
评论 #21754057 未加载
finaliteration超过 5 年前
I’ve been using Makefiles for Go development basically since I started with the language. It’s really effective for me and makes compilation and, in my case, deployment to AWS Lambdas via CloudFormation commands (also invoked by Make) really simple. It’s also easy to bring someone up to speed with how building and deploying works.
评论 #21752018 未加载
apeace超过 5 年前
Great article, but I&#x27;m not sure it&#x27;s a good idea to segment your Docker images by environment. Part of Docker&#x27;s appeal is that you can be sure your staging &amp; production containers are bit-for-bit the same. I use a workflow like this:<p>* For all commits on all branches, run tests. If tests don&#x27;t pass, don&#x27;t push containers to registry.<p>* For all commits on all branches, build and push a container `{branchname}-{commitsha}` (assuming tests pass).<p>* Code review, etc.<p>* Merge pull request to `master` branch (tests will run, and only push a container if they pass).<p>* Deploy `master-{commitsha}` to staging.<p>* Do your final testing on staging.<p>* Deploy the same `master-{commitsha}` to production.<p>Now you&#x27;re deploying to production from the master branch, which passed tests, and the container is the same one as you tested on staging.<p>Plus, you can always deploy your non-master `{branchname}-{commitsha}` images to a separate environment, or to staging, if you need to do a bit of experimenting.
cesarb超过 5 年前
I noticed you didn&#x27;t mention &quot;.DELETE_ON_ERROR&quot;. AFAIK, it&#x27;s recommended to always use it (according to the GNU make manual: &quot;[...] &#x27;make&#x27; will do this if &#x27;.DELETE_ON_ERROR&#x27; appears as a target. This is almost always what you want &#x27;make&#x27; to do, but it is not historical practice; so for compatibility, you must explicitly request it.&quot;)
alexhutcheson超过 5 年前
Alternatively, you could use Bazel[1], and automatically generate most of your build rules with Gazelle[2].<p>This would allow you to extend your build system beyond what&#x27;s available via &quot;go build&quot;, while avoiding the well-known pitfalls of Makefiles (config complexity, reproducibility, etc.)<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;bazelbuild&#x2F;rules_go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bazelbuild&#x2F;rules_go</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;bazelbuild&#x2F;bazel-gazelle" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bazelbuild&#x2F;bazel-gazelle</a>
评论 #21753429 未加载
hedora超过 5 年前
I suggest reading “recursive make considered harmful”. It is a wonderful introduction to make, and explains how to avoid a few pitfalls most make users (including this article) run into.<p>In particular, the targets in the subdirectory makefiles can and should be auto-generated using make itself. There’s no need for the makefiles in the subdirectories (there is also no need to use an external tool to generate them, which is the other mistake people often make).
rynop超过 5 年前
I think the example discussed in this blog is selling Make short. A more complete example with: protoc(protobuf), Docker, DynamoDB local, go modules (and vendoring), and testing can be found at <a href="https:&#x2F;&#x2F;github.com&#x2F;rynop&#x2F;abp-sam-twirp&#x2F;blob&#x2F;master&#x2F;Makefile" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rynop&#x2F;abp-sam-twirp&#x2F;blob&#x2F;master&#x2F;Makefile</a>
narven超过 5 年前
Nice article. I use makefiles a lot, mostly for all projects that I use, both for frontend and backend, mainly to have the same commands independently of framework&#x2F;platform that im using. For me its helpful to just run `make` both to build and run a go project and a react project.<p>Another thing you can add is:<p>.DEFAULT_GOAL := start<p>start: fmt swag vet build run<p>Helps define you default command soyou just need to run `make` and will run all inside of `start`<p>Since most of us use `.env` files for enviroment files, you can use something like:<p># this allows to import into this file all current system envs<p>include .env<p>export<p>And it will inject all of .env file into the current running `process`<p>Also have some other shortcuts (variables):<p>GOCMD=go<p>GOBUILD=$(GOCMD) build<p>GOCLEAN=$(GOCMD) clean<p>GOTEST=$(GOCMD) test<p>GOFMT=gofmt -w<p>GOGET=$(GOCMD) mod download<p>GOVER=$(COCMD) vet<p>GOFILES=$(shell find . -name &quot;*.go&quot; -type f)<p>BINARY_NAME=my-cool-project<p>BINARY_UNIX=$(BINARY_NAME)_prod
ascotan超过 5 年前
Going to throw out my 2 cents here:<p>1. I don&#x27;t like multiple makefiles. Icky with lots of duplication and high maintenance. Bad article.<p>2. When possible I use target expansion to generate targets in the main makefile:<p>APPS:= app1 app2 app3<p>$(APPS:%=build.%)<p>3. I prefer to use makefile functions rather than reaching for &quot;bash&quot; where possible: <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;make&#x2F;manual&#x2F;html_node&#x2F;Functions.html#Functions" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;make&#x2F;manual&#x2F;html_node&#x2F;Functions...</a><p>4. If something is really complicated - extract to bash<p>Make has been written in 10K languages and the original is still the best
3fe9a03ccd14ca5超过 5 年前
I don’t mind Makefiles, and usually use them for basic command configuration.<p>However, when the logic gets even a little complicated and I almost always reach for bash. Everybody knows a little bash, and it’s available in almost all systems.
jjuel超过 5 年前
Not related to the contents of the article, but I love the font on that site. I also love the simplicity of the site as well. Nothing takes away from the ability to read. It is so clear and concise.
knowsuchagency超过 5 年前
There really is no perfect build tool, but in my experience, nothing touches invoke <a href="http:&#x2F;&#x2F;www.pyinvoke.org" rel="nofollow">http:&#x2F;&#x2F;www.pyinvoke.org</a> for building and automation.<p>Any project will eventually have build and deployment scripts with non-trivial amounts of logic in them.<p>The question then becomes whether you want all that complex logic in shell scripts, makefiles, or Python.<p>For me, it&#x27;s a no-brainer. I&#x27;ll take the latter every time.
cwojno超过 5 年前
You don&#x27;t use makefiles in go! You just take your code, copy go.mod and go.sum into a Docker image, then RUN mod download, then re-copy the rest of the code and run bui...<p>Shit... Docker is a makefile...
roadbeats超过 5 年前
It looks very close to <a href="https:&#x2F;&#x2F;github.com&#x2F;azer&#x2F;go-makefile-example" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;azer&#x2F;go-makefile-example</a>
e2le超过 5 年前
For projects that use tags to turn on&#x2F;off compilation options, Makefiles and shell scripts make sense.