cbrake
August 29, 2022, 3:48pm
1
This is fascinating stuff – Zig is used to cross compile CGO code in Go projects:
In the second post, native SQLite CGO code is cross-compiled using Zig:
I’ve been using the modern-C SQLite port in SIOT, but this may open up the option to use the native port if needed.
Pocketbase does something really neat – if CGO is enabled, native sqlite is used, otherwise the modern-C translation.
//go:build cgo
package core
import (
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/pocketbase/dbx"
)
func connectDB(dbPath string) (*dbx.DB, error) {
pragmas := "_foreign_keys=1&_journal_mode=WAL&_synchronous=NORMAL&_busy_timeout=8000"
db, openErr := dbx.MustOpen("sqlite3", fmt.Sprintf("%s?%s", dbPath, pragmas))
if openErr != nil {
return nil, openErr
}
// additional pragmas not supported through the dsn string
This file has been truncated. show original
//go:build !cgo
package core
import (
"fmt"
"github.com/pocketbase/dbx"
_ "modernc.org/sqlite"
)
func connectDB(dbPath string) (*dbx.DB, error) {
pragmas := "_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=busy_timeout(8000)&_pragma=journal_size_limit(100000000)"
return dbx.MustOpen("sqlite", fmt.Sprintf("%s?%s", dbPath, pragmas))
}
cbrake
September 1, 2022, 1:27pm
2
Seems there are tradeoffs – the following is from a SQLite discord group:
khem
June 13, 2023, 4:47am
3
I think this is a nice use of zig’s capabilities for solving hard problem of cross-compiler toolchain and configuration management. This is on line with what go compiler does.
https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html
1 Like
cbrake
June 13, 2023, 12:24pm
4
I like this comment:
I have carefully designed Zig since the very beginning to treat cross compilation as a first class use case. Now that the zig cc
frontend is available, it brings these capabilities to C code.
The only sane way to do cross-compiling is to design it in from the start.