Skip to content

Commit

Permalink
main: add -ldflags='-extldflags=...' support
Browse files Browse the repository at this point in the history
This matches upstream Go. Example:

    $ go test -ldflags='-extldflags=-foobar' os
    # os.test
    /usr/local/go1.23.1/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
    /usr/bin/gcc -s -o $WORK/b001/os.test -rdynamic /tmp/go-link-914594215/go.o /tmp/go-link-914594215/000000.o /tmp/go-link-914594215/000001.o /tmp/go-link-914594215/000002.o /tmp/go-link-914594215/000003.o /tmp/go-link-914594215/000004.o /tmp/go-link-914594215/000005.o /tmp/go-link-914594215/000006.o /tmp/go-link-914594215/000007.o /tmp/go-link-914594215/000008.o /tmp/go-link-914594215/000009.o /tmp/go-link-914594215/000010.o /tmp/go-link-914594215/000011.o /tmp/go-link-914594215/000012.o /tmp/go-link-914594215/000013.o /tmp/go-link-914594215/000014.o /tmp/go-link-914594215/000015.o /tmp/go-link-914594215/000016.o /tmp/go-link-914594215/000017.o /tmp/go-link-914594215/000018.o /tmp/go-link-914594215/000019.o /tmp/go-link-914594215/000020.o /tmp/go-link-914594215/000021.o -O2 -g -O2 -g -lresolv -O2 -g -lpthread -foobar
    gcc: error: unrecognized command-line option ‘-foobar’

    FAIL    os [build failed]
    FAIL

And TinyGo, with this patch:

    $ tinygo test -ldflags='-extldflags=-foobar' os
    FAIL    os      0.000s
    ld.lld: error: unknown argument '-foobar'

Also note that Go doesn't support the `-extldflags` directly (which was
previously the case with TinyGo):

    $ go test -extldflags='-foobar' os
    flag provided but not defined: -extldflags
    [...]
  • Loading branch information
aykevl authored and deadprogram committed Sep 18, 2024
1 parent 892efae commit dcca47f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,19 +1304,20 @@ func (m globalValuesFlag) Set(value string) error {

// parseGoLinkFlag parses the -ldflags parameter. Its primary purpose right now
// is the -X flag, for setting the value of global string variables.
func parseGoLinkFlag(flagsString string) (map[string]map[string]string, error) {
func parseGoLinkFlag(flagsString string) (map[string]map[string]string, string, error) {
set := flag.NewFlagSet("link", flag.ExitOnError)
globalVarValues := make(globalValuesFlag)
set.Var(globalVarValues, "X", "Set the value of the string variable to the given value.")
extLDFlags := set.String("extldflags", "", "additional flags to pass to external linker")
flags, err := shlex.Split(flagsString)
if err != nil {
return nil, err
return nil, "", err
}
err = set.Parse(flags)
if err != nil {
return nil, err
return nil, "", err
}
return map[string]map[string]string(globalVarValues), nil
return map[string]map[string]string(globalVarValues), *extLDFlags, nil
}

// getListOfPackages returns a standard list of packages for a given list that might
Expand Down Expand Up @@ -1388,7 +1389,6 @@ func main() {
cpuprofile := flag.String("cpuprofile", "", "cpuprofile output")
monitor := flag.Bool("monitor", false, "enable serial monitor")
baudrate := flag.Int("baudrate", 115200, "baudrate of serial monitor")
extLDFlags := flag.String("extldflags", "", "additional flags to pass to external linker")

// Internal flags, that are only intended for TinyGo development.
printIR := flag.Bool("internal-printir", false, "print LLVM IR")
Expand Down Expand Up @@ -1449,7 +1449,7 @@ func main() {
}

flag.CommandLine.Parse(os.Args[2:])
globalVarValues, err := parseGoLinkFlag(*ldflags)
globalVarValues, extLDFlags, err := parseGoLinkFlag(*ldflags)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down Expand Up @@ -1504,7 +1504,7 @@ func main() {
Timeout: *timeout,
WITPackage: witPackage,
WITWorld: witWorld,
ExtLDFlags: *extLDFlags,
ExtLDFlags: extLDFlags,
}
if *printCommands {
options.PrintCommands = printCommand
Expand Down

0 comments on commit dcca47f

Please sign in to comment.