1 // Copyright 2022 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.
17 func TestFortran(t *testing.T) {
18 // Find the FORTRAN compiler.
21 fc, _ = exec.LookPath("gfortran")
24 t.Skip("fortran compiler not found (try setting $FC)")
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.
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")
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)
50 libPath, err := exec.Command(fc, append([]string{"-print-file-name=libgfortran." + libExt}, fcExtra...)...).CombinedOutput()
52 t.Errorf("error invoking %s: %s", fc, err)
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
60 t.Logf("CGO_LDFLAGS=%s", cgoLDFlags)
61 os.Setenv("CGO_LDFLAGS", cgoLDFlags)
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)
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")
79 t.Errorf("%s\nOutput:\n%s", err, string(out))