...
1
2
3
4
5 package fortran
6
7 import (
8 "internal/testenv"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "runtime"
13 "strings"
14 "testing"
15 )
16
17 func TestFortran(t *testing.T) {
18 testenv.MustHaveGoRun(t)
19 testenv.MustHaveCGO(t)
20
21
22 fc := os.Getenv("FC")
23 if fc == "" {
24 fc, _ = exec.LookPath("gfortran")
25 }
26 if fc == "" {
27 t.Skip("fortran compiler not found (try setting $FC)")
28 }
29
30 var fcExtra []string
31 if strings.Contains(fc, "gfortran") {
32
33
34
35
36
37
38
39 if runtime.GOARCH == "386" {
40 fcExtra = append(fcExtra, "-m32")
41 }
42
43
44
45
46 libExt := "so"
47 switch runtime.GOOS {
48 case "darwin":
49 libExt = "dylib"
50 case "aix":
51 libExt = "a"
52 }
53 libPath, err := exec.Command(fc, append([]string{"-print-file-name=libgfortran." + libExt}, fcExtra...)...).CombinedOutput()
54 if err != nil {
55 t.Errorf("error invoking %s: %s", fc, err)
56 }
57 libDir := filepath.Dir(string(libPath))
58 cgoLDFlags := os.Getenv("CGO_LDFLAGS")
59 cgoLDFlags += " -L " + libDir
60 if runtime.GOOS != "aix" {
61 cgoLDFlags += " -Wl,-rpath," + libDir
62 }
63 t.Logf("CGO_LDFLAGS=%s", cgoLDFlags)
64 os.Setenv("CGO_LDFLAGS", cgoLDFlags)
65
66 }
67
68
69 fcArgs := append([]string{"testdata/helloworld/helloworld.f90", "-o", "/dev/null"}, fcExtra...)
70 t.Logf("%s %s", fc, fcArgs)
71 if err := exec.Command(fc, fcArgs...).Run(); err != nil {
72 t.Skipf("skipping Fortran test: could not build helloworld.f90 with %s: %s", fc, err)
73 }
74
75
76 t.Log("go", "run", "./testdata/testprog")
77 var stdout, stderr strings.Builder
78 cmd := exec.Command("go", "run", "./testdata/testprog")
79 cmd.Stdout = &stdout
80 cmd.Stderr = &stderr
81 err := cmd.Run()
82 t.Logf("%v", cmd)
83 if stderr.Len() != 0 {
84 t.Logf("stderr:\n%s", stderr.String())
85 }
86 if err != nil {
87 t.Errorf("%v\n%s", err, stdout.String())
88 } else if stdout.String() != "ok\n" {
89 t.Errorf("stdout:\n%s\nwant \"ok\"", stdout.String())
90 }
91 }
92
View as plain text