Exemple #1
0
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
}
Exemple #2
0
func getRuntimeModule() (llvm.Module, error) {
	gofiles, llfiles, err := getRuntimeFiles()
	if err != nil {
		return llvm.Module{}, err
	}

	var runtimeModule *llgo.Module
	runtimeModule, err = compileFiles(testCompiler, gofiles, "runtime")
	if err != nil {
		return llvm.Module{}, err
	}

	if llfiles != nil {
		outfile, err := ioutil.TempFile("", "runtime.ll")
		if err != nil {
			runtimeModule.Dispose()
			return llvm.Module{}, err
		}
		defer func() {
			outfile.Close()
			os.Remove(outfile.Name())
		}()
		err = llvm.WriteBitcodeToFile(runtimeModule.Module, outfile)
		if err != nil {
			runtimeModule.Dispose()
			return llvm.Module{}, err
		}
		runtimeModule.Dispose()
		o := outfile.Name()
		for _, llfile := range llfiles {
			cmd := exec.Command("llvm-link", "-o", o, o, llfile)
			output, err := cmd.CombinedOutput()
			if err != nil {
				err = fmt.Errorf("llvm-link failed: %s", err)
				fmt.Fprintf(os.Stderr, string(output))
				return llvm.Module{}, err
			}
		}
		runtimeModule.Module, err = llvm.ParseBitcodeFile(o)
		if err != nil {
			return llvm.Module{}, err
		}
	}

	return runtimeModule.Module, nil
}