Example #1
0
func main() {
	if len(os.Args) < 2 {
		fmt.Println("usage: go run introspector.go <namespace> [symbol...]")
		return
	}

	namespace := os.Args[1]
	gogi.Init()

	loaded := gogi.LoadNamespace(namespace)
	if !loaded {
		fmt.Printf("Failed to load namespace '%s'\n", namespace)
		return
	}

	if len(os.Args) == 2 {
		infos := gogi.GetInfos(namespace)
		for _, info := range infos {
			fmt.Println(info.GetName())
		}
	} else {
		for i := 2; i < len(os.Args); i++ {
			symbol := os.Args[i]
			info := gogi.GetInfoByName(namespace, symbol)
			if info != nil {
				Display(info)
			} else {
				fmt.Printf("Symbol '%s' not found\n", symbol)
			}
		}
	}
}
Example #2
0
func Process(namespace string) {
	infos := gogi.GetInfos(namespace)

	fmt.Printf("Generating bindings for %s...\n", namespace)

	var c_code string
	var go_code string
	for _, info := range infos {
		if info.IsDeprecated() {
			continue
		}
		var g, c string
		switch info.Type {
		case gogi.Object:
			g, c = gogi.WriteObject(info)
		case gogi.Struct:
			g, c = gogi.WriteStruct(info)
		case gogi.Enum, gogi.Flags:
			g, c = gogi.WriteEnum(info)
		case gogi.Function:
			g, c = gogi.WriteFunction(info, nil)
		default:
			//fmt.Printf("unknown info '%s' of type %s\n", info.GetName(), gogi.InfoTypeToString(info.Type))
		}

		/*
			if info.Type == gogi.Object {
				switch info.GetName() {
					case "Window", "Bin", "Container", "Widget", "InitiallyUnowned", "Object":
						g, c := gogi.WriteObject(info)
						go_code += g + "\n"
						if c != "" {
							c_code += c + "\n"
						}
				}
			} else if info.Type == gogi.Enum {
				g, c := gogi.WriteEnum(info)
				go_code += g + "\n"
				if c != "" {
					c_code += c + "\n"
				}
			}
		*/

		if g != "" {
			go_code += g + "\n"
		}
		if c != "" {
			c_code += c + "\n"
		}
	}

	pkg := strings.ToLower(namespace)
	deps, deps_exist := knownPackages[namespace]
	pkg_root := CreatePackageRoot(pkg)

	f := OpenSourceFile(pkg_root, pkg)
	f.WriteString("package " + pkg + "\n\n")
	f.WriteString("/*\n")
	if deps_exist {
		f.WriteString(fmt.Sprintf("#cgo pkg-config: %s\n", strings.Join(deps.Pkgs, " ")))
		for _, header := range deps.Headers {
			f.WriteString(fmt.Sprintf("#include <%s>\n", header))
		}
		for key, value := range deps.Typedefs {
			f.WriteString(fmt.Sprintf("#define %s %s\n", key, value))
		}
		f.WriteString("\n")
		f.WriteString("GList *EMPTY_GLIST = NULL;\n")
	}
	f.WriteString(c_code + "\n")
	f.WriteString("*/\nimport \"C\"\n")
	for _, imp := range deps.Imports {
		f.WriteString(fmt.Sprintf("import \"%s\"\n", imp))
	}
	// TODO: find a way to keep track of additional imports
	//f.WriteString("import \"unsafe\"\n\n")
	f.WriteString(go_code)
	f.WriteString("\n" + common)
	f.Close()

	// now build it
	/*
		println("Compiling...")
		cmd := exec.Command("go", "install", pkg)
		err = cmd.Run()
		if err != nil {
			println(err.Error())
		}
	*/
}