func getRuntimeModuleFile() (string, error) { if runtimemodulefile != "" { return runtimemodulefile, nil } gofiles, llfiles, cfiles, err := getRuntimeFiles() if err != nil { return "", err } var runtimeModule *llgo.Module runtimeModule, err = compileFiles(testCompiler, gofiles, "runtime") defer runtimeModule.Dispose() if err != nil { return "", err } outfile := filepath.Join(tempdir, "runtime.bc") f, err := os.Create(outfile) if err != nil { return "", err } err = llvm.WriteBitcodeToFile(runtimeModule.Module, f) if err != nil { f.Close() return "", err } f.Close() for i, cfile := range cfiles { bcfile := filepath.Join(tempdir, fmt.Sprintf("%d.bc", i)) args := []string{"-c", "-emit-llvm", "-o", bcfile, cfile} if runtime.GOOS != "darwin" { // TODO(q): -g breaks badly on my system at the moment, // so is not enabled on darwin for now args = append([]string{"-g"}, args...) } cmd := exec.Command("clang", args...) cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { return "", fmt.Errorf("clang failed: %s", err) } llfiles = append(llfiles, bcfile) } if llfiles != nil { args := append([]string{"-o", outfile, outfile}, llfiles...) cmd := exec.Command("llvm-link", args...) output, err := cmd.CombinedOutput() if err != nil { err = fmt.Errorf("llvm-link failed: %s", err) fmt.Fprintf(os.Stderr, string(output)) return "", err } } runtimemodulefile = outfile return runtimemodulefile, nil }
func runMainFunction(m *llgo.Module) (output []string, err error) { runtimeModule, err := getRuntimeModuleFile() if err != nil { return } err = llvm.VerifyModule(m.Module, llvm.ReturnStatusAction) if err != nil { return nil, fmt.Errorf("Verification failed: %v", err) } bcpath := filepath.Join(tempdir, "test.bc") bcfile, err := os.Create(bcpath) if err != nil { return nil, err } llvm.WriteBitcodeToFile(m.Module, bcfile) bcfile.Close() cmd := exec.Command("llvm-link", "-o", bcpath, bcpath, runtimeModule) data, err := cmd.CombinedOutput() if err != nil { output = strings.Split(strings.TrimSpace(string(data)), "\n") return output, fmt.Errorf("llvm-link failed: %v", err) } exepath := filepath.Join(tempdir, "test") args := []string{"-pthread", "-o", exepath, bcpath} if runtime.GOOS != "darwin" { // TODO(q): -g breaks badly on my system at the moment, so is not enabled on darwin for now args = append([]string{"-g"}, args...) } cmd = exec.Command("clang", args...) data, err = cmd.CombinedOutput() if err != nil { output = strings.Split(strings.TrimSpace(string(data)), "\n") return output, fmt.Errorf("clang failed: %v", err) } cmd = exec.Command(exepath) data, err = cmd.CombinedOutput() output = strings.Split(strings.TrimSpace(string(data)), "\n") return output, err }
func writeObjectFile(m *llgo.Module) error { var outfile *os.File switch *outputFile { case "-": outfile = os.Stdout default: var err error outfile, err = os.Create(*outputFile) if err != nil { return err } } err := llvm.VerifyModule(m.Module, llvm.ReturnStatusAction) if err != nil { return fmt.Errorf("Verification failed: %v", err) } return llvm.WriteBitcodeToFile(m.Module, outfile) }