github.com/adwpc/xmobile@v0.0.0-20231212131043-3f9720cf0e99/cmd/gomobile/build_darwin_test.go (about)

     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  	"path/filepath"
    11  	"testing"
    12  	"text/template"
    13  )
    14  
    15  func TestAppleBuild(t *testing.T) {
    16  	if !xcodeAvailable() {
    17  		t.Skip("Xcode is missing")
    18  	}
    19  	defer func() {
    20  		xout = os.Stderr
    21  		buildN = false
    22  		buildX = false
    23  	}()
    24  	buildN = true
    25  	buildX = true
    26  	buildTarget = "ios"
    27  	buildBundleID = "org.golang.todo"
    28  	gopath = filepath.SplitList(goEnv("GOPATH"))[0]
    29  	oldTags := buildTags
    30  	buildTags = []string{"tag1"}
    31  	defer func() {
    32  		buildTags = oldTags
    33  	}()
    34  	tests := []struct {
    35  		pkg  string
    36  		main bool
    37  	}{
    38  		{"github.com/adwpc/xmobile/example/basic", true},
    39  		{"github.com/adwpc/xmobile/bind/testdata/testpkg", false},
    40  	}
    41  	for _, test := range tests {
    42  		buf := new(bytes.Buffer)
    43  		xout = buf
    44  		var tmpl *template.Template
    45  		if test.main {
    46  			buildO = "basic.app"
    47  			tmpl = appleMainBuildTmpl
    48  		} else {
    49  			buildO = ""
    50  			tmpl = appleOtherBuildTmpl
    51  		}
    52  		cmdBuild.flag.Parse([]string{test.pkg})
    53  		err := runBuild(cmdBuild)
    54  		if err != nil {
    55  			t.Log(buf.String())
    56  			t.Fatal(err)
    57  		}
    58  
    59  		teamID, err := detectTeamID()
    60  		if err != nil {
    61  			t.Fatalf("detecting team ID failed: %v", err)
    62  		}
    63  
    64  		output, err := defaultOutputData(teamID)
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  
    69  		data := struct {
    70  			outputData
    71  			TeamID string
    72  			Pkg    string
    73  			Main   bool
    74  			BuildO string
    75  		}{
    76  			outputData: output,
    77  			TeamID:     teamID,
    78  			Pkg:        test.pkg,
    79  			Main:       test.main,
    80  			BuildO:     buildO,
    81  		}
    82  
    83  		got := filepath.ToSlash(buf.String())
    84  
    85  		wantBuf := new(bytes.Buffer)
    86  
    87  		if err := tmpl.Execute(wantBuf, data); err != nil {
    88  			t.Fatalf("computing diff failed: %v", err)
    89  		}
    90  
    91  		diff, err := diff(got, wantBuf.String())
    92  
    93  		if err != nil {
    94  			t.Fatalf("computing diff failed: %v", err)
    95  		}
    96  		if diff != "" {
    97  			t.Errorf("unexpected output:\n%s", diff)
    98  		}
    99  	}
   100  }
   101  
   102  var appleMainBuildTmpl = template.Must(infoplistTmpl.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
   103  WORK=$WORK
   104  mkdir -p $WORK/main.xcodeproj
   105  echo "{{.Xproj}}" > $WORK/main.xcodeproj/project.pbxproj
   106  mkdir -p $WORK/main
   107  echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
   108  mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
   109  echo "{{.Xcontents}}" > $WORK/main/Images.xcassets/AppIcon.appiconset/Contents.json
   110  GOMODCACHE=$GOPATH/pkg/mod GOOS=ios GOARCH=arm64 GOFLAGS=-tags=ios CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_CXXFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_LDFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_ENABLED=1 DARWIN_SDK=iphoneos go build -tags tag1 -x -ldflags=-w -o=$WORK/ios/arm64 {{.Pkg}}
   111  GOMODCACHE=$GOPATH/pkg/mod GOOS=ios GOARCH=amd64 GOFLAGS=-tags=ios CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_CXXFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_LDFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_ENABLED=1 DARWIN_SDK=iphonesimulator go build -tags tag1 -x -ldflags=-w -o=$WORK/iossimulator/amd64 {{.Pkg}}
   112  xcrun lipo -o $WORK/main/main -create $WORK/ios/arm64 $WORK/iossimulator/amd64
   113  mkdir -p $WORK/main/assets
   114  xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}
   115  mv $WORK/build/Release-iphoneos/main.app {{.BuildO}}
   116  `))
   117  
   118  var appleOtherBuildTmpl = template.Must(infoplistTmpl.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
   119  WORK=$WORK
   120  GOMODCACHE=$GOPATH/pkg/mod GOOS=ios GOARCH=arm64 GOFLAGS=-tags=ios CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_CXXFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_LDFLAGS=-isysroot iphoneos -miphoneos-version-min=13.0 -fembed-bitcode -arch arm64 CGO_ENABLED=1 DARWIN_SDK=iphoneos go build -tags tag1 -x {{.Pkg}}
   121  GOMODCACHE=$GOPATH/pkg/mod GOOS=ios GOARCH=arm64 GOFLAGS=-tags=ios CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch arm64 CGO_CXXFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch arm64 CGO_LDFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch arm64 CGO_ENABLED=1 DARWIN_SDK=iphonesimulator go build -tags tag1 -x {{.Pkg}}
   122  GOMODCACHE=$GOPATH/pkg/mod GOOS=ios GOARCH=amd64 GOFLAGS=-tags=ios CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_CXXFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_LDFLAGS=-isysroot iphonesimulator -mios-simulator-version-min=13.0 -fembed-bitcode -arch x86_64 CGO_ENABLED=1 DARWIN_SDK=iphonesimulator go build -tags tag1 -x {{.Pkg}}
   123  `))