Coder Social home page Coder Social logo

gengo's Introduction

gengo

Gengo is a simple and lightweight pure-Go wrapper generator for C libraries. It uses Clang to parse the C header files and generates Go code that wraps around a shared library with high-performance calls without needing any Cgo.

Usage

Example usage for Zydis:

package main

import (
	"log"
	"os"

	"github.com/can1357/gengo/clang"
	"github.com/can1357/gengo/gengo"
)

func main() {
	pkg := gengo.NewPackage("zydis",
		gengo.WithRemovePrefix(
			"Zydis_", "Zyan_", "Zycore_",
			"Zydis", "Zyan", "Zycore",
		),
		gengo.WithInferredMethods([]gengo.MethodInferenceRule{
			{Name: "ZydisDecoder", Receiver: "Decoder"},
			{Name: "ZydisEncoder", Receiver: "EncoderRequest"},
			{Name: "ZydisFormatterBuffer", Receiver: "FormatterBuffer"},
			{Name: "ZydisFormatter", Receiver: "ZydisFormatter *"},
			{Name: "ZyanVector", Receiver: "Vector"},
			{Name: "ZyanStringView", Receiver: "StringView"},
			{Name: "ZyanString", Receiver: "String"},
			{Name: "ZydisRegister", Receiver: "Register"},
			{Name: "ZydisMnemonic", Receiver: "Mnemonic"},
			{Name: "ZydisISASet", Receiver: "ISASet"},
			{Name: "ZydisISAExt", Receiver: "ISAExt"},
			{Name: "ZydisCategory", Receiver: "Category"},
		}),
		gengo.WithForcedSynthetic(
			"ZydisShortString_",
			"struct ZydisShortString_",
		),
	)
	err := pkg.Transform("zydis", &clang.Options{
		Sources: []string{"./Zydis.h"},
		AdditionalParams: []string{
			"-DZYAN_NO_LIBC",
			"-DZYAN_STATIC_ASSERT",
		},
	})
	if err != nil {
		log.Fatalf("Failed to transform: %v", err)
	}

	if err := pkg.WriteToDir("zydis"); err != nil {
		log.Fatalf("Failed to write the directory: %v", err)
	}
}

gengo's People

Contributors

can1357 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gengo's Issues

AST Record Layout bug

*** Dumping AST Record Layout
         0 | struct __NSConstantString_tag
         0 |   const int * isa
         8 |   int flags
        16 |   const char * str
        24 |   long length
           | [sizeof=32, align=8,
           |  nvsize=32, nvalign=8]


GOROOT=C:\Program Files\Go #gosetup
GOPATH=C:\Users\Admin\go #gosetup
"C:\Program Files\Go\bin\go.exe" test -c -o C:\Users\Admin\AppData\Local\JetBrains\GoLand2024.1\tmp\GoLand\___TestDemoDll_in_github_com_can1357_gengo_bind_demo.test.exe github.com/can1357/gengo/bind/demo #gosetup
"C:\Program Files\Go\bin\go.exe" tool test2json -t C:\Users\Admin\AppData\Local\JetBrains\GoLand2024.1\tmp\GoLand\___TestDemoDll_in_github_com_can1357_gengo_bind_demo.test.exe -test.v -test.paniconexit0 -test.run ^\QTestDemoDll\E$ #gosetup
2024-06-19 09:18:26    Trace ->  --------- title --------- โ”‚ ------------------ info ------------------ //runtime.doInit1+0xec C:/Program Files/Go/src/runtime/proc.go:7176
=== RUN   TestDemoDll
panic: input does not match format

goroutine 21 [running]:
github.com/ddkwork/golibrary/mylog.check[...](0x0?)
	D:/workspace/workspace/branch/golibrary/mylog/check.go:216 +0x186
github.com/ddkwork/golibrary/mylog.Check2[...](0x2, {0xccbf40, 0xc000280170?})
	D:/workspace/workspace/branch/golibrary/mylog/check.go:27 +0x45
github.com/can1357/gengo/clang.(*RecordLayout).UnmarshalString(0xc0002d0c00, {0xc00049401e?, 0xc1fbca?})
	D:/workspace/workspace/branch/gui/gengo/clang/layout_parse.go:78 +0x4c9
github.com/can1357/gengo/clang.(*LayoutMap).UnmarshalString(0xc00028e2c0, {0xc000494000?, 0x103?})
	D:/workspace/workspace/branch/gui/gengo/clang/layout_parse.go:145 +0x119
github.com/can1357/gengo/clang.ParseLayoutMap(...)
	D:/workspace/workspace/branch/gui/gengo/clang/layout_parse.go:156
github.com/can1357/gengo/clang.Parse.func2()
	D:/workspace/workspace/branch/gui/gengo/clang/invoke.go:82 +0x185
golang.org/x/sync/errgroup.(*Group).Go.func1()
	D:/desk/go/golang.org/x/sync@v0.7.0/errgroup/errgroup.go:78 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 19
	D:/desk/go/golang.org/x/sync@v0.7.0/errgroup/errgroup.go:75 +0x96


Process finished with the exit code 1
we need:

func (r *RecordLayout) UnmarshalString(data string) error {
	// mylog.Check(errors.New("improperly terminated layout"))
	first := true
	for _, line := range strings.Split(data, "\n") {
		before, after, found := strings.Cut(line, "|")
		if !found {
			continue
		}

		// If before is empty, then it is the size and align.
		before = strings.TrimSpace(before)
		if before == "" {
			after = strings.TrimSpace(after)
			//mylog.Check2(fmt.Sscanf(after, "[sizeof=%d, align=%d]", &r.Size, &r.Align))
			mylog.Check2(fmt.Sscanf(after, "[sizeof=%d, align=%d", &r.Size, &r.Align))
			break
		}

remove "]"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.