diff --git a/build b/build index 9563275..dbbc0f5 100755 --- a/build +++ b/build @@ -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 \ No newline at end of file diff --git a/build_all b/build_all new file mode 100755 index 0000000..670e928 --- /dev/null +++ b/build_all @@ -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 \ No newline at end of file diff --git a/contrib/build/Dockerfile b/contrib/build/Dockerfile new file mode 100644 index 0000000..3406f7b --- /dev/null +++ b/contrib/build/Dockerfile @@ -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 \ No newline at end of file diff --git a/contrib/build/build b/contrib/build/build new file mode 100755 index 0000000..4843eb1 --- /dev/null +++ b/contrib/build/build @@ -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 diff --git a/build_all.go b/contrib/build/build_all.go similarity index 83% rename from build_all.go rename to contrib/build/build_all.go index b880d61..8b963e5 100644 --- a/build_all.go +++ b/contrib/build/build_all.go @@ -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...")