Skip to content

Commit bad5f31

Browse files
committed
Merge branch 'main' of https://github.com/goplus/llcppg into doc/import-from-conan
2 parents cc6fd24 + 5cf6503 commit bad5f31

File tree

32 files changed

+507
-433
lines changed

32 files changed

+507
-433
lines changed

README.md

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -179,61 +179,50 @@ cJSON_bool Bool
179179

180180
### Customizing Bindings
181181
#### Function Customization
182-
When you run llcppg directly with the above configuration, it will generate function names according to the configuration. After execution, you'll find a `llcppg.symb.json` file in the current directory.
182+
After execution, you can directly see the mapped functions in the result.
183183

184+
```go
185+
//go:linkname CreateObject C.cJSON_CreateObject
186+
func CreateObject() *CJSON
187+
// llgo:link (*CJSON).AddItemToObject C.cJSON_AddItemToObject
188+
func (recv_ *CJSON) AddItemToObject(string *c.Char, item *CJSON) Bool {
189+
return 0
190+
}
191+
/* Render a cJSON entity to text for transfer/storage without any formatting. */
192+
// llgo:link (*CJSON).PrintUnformatted C.cJSON_PrintUnformatted
193+
func (recv_ *CJSON) PrintUnformatted() *c.Char {
194+
return nil
195+
}
196+
```
197+
You can specify function mapping behavior in `llcppg.cfg` by config the `symMap` field:
184198
```json
185-
[
186-
{
187-
"mangle": "cJSON_CreateObject",
188-
"c++": "cJSON_CreateObject()",
189-
"go": "CreateObject"
190-
},
191-
{
192-
"mangle": "cJSON_AddItemToObject",
193-
"c++": "cJSON_AddItemToObject(cJSON *, const char *, cJSON *)",
194-
"go": "(*CJSON).AddItemToObject"
195-
},
196-
{
197-
"mangle": "cJSON_PrintUnformatted",
198-
"c++": "cJSON_PrintUnformatted(const cJSON *)",
199-
"go": "(*CJSON).PrintUnformatted"
200-
},
201-
]
202-
```
203-
204-
- `mangle` field contains the symbol name of function
205-
- `c++` field shows the function prototype from the header file
206-
- `go` field displays the function name that will be generated in LLGo binding.
199+
{
200+
"symMap":{
201+
"mangle":"<goFuncName> | <.goMethodName> | -"
202+
}
203+
}
204+
```
205+
`mangle` is the symbol name of the function. For the value of `mangle`, you can customize it as:
206+
1. `goFuncName` - generates a regular function named `goFuncName`
207+
2. `.goMethodName` - generates a method named `goMethodName` (if it doesn't meet the rules for generating a method, it will be generated as a regular function)
208+
3. `-` - completely ignore this function
207209

208-
You can customize this field to:
209-
1. Change function names (e.g. "CreateObject" to "Object" for simplicity)
210-
2. Remove the method receiver prefix to generate a function instead of a method
211-
3. Set it to "-" to completely ignore this function
212-
213-
For example, to convert `(*CJSON).PrintUnformatted` from a method to a function, simply remove the `(*CJSON).` prefix in the configuration file:
210+
For example, to convert `(*CJSON).PrintUnformatted` from a method to a function, you can use follow config:
214211

215212
```json
216-
[
217-
{
218-
"mangle": "cJSON_PrintUnformatted",
219-
"c++": "cJSON_PrintUnformatted(const cJSON *)",
220-
"go": "PrintUnformatted"
221-
},
222-
]
213+
{
214+
"symMap":{
215+
"cJSON_PrintUnformatted":"PrintUnformatted"
216+
}
217+
}
223218
```
224219
This will generate a function instead of a method in the Go code:
225220
```go
226221
//go:linkname PrintUnformatted C.cJSON_PrintUnformatted
227-
func PrintUnformatted(item *CJSON) *int8
222+
func PrintUnformatted(item *CJSON) *c.Char
228223
```
229224

230-
You can customize the generated bindings by modifying the `llcppg.symb.json` file.
231-
232-
After modifying the file, run llcppg again to apply your customized bindings.
233-
234-
The symbol table is generated by llcppsymg, which is internally called by llcppg to generate the symbol table as input for Go code generation.
235-
236-
You can also run llcppsymg separately to customize the symbol table before running llcppg. To do this, use the command:
225+
You can also run llcppsymg separately. To do this, use the command:
237226

238227
```sh
239228
llcppg -symbgen

_llcppgtest/cjson/conf/linux/llcppg.cfg

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,15 @@
1111
},
1212
"trimPrefixes": ["cJSON_", "cJSONUtils_"],
1313
"cplusplus": false,
14-
"mix":true
14+
"mix":true,
15+
"symMap": {
16+
"cJSON_PrintUnformatted":".CStr",
17+
"cJSON_CreateObject":"Object",
18+
"cJSON_CreateArray":"Array",
19+
"cJSON_CreateString":"String",
20+
"cJSON_free":"FreeCStr",
21+
"cJSON_AddItemToArray":".AddItem",
22+
"cJSON_AddItemToObject":".SetItem",
23+
"cJSON_free":"FreeCStr"
24+
}
1525
}

_llcppgtest/cjson/conf/linux/llcppg.symb.json

Lines changed: 0 additions & 42 deletions
This file was deleted.

_llcppgtest/cjson/llcppg.cfg

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,15 @@
1010
"cJSON": "JSON"
1111
},
1212
"trimPrefixes": ["cJSON_", "cJSONUtils_"],
13-
"cplusplus": false
13+
"cplusplus": false,
14+
"symMap": {
15+
"cJSON_PrintUnformatted":".CStr",
16+
"cJSON_CreateObject":"Object",
17+
"cJSON_CreateArray":"Array",
18+
"cJSON_CreateString":"String",
19+
"cJSON_free":"FreeCStr",
20+
"cJSON_AddItemToArray":".AddItem",
21+
"cJSON_AddItemToObject":".SetItem",
22+
"cJSON_free":"FreeCStr"
23+
}
1424
}

_llcppgtest/cjson/llcppg.symb.json

Lines changed: 0 additions & 42 deletions
This file was deleted.

_llcppgtest/raylib/llcppg.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"raymath.h",
88
"rlgl.h"
99
],
10-
"cplusplus": false
10+
"cplusplus": false,
11+
"symMap": {
12+
"ClearBackground": "ClearBackground"
13+
}
1114
}

_llcppgtest/raylib/llcppg.symb.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

_llcppgtest/sqlite/conf/linux/llcppg.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
],
99
"trimPrefixes": ["sqlite3_","SQLITE_"],
1010
"cplusplus": false,
11-
"mix":true
11+
"mix":true,
12+
"symMap":{
13+
"sqlite3_finalize":".Close"
14+
}
1215
}

_llcppgtest/sqlite/conf/linux/llcppg.symb.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

_llcppgtest/sqlite/llcppg.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"sqlite3.h"
88
],
99
"trimPrefixes": ["sqlite3_","SQLITE_"],
10-
"cplusplus": false
10+
"cplusplus": false,
11+
"symMap":{
12+
"sqlite3_finalize":".Close"
13+
}
1114
}

0 commit comments

Comments
 (0)