runBuild calls genPkg and then executes commands to build the resulting files exe = executable mode to build an executable instead of a library mode = gen, build, pkg, exe
(mode bind.BuildMode, cfg *BuildCfg)
| 93 | // exe = executable mode to build an executable instead of a library |
| 94 | // mode = gen, build, pkg, exe |
| 95 | func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { |
| 96 | var err error |
| 97 | cfg.OutputDir, err = genOutDir(cfg.OutputDir) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | err = genPkg(mode, cfg) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | fmt.Printf("\n--- building package ---\n%s\n", cfg.Cmd) |
| 107 | |
| 108 | buildname := cfg.Name + "_go" |
| 109 | var cmdout []byte |
| 110 | cwd, err := os.Getwd() |
| 111 | os.Chdir(cfg.OutputDir) |
| 112 | defer os.Chdir(cwd) |
| 113 | |
| 114 | os.Remove(cfg.Name + ".c") // may fail, we don't care |
| 115 | |
| 116 | fmt.Printf("goimports -w %v\n", cfg.Name+".go") |
| 117 | cmd := exec.Command("goimports", "-w", cfg.Name+".go") |
| 118 | cmdout, err = cmd.CombinedOutput() |
| 119 | if err != nil { |
| 120 | fmt.Printf("cmd had error: %v output:\no%v\n", err, string(cmdout)) |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | pycfg, err := bind.GetPythonConfig(cfg.VM) |
| 125 | |
| 126 | if mode == bind.ModeExe { |
| 127 | of, err := os.Create(buildname + ".h") // overwrite existing |
| 128 | fmt.Fprintf(of, "typedef uint8_t bool;\n") |
| 129 | of.Close() |
| 130 | |
| 131 | fmt.Printf("%v build.py # will fail, but needed to generate .c file\n", cfg.VM) |
| 132 | cmd = exec.Command(cfg.VM, "build.py") |
| 133 | cmd.Run() // will fail, we don't care about errors |
| 134 | |
| 135 | args := []string{"build", "-mod=mod", "-buildmode=c-shared"} |
| 136 | if cfg.BuildTags != "" { |
| 137 | args = append(args, "-tags", cfg.BuildTags) |
| 138 | } |
| 139 | args = append(args, "-o", buildname+libExt, ".") |
| 140 | |
| 141 | fmt.Printf("go %v\n", strings.Join(args, " ")) |
| 142 | cmd = exec.Command("go", args...) |
| 143 | cmdout, err = cmd.CombinedOutput() |
| 144 | if err != nil { |
| 145 | fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout)) |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | fmt.Printf("%v build.py # should work this time\n", cfg.VM) |
| 150 | cmd = exec.Command(cfg.VM, "build.py") |
| 151 | cmdout, err = cmd.CombinedOutput() |
| 152 | if err != nil { |
no test coverage detected