init_test.go 5.1 KB

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