This is an automated email from the ASF dual-hosted git repository. sjanc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git
commit 98bc88682b07b24fcf5f568f3088d209ad7e5a67 Author: Szymon Janc <[email protected]> AuthorDate: Mon Oct 19 17:47:43 2026 +0200 newt/builder: Return errors from size report tools Failure to run nm or objdump in "newt size -R/-F/-d" called os.Exit(1) from library code with a terse message. Return a newt error naming the tool, its path, the compiler.yml key and the tool's error output. Assisted-by: Claude:claude-opus-5-5 --- newt/builder/size_report.go | 50 ++++++++++++++++++++++++-------------- newt/builder/size_report_test.go | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/newt/builder/size_report.go b/newt/builder/size_report.go index 79699428..5f7d590a 100644 --- a/newt/builder/size_report.go +++ b/newt/builder/size_report.go @@ -30,37 +30,51 @@ import ( "strings" ) +// toolCmdError creates an error describing failure of a binutils tool run by +// the size report. Tool's standard error output is included if available. +func toolCmdError(toolName string, toolKey string, toolPath string, + err error) error { + + if toolPath == "" { + return util.FmtNewtError( + "Cannot run %s: %s is not set in compiler.yml", toolName, toolKey) + } + + msg := err.Error() + if exitErr, ok := err.(*exec.ExitError); ok { + stderr := strings.TrimSpace(string(exitErr.Stderr)) + if stderr != "" { + msg += ": " + stderr + } + } + + return util.FmtNewtError("Error running %s command \"%s\" "+ + "(see %s in compiler.yml): %s", toolName, toolPath, toolKey, msg) +} + func runNmCommand(nmPath string, elfFilePath string) ([]byte, error) { - var ( - cmdOut []byte - err error - ) - cmdName := nmPath cmdArgs := []string{elfFilePath, "-S", "-l", "--size-sort"} - if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil { - fmt.Fprintln(os.Stderr, "There was an error running nm command: ", err) - os.Exit(1) + cmdOut, err := exec.Command(nmPath, cmdArgs...).Output() + if err != nil { + return nil, toolCmdError("nm", "compiler.path.nm", nmPath, err) } - return cmdOut, err + return cmdOut, nil } func runObjdumpCommand(odPath string, elfFilePath string, params string) ([]byte, error) { - var ( - cmdOut []byte - err error - ) - cmdName := odPath + cmdArgs := []string{params, elfFilePath} - if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil { - fmt.Fprintln(os.Stderr, "There was an error running objdump command: ", + + cmdOut, err := exec.Command(odPath, cmdArgs...).Output() + if err != nil { + return nil, toolCmdError("objdump", "compiler.path.objdump", odPath, err) - os.Exit(1) } - return cmdOut, err + return cmdOut, nil } func runAddr2lineCommand(a2lPath string, elfFilePath string, diff --git a/newt/builder/size_report_test.go b/newt/builder/size_report_test.go new file mode 100644 index 00000000..346f69b4 --- /dev/null +++ b/newt/builder/size_report_test.go @@ -0,0 +1,52 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package builder + +import ( + "path/filepath" + "strings" + "testing" +) + +// Failure to run nm or objdump must be reported as an error instead of +// exiting newt. +func TestSizeReportToolErrors(t *testing.T) { + missing := filepath.Join(t.TempDir(), "no-such-tool") + elf := filepath.Join(t.TempDir(), "app.elf") + + _, err := runNmCommand(missing, elf) + if err == nil || !strings.Contains(err.Error(), "compiler.path.nm") { + t.Errorf("unexpected nm error: %v", err) + } + + _, err = runObjdumpCommand(missing, elf, "-hw") + if err == nil || + !strings.Contains(err.Error(), "compiler.path.objdump") { + + t.Errorf("unexpected objdump error: %v", err) + } + + _, err = runObjdumpCommand("", elf, "-hw") + if err == nil || !strings.Contains(err.Error(), + "compiler.path.objdump is not set") { + + t.Errorf("unexpected objdump error: %v", err) + } +}
