Skip to content

Commit

Permalink
Merge pull request #105 from davidalpert/GH-103
Browse files Browse the repository at this point in the history
GH-103 - Refactor build script
  • Loading branch information
darcys22 authored Nov 24, 2020
2 parents 9a55d75 + 57e04d7 commit 26995bb
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 223 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ password.txt
# Ignore Build
release
/build/_workspace/
/build/cache/
/build/.cache/
/build/bin/
/build/dist/
build/_vendor/pkg
cert
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
env:
- GO111MODULE=on
script:
- go run utils/ci.go install
- go run utils/ci.go build
- go run utils/ci.go test -coverage $TEST_PACKAGES

- stage: build
Expand All @@ -36,7 +36,7 @@ jobs:
env:
- GO111MODULE=on
script:
- go run utils/ci.go install
- go run utils/ci.go build
- go run utils/ci.go test -coverage $TEST_PACKAGES


Expand Down
128 changes: 87 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,108 @@
BINARY := godbledger
VERSION ?= latest
PLATFORMS := linux
os = $(word 1, $@)

GOBIN = ./build/bin GO ?= latest
GODIST = ./build/dist
GO ?= latest
GORUN = env GO111MODULE=on go run

.PHONY: $(PLATFORMS)
$(PLATFORMS):
mkdir -p release/$(BINARY)-$(os)-x64-v$(VERSION)/
GOOS=$(os) GOARCH=amd64 GO111MODULE=on go build -o release/$(BINARY)-$(os)-x64-v$(VERSION)/ ./...
xtarget = $(strip $(subst build-,,$@)) # e.g. 'build-linux-amd64' -> 'linux-amd64'
xdest = $(GODIST)/$(xtarget)

.PHONY: release
release: linux
# 'default' target builds all binaries for local development/testing
default: build-native

# 'release' target builds os-specific builds of only godbledger using xgo/docker
release: build-cross

PHONY: clean
clean:
rm -rf build/.cache
rm -rf build/bin
rm -rf build/dist
rm -rf release/
rm -rf cert/

build-native:
$(GORUN) utils/ci.go build

lint:
GO111MODULE=on go run utils/ci.go lint
$(GORUN) utils/ci.go lint

# our tests include an integration test which expects the local
# GOOS-based build output to be in the ./build/bin folder
test: build-native
$(GORUN) utils/ci.go test

travis:
GO111MODULE=on go run utils/ci.go install
GO111MODULE=on go run utils/ci.go test -coverage $$TEST_PACKAGES
travis: build-native
$(GORUN) utils/ci.go test -coverage $$TEST_PACKAGES

# -------------------------------------
# release_pattern=current
#
linux:
mkdir -p release/godbledger-linux-x64-v$(VERSION)/
GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o release/godbledger-linux-x64-v$(VERSION)/ ./...

linux-arm-7:
mkdir -p release/$(BINARY)-arm7-v$(VERSION)/
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 GO111MODULE=on go build -o release/$(BINARY)-arm7-v$(VERSION)/ ./...
mkdir -p release/godbledger-arm7-v$(VERSION)/
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 GO111MODULE=on go build -o release/godbledger-arm7-v$(VERSION)/ ./...

linux-arm-64:
mkdir -p release/$(BINARY)-arm64-v$(VERSION)/
env CC=aarch64-linux-gnu-gcc CXX=aarch-linux-gnu-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -o release/$(BINARY)-arm64-v$(VERSION)/ ./...
mkdir -p release/godbledger-arm64-v$(VERSION)/
env CC=aarch64-linux-gnu-gcc CXX=aarch-linux-gnu-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -o release/godbledger-arm64-v$(VERSION)/ ./...
# -------------------------------

build-cross: build-linux build-darwin build-windows

godbledger-linux-arm: godbledger-linux-arm-5 godbledger-linux-arm-6 godbledger-linux-arm-7 godbledger-linux-arm64
build-linux: build-linux-386 build-linux-amd64 build-linux-arm
@echo "Linux cross compilation done:"
@ls -ld $(GODIST)/linux-*

build-linux-386:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/386 -- --go=$(GO)

build-linux-amd64:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/amd64 -- --go=$(GO)

build-linux-arm: build-linux-arm-5 build-linux-arm-6 build-linux-arm-7 build-linux-arm64
@echo "Linux ARM cross compilation done:"
@ls -ld $(GOBIN)/godbledger-linux-* | grep arm

godbledger-linux-arm-5:
$(GORUN) utils/ci.go xgo -- --go=$(GO) --targets=linux/arm-5 -v ./..
@echo "Linux ARMv5 cross compilation done:"
@ls -ld $(GOBIN)/godbledger-linux-* | grep arm-5

godbledger-linux-arm-6:
$(GORUN) utils/ci.go xgo -- --go=$(GO) --targets=linux/arm-6 -v ./godbledger
@echo "Linux ARMv6 cross compilation done:"
@ls -ld $(GOBIN)/godbledger-linux-* | grep arm-6

godbledger-linux-arm-7:
$(GORUN) utils/ci.go xgo -- --go=$(GO) --targets=linux/arm-7 -v ./godbledger
@echo "Linux ARMv7 cross compilation done:"
@ls -ld $(GOBIN)/godbledger-linux-* | grep arm-7

godbledger-linux-arm64:
$(GORUN) utils/ci.go xgo -- --go=$(GO) --targets=linux/arm64 -v ./godbledger
@echo "Linux ARM64 cross compilation done:"
@ls -ld $(GOBIN)/godbledger-linux-* | grep arm64
@ls -ld $(GODIST)/linux-arm*

build-linux-arm-5:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/arm-5 -- --go=$(GO)

build-linux-arm-6:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/arm-6 -- --go=$(GO)

build-linux-arm-7:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/arm-7 -- --go=$(GO)

build-linux-arm64:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target linux/arm64 -- --go=$(GO)

build-darwin: build-darwin-10.6-amd64
@echo "Darwin cross compilation done:"
@ls -ld $(GODIST)/darwin-*

build-darwin-10.6-amd64:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target darwin-10.6/amd64 -- --go=$(GO)

build-windows: build-windows-4.0-386 build-windows-4.0-amd64
@echo "Windows cross compilation done:"
@ls -ld $(GODIST)/windows-*

build-windows-4.0-386:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target windows-4.0/386 -- --go=$(GO)

build-windows-4.0-amd64:
@echo "building $(xtarget)"
$(GORUN) utils/ci.go xgo --target windows-4.0/amd64 -- --go=$(GO)

.PHONY: cert
cert:
Expand Down
77 changes: 72 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Go DB Ledger
# Go DB Ledger

[![Build Status]][Build Link] [![Book Status]][Book Link] [![Chat Badge]][Chat Link]

Expand Down Expand Up @@ -31,7 +31,7 @@ https://github.com/darcys22/godbledger/wiki/Quickstart
| `Ledger_cli` | A CLI client that can be used to transmit transactions to the server. |
| `Reporter` | Builds basic reports from the database on the command line. |

### Communicating with Godbledger and software examples
## Communicating with Godbledger and software examples

**GRPC and Proto Buffers**
The primary way to communicate with Godbledger is through the GRPC endpoint, submitting a transaction that contains your journal entry/transaction.
Expand Down Expand Up @@ -64,7 +64,7 @@ The PDF files are generated from [handlebars](https://handlebarsjs.com/) iterati

Templates can be viewed [here](https://github.com/darcys22/pdf-generator)

### Database and configuration
## Database and configuration

Godbledger will set a default configuration if none has been provided using Sqlite3 as the default database.

Expand All @@ -73,13 +73,13 @@ The config file can be found by default at:
~/.ledger/config.toml
```

### Building the Proto Buffers
## Building the Proto Buffers
Call from the root directory
```
protoc -I proto/ proto/transaction.proto --go_out=plugins=grpc:proto
```

### SQL Querys
## SQL Querys
default stored location for database is .ledger/ledgerdata `sqlite3 ledger.db`

**Select all transactions**
Expand All @@ -94,6 +94,73 @@ SELECT * FROM accounts where account_id in (select account_id from account_tag w
```

## Contributing

### Local Development

1. Install golang version 1.13 or higher for your OS and architecture:

- https://golang.org/doc/install

1. To build the `godbledger` executables natively for your OS and architecture you can simply use Make

```
make
```

The default make target is `build-native` which builds binaries native to your environment into the `./build/bin/native/` folder.

NOTE: on windows you may need to install a C++ tool chain (e.g. [`tdm-gcc`](https://jmeubank.github.io/tdm-gcc/)) in order to cross compile the sqlite dependency.

After building you can run the version you just built:

```
./build/bin/native/godbledger
```

1. Run the linter to discover any style or structural errors:

```
make lint
```

1. Run the tests

```
make test
```

NOTE: the test suite depends on the `build-native` target as it includes an integration test which spins up an instance of `godbledger`

### Build architecture

The primary entrypoint into the build scripts is the `Makefile` which provides the aforementioned build targets:
- `build-native` (default)
- `lint`
- `test`

All three of which call into the `./utils/ci.go` script to do the actual work of setting up required env vars, building the executiables, and configuring output folders.

An additional `./utils/make-release.sh` script is available to help orchestrate the creation of zip/tarfiles.

### Cross-compiling with xgo/docker

In addition to the default, native build target, the `Makefile` also offers a `build-cross` target which uses a forked version of `xgo` (https://github.com/techknowlogick/xgo) to build for different operating systems and architectures, including linux variants, a MacOS-compatible binary, and windows-compatible exe files.

```
make build-cross
```

Go tooling natively offers cross-compiling features when the `CGO_ENABLED=0` flag is set; `godbledger`'s `go-sqlite3` dependency however requires `CGO_ENABLED=1` in order to link in the C-level bindings for SQLite. Cross-compiling golang when `CGO` is enable is significantly more complicated as each platform and architecture can require a custom C++ toolchain.

`xgo` achieves consistency in cross-compilation using Docker, so running Docker Engine on your dev box is a requirement to running the `build-cross` target.

#### Install Docker Engine

The Docker web site includes detailed instructions on [installing and running Docker Engine](https://docs.docker.com/engine/install/) on a variety of supported platforms.

NOTE: if installing Docker Engine on a linux system make sure to follow the [Post-installation steps for Linux](https://docs.docker.com/engine/install/linux-postinstall/) in order to be able to run `docker` commands from local user accounts.

### TODO/Milestones
- ~~GoDBLedger server runs and accepts transactions~~
- ~~trial balance and transaction reports of journals~~
Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/cespare/cp v1.1.1 // indirect
github.com/ethereum/go-ethereum v1.9.15
github.com/go-sql-driver/mysql v1.5.0
github.com/gogo/protobuf v1.1.1
github.com/golang/protobuf v1.4.2
github.com/gophers/xgo v0.0.0-20200104073656-1317f74b9001 // indirect
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7
github.com/karalabe/xgo v0.0.0-20191115072854-c5ccff8648a7 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/marcmak/calc v0.0.0-20150509200512-5bbbfc3b3149
github.com/mattn/go-colorable v0.1.7
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/onsi/ginkgo v1.7.0 // indirect
github.com/onsi/gomega v1.4.3 // indirect
github.com/rs/xid v1.2.1
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.4.0
github.com/urfave/cli/v2 v2.2.0
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/grpc v1.30.0
src.techknowlogick.com/xgo v1.0.1-0.20200717030703-5c3bb7fc435e // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
Loading

0 comments on commit 26995bb

Please sign in to comment.