so_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 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. //go:build cgo
  5. // +build cgo
  6. package so_test
  7. import (
  8. "log"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "testing"
  15. )
  16. func requireTestSOSupported(t *testing.T) {
  17. t.Helper()
  18. switch runtime.GOARCH {
  19. case "arm64":
  20. if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
  21. t.Skip("No exec facility on iOS.")
  22. }
  23. case "ppc64":
  24. if runtime.GOOS == "linux" {
  25. t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
  26. }
  27. }
  28. if runtime.GOOS == "android" {
  29. t.Skip("No exec facility on Android.")
  30. }
  31. }
  32. func TestSO(t *testing.T) {
  33. requireTestSOSupported(t)
  34. GOPATH, err := os.MkdirTemp("", "cgosotest")
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. defer os.RemoveAll(GOPATH)
  39. modRoot := filepath.Join(GOPATH, "src", "cgosotest")
  40. if err := overlayDir(modRoot, "testdata"); err != nil {
  41. log.Panic(err)
  42. }
  43. if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
  44. log.Panic(err)
  45. }
  46. cmd := exec.Command("go", "env", "CC", "GOGCCFLAGS")
  47. cmd.Dir = modRoot
  48. cmd.Stderr = new(strings.Builder)
  49. cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
  50. out, err := cmd.Output()
  51. if err != nil {
  52. t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
  53. }
  54. lines := strings.Split(string(out), "\n")
  55. if len(lines) != 3 || lines[2] != "" {
  56. t.Fatalf("Unexpected output from %s:\n%s", strings.Join(cmd.Args, " "), lines)
  57. }
  58. cc := lines[0]
  59. if cc == "" {
  60. t.Fatal("CC environment variable (go env CC) cannot be empty")
  61. }
  62. gogccflags := strings.Split(lines[1], " ")
  63. // build shared object
  64. ext := "so"
  65. args := append(gogccflags, "-shared")
  66. switch runtime.GOOS {
  67. case "darwin", "ios":
  68. ext = "dylib"
  69. args = append(args, "-undefined", "suppress", "-flat_namespace")
  70. case "windows":
  71. ext = "dll"
  72. args = append(args, "-DEXPORT_DLL")
  73. // At least in mingw-clang it is not permitted to just name a .dll
  74. // on the command line. You must name the corresponding import
  75. // library instead, even though the dll is used when the executable is run.
  76. args = append(args, "-Wl,-out-implib,libcgosotest.a")
  77. case "aix":
  78. ext = "so.1"
  79. }
  80. sofname := "libcgosotest." + ext
  81. args = append(args, "-o", sofname, "cgoso_c.c")
  82. cmd = exec.Command(cc, args...)
  83. cmd.Dir = modRoot
  84. cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
  85. out, err = cmd.CombinedOutput()
  86. if err != nil {
  87. t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
  88. }
  89. t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
  90. if runtime.GOOS == "aix" {
  91. // Shared object must be wrapped by an archive
  92. cmd = exec.Command("ar", "-X64", "-q", "libcgosotest.a", "libcgosotest.so.1")
  93. cmd.Dir = modRoot
  94. out, err = cmd.CombinedOutput()
  95. if err != nil {
  96. t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
  97. }
  98. }
  99. cmd = exec.Command("go", "build", "-o", "main.exe", "main.go")
  100. cmd.Dir = modRoot
  101. cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
  102. out, err = cmd.CombinedOutput()
  103. if err != nil {
  104. t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
  105. }
  106. t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
  107. cmd = exec.Command("./main.exe")
  108. cmd.Dir = modRoot
  109. cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
  110. if runtime.GOOS != "windows" {
  111. s := "LD_LIBRARY_PATH"
  112. if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
  113. s = "DYLD_LIBRARY_PATH"
  114. }
  115. cmd.Env = append(os.Environ(), s+"=.")
  116. // On FreeBSD 64-bit architectures, the 32-bit linker looks for
  117. // different environment variables.
  118. if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
  119. cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
  120. }
  121. }
  122. out, err = cmd.CombinedOutput()
  123. if err != nil {
  124. t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
  125. }
  126. t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
  127. }