-
Notifications
You must be signed in to change notification settings - Fork 47
feat: support libc/compiler-rt for small places #1246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5587fd2
feat: add libc
MeteorsLiu f3ecce8
feat: support libc for small devices
MeteorsLiu 4639ee1
feat: support compiler-rt
MeteorsLiu 3f2fe83
fix: check needStart by libc name
MeteorsLiu 61143a6
fix: newlib url and rt url
MeteorsLiu 19ddee0
test: add compile test
MeteorsLiu 53e2248
fix: remove debug compiler options
MeteorsLiu 1b3889e
feat: add target name design
MeteorsLiu 997ea28
feat: support riscv32
MeteorsLiu 012a5d7
fix: remove unused file
MeteorsLiu 8ecbfe9
feat: support arm/risc64/avr for compiler rt
MeteorsLiu 96c5ce9
fix: compiler rt file path
MeteorsLiu 5997a29
fix: update url
MeteorsLiu cb2fa5d
fix: picolibc.h
MeteorsLiu 1d3ecb2
fix: export libc cflags for compiler-rt
MeteorsLiu f875347
test: fix compile test
MeteorsLiu a8bc617
test: skip llgo test
MeteorsLiu 4b383b1
ci: revert
MeteorsLiu b99c073
test: add test for compile options
MeteorsLiu 82bb6e8
test: add rt test
MeteorsLiu 5a4c83e
test: add _embdemo print test
MeteorsLiu 1bd9ceb
test: fix _embdemo
MeteorsLiu dd3c1f9
test: fix _embdemo write test
MeteorsLiu 977806a
test: add more libc test
MeteorsLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| package main | ||
|
|
||
| import "github.com/goplus/lib/c" | ||
|
|
||
| func myprint(s *c.Char) { | ||
| for i := 0; i < int(c.Strlen(s)); i++ { | ||
| WriteByte(byte(c.Index(s, i))) | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| for { | ||
| myprint(c.Str("hello world")) | ||
| sleep(1) | ||
| } | ||
| } | ||
This file contains hidden or 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,13 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| _ "unsafe" | ||
|
|
||
| "github.com/goplus/lib/c" | ||
| ) | ||
|
|
||
| //go:linkname WriteByte C.board_uart_write_char | ||
| func WriteByte(b byte) | ||
|
|
||
| //go:linkname sleep sleep | ||
| func sleep(c c.Int) |
This file contains hidden or 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,31 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| _ "unsafe" | ||
|
|
||
| "github.com/goplus/lib/c" | ||
| ) | ||
|
|
||
| //go:linkname write C.write | ||
| func write(c.Int, *c.Char, c.SizeT) int | ||
|
|
||
| func main() { | ||
| buf := c.Malloc(6) | ||
| c.Memset(buf, 0, 6) | ||
| c.Strncpy((*c.Char)(buf), c.Str("abcde"), 5) | ||
|
|
||
| if c.Strcmp((*c.Char)(buf), c.Str("abcde")) == 0 { | ||
| write(1, c.Str("pass strcmp"), 11) | ||
| } | ||
|
|
||
| if byte(c.Index((*c.Char)(buf), 0)) == 'a' { | ||
| write(1, c.Str("pass index"), 10) | ||
| } | ||
|
|
||
| c.Memset(buf, c.Int('A'), 5) | ||
| if c.Strcmp((*c.Char)(buf), c.Str("AAAAA")) == 0 { | ||
| write(1, c.Str("pass memeset"), 11) | ||
| } | ||
|
|
||
| write(1, (*c.Char)(buf), 5) | ||
| } |
This file contains hidden or 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 hidden or 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 compile | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/goplus/llgo/internal/clang" | ||
| ) | ||
|
|
||
| type CompileOptions struct { | ||
| CC string // Compiler to use | ||
| Linker string | ||
| CCFLAGS []string | ||
| CFLAGS []string | ||
| LDFLAGS []string | ||
| } | ||
|
|
||
| type CompileGroup struct { | ||
| OutputFileName string | ||
| Files []string // List of source files to compile | ||
| CFlags []string // C compiler flags | ||
| CCFlags []string | ||
| LDFlags []string // Linker flags | ||
| } | ||
|
|
||
| func (g CompileGroup) IsCompiled(outputDir string) bool { | ||
| archive := filepath.Join(outputDir, filepath.Base(g.OutputFileName)) | ||
| _, err := os.Stat(archive) | ||
| return err == nil | ||
| } | ||
|
|
||
| func (g CompileGroup) Compile( | ||
| outputDir string, options CompileOptions, | ||
| ) (err error) { | ||
| if g.IsCompiled(outputDir) { | ||
| return | ||
| } | ||
| tmpCompileDir, err := os.MkdirTemp("", "compile-group*") | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer os.RemoveAll(tmpCompileDir) | ||
|
|
||
| compileLDFlags := append(slices.Clone(options.LDFLAGS), g.LDFlags...) | ||
| compileCCFlags := append(slices.Clone(options.CCFLAGS), g.CCFlags...) | ||
| compileCFFlags := append(slices.Clone(options.CFLAGS), g.CFlags...) | ||
|
|
||
| cfg := clang.NewConfig(options.CC, compileCCFlags, compileCFFlags, compileLDFlags, options.Linker) | ||
|
|
||
| var objFiles []string | ||
|
|
||
| compiler := clang.NewCompiler(cfg) | ||
|
|
||
| compiler.Verbose = true | ||
|
|
||
| archive := filepath.Join(outputDir, filepath.Base(g.OutputFileName)) | ||
| fmt.Fprintf(os.Stderr, "Start to compile group %s to %s...\n", g.OutputFileName, archive) | ||
|
|
||
| for _, file := range g.Files { | ||
| var tempObjFile *os.File | ||
| tempObjFile, err = os.CreateTemp(tmpCompileDir, fmt.Sprintf("%s*.o", strings.ReplaceAll(file, string(os.PathSeparator), "-"))) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| lang := "c" | ||
| if filepath.Ext(file) == ".S" { | ||
| lang = "assembler-with-cpp" | ||
| } | ||
| err = compiler.Compile("-o", tempObjFile.Name(), "-x", lang, "-c", file) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| objFiles = append(objFiles, tempObjFile.Name()) | ||
| } | ||
|
|
||
| args := []string{"rcs", archive} | ||
| args = append(args, objFiles...) | ||
|
|
||
| ccDir := filepath.Dir(options.CC) | ||
| llvmAr := filepath.Join(ccDir, "llvm-ar") | ||
|
|
||
| cmd := exec.Command(llvmAr, args...) | ||
| // TODO(MeteorsLiu): support verbose | ||
| // cmd.Stdout = os.Stdout | ||
| // cmd.Stderr = os.Stderr | ||
| err = cmd.Run() | ||
| return | ||
| } | ||
|
|
||
| // CompileConfig represents compilation configuration | ||
| type CompileConfig struct { | ||
| Url string | ||
| Name string // compile name (e.g., "picolibc", "musl", "glibc") | ||
| Groups []CompileGroup | ||
| ArchiveSrcDir string | ||
| LibcCFlags []string | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1257 the issue for how to run this demo