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