2024-08-06 01:03:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-07 06:51:30 +00:00
|
|
|
"encoding/json"
|
2024-08-06 01:03:23 +00:00
|
|
|
"flag"
|
2024-08-06 02:29:12 +00:00
|
|
|
"io/ioutil"
|
2024-08-06 01:03:23 +00:00
|
|
|
"log"
|
2024-08-19 07:14:47 +00:00
|
|
|
"os"
|
2024-10-20 04:55:18 +00:00
|
|
|
"os/exec"
|
2024-08-06 02:29:12 +00:00
|
|
|
"path/filepath"
|
2024-10-07 06:25:27 +00:00
|
|
|
"runtime"
|
2024-08-06 01:03:23 +00:00
|
|
|
"strings"
|
2024-10-07 06:25:27 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-10-18 21:08:56 +00:00
|
|
|
ClangSubprocessCount = 2
|
2024-10-16 05:05:07 +00:00
|
|
|
BaseModule = "github.com/mappu/miqt"
|
2024-08-06 01:03:23 +00:00
|
|
|
)
|
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
func cacheFilePath(inputHeader string) string {
|
|
|
|
return filepath.Join("cachedir", strings.Replace(inputHeader, `/`, `__`, -1)+".json")
|
|
|
|
}
|
|
|
|
|
2024-10-20 05:29:37 +00:00
|
|
|
func importPathForQtPackage(packageName string) string {
|
|
|
|
return BaseModule + "/" + packageName
|
|
|
|
}
|
|
|
|
|
2024-10-14 05:54:39 +00:00
|
|
|
func findHeadersInDir(srcDir string) []string {
|
|
|
|
content, err := os.ReadDir(srcDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []string
|
|
|
|
|
|
|
|
for _, includeFile := range content {
|
|
|
|
if includeFile.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(includeFile.Name(), `.h`) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fullPath := filepath.Join(srcDir, includeFile.Name())
|
|
|
|
if !AllowHeader(fullPath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, fullPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-10-14 05:55:16 +00:00
|
|
|
func cleanGeneratedFilesInDir(dirpath string) {
|
|
|
|
log.Printf("Cleaning up output directory %q...", dirpath)
|
|
|
|
|
2024-10-16 05:05:24 +00:00
|
|
|
_ = os.MkdirAll(dirpath, 0755)
|
|
|
|
|
2024-10-14 05:55:16 +00:00
|
|
|
existing, err := os.ReadDir(dirpath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaned := 0
|
|
|
|
for _, e := range existing {
|
|
|
|
if e.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(e.Name(), `gen_`) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// One of ours, clean up
|
|
|
|
err := os.Remove(filepath.Join(dirpath, e.Name()))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WARNING: Failed to remove existing file %q", e.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaned++
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Removed %d file(s).", cleaned)
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:55:18 +00:00
|
|
|
func pkgConfigCflags(packageName string) string {
|
|
|
|
stdout, err := exec.Command(`pkg-config`, `--cflags`, packageName).Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(stdout)
|
|
|
|
}
|
|
|
|
|
2024-08-06 01:03:23 +00:00
|
|
|
func main() {
|
|
|
|
clang := flag.String("clang", "clang", "Custom path to clang")
|
2024-10-16 05:07:05 +00:00
|
|
|
outDir := flag.String("outdir", "../../", "Output directory for generated gen_** files")
|
2024-10-20 05:02:08 +00:00
|
|
|
extraLibsDir := flag.String("extralibs", "/usr/local/src/", "Base directory to find extra library checkouts")
|
2024-08-06 01:03:23 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2024-10-16 05:07:05 +00:00
|
|
|
generate(
|
|
|
|
"qt",
|
|
|
|
[]string{
|
|
|
|
"/usr/include/x86_64-linux-gnu/qt5/QtCore",
|
|
|
|
"/usr/include/x86_64-linux-gnu/qt5/QtGui",
|
|
|
|
"/usr/include/x86_64-linux-gnu/qt5/QtWidgets",
|
|
|
|
},
|
|
|
|
*clang,
|
2024-10-20 04:55:18 +00:00
|
|
|
strings.Fields(pkgConfigCflags("Qt5Widgets")),
|
2024-10-20 05:29:37 +00:00
|
|
|
*outDir,
|
2024-10-20 04:58:00 +00:00
|
|
|
ClangMatchSameHeaderDefinitionOnly,
|
2024-10-16 05:07:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
generate(
|
2024-10-20 05:29:37 +00:00
|
|
|
"qt/qprintsupport",
|
2024-10-16 05:07:05 +00:00
|
|
|
[]string{
|
|
|
|
"/usr/include/x86_64-linux-gnu/qt5/QtPrintSupport",
|
|
|
|
},
|
|
|
|
*clang,
|
2024-10-20 04:55:18 +00:00
|
|
|
strings.Fields(pkgConfigCflags("Qt5PrintSupport")),
|
2024-10-20 05:29:37 +00:00
|
|
|
*outDir,
|
2024-10-20 04:58:00 +00:00
|
|
|
ClangMatchSameHeaderDefinitionOnly,
|
2024-10-16 05:07:05 +00:00
|
|
|
)
|
2024-10-20 04:59:13 +00:00
|
|
|
|
|
|
|
// Depends on QtCore/Gui/Widgets, QPrintSupport
|
|
|
|
generate(
|
2024-10-20 05:29:37 +00:00
|
|
|
"qt-restricted-extras/qscintilla",
|
2024-10-20 04:59:13 +00:00
|
|
|
[]string{
|
|
|
|
"/usr/include/x86_64-linux-gnu/qt5/Qsci",
|
|
|
|
},
|
|
|
|
*clang,
|
|
|
|
strings.Fields(pkgConfigCflags("Qt5PrintSupport")),
|
2024-10-20 05:29:37 +00:00
|
|
|
*outDir,
|
2024-10-20 04:59:13 +00:00
|
|
|
ClangMatchSameHeaderDefinitionOnly,
|
|
|
|
)
|
2024-10-20 05:02:08 +00:00
|
|
|
|
|
|
|
// Depends on QtCore/Gui/Widgets
|
|
|
|
generate(
|
2024-10-20 05:29:37 +00:00
|
|
|
"qt-extras/scintillaedit",
|
2024-10-20 05:02:08 +00:00
|
|
|
[]string{
|
|
|
|
filepath.Join(*extraLibsDir, "scintilla/qt/ScintillaEdit/ScintillaEdit.h"),
|
|
|
|
},
|
|
|
|
*clang,
|
|
|
|
strings.Fields("--std=c++1z "+pkgConfigCflags("ScintillaEdit")),
|
2024-10-20 05:29:37 +00:00
|
|
|
*outDir,
|
2024-10-20 05:02:08 +00:00
|
|
|
(&clangMatchUnderPath{filepath.Join(*extraLibsDir, "scintilla")}).Match,
|
|
|
|
)
|
2024-10-16 05:07:05 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 04:58:00 +00:00
|
|
|
func generate(packageName string, srcDirs []string, clangBin string, cflags []string, outDir string, matcher ClangMatcher) {
|
2024-08-19 07:14:47 +00:00
|
|
|
|
2024-10-16 05:07:05 +00:00
|
|
|
var includeFiles []string
|
|
|
|
for _, srcDir := range srcDirs {
|
2024-10-20 04:55:29 +00:00
|
|
|
if strings.HasSuffix(srcDir, `.h`) {
|
|
|
|
includeFiles = append(includeFiles, srcDir) // single .h
|
|
|
|
} else {
|
|
|
|
includeFiles = append(includeFiles, findHeadersInDir(srcDir)...)
|
|
|
|
}
|
2024-08-19 07:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Found %d header files to process.", len(includeFiles))
|
|
|
|
|
2024-10-20 05:29:37 +00:00
|
|
|
outDir = filepath.Join(outDir, packageName)
|
|
|
|
|
2024-10-16 05:07:05 +00:00
|
|
|
cleanGeneratedFilesInDir(outDir)
|
2024-08-06 01:03:23 +00:00
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
var processHeaders []*CppParsedHeader
|
2024-09-16 07:03:45 +00:00
|
|
|
atr := astTransformRedundant{
|
|
|
|
preserve: make(map[string]*CppEnum),
|
|
|
|
}
|
2024-08-26 10:45:11 +00:00
|
|
|
|
2024-09-19 07:39:05 +00:00
|
|
|
InsertTypedefs()
|
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
//
|
|
|
|
// PASS 0 (Fill clang cache)
|
|
|
|
//
|
|
|
|
|
2024-10-20 04:58:00 +00:00
|
|
|
generateClangCaches(includeFiles, clangBin, cflags, matcher)
|
2024-09-11 05:44:36 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
// The cache should now be fully populated.
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
//
|
|
|
|
// PASS 1 (clang2il)
|
|
|
|
//
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
for _, inputHeader := range includeFiles {
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
cacheFile := cacheFilePath(inputHeader)
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
astJson, err := ioutil.ReadFile(cacheFile)
|
|
|
|
if err != nil {
|
|
|
|
panic("Expected cache to be created for " + inputHeader + ", but got error " + err.Error())
|
|
|
|
}
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-07 06:25:27 +00:00
|
|
|
// Json decode
|
|
|
|
var astInner []interface{} = nil
|
|
|
|
err = json.Unmarshal(astJson, &astInner)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2024-08-07 06:51:30 +00:00
|
|
|
}
|
2024-08-10 01:32:43 +00:00
|
|
|
|
2024-10-16 05:05:38 +00:00
|
|
|
if astInner == nil {
|
|
|
|
panic("Null in cache file for " + inputHeader)
|
|
|
|
}
|
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
// Convert it to our intermediate format
|
2024-08-27 07:12:08 +00:00
|
|
|
parsed, err := parseHeader(astInner, "")
|
2024-08-10 01:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-07 06:51:30 +00:00
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
parsed.Filename = inputHeader // Stash
|
|
|
|
|
2024-08-15 07:51:49 +00:00
|
|
|
// AST transforms on our IL
|
2024-08-25 07:08:28 +00:00
|
|
|
astTransformChildClasses(parsed) // must be first
|
2024-08-15 07:51:49 +00:00
|
|
|
astTransformOptional(parsed)
|
|
|
|
astTransformOverloads(parsed)
|
2024-09-16 07:03:45 +00:00
|
|
|
atr.Process(parsed)
|
2024-08-15 07:51:49 +00:00
|
|
|
|
2024-08-26 10:48:37 +00:00
|
|
|
// Update global state tracker (AFTER astTransformChildClasses)
|
|
|
|
for _, c := range parsed.Classes {
|
2024-10-16 05:07:05 +00:00
|
|
|
KnownClassnames[c.ClassName] = lookupResultClass{packageName}
|
2024-08-26 10:48:37 +00:00
|
|
|
}
|
2024-08-26 10:49:33 +00:00
|
|
|
for _, td := range parsed.Typedefs {
|
2024-10-16 05:07:05 +00:00
|
|
|
KnownTypedefs[td.Alias] = lookupResultTypedef{packageName, td /* copy */}
|
2024-08-26 10:49:33 +00:00
|
|
|
}
|
2024-09-04 06:54:10 +00:00
|
|
|
for _, en := range parsed.Enums {
|
2024-10-16 05:07:05 +00:00
|
|
|
KnownEnums[en.EnumName] = lookupResultEnum{packageName, en /* copy */}
|
2024-09-04 06:54:10 +00:00
|
|
|
}
|
2024-08-26 10:49:33 +00:00
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
processHeaders = append(processHeaders, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// PASS 2
|
|
|
|
//
|
|
|
|
|
|
|
|
for _, parsed := range processHeaders {
|
|
|
|
|
|
|
|
log.Printf("Processing %q...", parsed.Filename)
|
2024-08-26 10:49:33 +00:00
|
|
|
|
|
|
|
// More AST transforms on our IL
|
|
|
|
astTransformTypedefs(parsed)
|
2024-08-27 06:44:10 +00:00
|
|
|
astTransformBlocklist(parsed) // Must happen after typedef transformation
|
2024-08-26 10:49:33 +00:00
|
|
|
|
2024-08-15 07:51:49 +00:00
|
|
|
{
|
|
|
|
// Save the IL file for debug inspection
|
2024-08-14 05:43:54 +00:00
|
|
|
jb, err := json.MarshalIndent(parsed, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
err = ioutil.WriteFile(cacheFilePath(parsed.Filename)+".ours.json", jb, 0644)
|
2024-08-14 05:43:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 07:11:36 +00:00
|
|
|
// Breakout if there is nothing bindable
|
|
|
|
if parsed.Empty() {
|
|
|
|
log.Printf("Nothing in this header was bindable.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
// Emit 3 code files from the intermediate format
|
2024-10-16 05:07:05 +00:00
|
|
|
outputName := filepath.Join(outDir, "gen_"+strings.TrimSuffix(filepath.Base(parsed.Filename), `.h`))
|
2024-08-14 05:43:54 +00:00
|
|
|
|
2024-10-16 05:07:05 +00:00
|
|
|
goSrc, err := emitGo(parsed, filepath.Base(parsed.Filename), packageName)
|
2024-08-07 06:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-10 01:32:43 +00:00
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
err = ioutil.WriteFile(outputName+".go", []byte(goSrc), 0644)
|
2024-08-10 01:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-07 06:51:30 +00:00
|
|
|
|
2024-08-26 10:45:11 +00:00
|
|
|
bindingCppSrc, err := emitBindingCpp(parsed, filepath.Base(parsed.Filename))
|
2024-08-14 05:43:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
err = ioutil.WriteFile(outputName+".cpp", []byte(bindingCppSrc), 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-06 01:03:23 +00:00
|
|
|
|
2024-10-16 05:07:05 +00:00
|
|
|
bindingHSrc, err := emitBindingHeader(parsed, filepath.Base(parsed.Filename), packageName)
|
2024-08-14 05:43:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
err = ioutil.WriteFile(outputName+".h", []byte(bindingHSrc), 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
// Done
|
2024-08-06 02:29:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-14 05:43:54 +00:00
|
|
|
log.Printf("Processing %d file(s) completed", len(includeFiles))
|
2024-08-06 01:03:23 +00:00
|
|
|
}
|
2024-10-14 05:55:59 +00:00
|
|
|
|
2024-10-20 04:58:00 +00:00
|
|
|
func generateClangCaches(includeFiles []string, clangBin string, cflags []string, matcher ClangMatcher) {
|
2024-10-14 05:55:59 +00:00
|
|
|
|
|
|
|
var clangChan = make(chan string, 0)
|
|
|
|
var clangWg sync.WaitGroup
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for i := 0; i < ClangSubprocessCount; i++ {
|
|
|
|
clangWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer clangWg.Done()
|
|
|
|
log.Printf("Clang worker: starting")
|
|
|
|
|
|
|
|
for {
|
|
|
|
inputHeader, ok := <-clangChan
|
|
|
|
if !ok {
|
|
|
|
return // Done
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Clang worker got message for file %q", inputHeader)
|
|
|
|
|
|
|
|
// Parse the file
|
|
|
|
// This seems to intermittently fail, so allow retrying
|
2024-10-20 04:58:00 +00:00
|
|
|
astInner := mustClangExec(ctx, clangBin, inputHeader, cflags, matcher)
|
2024-10-14 05:55:59 +00:00
|
|
|
|
|
|
|
// Write to cache
|
|
|
|
jb, err := json.MarshalIndent(astInner, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(cacheFilePath(inputHeader), jb, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
astInner = nil
|
|
|
|
jb = nil
|
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
}
|
|
|
|
log.Printf("Clang worker: exiting")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, inputHeader := range includeFiles {
|
|
|
|
|
|
|
|
// Check if there is a matching cache hit
|
|
|
|
cacheFile := cacheFilePath(inputHeader)
|
|
|
|
|
|
|
|
if _, err := os.Stat(cacheFile); err != nil && os.IsNotExist(err) {
|
|
|
|
|
|
|
|
// Nonexistent cache file, regenerate from clang
|
|
|
|
log.Printf("No AST cache for file %q, running clang...", filepath.Base(inputHeader))
|
|
|
|
clangChan <- inputHeader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done with all clang workers
|
|
|
|
close(clangChan)
|
|
|
|
clangWg.Wait()
|
|
|
|
}
|