Skip to content

Commit

Permalink
Merge pull request #3 from maximumadmin/arm-fix-and-build-improvements
Browse files Browse the repository at this point in the history
Arm fix and build improvements
  • Loading branch information
maximumadmin authored Mar 4, 2021
2 parents fd930d3 + 8e6f508 commit e284959
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
26 changes: 9 additions & 17 deletions src/utsname/utsname.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,22 @@ 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
err := syscall.Uname(&uname)
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[:]),
}
}
12 changes: 12 additions & 0 deletions src/utsname/utsname_amd64.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions src/utsname/utsname_arm.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e284959

Please sign in to comment.