Просмотр исходного кода

Added build info

- At connect time, build info is output in a notice with the form: 'Built: "2015-02-10T21:30:55+0000" from "git@github.com:Psiphon-Labs/psiphon-tunnel-core.git" at rev "d3a9bdcc04e1e33d7595b3ba06515785180eecd6"'
- Build info values are inserted into code via ldflags at build/link time.
- If values are not supplied, there is no problem -- the build values will just be empty.

The reason I chose to use the "-ldflags -X" trick rather than "go generate" is that it's more OS/environment agnostic. For example, the "date" command is very different between Linux and Windows, or git might not be present on the build machine (but the repo/rev could still be passed in).
Adam Pritchard 11 лет назад
Родитель
Сommit
366ad3e3b5
4 измененных файлов с 78 добавлено и 15 удалено
  1. 13 5
      AndroidLibrary/make.bash
  2. 18 10
      ConsoleClient/make.bash
  3. 46 0
      psiphon/buildinfo.go
  4. 1 0
      psiphon/controller.go

+ 13 - 5
AndroidLibrary/make.bash

@@ -1,6 +1,7 @@
 #!/usr/bin/env bash
 
 set -e
+#set -xv # verbose output for testing
 
 if [ ! -f make.bash ]; then
   echo 'make.bash must be run from $GOPATH/src/github.com/Psiphon-Labs/psiphon-tunnel-core/AndroidLibrary'
@@ -8,19 +9,26 @@ if [ ! -f make.bash ]; then
 fi
 
 # Make sure we have our dependencies
-echo 'go-getting dependencies...'
+echo -e "go-getting dependencies...\n"
 go get -d -v ./...
 
 # Force an update of the go-mobile package, since it's being improved rapidly
 # NOTE: for some reason this either doesn't complete or stalls for a very long time.
-#echo 'Updating go-mobile...'
+#echo -e "Updating go-mobile...\n"
 #go get -u -d -v golang.org/x/mobile/...
 
-echo 'Building library...'
+LDFLAGS="\
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildDate `date --iso-8601=seconds` \
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRepo `git config --get remote.origin.url` \
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRev `git rev-parse HEAD` \
+"
+echo -e "LDFLAGS=$LDFLAGS\n"
+
+echo -e "Building library...\n"
 CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \
-  go build -a -v -ldflags="-shared" -o libgojni.so ./libpsi
+  go build -a -v -ldflags="-shared $LDFLAGS" -o libgojni.so ./libpsi
 
 mkdir -p libs/armeabi-v7a
 mv -f libgojni.so libs/armeabi-v7a/libgojni.so
 
-echo 'Library can be found at: libs/armeabi-v7a/libgojni.so'
+echo -e "Library can be found at: libs/armeabi-v7a/libgojni.so\n"

+ 18 - 10
ConsoleClient/make.bash

@@ -1,35 +1,43 @@
 #!/usr/bin/env bash
 
 set -e
+#set -xv # verbose output for testing
 
 if [ ! -f make.bash ]; then
   echo 'make.bash must be run from $GOPATH/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ConsoleClient'
   exit 1
 fi
 
+CGO_ENABLED=1
+
 # Make sure we have our dependencies
-echo 'go-getting dependencies...'
+echo -e "go-getting dependencies...\n"
 go get -d -v ./...
 
-CGO_ENABLED=1
+LDFLAGS="\
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildDate `date --iso-8601=seconds` \
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRepo `git config --get remote.origin.url` \
+-X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRev `git rev-parse HEAD` \
+"
+echo -e "LDFLAGS=$LDFLAGS\n"
 
-echo 'Building windows-386...'
+echo -e "\nBuilding windows-386..."
 CC=/usr/bin/i686-w64-mingw32-gcc \
-  gox -verbose -osarch windows/386 -output windows_386_psiphon-tunnel-core
+  gox -verbose -ldflags "$LDFLAGS" -osarch windows/386 -output windows_386_psiphon-tunnel-core
 # We are finding that UPXing the full Windows Psiphon client produces better results
 # if psiphon-tunnel-core.exe is not already UPX'd.
 #upx --best windows_386_psiphon-tunnel-core.exe
 
-echo 'Building windows-amd64...'
+echo -e "\nBuilding windows-amd64..."
 CC=/usr/bin/x86_64-w64-mingw32-gcc \
-  gox -verbose -osarch windows/amd64 -output windows_amd64_psiphon-tunnel-core
+  gox -verbose -ldflags "$LDFLAGS" -osarch windows/amd64 -output windows_amd64_psiphon-tunnel-core
 upx --best windows_amd64_psiphon-tunnel-core.exe
 
-echo 'Building linux-amd64...'
-gox -verbose -osarch linux/amd64 -output linux_amd64_psiphon-tunnel-core
+echo -e "\nBuilding linux-amd64..."
+gox -verbose -ldflags "$LDFLAGS" -osarch linux/amd64 -output linux_amd64_psiphon-tunnel-core
 upx --best linux_amd64_psiphon-tunnel-core
 
-echo 'Building linux-386...'
+echo -e "\nBuilding linux-386..."
 CFLAGS=-m32 \
-  gox -verbose -osarch linux/386 -output linux_386_psiphon-tunnel-core
+  gox -verbose -ldflags "$LDFLAGS" -osarch linux/386 -output linux_386_psiphon-tunnel-core
 upx --best linux_386_psiphon-tunnel-core

+ 46 - 0
psiphon/buildinfo.go

@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015, Psiphon Inc.
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package psiphon
+
+import "strings"
+
+/*
+These values should be filled in at build time using the `-X` option[1] to the
+Go linker (probably via `-ldflags` option to `go build` -- like `-ldflags "-X var1 abc -X var2 xyz"`).
+[1]: http://golang.org/cmd/ld/
+Without those build flags, the build info in the notice will simply be empty strings.
+Suggestions for how to fill in the values will be given for each variable.
+*/
+// -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildDate `date --iso-8601=seconds`
+var buildDate string
+
+// -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRepo `git config --get remote.origin.url`
+var buildRepo string
+
+// -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRev `git rev-parse HEAD`
+var buildRev string
+
+func NoticeBuildInfo() {
+	NoticeInfo(
+		"Built: %#v from %#v at rev %#v",
+		strings.TrimSpace(buildDate),
+		strings.TrimSpace(buildRepo),
+		strings.TrimSpace(buildRev))
+}

+ 1 - 0
psiphon/controller.go

@@ -96,6 +96,7 @@ func NewController(config *Config) (controller *Controller, err error) {
 // - a local SOCKS proxy that port forwards through the pool of tunnels
 // - a local HTTP proxy that port forwards through the pool of tunnels
 func (controller *Controller) Run(shutdownBroadcast <-chan struct{}) {
+	NoticeBuildInfo()
 	NoticeCoreVersion(VERSION)
 
 	// Start components