diff --git a/Makefile b/Makefile index 12450d9..0769099 100644 --- a/Makefile +++ b/Makefile @@ -18,13 +18,29 @@ build: go build -v -o $(OUT_FILE) $(GO_FILE) @ls -lh $(OUT_FILE) -# Build production binary. +# Build statically linked production binary. release: clean @{\ - LD_FLAGS="-s -w" ;\ - CGO_ENABLED=0 go build -v -a -trimpath \ - -ldflags "$$LD_FLAGS" \ - -o $(OUT_FILE) $(GO_FILE) ;\ + export GOFLAGS="-a -trimpath -ldflags=-w -ldflags=-s" ;\ + if [ "$${GOARCH}" != "arm" ]; then \ + export GOFLAGS="$${GOFLAGS} -buildmode=pie" ;\ + fi ;\ + CGO_ENABLED=0 go build -o $(OUT_FILE) $(GO_FILE) ;\ + } + @ls -lh $(OUT_FILE) + +# Build dinamically linked production binary. +release-dynamic: clean + @{\ + export CGO_CPPFLAGS="$${CPPFLAGS}" ;\ + export CGO_CFLAGS="$${CFLAGS}" ;\ + export CGO_CXXFLAGS="$${CXXFLAGS}" ;\ + export CGO_LDFLAGS="$${LDFLAGS}" ;\ + export GOFLAGS="-a -trimpath -ldflags=-linkmode=external -ldflags=-w -ldflags=-s" ;\ + if [ "$${GOARCH}" != "arm" ]; then \ + export GOFLAGS="$${GOFLAGS} -buildmode=pie" ;\ + fi ;\ + go build -o $(OUT_FILE) $(GO_FILE) ;\ } @ls -lh $(OUT_FILE) diff --git a/README.md b/README.md index 98a4a1c..5264f0d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ See also https://fedoraproject.org/wiki/Changes/SwapOnZRAM#Benefit_to_Fedora ## Compiling * Install `go`, this depends on the distribution you are using e.g. for Ubuntu the command should be `sudo apt-get install golang`. -* Run `make release` +* Run `make release` to make a x86_64 build, to make an ARM build (i.e. for the Raspberry Pi) run `GOOS=linux GOARCH=arm GOARM=7 make release` * A new executable called `zramd.bin` will be created in the current directory, now you can uninstall `go` if you like. * Optionally on distributions using systemd, you can install `zramd` by just running `make install`, see below for additional installation methods. diff --git a/src/utsname/utsname.go b/src/utsname/utsname.go index 8cfa62b..4949929 100644 --- a/src/utsname/utsname.go +++ b/src/utsname/utsname.go @@ -24,17 +24,6 @@ func (uname *UTSName) KernelVersion() (int, int) { return int(major), int(minor) } -func parseInt8(data []int8) string { - b := make([]byte, 0, len(data)) - for _, v := range data { - if v == 0x00 { - break - } - b = append(b, byte(v)) - } - return string(b) -} - // Uname returns information about the current kernel. func Uname() *UTSName { var uname syscall.Utsname @@ -42,12 +31,15 @@ func Uname() *UTSName { if err != nil { panic(err) } + // Keep in mind that we are using 2 sightly different implementations to parse + // char slices as they use different data types depending on the architecture, + // see also https://github.com/golang/go/issues/13318. return &UTSName{ - SysName: parseInt8(uname.Sysname[:]), - NodeName: parseInt8(uname.Nodename[:]), - Release: parseInt8(uname.Release[:]), - Version: parseInt8(uname.Version[:]), - Machine: parseInt8(uname.Machine[:]), - DomainName: parseInt8(uname.Domainname[:]), + SysName: parseCharSlice(uname.Sysname[:]), + NodeName: parseCharSlice(uname.Nodename[:]), + Release: parseCharSlice(uname.Release[:]), + Version: parseCharSlice(uname.Version[:]), + Machine: parseCharSlice(uname.Machine[:]), + DomainName: parseCharSlice(uname.Domainname[:]), } } diff --git a/src/utsname/utsname_amd64.go b/src/utsname/utsname_amd64.go new file mode 100644 index 0000000..7110dd2 --- /dev/null +++ b/src/utsname/utsname_amd64.go @@ -0,0 +1,12 @@ +package utsname + +func parseCharSlice(data []int8) string { + b := make([]byte, 0, len(data)) + for _, v := range data { + if v == 0x00 { + break + } + b = append(b, byte(v)) + } + return string(b) +} diff --git a/src/utsname/utsname_arm.go b/src/utsname/utsname_arm.go new file mode 100644 index 0000000..6b009e0 --- /dev/null +++ b/src/utsname/utsname_arm.go @@ -0,0 +1,12 @@ +package utsname + +func parseCharSlice(data []uint8) string { + b := make([]byte, 0, len(data)) + for _, v := range data { + if v == 0x00 { + break + } + b = append(b, byte(v)) + } + return string(b) +}