corpix / go-makefile

Makefile generator for go projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-makefile

Build Status

Little tool to generate a Makefile for your project.

Use it to get a minimal boilerplate with a Makefile for your project.

Usage

Two kinds of Makefiles could be generated by this tool:

  • package
  • application

Each of them will contain rules for corresponding entity.

Application Makefile will contain everything the package needs but not vice versa.

Kinds could be simplified until they are not ambiguous, for example:
application -> app -> a
package -> pack -> p

$ cd $GOPATH/src/github.com/corpix/some-package
$ go-makefile --write-config .go-makefile.json > Makefile

This will write code to the Makefile in current directory:

.DEFAULT_GOAL = all

version  := $(shell git rev-list --count HEAD).$(shell git rev-parse --short HEAD)

name     := some-package
package  := github.com/corpix/$(name)
packages := $(shell go list ./... | grep -v /vendor/)

.PHONY: all
all:: dependencies
all:: build

.PHONY: dependencies
dependencies::
        dep ensure

.PHONY: test
test::
        go test -v $(packages)

.PHONY: bench
bench::
        go test -bench=. -v $(packages)

.PHONY: lint
lint::
        go vet -v $(packages)

.PHONY: check
check:: lint test

.PHONY: clean
clean::
        git clean -xddff

If you need additional includes(or other kind, or specify github username and project) you could pass --include parameter in this manner:

go-makefile             \
    --kind app          \
    --user your-name    \
    --name project-name \
    --include           \
    build.mk            \
    ci.mk

Includes could be used to extend the builtin Makefile targets, all targets are «(double-colon)[#goals]».

All of them will be appended to the list of the includes which will be appended to the end of the Makefile.

Saving configuration

You could save parameters which was used to generate Makefile.

There are --read-config and --write-config arguments.

To write your config while generating Makefile you could use:

go-makefile --kind app --user your-name --name project-name --write-config go-makefile.json

To read previously stored config and use them to generate Makefile:

go-makefile --read-config go-makefile.json

Goals

Each goal is a double-colon rule so you could define your custom logic for any Makefile rule using includes.

all

Available for --kind package and --kind application.

For --kind package:

  • install all dependencies

For --kind application:

  • install all dependencies
  • build an application

build

Available for --kind package and --kind application.

Run go build against project sources. For project name test entrypoint should be in test/main.go.

test

Available for --kind package and --kind application.

Will reach dependencies target after that will run tests for project package.

bench

Available for --kind package and --kind application.

Will reach dependencies target after that will run tests and benchmarks for project package.

dependencies

Available for --kind package and --kind application.

Install dependencies with dep.

$(name)

Available only for --kind application.
Name of this target depends on the --name flag which you should specify to generate a Makefile.

It will build a binary release for the project.

Variables

name

Name of the project.

It depends on the --name flag which you should specify to generate a Makefile.

package

Package of the project.

This is an absolute package name which should be used to import your project like any other go project would do.

It depends on the --project flag which you should specify to generate a Makefile.

version

Version number retrieved from the git version control.

It could be customized with --version-generator argument.

It will look like 100.abcdef where:

  • 100 is a commit count from the first commit to HEAD
  • abcdef is a short HEAD sha1 sum

build_id

Build id which usually written to the binary file and helps identify the build.

It will be derived from the version.

It could be customized with --build-id-generator argument.

ldflags

Linker flags.

It passes this variables to go build tools:

  • version
  • build_id

Recommended application layout

Our example project name is github.com/corpix/hello.

~/go/src/github.com/corpix/hello
├── cli
│   └── cli.go
└── hello
    └── main.go

Where:

  • hello/main.go is an entrypoint
  • cli/cli.go is a part of the package cli

cli/cli.go

It should have version variable to allow linker automatically tag a build with version.

package cli

var version string

func Run() { ... }

License

MIT

About

Makefile generator for go projects

License:MIT License


Languages

Language:Python 71.7%Language:Shell 17.0%Language:Nix 10.0%Language:Makefile 1.2%