Skip to content

Commit

Permalink
Fix link order on gcc and add clang as default on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
codecat committed Jul 13, 2020
1 parent bde8962 commit 06bb8b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
37 changes: 23 additions & 14 deletions compiler_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type linuxCompiler struct {
toolset string
}

func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) error {
Expand Down Expand Up @@ -43,7 +44,7 @@ func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) e

args = append(args, path)

cmd := exec.Command("gcc", args...)
cmd := exec.Command(ci.toolset, args...)

if options.Verbose {
log.Trace("%s", strings.Join(cmd.Args, " "))
Expand All @@ -60,7 +61,7 @@ func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) e
func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *CompilerOptions) (string, error) {
args := make([]string, 0)

exeName := "gcc"
exeName := ci.toolset

switch outType {
case LinkExe:
Expand All @@ -82,8 +83,11 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
} else {
args = append(args, "-o", outPath)
args = append(args, "-std=c++17")
args = append(args, "-static-libgcc")
args = append(args, "-static-libstdc++")

if ci.toolset == "gcc" {
args = append(args, "-static-libgcc")
args = append(args, "-static-libstdc++")
}

if options.Static {
args = append(args, "-static")
Expand All @@ -93,16 +97,6 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
for _, dir := range options.LinkDirectories {
args = append(args, "-L"+dir)
}

// Add libraries to link
for _, link := range options.LinkLibraries {
args = append(args, "-l"+link)
}

// Add additional linker flags
for _, flag := range options.LinkerFlags {
args = append(args, flag)
}
}

filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
Expand All @@ -116,6 +110,21 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
return nil
})

if outType != LinkLib {
// Link to some common standard libraries
args = append(args, "-lstdc++")

// Add libraries to link
for _, link := range options.LinkLibraries {
args = append(args, "-l"+link)
}

// Add additional linker flags
for _, flag := range options.LinkerFlags {
args = append(args, flag)
}
}

cmd := exec.Command(exeName, args...)

if options.Verbose {
Expand Down
19 changes: 18 additions & 1 deletion getcompiler_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@

package main

import (
"errors"
"os/exec"
)

func getCompiler() (Compiler, error) {
//TODO: Check if we have gcc and ld installed
//TODO: Add option for other flavors of gcc (eg. mingw)

return linuxCompiler{}, nil
toolset := ""

if _, err := exec.LookPath("clang"); err == nil {
toolset = "clang"
} else if _, err := exec.LookPath("gcc"); err == nil {
toolset = "gcc"
} else {
return nil, errors.New("couldn't find clang or gcc in the PATH")
}

return linuxCompiler{
toolset: toolset,
}, nil
}

0 comments on commit 06bb8b3

Please sign in to comment.