init_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "testing"
  14. "text/template"
  15. )
  16. var gopath string
  17. func TestInit(t *testing.T) {
  18. if runtime.GOOS == "android" || runtime.GOOS == "ios" {
  19. t.Skipf("not available on %s", runtime.GOOS)
  20. }
  21. if _, err := exec.LookPath("diff"); err != nil {
  22. t.Skip("command diff not found, skipping")
  23. }
  24. buf := new(bytes.Buffer)
  25. gopathorig := os.Getenv("GOPATH")
  26. defer func() {
  27. xout = os.Stderr
  28. buildN = false
  29. buildX = false
  30. os.Setenv("GOPATH", gopathorig)
  31. }()
  32. xout = buf
  33. buildN = true
  34. buildX = true
  35. // Test that first GOPATH element is chosen correctly.
  36. var err error
  37. gopath, err = ioutil.TempDir("", "gomobile-test")
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. paths := []string{gopath, "/path2", "/path3"}
  42. if goos == "windows" {
  43. gopath = filepath.ToSlash(`C:\GOPATH1`)
  44. paths = []string{gopath, `C:\PATH2`, `C:\PATH3`}
  45. }
  46. os.Setenv("GOPATH", strings.Join(paths, string(os.PathListSeparator)))
  47. os.Setenv("GOROOT_BOOTSTRAP", "go1.4")
  48. if goos == "windows" {
  49. os.Setenv("HOMEDRIVE", "C:")
  50. }
  51. emptymod, err := ioutil.TempDir("", "gomobile-test")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer os.RemoveAll(emptymod)
  56. // Create go.mod, but without Go files.
  57. f, err := os.Create(filepath.Join(emptymod, "go.mod"))
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. defer f.Close()
  62. if _, err := f.WriteString("module example.com/m\n"); err != nil {
  63. t.Fatal(err)
  64. }
  65. if err := f.Sync(); err != nil {
  66. t.Fatal(err)
  67. }
  68. dirs := []struct {
  69. dir string
  70. name string
  71. }{
  72. {
  73. dir: ".",
  74. name: "current",
  75. },
  76. {
  77. dir: emptymod,
  78. name: "emptymod",
  79. },
  80. }
  81. for _, dir := range dirs {
  82. dir := dir
  83. t.Run(dir.name, func(t *testing.T) {
  84. wd, err := os.Getwd()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := os.Chdir(dir.dir); err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.Chdir(wd)
  92. if err := runInit(cmdInit); err != nil {
  93. t.Log(buf.String())
  94. t.Fatal(err)
  95. }
  96. if dir.name == "emptymod" {
  97. return
  98. }
  99. diff, err := diffOutput(buf.String(), initTmpl)
  100. if err != nil {
  101. t.Fatalf("computing diff failed: %v", err)
  102. }
  103. if diff != "" {
  104. t.Errorf("unexpected output:\n%s", diff)
  105. }
  106. })
  107. }
  108. }
  109. func diffOutput(got string, wantTmpl *template.Template) (string, error) {
  110. got = filepath.ToSlash(got)
  111. wantBuf := new(bytes.Buffer)
  112. data, err := defaultOutputData("")
  113. if err != nil {
  114. return "", err
  115. }
  116. if err := wantTmpl.Execute(wantBuf, data); err != nil {
  117. return "", err
  118. }
  119. want := wantBuf.String()
  120. if got != want {
  121. return diff(got, want)
  122. }
  123. return "", nil
  124. }
  125. type outputData struct {
  126. GOOS string
  127. GOARCH string
  128. GOPATH string
  129. NDKARCH string
  130. EXE string // .extension for executables. (ex. ".exe" for windows)
  131. Xproj string
  132. Xcontents string
  133. Xinfo infoplistTmplData
  134. }
  135. func defaultOutputData(teamID string) (outputData, error) {
  136. projPbxproj := new(bytes.Buffer)
  137. if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
  138. TeamID: teamID,
  139. }); err != nil {
  140. return outputData{}, err
  141. }
  142. data := outputData{
  143. GOOS: goos,
  144. GOARCH: goarch,
  145. GOPATH: gopath,
  146. NDKARCH: archNDK(),
  147. Xproj: projPbxproj.String(),
  148. Xcontents: contentsJSON,
  149. Xinfo: infoplistTmplData{BundleID: "org.golang.todo.basic", Name: "Basic"},
  150. }
  151. if goos == "windows" {
  152. data.EXE = ".exe"
  153. }
  154. return data, nil
  155. }
  156. var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
  157. rm -r -f "$GOMOBILE"
  158. mkdir -p $GOMOBILE
  159. WORK={{.GOPATH}}/pkg/gomobile/work
  160. GOMODCACHE={{.GOPATH}}/pkg/mod go install -x golang.org/x/mobile/cmd/gobind@latest
  161. cp $OPENAL_PATH/include/AL/al.h $GOMOBILE/include/AL/al.h
  162. mkdir -p $GOMOBILE/include/AL
  163. cp $OPENAL_PATH/include/AL/alc.h $GOMOBILE/include/AL/alc.h
  164. mkdir -p $GOMOBILE/include/AL
  165. mkdir -p $WORK/build/armeabi
  166. PWD=$WORK/build/armeabi cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=armv7a-linux-androideabi16
  167. PWD=$WORK/build/armeabi $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make
  168. cp $WORK/build/armeabi/libopenal.so $GOMOBILE/lib/armeabi-v7a/libopenal.so
  169. mkdir -p $GOMOBILE/lib/armeabi-v7a
  170. mkdir -p $WORK/build/arm64
  171. PWD=$WORK/build/arm64 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=aarch64-linux-android21
  172. PWD=$WORK/build/arm64 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make
  173. cp $WORK/build/arm64/libopenal.so $GOMOBILE/lib/arm64-v8a/libopenal.so
  174. mkdir -p $GOMOBILE/lib/arm64-v8a
  175. mkdir -p $WORK/build/x86
  176. PWD=$WORK/build/x86 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=i686-linux-android16
  177. PWD=$WORK/build/x86 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make
  178. cp $WORK/build/x86/libopenal.so $GOMOBILE/lib/x86/libopenal.so
  179. mkdir -p $GOMOBILE/lib/x86
  180. mkdir -p $WORK/build/x86_64
  181. PWD=$WORK/build/x86_64 cmake $OPENAL_PATH -DCMAKE_TOOLCHAIN_FILE=$OPENAL_PATH/XCompile-Android.txt -DHOST=x86_64-linux-android21
  182. PWD=$WORK/build/x86_64 $NDK_PATH/prebuilt/{{.NDKARCH}}/bin/make
  183. cp $WORK/build/x86_64/libopenal.so $GOMOBILE/lib/x86_64/libopenal.so
  184. mkdir -p $GOMOBILE/lib/x86_64
  185. rm -r -f "$WORK"
  186. `))