-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56e58a9
commit 3418ba0
Showing
10 changed files
with
301 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ example/build | |
tests/build | ||
|
||
/fireball | ||
/fireball.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package build | ||
|
||
import "runtime" | ||
|
||
type Linker interface { | ||
Check() error | ||
|
||
AddLibrary(library string) | ||
AddInput(input string) | ||
|
||
Link(output string) error | ||
} | ||
|
||
func GetLinker() Linker { | ||
switch runtime.GOOS { | ||
case "windows": | ||
return &windowsLinker{} | ||
case "linux": | ||
return &linuxLinker{} | ||
|
||
default: | ||
panic("Operating system not implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package build | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
type linuxLinker struct { | ||
libraries []string | ||
inputs []string | ||
} | ||
|
||
func (l *linuxLinker) Check() error { | ||
l.libraries = append(l.libraries, "c") | ||
l.libraries = append(l.libraries, "m") | ||
|
||
return nil | ||
} | ||
|
||
func (l *linuxLinker) AddLibrary(library string) { | ||
l.libraries = append(l.libraries, library) | ||
} | ||
|
||
func (l *linuxLinker) AddInput(input string) { | ||
l.inputs = append(l.inputs, input) | ||
} | ||
|
||
func (l *linuxLinker) Link(output string) error { | ||
cmd := exec.Command("ld.lld") | ||
|
||
cmd.Args = append(cmd.Args, "-L/usr/lib") | ||
|
||
cmd.Args = append(cmd.Args, "-dynamic-linker") | ||
cmd.Args = append(cmd.Args, "/lib64/ld-linux-x86-64.so.2") | ||
|
||
cmd.Args = append(cmd.Args, "/usr/lib/crt1.o") | ||
cmd.Args = append(cmd.Args, "/usr/lib/crti.o") | ||
|
||
for _, library := range l.libraries { | ||
cmd.Args = append(cmd.Args, "-l"+library) | ||
} | ||
|
||
for _, input := range l.inputs { | ||
cmd.Args = append(cmd.Args, withExtension(input, "o")) | ||
} | ||
|
||
cmd.Args = append(cmd.Args, "/usr/lib/crtn.o") | ||
|
||
cmd.Args = append(cmd.Args, "-o") | ||
cmd.Args = append(cmd.Args, output) | ||
|
||
return execute(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package build | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
type windowsLinker struct { | ||
ucrtPath string | ||
umPath string | ||
msvcPath string | ||
|
||
libraries []string | ||
inputs []string | ||
} | ||
|
||
func (w *windowsLinker) Check() error { | ||
if err := w.findKit(); err != nil { | ||
return err | ||
} | ||
if err := w.findMsvc(); err != nil { | ||
return err | ||
} | ||
|
||
w.libraries = append(w.libraries, "libucrt.lib") | ||
w.libraries = append(w.libraries, "libcmt.lib") | ||
|
||
return nil | ||
} | ||
|
||
func (w *windowsLinker) findKit() error { | ||
base := "C:\\Program Files (x86)\\Windows Kits\\10\\Lib" | ||
|
||
entries, err := os.ReadDir(base) | ||
if err != nil { | ||
return errors.New("failed to find a valid windows development kit") | ||
} | ||
|
||
w.ucrtPath = filepath.Join(base, entries[0].Name(), "ucrt", "x64") | ||
_, err = os.Stat(w.ucrtPath) | ||
if err != nil { | ||
return errors.New("failed to find a valid windows development kit, ucrt") | ||
} | ||
|
||
w.umPath = filepath.Join(base, entries[0].Name(), "um", "x64") | ||
_, err = os.Stat(w.umPath) | ||
if err != nil { | ||
return errors.New("failed to find a valid windows development kit, um") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *windowsLinker) findMsvc() error { | ||
base := "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC" | ||
|
||
entries, err := os.ReadDir(base) | ||
if err != nil { | ||
return errors.New("failed to find a MSVC installation") | ||
} | ||
|
||
w.msvcPath = filepath.Join(base, entries[0].Name(), "lib", "x64") | ||
_, err = os.Stat(w.msvcPath) | ||
if err != nil { | ||
return errors.New("failed to find a MSVC installation, lib") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *windowsLinker) AddLibrary(library string) { | ||
w.libraries = append(w.libraries, library) | ||
} | ||
|
||
func (w *windowsLinker) AddInput(input string) { | ||
w.inputs = append(w.inputs, input) | ||
} | ||
|
||
func (w *windowsLinker) Link(output string) error { | ||
cmd := exec.Command("lld-link") | ||
|
||
cmd.Args = append(cmd.Args, fmt.Sprintf("/libpath:%s", w.ucrtPath)) | ||
cmd.Args = append(cmd.Args, fmt.Sprintf("/libpath:%s", w.umPath)) | ||
cmd.Args = append(cmd.Args, fmt.Sprintf("/libpath:%s", w.msvcPath)) | ||
|
||
for _, library := range w.libraries { | ||
cmd.Args = append(cmd.Args, library) | ||
} | ||
|
||
for _, input := range w.inputs { | ||
cmd.Args = append(cmd.Args, fmt.Sprintf("%s", input)) | ||
} | ||
|
||
cmd.Args = append(cmd.Args, "/machine:x64") | ||
cmd.Args = append(cmd.Args, "/subsystem:console") | ||
cmd.Args = append(cmd.Args, "/debug") | ||
cmd.Args = append(cmd.Args, fmt.Sprintf("/out:%s", output)) | ||
|
||
return execute(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.