Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/internal/base/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewPassArgs(flag *flag.FlagSet) *PassArgs {

func PassBuildFlags(cmd *Command) *PassArgs {
p := NewPassArgs(&cmd.Flag)
p.Bool("n", "x")
p.Bool("n")
// Note: "a" flag removed - now handled by flags.AddBuildFlags()
p.Bool("linkshared", "race", "msan", "asan",
"trimpath", "work")
Expand Down
3 changes: 3 additions & 0 deletions cmd/internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var SizeReport bool
var SizeFormat string
var SizeLevel string
var ForceRebuild bool
var PrintCommands bool

const DefaultTestTimeout = "10m" // Matches Go's default test timeout

Expand All @@ -50,6 +51,7 @@ func AddCommonFlags(fs *flag.FlagSet) {

func AddBuildFlags(fs *flag.FlagSet) {
fs.BoolVar(&ForceRebuild, "a", false, "Force rebuilding of packages that are already up-to-date")
fs.BoolVar(&PrintCommands, "x", false, "Print the commands")
fs.StringVar(&Tags, "tags", "", "Build tags")
fs.StringVar(&BuildEnv, "buildenv", "", "Build environment")
if buildenv.Dev {
Expand Down Expand Up @@ -174,6 +176,7 @@ func UpdateConfig(conf *build.Config) error {
conf.CompilerHash = compilerhash.Value()
conf.Tags = Tags
conf.Verbose = Verbose
conf.PrintCommands = PrintCommands
conf.Target = Target
conf.Port = Port
conf.BaudRate = BaudRate
Expand Down
48 changes: 30 additions & 18 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type Config struct {
AbiMode AbiMode
GenExpect bool // only valid for ModeCmpTest
Verbose bool
PrintCommands bool
GenLL bool // generate pkg .ll files
CheckLLFiles bool // check .ll files valid
CheckLinkArgs bool // check linkargs valid
Expand Down Expand Up @@ -544,7 +545,7 @@ func (c *context) compiler() *clang.Cmd {
c.crossCompile.Linker,
)
cmd := clang.NewCompiler(config)
cmd.Verbose = c.buildConf.Verbose
cmd.Verbose = c.buildConf.Verbose || c.buildConf.PrintCommands
return cmd
}

Expand All @@ -557,7 +558,7 @@ func (c *context) linker() *clang.Cmd {
c.crossCompile.Linker,
)
cmd := clang.NewLinker(config)
cmd.Verbose = c.buildConf.Verbose
cmd.Verbose = c.buildConf.Verbose || c.buildConf.PrintCommands
return cmd
}

Expand Down Expand Up @@ -802,6 +803,7 @@ func compileExtraFiles(ctx *context, verbose bool) ([]string, error) {
return nil, nil
}

printCmds := ctx.buildConf.PrintCommands || verbose
var objFiles []string
llgoRoot := env.LLGoROOT()

Expand Down Expand Up @@ -839,7 +841,7 @@ func compileExtraFiles(ctx *context, verbose bool) ([]string, error) {
if ctx.buildConf.GenLL {
llFile := baseName + ".ll"
llArgs := append(slices.Clone(baseArgs), "-emit-llvm", "-S", "-o", llFile, "-c", srcFile)
if verbose {
if printCmds {
fmt.Fprintf(os.Stderr, "Compiling extra file (ll): clang %s\n", strings.Join(llArgs, " "))
}
cmd := ctx.compiler()
Expand All @@ -851,7 +853,7 @@ func compileExtraFiles(ctx *context, verbose bool) ([]string, error) {
// Always compile to .o for linking
objFile := baseName + ".o"
objArgs := append(baseArgs, "-o", objFile, "-c", srcFile)
if verbose {
if printCmds {
fmt.Fprintf(os.Stderr, "Compiling extra file: clang %s\n", strings.Join(objArgs, " "))
}
cmd := ctx.compiler()
Expand Down Expand Up @@ -926,7 +928,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, outputPa
// Generate main module file (needed for global variables even in library modes)
// This is compiled directly to .o and added to linkInputs (not cached)
entryPkg := genMainModule(ctx, llssa.PkgRuntime, pkg, needRuntime, needPyInit, needAbiInit)
entryObjFile, err := exportObject(ctx, entryPkg.PkgPath, entryPkg.ExportFile, []byte(entryPkg.LPkg.String()))
entryObjFile, err := exportObject(ctx, "entry_main", entryPkg.ExportFile, []byte(entryPkg.LPkg.String()))
if err != nil {
return err
}
Expand Down Expand Up @@ -971,9 +973,10 @@ func isRuntimePkg(pkgPath string) bool {
}

func linkObjFiles(ctx *context, app string, objFiles, linkArgs []string, verbose bool) error {
printCmds := ctx.buildConf.PrintCommands || verbose
// Handle c-archive mode differently - use ar tool instead of linker
if ctx.buildConf.BuildMode == BuildModeCArchive {
return ctx.createArchiveFile(app, objFiles, verbose)
return ctx.createArchiveFile(app, objFiles, printCmds)
}

buildArgs := []string{"-o", app}
Expand All @@ -998,7 +1001,7 @@ func linkObjFiles(ctx *context, app string, objFiles, linkArgs []string, verbose
if strings.HasSuffix(objFile, ".ll") {
oFile := strings.TrimSuffix(objFile, ".ll") + ".o"
args := []string{"-o", oFile, "-c", objFile, "-Wno-override-module"}
if verbose {
if printCmds {
fmt.Fprintln(os.Stderr, "clang", args)
}
if err := ctx.compiler().Compile(args...); err != nil {
Expand All @@ -1015,7 +1018,7 @@ func linkObjFiles(ctx *context, app string, objFiles, linkArgs []string, verbose
buildArgs = append(buildArgs, objFiles...)

cmd := ctx.linker()
cmd.Verbose = verbose
cmd.Verbose = printCmds
return cmd.Link(buildArgs...)
}

Expand Down Expand Up @@ -1069,7 +1072,11 @@ func (c *context) createArchiveFile(archivePath string, objFiles []string, verbo
args := append([]string{"rcs", tmpName}, objFiles...)
arCmd := c.archiver()
cmd := exec.Command(arCmd, args...)
if len(verbose) > 0 && verbose[0] {
printCmds := c.buildConf.PrintCommands
if len(verbose) > 0 {
printCmds = printCmds || verbose[0]
}
if printCmds {
fmt.Fprintf(os.Stderr, "%s %s\n", filepath.Base(arCmd), strings.Join(args, " "))
}
if output, err := cmd.CombinedOutput(); err != nil {
Expand Down Expand Up @@ -1141,20 +1148,21 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) error {

ctx.cTransformer.TransformModule(ret.Path(), ret.Module())

cgoLLFiles, cgoLdflags, err := buildCgo(ctx, aPkg, aPkg.Package.Syntax, externs, verbose)
printCmds := ctx.buildConf.Verbose || ctx.buildConf.PrintCommands
cgoLLFiles, cgoLdflags, err := buildCgo(ctx, aPkg, aPkg.Package.Syntax, externs, printCmds)
if err != nil {
return fmt.Errorf("build cgo of %v failed: %v", pkgPath, err)
}
aPkg.ObjFiles = append(aPkg.ObjFiles, cgoLLFiles...)
aPkg.ObjFiles = append(aPkg.ObjFiles, concatPkgLinkFiles(ctx, pkg, verbose)...)
aPkg.ObjFiles = append(aPkg.ObjFiles, concatPkgLinkFiles(ctx, pkg, printCmds)...)
aPkg.LinkArgs = append(aPkg.LinkArgs, cgoLdflags...)
if aPkg.AltPkg != nil {
altLLFiles, altLdflags, e := buildCgo(ctx, aPkg, aPkg.AltPkg.Syntax, externs, verbose)
altLLFiles, altLdflags, e := buildCgo(ctx, aPkg, aPkg.AltPkg.Syntax, externs, printCmds)
if e != nil {
return fmt.Errorf("build cgo of %v failed: %v", pkgPath, e)
}
aPkg.ObjFiles = append(aPkg.ObjFiles, altLLFiles...)
aPkg.ObjFiles = append(aPkg.ObjFiles, concatPkgLinkFiles(ctx, aPkg.AltPkg.Package, verbose)...)
aPkg.ObjFiles = append(aPkg.ObjFiles, concatPkgLinkFiles(ctx, aPkg.AltPkg.Package, printCmds)...)
aPkg.LinkArgs = append(aPkg.LinkArgs, altLdflags...)
}
if pkg.ExportFile != "" {
Expand Down Expand Up @@ -1207,7 +1215,8 @@ func exportObject(ctx *context, pkgPath string, exportFile string, data []byte)
}
objFile.Close()
args := []string{"-o", objFile.Name(), "-c", f.Name(), "-Wno-override-module"}
if ctx.buildConf.Verbose {
if ctx.buildConf.Verbose || ctx.buildConf.PrintCommands {
fmt.Fprintf(os.Stderr, "# compiling %s for pkg: %s\n", f.Name(), pkgPath)
fmt.Fprintln(os.Stderr, "clang", args)
}
cmd := ctx.compiler()
Expand Down Expand Up @@ -1564,11 +1573,11 @@ func clFiles(ctx *context, files string, pkg *packages.Package, procFile func(li
}
for _, file := range strings.Split(files, ";") {
cFile := filepath.Join(dir, strings.TrimSpace(file))
clFile(ctx, args, cFile, expFile, procFile, verbose)
clFile(ctx, args, cFile, expFile, pkg.PkgPath, procFile, verbose)
}
}

func clFile(ctx *context, args []string, cFile, expFile string, procFile func(linkFile string), verbose bool) {
func clFile(ctx *context, args []string, cFile, expFile, pkgPath string, procFile func(linkFile string), verbose bool) {
baseName := expFile + filepath.Base(cFile)
ext := filepath.Ext(cFile)

Expand All @@ -1578,10 +1587,12 @@ func clFile(ctx *context, args []string, cFile, expFile string, procFile func(li
}

// If GenLL is enabled, first emit .ll for debugging, then compile to .o
printCmds := ctx.buildConf.PrintCommands || verbose
if ctx.buildConf.GenLL {
llFile := baseName + ".ll"
llArgs := append(slices.Clone(args), "-emit-llvm", "-S", "-o", llFile, "-c", cFile)
if verbose {
if printCmds {
fmt.Fprintf(os.Stderr, "# compiling %s for pkg: %s\n", llFile, pkgPath)
fmt.Fprintln(os.Stderr, "clang", llArgs)
}
cmd := ctx.compiler()
Expand All @@ -1592,7 +1603,8 @@ func clFile(ctx *context, args []string, cFile, expFile string, procFile func(li
// Always compile to .o for linking
objFile := baseName + ".o"
objArgs := append(args, "-o", objFile, "-c", cFile)
if verbose {
if printCmds {
fmt.Fprintf(os.Stderr, "# compiling %s for pkg: %s\n", objFile, pkgPath)
fmt.Fprintln(os.Stderr, "clang", objArgs)
}
cmd := ctx.compiler()
Expand Down
22 changes: 14 additions & 8 deletions internal/build/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs []string,
}
}
for _, cfile := range cfiles {
clFile(ctx, cflags, cfile, pkg.ExportFile, func(linkFile string) {
clFile(ctx, cflags, cfile, pkg.ExportFile, pkg.PkgPath, func(linkFile string) {
llfiles = append(llfiles, linkFile)
}, verbose)
}
Expand Down Expand Up @@ -122,14 +122,14 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs []string,
tmpName := tmpFile.Name()
defer os.Remove(tmpName)
code := cgoHeader + "\n\n" + preamble.src
externDecls, err := genExternDeclsByClang(pkg, code, cflags, cgoSymbols)
externDecls, err := genExternDeclsByClang(pkg, code, cflags, cgoSymbols, verbose)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate extern decls: %v", err)
}
if err = os.WriteFile(tmpName, []byte(code+"\n\n"+externDecls), 0644); err != nil {
return nil, nil, fmt.Errorf("failed to write temp file: %v", err)
}
clFile(ctx, cflags, tmpName, pkg.ExportFile, func(linkFile string) {
clFile(ctx, cflags, tmpName, pkg.ExportFile, pkg.PkgPath, func(linkFile string) {
llfiles = append(llfiles, linkFile)
}, verbose)
}
Expand All @@ -146,7 +146,7 @@ type clangASTNode struct {
Inner []clangASTNode `json:"inner,omitempty"`
}

func genExternDeclsByClang(pkg *aPackage, src string, cflags []string, cgoSymbols map[string]string) (string, error) {
func genExternDeclsByClang(pkg *aPackage, src string, cflags []string, cgoSymbols map[string]string, verbose bool) (string, error) {
tmpSrc, err := os.CreateTemp("", "cgo-src-*.c")
if err != nil {
return "", fmt.Errorf("failed to create temp file: %v", err)
Expand All @@ -156,11 +156,11 @@ func genExternDeclsByClang(pkg *aPackage, src string, cflags []string, cgoSymbol
return "", fmt.Errorf("failed to write temp file: %v", err)
}
symbolNames := make(map[string]bool)
if err := getFuncNames(tmpSrc.Name(), cflags, symbolNames); err != nil {
if err := getFuncNames(tmpSrc.Name(), cflags, symbolNames, verbose); err != nil {
return "", fmt.Errorf("failed to get func names: %v", err)
}
macroNames := make(map[string]bool)
if err := getMacroNames(tmpSrc.Name(), cflags, macroNames); err != nil {
if err := getMacroNames(tmpSrc.Name(), cflags, macroNames, verbose); err != nil {
return "", fmt.Errorf("failed to get macro names: %v", err)
}

Expand Down Expand Up @@ -213,9 +213,12 @@ static void _init_%s() {
return b.String(), nil
}

func getMacroNames(file string, cflags []string, macroNames map[string]bool) error {
func getMacroNames(file string, cflags []string, macroNames map[string]bool, verbose bool) error {
args := append([]string{"-dM", "-E"}, cflags...)
args = append(args, file)
if verbose {
fmt.Fprintf(os.Stderr, "clang %s\n", strings.Join(args, " "))
}
cmd := exec.Command("clang", args...)
output, err := cmd.Output()
if err != nil {
Expand All @@ -233,9 +236,12 @@ func getMacroNames(file string, cflags []string, macroNames map[string]bool) err
return nil
}

func getFuncNames(file string, cflags []string, symbolNames map[string]bool) error {
func getFuncNames(file string, cflags []string, symbolNames map[string]bool, verbose bool) error {
args := append([]string{"-Xclang", "-ast-dump=json", "-fsyntax-only"}, cflags...)
args = append(args, file)
if verbose {
fmt.Fprintf(os.Stderr, "clang %s\n", strings.Join(args, " "))
}
cmd := exec.Command("clang", args...)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
Expand Down
15 changes: 12 additions & 3 deletions internal/build/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func runNative(ctx *context, app, pkgDir, pkgName string, conf *Config, mode Mod
} else {
args = conf.RunArgs
}
if conf.PrintCommands {
fmt.Fprintf(os.Stderr, "%s %s\n", app, strings.Join(args, " "))
}
cmd := exec.Command(app, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand All @@ -68,6 +71,9 @@ func runNative(ctx *context, app, pkgDir, pkgName string, conf *Config, mode Mod
mockable.Exit(s.ExitCode())
}
case ModeTest:
if conf.PrintCommands {
fmt.Fprintf(os.Stderr, "%s %s\n", app, strings.Join(conf.RunArgs, " "))
}
cmd := exec.Command(app, conf.RunArgs...)
cmd.Dir = pkgDir
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -106,9 +112,9 @@ func runInEmulator(emulator string, envMap map[string]string, pkgDir, pkgName st

switch mode {
case ModeRun:
return runEmuCmd(envMap, emulator, conf.RunArgs, verbose)
return runEmuCmd(envMap, emulator, conf.RunArgs, verbose, conf.PrintCommands)
case ModeTest:
return runEmuCmd(envMap, emulator, conf.RunArgs, verbose)
return runEmuCmd(envMap, emulator, conf.RunArgs, verbose, conf.PrintCommands)
case ModeCmpTest:
cmpTest(pkgDir, pkgName, envMap["out"], conf.GenExpect, conf.RunArgs)
return nil
Expand All @@ -117,7 +123,7 @@ func runInEmulator(emulator string, envMap map[string]string, pkgDir, pkgName st
}

// runEmuCmd runs the application in emulator by formatting the emulator command template
func runEmuCmd(envMap map[string]string, emulatorTemplate string, runArgs []string, verbose bool) error {
func runEmuCmd(envMap map[string]string, emulatorTemplate string, runArgs []string, verbose bool, printCmds bool) error {
// Expand the emulator command template
emulatorCmd := emulatorTemplate
for placeholder, path := range envMap {
Expand Down Expand Up @@ -145,6 +151,9 @@ func runEmuCmd(envMap map[string]string, emulatorTemplate string, runArgs []stri

// Add run arguments to the end
cmdParts = append(cmdParts, runArgs...)
if printCmds {
fmt.Fprintf(os.Stderr, "%s %s\n", cmdParts[0], strings.Join(cmdParts[1:], " "))
}

// Execute the emulator command
cmd := exec.Command(cmdParts[0], cmdParts[1:]...)
Expand Down
Loading