瀏覽代碼

Added instructions for cross-compilation

Vladimir Vivien 2 年之前
父節點
當前提交
25bde243cb

+ 3 - 1
README.md

@@ -26,7 +26,9 @@ It hides all the complexities of working with V4L2 and provides idiomatic Go typ
 
 * Go compiler/tools
 * Kernel minimum v5.10.x
-* A locally configured C compiler (i.e. gcc)
+* A locally configured C compiler (or a cross-compiler if building off-device)
+
+See [example/simplecam](./examples/simplecam/README.md) for further example of building projects that uses go4vl including cross-compilation instructions.
 
 All examples have been tested using a Raspberry PI 3, running 32-bit Raspberry PI OS.
 The package should work with no problem on your 64-bit Linux OS.

+ 11 - 0
build.sh

@@ -0,0 +1,11 @@
+# docker run --rm --platform=linux/amd64 \
+#   -v "$(pwd):/myapp" \
+#   -w /myapp \
+#   -e GOOS=linux -e GOARCH=arm \
+#   golang:1.19 go build -v ./examples/simplecam
+
+CGO_ENABLED=1 \
+    GOOS=linux \
+    GOARCH=arm \
+    CC="zig cc -target arm-linux-musleabihf"\
+    CXX="zig c++ -target arm-linux-musleabihf" go build .

+ 23 - 0
examples/simplecam/Dockerfile

@@ -0,0 +1,23 @@
+# syntax=docker/dockerfile:1
+
+FROM --platform=$BUILDPLATFORM crazymax/goxx:latest AS base
+
+ENV OUTPUT="simple-cam"
+ENV CGO_ENABLED=1
+WORKDIR /src
+
+FROM base AS build
+ARG TARGETPLATFORM
+RUN --mount=type=cache,sharing=private,target=/var/cache/apt \
+  --mount=type=cache,sharing=private,target=/var/lib/apt/lists \
+  goxx-apt-get install -y binutils gcc g++ pkg-config
+RUN --mount=type=bind,source=. \
+  --mount=type=cache,target=/root/.cache \
+  --mount=type=cache,target=/go/pkg/mod \
+  goxx-go build -o /out/${OUTPUT} .
+
+FROM scratch AS artifact
+COPY --from=build /out /
+
+## Build with the following command
+# docker build --platform "linux/arm/v6" --output "./build"  .

+ 24 - 1
examples/simplecam/README.md

@@ -1,4 +1,27 @@
-# camserv
+# simplecam
 
 This is a simple example shows how easy it is to use go4vl to 
 create a simple web application to stream camera images.
+
+## Build
+There are three ways to build the example.
+
+### On device
+You can always setup Go on your device and build your source code directly on the device.
+
+### Cross-compile with Zig
+The Zig language is (itself) full C/C++ cross-compiler. With flag compatibility with gcc and clang, 
+Zig can be used as a drop-in replacement for those compilers allowing easy cross-compilation of source code.
+
+It turns out that Zig can be used as the cross-compiler for building Go code with CGo enabled. Assuming you have
+the Zig build tools installed, you can cross-compile the source code to target Linux/Arm/v7 as shown below:;
+
+```
+CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 CC="zig cc -target arm-linux-musleabihf" CXX="zig c++ -target arm-linux-musleabihf" go build -o simple-cam .
+```
+
+The previous build command will create a staticly linked binary that can run on Linux for the Arm/v7 architecture:
+
+```
+simple-cam: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), static-pie linked, Go BuildID=WYa4l3EGlIvd9EErrWkc/5Aa4CZdUXG8bERpToUcN/jjwKBQqSAfDbNfJGzSou/27sOKN7B1e0dtPc7PqmR, with debug_info, not stripped
+```

+ 7 - 0
examples/simplecam/go.mod

@@ -0,0 +1,7 @@
+module github.com/vladimirvivien/go4vl/simplecam
+
+go 1.19
+
+require github.com/vladimirvivien/go4vl v0.0.5
+
+require golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect

+ 4 - 0
examples/simplecam/go.sum

@@ -0,0 +1,4 @@
+github.com/vladimirvivien/go4vl v0.0.5 h1:jHuo/CZOAzYGzrSMOc7anOMNDr03uWH5c1B5kQ+Chnc=
+github.com/vladimirvivien/go4vl v0.0.5/go.mod h1:FP+/fG/X1DUdbZl9uN+l33vId1QneVn+W80JMc17OL8=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

+ 20 - 7
examples/webcam/README.md

@@ -11,10 +11,11 @@ Before you can build and run the code, you must satisfy the following prerequisi
 * Go compiler/tools
 * Linux OS (32- or 64-bit)
 * Kernel minimum v5.10.x or higher
-* A locally configured C compiler (i.e. gcc)
-* Header files for V4L2 (i.e. /usr/include/linux/videodev2.h)
+* A locally configured C compiler (or cross-compiler if building off-device)
 * A video camera (with support for Video for Linux API)
 
+
+### On-device preparation
 If you are running a system that has not been upgraded in a while, ensure to issue the following commands:
 
 ```
@@ -22,17 +23,29 @@ sudo apt update
 sudo apt full-upgrade
 ```
 
-This example has been tested using a Raspberry Pi 3 running 32-bit Linux, with kernel version 5.14, with cheap USB video camera attached.
+This example has been tested using a Raspberry Pi 3 running 32-bit Linux, with kernel version 5.14, with an attached USB video camera.
 
-### Build and run
+### On-device build 
 
-From within this directory, build with the following command:
+Copy (or use git) the code on the device. From within the webcam directory, run the helper script:
 
 ```
-go build -o webcam webcam.go
+./build-on-device.sh
 ```
 
-Once built, you can start the webcam with the following command (and output as shown):
+This will build the webcam binary.
+
+### Cross-compile build
+If you are building off-device, on a platform different then Linux/Arm/v7, use the cross-build helper script to compile the binary:
+
+```
+./cross-build.sh
+```
+This will create a binary for the Linux/Arm/v7 using the Zig compiler as a cross compiler (see examples/simplecam for detail about Zig).
+
+### Run
+
+Once built, run the binary on a target machine (a Raspberry Pi for instance) that has a camera capture device attached:
 
 ```
  ./webcam

+ 1 - 1
examples/webcam/build.sh → examples/webcam/build-on-device.sh

@@ -6,4 +6,4 @@ go get github.com/fogleman/gg@8febc0f526adecda6f8ae80f3869b7cd77e52984
 
 go mod tidy
 
-go build .
+go build -o webcam .

+ 11 - 0
examples/webcam/cross-build.sh

@@ -0,0 +1,11 @@
+#! /bin/bash
+# Run the following once to pull correct dependencies
+go get github.com/vladimirvivien/go4vl@latest
+go get github.com/esimov/pigo/core@latest
+go get github.com/fogleman/gg@8febc0f526adecda6f8ae80f3869b7cd77e52984
+
+go mod tidy
+
+# build for 32-bit arm
+CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 CC="zig cc -target arm-linux-musleabihf" CXX="zig c++ -target arm-linux-musleabihf" go build -o webcam .
+

+ 4 - 3
examples/webcam/go.mod

@@ -1,14 +1,15 @@
-module github.com/vladimirvivien/go4vl/exampels/webcam
+module github.com/vladimirvivien/go4vl/webcam
 
 go 1.19
 
 require (
-	github.com/esimov/pigo v1.4.5
+	github.com/esimov/pigo v1.4.6
 	github.com/fogleman/gg v1.3.1-0.20210928143535-8febc0f526ad
+	github.com/vladimirvivien/go4vl v0.0.5
 )
 
 require (
 	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 	golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
 	golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
-)
+)

+ 19 - 0
examples/webcam/go.sum

@@ -0,0 +1,19 @@
+github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
+github.com/esimov/pigo v1.4.6 h1:wpB9FstbqeGP/CZP+nTR52tUJe7XErq8buG+k4xCXlw=
+github.com/esimov/pigo v1.4.6/go.mod h1:uqj9Y3+3IRYhFK071rxz1QYq0ePhA6+R9jrUZavi46M=
+github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
+github.com/fogleman/gg v1.3.1-0.20210928143535-8febc0f526ad h1:eTAQhxh1k3Hn1zR/Idl9deMm/I7jtOCORqpVriuuOuI=
+github.com/fogleman/gg v1.3.1-0.20210928143535-8febc0f526ad/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
+github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
+github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
+github.com/vladimirvivien/go4vl v0.0.5 h1:jHuo/CZOAzYGzrSMOc7anOMNDr03uWH5c1B5kQ+Chnc=
+github.com/vladimirvivien/go4vl v0.0.5/go.mod h1:FP+/fG/X1DUdbZl9uN+l33vId1QneVn+W80JMc17OL8=
+golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
+golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM=
+golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20201107080550-4d91cf3a1aaf/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20191110171634-ad39bd3f0407/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=