github.com/bir3/gocompiler@v0.3.205/vfs/vfs.go (about)

     1  // Copyright 2022 Bergur Ragnarsson.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package vfs
     6  
     7  import (
     8  	"embed"
     9  	"fmt"
    10  	"io/fs"
    11  	"log"
    12  	"os"
    13  	"path"
    14  	"path/filepath"
    15  
    16  	"strings"
    17  
    18  	"github.com/bir3/gocompiler/src/cmd/gocmd/extract_stdlib"
    19  )
    20  
    21  //go:embed goroot
    22  var content embed.FS
    23  
    24  /*
    25  	"vfs2" = historical name, not virtual file system
    26  */
    27  
    28  var GOROOT = "/github.com/bir3/gocompiler/missing-init"
    29  
    30  const gorootPrefixLen = len("/_/github.com/bir3/gocompiler/vfs/")
    31  
    32  // var GorootSrc string
    33  var GorootTool string
    34  
    35  var SharedExe string // used by so we can run as "compile", "asm", etc.
    36  var SharedExeError error
    37  
    38  func init() {
    39  	// ; cmd/go/internal/cfg/cfg.go
    40  
    41  	/*
    42  		user-config-dir/gocompiler/src-xxxx/done
    43  
    44  		override with env GOCOMPILER_DIR/src-xxxx/done
    45  	*/
    46  	//var err error
    47  
    48  	d, err := configDir("gocompiler/stdlib-go1.20.5-232e") //syncvar:
    49  	if err != nil {
    50  		return // compiler will fail due to missing GOROOT
    51  	}
    52  	f, err := content.Open("goroot/stdlib-go1.20.5-232e.tar.zst") //syncvar:
    53  	if err != nil {
    54  		panic(fmt.Sprintf("gocompiler stdlib init failed - %s", err))
    55  	}
    56  	defer f.Close()
    57  	GOROOT = d
    58  
    59  	err = extract_stdlib.ExtractStdlib(f, d)
    60  	if err != nil {
    61  		fmt.Fprintf(os.Stderr, "ERROR: gocompiler: extract stdlib to %s failed with %v\n", d, err)
    62  		os.Exit(3)
    63  	}
    64  	//GorootSrc = filepath.Join(GOROOT, "src") + string(os.PathSeparator)
    65  	GorootTool = filepath.Join(GOROOT, "pkg", "tool") + string(os.PathSeparator)
    66  
    67  	SharedExe, err = os.Executable()
    68  	if err != nil {
    69  		SharedExeError = err
    70  	}
    71  	switch os.Getenv("GOCOMPILERLIB_LOG") {
    72  	case "1":
    73  		basename := os.Args[0]
    74  		basename = basename[strings.LastIndex(basename, `/`)+1:]
    75  		basename = basename[strings.LastIndex(basename, `\`)+1:]
    76  		basename = strings.TrimSuffix(basename, ".exe")
    77  
    78  		filename := fmt.Sprintf("/tmp/gocompilerlib-%s-%d", basename, os.Getpid())
    79  		LogFile, _ = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) //Create("/tmp/r2log")
    80  
    81  	}
    82  }
    83  
    84  func configDir(folder string) (string, error) {
    85  	var err error
    86  	d := os.Getenv("GOCOMPILER_DIR")
    87  	if d == "" {
    88  		d, err = os.UserCacheDir()
    89  	}
    90  	d = path.Join(d, folder)
    91  	if err == nil {
    92  		err = os.MkdirAll(d, 0755)
    93  	}
    94  	if err != nil {
    95  		return "", fmt.Errorf("failed to create folder for stdlib - %w", err)
    96  	}
    97  	return d, nil
    98  }
    99  
   100  var LogFile *os.File
   101  
   102  func DebugShowEmbed() {
   103  	fs.WalkDir(content, "goroot", func(path string, d fs.DirEntry, err error) error {
   104  		if err != nil {
   105  			log.Fatal(err)
   106  		}
   107  		fmt.Println(path)
   108  		return nil
   109  	})
   110  }