|
| 1 | +package compiletime |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/stergiotis/boxer/public/config" |
| 6 | + "github.com/stergiotis/boxer/public/observability/eh" |
| 7 | + "github.com/stergiotis/boxer/public/observability/eh/eb" |
| 8 | + "github.com/urfave/cli/v2" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "syscall" |
| 13 | +) |
| 14 | + |
| 15 | +func makeFileReadOnly(path string) error { |
| 16 | + s, err := os.Stat(path) |
| 17 | + if err != nil { |
| 18 | + return eb.Build().Str("path", path).Errorf("unable to stat file: %w", err) |
| 19 | + } |
| 20 | + err = os.Chmod(path, s.Mode()&^(os.FileMode(syscall.S_IWUSR)|os.FileMode(syscall.S_IWGRP)|os.FileMode(syscall.S_IWOTH))) |
| 21 | + if err != nil { |
| 22 | + return eb.Build().Str("path", path).Errorf("unable to chmod file: %w", err) |
| 23 | + } |
| 24 | + return nil |
| 25 | +} |
| 26 | +func emitToFile(path string, emitter Emitter) (err error) { |
| 27 | + _ = os.MkdirAll(filepath.Dir(path), 0o000) |
| 28 | + |
| 29 | + var w io.WriteCloser |
| 30 | + err = os.Remove(path) |
| 31 | + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 32 | + err = eb.Build().Str("path", path).Errorf("unable to remove output file: %w", err) |
| 33 | + return |
| 34 | + } |
| 35 | + w, err = os.Create(path) |
| 36 | + if err != nil { |
| 37 | + err = eb.Build().Str("path", path).Errorf("unable to create file: %w", err) |
| 38 | + return |
| 39 | + } |
| 40 | + _, err = emitter.Emit(w) |
| 41 | + _ = w.Close() |
| 42 | + if err != nil { |
| 43 | + err = eb.Build().Str("path", path).Errorf("unable to populate file: %w", err) |
| 44 | + return |
| 45 | + } |
| 46 | + err = makeFileReadOnly(path) |
| 47 | + if err != nil { |
| 48 | + err = eb.Build().Str("path", path).Errorf("unable to make file read-only: %w", err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + return |
| 53 | +} |
| 54 | +func generateBackendCode(idlDriver IDLDriver, cfg *Config, namer *Namer) (err error) { |
| 55 | + be := NewCodeTransformerBackendPresenterCpp(namer) |
| 56 | + err = idlDriver.DriveBackend(be) |
| 57 | + if err != nil { |
| 58 | + err = eh.Errorf("unable to generate code: %w", err) |
| 59 | + return |
| 60 | + } |
| 61 | + err = emitToFile(cfg.CppOutputFile, be) |
| 62 | + if err != nil { |
| 63 | + err = eh.Errorf("unable to generate cpp file: %w", err) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + return |
| 68 | +} |
| 69 | +func generateFrontendCode(idlDriver IDLDriver, cfg *Config, namer *Namer) (err error) { |
| 70 | + fe := NewCodeTransformerFrontendGo(namer) |
| 71 | + err = idlDriver.DriveFrontend(fe) |
| 72 | + if err != nil { |
| 73 | + err = eh.Errorf("unable to generate code: %w", err) |
| 74 | + return |
| 75 | + } |
| 76 | + err = emitToFile(cfg.GoOutputFile, fe) |
| 77 | + if err != nil { |
| 78 | + err = eh.Errorf("unable to generate go file: %w", err) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + return |
| 83 | +} |
| 84 | + |
| 85 | +func mainE(config *Config, namerCfg *NamerConfig) (err error) { |
| 86 | + namer := NewNamer(namerCfg) |
| 87 | + _ = os.Remove(config.GoOutputFile) |
| 88 | + _ = os.Remove(config.CppOutputFile) |
| 89 | + var idlDriver *IDLDriverGoFile |
| 90 | + idlDriver, err = NewIDLDriverGoFile(config.IdlBuildTag, config.IdlPackagePattern, runtime.FuncProcId(config.FuncProcIdOffset)) |
| 91 | + if err != nil { |
| 92 | + err = eh.Errorf("unable to process IDL file: %w", err) |
| 93 | + return |
| 94 | + } |
| 95 | + err = generateBackendCode(idlDriver, config, namer) |
| 96 | + if err != nil { |
| 97 | + err = eh.Errorf("unable to generate backend code: %w", err) |
| 98 | + return |
| 99 | + } |
| 100 | + err = generateFrontendCode(idlDriver, config, namer) |
| 101 | + if err != nil { |
| 102 | + err = eh.Errorf("unable to generate frontend code: %w", err) |
| 103 | + return |
| 104 | + } |
| 105 | + return |
| 106 | +} |
| 107 | +func NewCommand(cfg *Config, namerCfg *NamerConfig) *cli.Command { |
| 108 | + if cfg == nil { |
| 109 | + cfg = &Config{ |
| 110 | + IdlBuildTag: "fffi_idl_code", |
| 111 | + IdlPackagePattern: "", |
| 112 | + GoOutputFile: "", |
| 113 | + CppOutputFile: "", |
| 114 | + FuncProcIdOffset: 0, |
| 115 | + validated: false, |
| 116 | + nValidationMessages: 0, |
| 117 | + } |
| 118 | + } |
| 119 | + if namerCfg == nil { |
| 120 | + namerCfg = &NamerConfig{ |
| 121 | + RuneCppType: "rune_t", |
| 122 | + } |
| 123 | + } |
| 124 | + return &cli.Command{ |
| 125 | + Name: "generateFffiCode", |
| 126 | + Flags: append(cfg.ToCliFlags(config.IdentityNameTransf, config.IdentityNameTransf), namerCfg.ToCliFlags(config.IdentityNameTransf, config.IdentityNameTransf)...), |
| 127 | + Action: func(context *cli.Context) error { |
| 128 | + nMessages := cfg.FromContext(config.IdentityNameTransf, context) |
| 129 | + if nMessages > 0 { |
| 130 | + return eb.Build().Int("nMessages", nMessages).Errorf("unable to compose config") |
| 131 | + } |
| 132 | + nMessages = namerCfg.FromContext(config.IdentityNameTransf, context) |
| 133 | + if nMessages > 0 { |
| 134 | + return eb.Build().Int("nMessages", nMessages).Errorf("unable to compose namer config") |
| 135 | + } |
| 136 | + return mainE(cfg, namerCfg) |
| 137 | + }, |
| 138 | + } |
| 139 | +} |
0 commit comments