调整为容器化编译

This commit is contained in:
lwch
2021-10-18 15:07:43 +08:00
parent 4136eae5c2
commit 63f217402c
5 changed files with 156 additions and 26 deletions
+8 -14
View File
@@ -1,17 +1,11 @@
#!/bin/sh
VERSION="0.5.0"
HASH=`git log -n1 --pretty=format:%h`
REVERSION=`git log --oneline|wc -l|tr -d ' '`
BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'`
LDFLAGS="-X 'main.gitHash=$HASH'
-X 'main.gitReversion=$REVERSION'
-X 'main.buildTime=$BUILD_TIME'
-X 'main.version=$VERSION'"
VERSION=0.5.0
go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \
-prefix html/shell "$@" html/shell/...
go run contrib/bindata/main.go -pkg dashboard -o code/client/dashboard/assets.go \
-prefix html/dashboard "$@" html/dashboard/...
CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-svr code/server/*.go
CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-cli code/client/*.go
docker build -t natpass -f contrib/build/Dockerfile .
docker run -it --rm \
-v `pwd`:/build \
-v $HOME/.cache/go-build:/root/.cache/go-build \
-v $HOME/go/pkg/mod:/root/go/pkg/mod \
-e BUILD_VERSION=$VERSION \
natpass /build/contrib/build/build
Executable
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
docker build -t natpass -f contrib/build/Dockerfile .
docker run -it --rm \
-v `pwd`:/build \
-v $HOME/.cache/go-build:/root/.cache/go-build \
-v $HOME/go/pkg/mod:/root/go/pkg/mod \
natpass go run /build/contrib/build/build_all.go
+56
View File
@@ -0,0 +1,56 @@
# build libpng
FROM debian:bullseye-slim as libpng
ARG APT_MIRROR=mirrors.ustc.edu.cn
ARG LIBPNG_VERSION=1.6.37
ARG BUILD_VERSION=0.5.0
WORKDIR /build
RUN sed -i "s|deb.debian.org|$APT_MIRROR|g" /etc/apt/sources.list && \
sed -i "s|security.debian.org|$APT_MIRROR|g" /etc/apt/sources.list && \
apt-get update && apt-get upgrade -y && \
apt-get install -y gcc gcc-mingw-w64 libz-mingw-w64-dev make && \
apt-get install -y curl && \
curl -L http://prdownloads.sourceforge.net/libpng/libpng-$LIBPNG_VERSION.tar.gz|tar -xz && \
cd libpng-$LIBPNG_VERSION && \
sh -c "sed 's|^ZLIBINC.*$|ZLIBINC=/usr/x86_64-w64-mingw32/include|g' scripts/makefile.gcc | \
sed 's|^ZLIBLIB.*$|ZLIBLIB=/usr/x86_64-w64-mingw32/lib|g' | \
sed 's|^CC.*$|CC=x86_64-w64-mingw32-gcc|g' > Makefile" && \
make clean && make && cp libpng.a /build/libpng.x86_64.a && \
sh -c "sed 's|^ZLIBINC.*$|ZLIBINC=/usr/i686-w64-mingw32/include|g' scripts/makefile.gcc | \
sed 's|^ZLIBLIB.*$|ZLIBLIB=/usr/i686-w64-mingw32/lib|g' | \
sed 's|^CC.*$|CC=i686-w64-mingw32-gcc|g' > Makefile" && \
make clean && make && cp libpng.a /build/libpng.i686.a
FROM debian:bullseye-slim
ARG APT_MIRROR=mirrors.ustc.edu.cn
ARG GO_VERSION=1.17.2
ARG GO_PROXY=https://goproxy.cn,direct
VOLUME /build
WORKDIR /build
RUN sed -i "s|deb.debian.org|$APT_MIRROR|g" /etc/apt/sources.list && \
sed -i "s|security.debian.org|$APT_MIRROR|g" /etc/apt/sources.list && \
apt-get update && apt-get upgrade -y && \
apt-get install -y gcc libc6-dev && \
apt-get install -y libx11-dev xorg-dev libxtst-dev libpng++-dev && \
apt-get install -y xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev && \
apt-get install -y libxkbcommon-dev && \
apt-get install -y xsel xclip && \
apt-get install -y gcc-multilib make && \
apt-get install -y gcc-mingw-w64 && \
apt-get install -y libz-mingw-w64-dev && \
apt-get install -y curl git && \
curl -L https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz|tar -xz -C /usr/local && \
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=libpng /build/libpng.x86_64.a /usr/x86_64-w64-mingw32/lib/libpng.a
COPY --from=libpng /build/libpng.i686.a /usr/i686-w64-mingw32/lib/libpng.a
ENV PATH=$PATH:/usr/local/go/bin
ENV GOPROXY=$GO_PROXY
CMD /bin/bash
+23
View File
@@ -0,0 +1,23 @@
#!/bin/sh
cd /build
HASH=`git log -n1 --pretty=format:%h`
REVERSION=`git log --oneline|wc -l|tr -d ' '`
BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'`
LDFLAGS="-X 'main.gitHash=$HASH'
-X 'main.gitReversion=$REVERSION'
-X 'main.buildTime=$BUILD_TIME'
-X 'main.version=$BUILD_VERSION'
--extldflags '-static -fpic -lssp'"
go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \
-prefix html/shell "$@" html/shell/...
go run contrib/bindata/main.go -pkg dashboard -o code/client/dashboard/assets.go \
-prefix html/dashboard "$@" html/dashboard/...
CC=x86_64-w64-mingw32-gcc
CXX=x86_64-w64-mingw32-g++
CGO_ENABLED=0 go build -v -ldflags "$LDFLAGS" -o bin/np-svr code/server/*.go
CGO_ENABLED=0 go build -v -ldflags "$LDFLAGS" -o bin/np-cli code/client/*.go
+61 -12
View File
@@ -26,6 +26,8 @@ const releaseDir = "release"
type target struct {
os string
arch string
cc string
cxx string
ext string
packExt string
}
@@ -33,18 +35,62 @@ type target struct {
// go tool dist list
var targets = []target{
// darwin
{"darwin", "amd64", "", ".tar.gz"},
{"darwin", "arm64", "", ".tar.gz"},
{
os: "darwin",
arch: "amd64",
packExt: ".tar.gz",
},
{
os: "darwin",
arch: "arm64",
packExt: ".tar.gz",
},
// linux
{"linux", "386", "", ".tar.gz"},
{"linux", "amd64", "", ".tar.gz"},
{"linux", "arm", "", ".tar.gz"},
{"linux", "arm64", "", ".tar.gz"},
{
os: "linux",
arch: "386",
packExt: ".tar.gz",
},
{
os: "linux",
arch: "amd64",
packExt: ".tar.gz",
},
{
os: "linux",
arch: "arm",
packExt: ".tar.gz",
},
{
os: "linux",
arch: "arm64",
packExt: ".tar.gz",
},
// windows
{"windows", "386", ".exe", ".zip"},
{"windows", "amd64", ".exe", ".zip"},
{"windows", "arm", ".exe", ".zip"},
{"windows", "arm64", ".exe", ".zip"},
{
os: "windows",
arch: "386",
cc: "i686-w64-mingw32-gcc", cxx: "i686-w64-mingw32-g++",
ext: ".exe", packExt: ".zip",
},
{
os: "windows",
arch: "amd64",
cc: "x86_64-w64-mingw32-gcc", cxx: "x86_64-w64-mingw32-g++",
ext: ".exe", packExt: ".zip",
},
{
os: "windows",
arch: "arm",
// TODO: cc, cxx?
ext: ".exe", packExt: ".zip",
},
{
os: "windows",
arch: "arm64",
// TODO: cc, cxx?
ext: ".exe", packExt: ".zip",
},
}
func main() {
@@ -98,7 +144,8 @@ func build(t target) {
ldflags := "-X 'main.gitHash=" + gitHash() + "' " +
"-X 'main.gitReversion=" + gitReversion() + "' " +
"-X 'main.buildTime=" + buildTime() + "' " +
"-X 'main.version=" + version + "'"
"-X 'main.version=" + version + "' " +
"--extldflags '-static -fpic -lssp'"
logging.Info("build server...")
cmd := exec.Command("go", "build", "-o", path.Join(buildDir, "np-svr"+t.ext),
@@ -122,7 +169,9 @@ func build(t target) {
cmd.Env = append(os.Environ(),
"CGO_ENABLED=0",
fmt.Sprintf("GOOS=%s", t.os),
fmt.Sprintf("GOARCH=%s", t.arch))
fmt.Sprintf("GOARCH=%s", t.arch),
fmt.Sprintf("CC=%s", t.cc),
fmt.Sprintf("CXX=%s", t.cxx))
runtime.Assert(cmd.Run())
logging.Info("packing...")