Update: Makefile for Golang CLI tools
This commit is contained in:
parent
31f054881a
commit
a31ea5c504
|
@ -7,7 +7,7 @@ slug = "a-makefile-for-golang-clie-tools"
|
||||||
+++
|
+++
|
||||||
|
|
||||||
It's no secret I love the power and simplicity of Go. To further train my skills I wrote a simple app that will roll dice from the
|
It's no secret I love the power and simplicity of Go. To further train my skills I wrote a simple app that will roll dice from the
|
||||||
command line, because you know, that's very useful. (I'm not even sure I'm going to use this a my next Dungeons & Dragons session.)
|
command line, because you know, that's very useful.
|
||||||
|
|
||||||
There are two goals for me in this project right now: make it trivial to use compile time variables and have a `Makefile` for easy
|
There are two goals for me in this project right now: make it trivial to use compile time variables and have a `Makefile` for easy
|
||||||
compilation, installation and clean up. I'm sure I'll think of other features to try. These will get their own posts.
|
compilation, installation and clean up. I'm sure I'll think of other features to try. These will get their own posts.
|
||||||
|
@ -22,7 +22,7 @@ manually edit code before each compile, because you would _never_ forget to do t
|
||||||
BuildTime = "2015-10-03T11:08:49+0200"
|
BuildTime = "2015-10-03T11:08:49+0200"
|
||||||
)
|
)
|
||||||
|
|
||||||
Luckily there's a nice alternative provided by Go. The `link` [docs](https://golang.org/cmd/link/) command allows you to set
|
Luckily there's a nice alternative provided by Go: the `link` [docs](https://golang.org/cmd/link/) command allows you to set
|
||||||
string variables at compile time with the `-X` option. Let's take a look at our code and build command.
|
string variables at compile time with the `-X` option. Let's take a look at our code and build command.
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,9 +35,11 @@ Compilation would look like this:
|
||||||
|
|
||||||
go build -ldflags "-X github.com/ariejan/roll/core.Version=1.0.0 -X github.com/ariejan/roll/core.BuildTime=2015-10-03T11:08:49+0200" main.go
|
go build -ldflags "-X github.com/ariejan/roll/core.Version=1.0.0 -X github.com/ariejan/roll/core.BuildTime=2015-10-03T11:08:49+0200" main.go
|
||||||
|
|
||||||
|
_Note: this is the format used with Go 1.5.1, previous versions do not use the `=` sign, instead separate the variable and value with a space._
|
||||||
|
|
||||||
## The Makefile
|
## The Makefile
|
||||||
|
|
||||||
Makefiles have always been scary to me. Lot's of magic and weird syntax and I never had a need or desire to dive into them. As it turns out,
|
Makefiles have always been scary to me. Lot's of magic and weird syntax and I've never had the need nor the desire to dive into them. As it turns out,
|
||||||
Makefiles can be very useful. Let's start by building one for the `roll` project.
|
Makefiles can be very useful. Let's start by building one for the `roll` project.
|
||||||
|
|
||||||
First, let's start with the build command that passes in `Version` and `BuildTime` and refactor it so it becomes more managable and we can
|
First, let's start with the build command that passes in `Version` and `BuildTime` and refactor it so it becomes more managable and we can
|
||||||
|
@ -124,3 +126,5 @@ mark these targets with `.PHONY`, telling Make not to expect a file to appear.
|
||||||
|
|
||||||
It's a basic `Makefile` that makes compiling your Golang command line tools a whole lot easier. Enjoy, and stay tuned for more
|
It's a basic `Makefile` that makes compiling your Golang command line tools a whole lot easier. Enjoy, and stay tuned for more
|
||||||
posts on Golang and Makefiles.
|
posts on Golang and Makefiles.
|
||||||
|
|
||||||
|
You can find the code for Roll at [https://github.com/ariejan/roll](https://github.com/ariejan/roll).
|
||||||
|
|
Loading…
Reference in New Issue
Block a user