From a819796ce20d4a1ef6c46fb6884a29362fe950a0 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 17 Jun 2024 11:02:52 +0800 Subject: [PATCH] build: LLGoFiles (support to compile c files) --- internal/build/build.go | 52 ++++++++++++++++++++----- py/llgo.cfg | 7 ---- py/llgo_autogen.lla | Bin 1639 -> 400 bytes py/module.ll | 82 ---------------------------------------- py/python.go | 1 + 5 files changed, 43 insertions(+), 99 deletions(-) delete mode 100644 py/llgo.cfg delete mode 100644 py/module.ll diff --git a/internal/build/build.go b/internal/build/build.go index d3f16c6f..d115f7c6 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -19,6 +19,7 @@ package build import ( "archive/zip" "fmt" + "go/constant" "go/token" "go/types" "io" @@ -147,7 +148,7 @@ func Do(args []string, conf *Config) { progSSA := ssa.NewProgram(initial[0].Fset, ssaBuildMode) patches := make(cl.Patches, len(altPkgPaths)) - altSSAPkgs(progSSA, patches, altPkgs[1:]) + altSSAPkgs(progSSA, patches, altPkgs[1:], verbose) ctx := &context{progSSA, prog, dedup, patches, make(map[string]none), mode, verbose} pkgs := buildAllPkgs(ctx, initial) @@ -234,9 +235,8 @@ func buildAllPkgs(ctx *context, initial []*packages.Package) (pkgs []*aPackage) // and set no export file pkg.ExportFile = "" case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule: - pkgPath := pkg.PkgPath - if isPkgInLLGo(pkgPath) { - pkg.ExportFile = concatPkgLinkFiles(pkgPath) + if isPkgInLLGo(pkg.PkgPath) { + pkg.ExportFile = concatPkgLinkFiles(pkg, ctx.verbose) } else { // panic("todo") // TODO(xsw): support packages out of llgo @@ -414,14 +414,14 @@ func altPkgs(initial []*packages.Package, alts ...string) []string { return alts } -func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package) { +func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package, verbose bool) { packages.Visit(alts, nil, func(p *packages.Package) { if p.Types != nil && !p.IllTyped { pkgSSA := prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true) if strings.HasPrefix(p.PkgPath, altPkgPathPrefix) { path := p.PkgPath[len(altPkgPathPrefix):] patches[path] = pkgSSA - if debugBuild { + if debugBuild || verbose { log.Println("==> Patching", path) } } @@ -563,11 +563,11 @@ func isSingleLinkFile(ret string) bool { return len(ret) > 0 && ret[0] != ' ' } -func concatPkgLinkFiles(pkgPath string) string { +func concatPkgLinkFiles(pkg *packages.Package, verbose bool) string { var b strings.Builder var ret string var n int - llgoPkgLinkFiles(pkgPath, "", func(linkFile string) { + llgoPkgLinkFiles(pkg, "", func(linkFile string) { if n == 0 { ret = linkFile } else { @@ -575,7 +575,7 @@ func concatPkgLinkFiles(pkgPath string) string { b.WriteString(linkFile) } n++ - }) + }, verbose) if n > 1 { b.WriteByte(' ') b.WriteString(ret) @@ -584,7 +584,39 @@ func concatPkgLinkFiles(pkgPath string) string { return ret } -func llgoPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { +// const LLGoFiles = "file1; file2; ..." +func llgoPkgLinkFiles(pkg *packages.Package, llFile string, procFile func(linkFile string), verbose bool) { + if o := pkg.Types.Scope().Lookup("LLGoFiles"); o != nil { + val := o.(*types.Const).Val() + if val.Kind() == constant.String { + clFiles(constant.StringVal(val), pkg, procFile, verbose) + } + } + unzipPkgLinkFiles(pkg.PkgPath, llFile, procFile) +} + +// files = "file1; file2; ..." +func clFiles(files string, pkg *packages.Package, procFile func(linkFile string), verbose bool) { + dir := filepath.Dir(pkg.CompiledGoFiles[0]) + expFile := pkg.ExportFile + for _, file := range strings.Split(files, ";") { + cFile := filepath.Join(dir, strings.TrimSpace(file)) + clFile(cFile, expFile, procFile, verbose) + } +} + +func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool) { + llFile := expFile + filepath.Base(cFile) + ".ll" + args := []string{"-emit-llvm", "-S", "-o", llFile, "-c", cFile} + if verbose { + fmt.Fprintln(os.Stderr, "clang", args) + } + err := clang.New("").Exec(args...) + check(err) + procFile(llFile) +} + +func unzipPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/" if llFile == "" { llFile = "llgo_autogen.ll" diff --git a/py/llgo.cfg b/py/llgo.cfg deleted file mode 100644 index dcc7c16d..00000000 --- a/py/llgo.cfg +++ /dev/null @@ -1,7 +0,0 @@ -{ - "cl": [ - "clang -emit-llvm -S -o module.ll -c _pyg/module.c", - "llgen .", - "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll module.ll", - ] -} diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index 36aace8390331918fee0d29f636308f0e8a26475..0bbf8a002158164eb4a3ede78fd4a7526b3e6c22 100644 GIT binary patch delta 73 zcmaFPGl7{mz?+#xgn@y9gQ3>%;zV9m_Px{c(+*6_pJ@4xmyt=187?<@534aVP;#;( Tn*<*t10zEyP?strkYE4+&D{|X delta 1340 zcmbQh{G5k3z?+#xgn@y9gW-F?xrw~0>>rnAr+r?UJ<;-AJxoT*`&&=5`r<|nhW)1CrxX$3a}BgDdyB_bD zX&`ldlKQ1hy5<}gl3d%9WW^K?#C087pmFM&Q=pdX&Elp%@2$N)@vD{vDc`>QX+glA z_w{yi`>o}kdVi^w3va6b*Rk%x?8=Rif96~jcK&uu>$8`koqy0l)w31~bNCKzTobo4 z^yrrlRiE9zh^I>#R0S7I5&u$sRQI(|?JAYanR*x5*M|P&J83gjX{rp{)R^T)Y-^@j z`b&*o|omJD;m(%~Z?HP;umCvyz zGJL;ZsLS7t=J?9N;?{aX($#-Y%!Vc0oleX$ZyPgjn6fVSeSCS@Nn`EuiI?SFr&)#n z(Oh=#Yq0-~n)I0h(>jCBW~}txkbibwXKJ3{NV{`oi=gR4pGbFygPCd5X`_0KXuO?xq5~wlzPh15TV=Tk@9@rere59c6eYU`{b$e6nvs2N_H^wm?V_$s6c5Oyr zeS<=r+w07L*5;)Fl}Bs?X5KyYq~_eT?MwH~Js#qyldxV_Zb8XmsZh^DQ^S{)%Y0B& zP;lg4XB!q`z?jYPF15mbegW5`4?;c6r@YG#h#2?#E-Wq=crLXgxqO0%Th;$9{{%c(VrP^k-@jdue)GcdoEO*3YJ6{8aLLV> zVfH0PIxow*CL{Lp4DY)?&hX5S@4qbQcZ0y_nH|1md2Zg%e|^?}!5#lm*O>9d z7VgYwZ|(yO9J?FxI^QR5Rx>}x9$d!}H7{mC=wyMG=7sh$3%jf2t{rZ)Zz^|cGwA<) zuK6o}fHxzP95cMInry;oTmdSr&