|
1 |
| -// Copyright 2016 The Go Authors. All rights reserved. |
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | package fortran
|
6 | 6 |
|
7 |
| -import "testing" |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "runtime" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | +) |
8 | 16 |
|
9 | 17 | func TestFortran(t *testing.T) {
|
10 |
| - if a := TheAnswer(); a != 42 { |
11 |
| - t.Errorf("Unexpected result for The Answer. Got: %d Want: 42", a) |
| 18 | + // Find the FORTRAN compiler. |
| 19 | + fc := os.Getenv("FC") |
| 20 | + if fc == "" { |
| 21 | + fc, _ = exec.LookPath("gfortran") |
| 22 | + } |
| 23 | + if fc == "" { |
| 24 | + t.Skip("fortran compiler not found (try setting $FC)") |
| 25 | + } |
| 26 | + |
| 27 | + var fcExtra []string |
| 28 | + if strings.Contains(fc, "gfortran") { |
| 29 | + // TODO: This duplicates but also diverges from logic from cmd/go |
| 30 | + // itself. For example, cmd/go merely adds -lgfortran without the extra |
| 31 | + // library path work. If this is what's necessary to run gfortran, we |
| 32 | + // should reconcile the logic here and in cmd/go.. Maybe this should |
| 33 | + // become a cmd/go script test to share that logic. |
| 34 | + |
| 35 | + // Add -m32 if we're targeting 386, in case this is a cross-compile. |
| 36 | + if runtime.GOARCH == "386" { |
| 37 | + fcExtra = append(fcExtra, "-m32") |
| 38 | + } |
| 39 | + |
| 40 | + // Find libgfortran. If the FORTRAN compiler isn't bundled |
| 41 | + // with the C linker, this may be in a path the C linker can't |
| 42 | + // find on its own. (See #14544) |
| 43 | + libExt := "so" |
| 44 | + switch runtime.GOOS { |
| 45 | + case "darwin": |
| 46 | + libExt = "dylib" |
| 47 | + case "aix": |
| 48 | + libExt = "a" |
| 49 | + } |
| 50 | + libPath, err := exec.Command(fc, append([]string{"-print-file-name=libgfortran." + libExt}, fcExtra...)...).CombinedOutput() |
| 51 | + if err != nil { |
| 52 | + t.Errorf("error invoking %s: %s", fc, err) |
| 53 | + } |
| 54 | + libDir := filepath.Dir(string(libPath)) |
| 55 | + cgoLDFlags := os.Getenv("CGO_LDFLAGS") |
| 56 | + cgoLDFlags += " -L " + libDir |
| 57 | + if runtime.GOOS != "aix" { |
| 58 | + cgoLDFlags += " -Wl,-rpath," + libDir |
| 59 | + } |
| 60 | + t.Logf("CGO_LDFLAGS=%s", cgoLDFlags) |
| 61 | + os.Setenv("CGO_LDFLAGS", cgoLDFlags) |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + // Do a test build that doesn't involve Go FORTRAN support. |
| 66 | + fcArgs := append([]string{"helloworld/helloworld.f90", "-o", "/dev/null"}, fcExtra...) |
| 67 | + t.Logf("%s %s", fc, fcArgs) |
| 68 | + if err := exec.Command(fc, fcArgs...).Run(); err != nil { |
| 69 | + t.Skipf("skipping Fortran test: could not build helloworld.f90 with %s: %s", fc, err) |
| 70 | + } |
| 71 | + |
| 72 | + // Finally, run the actual test. |
| 73 | + t.Log("go", "run", "./testdata/testprog") |
| 74 | + out, err := exec.Command("go", "run", "./testdata/testprog").CombinedOutput() |
| 75 | + if err == nil && string(out) != "ok\n" { |
| 76 | + err = fmt.Errorf("expected ok") |
| 77 | + } |
| 78 | + if err != nil { |
| 79 | + t.Errorf("%s\nOutput:\n%s", err, string(out)) |
12 | 80 | }
|
13 | 81 | }
|
0 commit comments