Merge pull request #1283 from cpunion/reorganize-demos
reorganize: consolidate demo directories
This commit is contained in:
16
.github/workflows/llgo.yml
vendored
16
.github/workflows/llgo.yml
vendored
@@ -22,14 +22,14 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Download model file
|
- name: Download model file
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ./_demo/llama2-c
|
mkdir -p ./_demo/c/llama2-c
|
||||||
wget -P ./_demo/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
|
wget -P ./_demo/c/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
|
||||||
|
|
||||||
- name: Upload model as artifact
|
- name: Upload model as artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: llama2-model
|
name: llama2-model
|
||||||
path: ./_demo/llama2-c/stories15M.bin
|
path: ./_demo/c/llama2-c/stories15M.bin
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
|
|
||||||
llgo:
|
llgo:
|
||||||
@@ -54,7 +54,7 @@ jobs:
|
|||||||
uses: actions/download-artifact@v5
|
uses: actions/download-artifact@v5
|
||||||
with:
|
with:
|
||||||
name: llama2-model
|
name: llama2-model
|
||||||
path: ./_demo/llama2-c/
|
path: ./_demo/c/llama2-c/
|
||||||
- name: Download platform-specific demo libs
|
- name: Download platform-specific demo libs
|
||||||
run: |
|
run: |
|
||||||
if ${{ startsWith(matrix.os, 'macos') }}; then
|
if ${{ startsWith(matrix.os, 'macos') }}; then
|
||||||
@@ -63,8 +63,8 @@ jobs:
|
|||||||
DEMO_PKG="cargs_linux_amd64.zip"
|
DEMO_PKG="cargs_linux_amd64.zip"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p ./_demo/cargs/libs
|
mkdir -p ./_demo/c/cargs/libs
|
||||||
cd ./_demo/cargs/libs
|
cd ./_demo/c/cargs/libs
|
||||||
wget https://github.com/goplus/llpkg/releases/download/cargs/v1.0.0/${DEMO_PKG}
|
wget https://github.com/goplus/llpkg/releases/download/cargs/v1.0.0/${DEMO_PKG}
|
||||||
unzip ${DEMO_PKG}
|
unzip ${DEMO_PKG}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ jobs:
|
|||||||
echo "Testing demo without RPATH (should fail)..."
|
echo "Testing demo without RPATH (should fail)..."
|
||||||
export LLGO_FULL_RPATH=false
|
export LLGO_FULL_RPATH=false
|
||||||
pkg-config --libs cargs
|
pkg-config --libs cargs
|
||||||
if (cd ./_demo/cargs && llgo run .); then
|
if (cd ./_demo/c/cargs && llgo run .); then
|
||||||
echo "ERROR: cargs demo should have failed without RPATH!"
|
echo "ERROR: cargs demo should have failed without RPATH!"
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
@@ -278,7 +278,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Test Cross Compilation (wasm)
|
- name: Test Cross Compilation (wasm)
|
||||||
shell: bash
|
shell: bash
|
||||||
working-directory: _demo
|
working-directory: _demo/c
|
||||||
run: |
|
run: |
|
||||||
echo "Testing cross-compilation wasm with Go 1.24.2"
|
echo "Testing cross-compilation wasm with Go 1.24.2"
|
||||||
|
|
||||||
|
|||||||
4
.github/workflows/targets.yml
vendored
4
.github/workflows/targets.yml
vendored
@@ -3,7 +3,7 @@ name: Targets
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- "**"
|
- "**"
|
||||||
- "!dependabot/**"
|
- "!dependabot/**"
|
||||||
pull_request:
|
pull_request:
|
||||||
@@ -43,5 +43,5 @@ jobs:
|
|||||||
|
|
||||||
- name: Build targets
|
- name: Build targets
|
||||||
run: |
|
run: |
|
||||||
cd _demo/targetsbuild
|
cd _demo/embed/targetsbuild
|
||||||
bash build.sh
|
bash build.sh
|
||||||
|
|||||||
4
.github/workflows/test_demo.sh
vendored
4
.github/workflows/test_demo.sh
vendored
@@ -1,11 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# llgo run subdirectories under _demo and _pydemo that contain *.go files
|
# llgo run subdirectories under _demo that contain *.go files
|
||||||
total=0
|
total=0
|
||||||
failed=0
|
failed=0
|
||||||
failed_cases=""
|
failed_cases=""
|
||||||
for d in ./_demo/* ./_pydemo/*; do
|
for d in ./_demo/go/* ./_demo/py/* ./_demo/c/*; do
|
||||||
if [ -d "$d" ] && [ -n "$(ls "$d"/*.go 2>/dev/null)" ]; then
|
if [ -d "$d" ] && [ -n "$(ls "$d"/*.go 2>/dev/null)" ]; then
|
||||||
total=$((total+1))
|
total=$((total+1))
|
||||||
echo "Testing $d"
|
echo "Testing $d"
|
||||||
|
|||||||
30
README.md
30
README.md
@@ -63,14 +63,14 @@ This is a simple example of calling the C `printf` function to print `Hello worl
|
|||||||
|
|
||||||
The `_demo` directory contains some C standard libary related demos (it start with `_` to prevent the `go` command from compiling it):
|
The `_demo` directory contains some C standard libary related demos (it start with `_` to prevent the `go` command from compiling it):
|
||||||
|
|
||||||
* [hello](_demo/hello/hello.go): call C `printf` to print `Hello world`
|
* [hello](_demo/c/hello/hello.go): call C `printf` to print `Hello world`
|
||||||
* [concat](_demo/concat/concat.go): call C `fprintf` with `stderr`
|
* [concat](_demo/c/concat/concat.go): call C `fprintf` with `stderr`
|
||||||
* [qsort](_demo/qsort/qsort.go): call C function with a callback (eg. `qsort`)
|
* [qsort](_demo/c/qsort/qsort.go): call C function with a callback (eg. `qsort`)
|
||||||
|
|
||||||
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
|
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd <demo-directory> # eg. cd _demo/hello
|
cd <demo-directory> # eg. cd _demo/c/hello
|
||||||
llgo run .
|
llgo run .
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ import _ "unsafe" // for go:linkname
|
|||||||
func Sqrt(x float64) float64
|
func Sqrt(x float64) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
You can directly integrate it into [your own code](_demo/linkname/linkname.go):
|
You can directly integrate it into [your own code](_demo/c/linkname/linkname.go):
|
||||||
|
|
||||||
<!-- embedme doc/_readme/llgo_call_c/call_c.go -->
|
<!-- embedme doc/_readme/llgo_call_c/call_c.go -->
|
||||||
|
|
||||||
@@ -204,17 +204,17 @@ func main() {
|
|||||||
|
|
||||||
Here we define two 3x3 matrices a and b, add them to get x, and then print the result.
|
Here we define two 3x3 matrices a and b, add them to get x, and then print the result.
|
||||||
|
|
||||||
The `_pydemo` directory contains some python related demos:
|
The `_demo/py/` directory contains some python related demos:
|
||||||
|
|
||||||
* [callpy](_pydemo/callpy/callpy.go): call Python standard library function `math.sqrt`
|
* [callpy](_demo/py/callpy/callpy.go): call Python standard library function `math.sqrt`
|
||||||
* [pi](_pydemo/pi/pi.go): print python constants `math.pi`
|
* [pi](_demo/py/pi/pi.go): print python constants `math.pi`
|
||||||
* [statistics](_pydemo/statistics/statistics.go): define a python list and call `statistics.mean` to get the mean
|
* [statistics](_demo/py/statistics/statistics.go): define a python list and call `statistics.mean` to get the mean
|
||||||
* [matrix](_pydemo/matrix/matrix.go): a basic `numpy` demo
|
* [matrix](_demo/py/matrix/matrix.go): a basic `numpy` demo
|
||||||
|
|
||||||
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
|
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd <demo-directory> # eg. cd _pydemo/callpy
|
cd <demo-directory> # eg. cd _demo/py/callpy
|
||||||
llgo run .
|
llgo run .
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -242,7 +242,7 @@ The currently supported libraries include:
|
|||||||
|
|
||||||
Here are some examples related to them:
|
Here are some examples related to them:
|
||||||
|
|
||||||
* [llama2-c](_demo/llama2-c): inference Llama 2 (It's the first llgo AI example)
|
* [llama2-c](_demo/c/llama2-c): inference Llama 2 (It's the first llgo AI example)
|
||||||
* [mkjson](https://github.com/goplus/lib/tree/main/c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
|
* [mkjson](https://github.com/goplus/lib/tree/main/c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
|
||||||
* [sqlitedemo](https://github.com/goplus/lib/tree/main/c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
|
* [sqlitedemo](https://github.com/goplus/lib/tree/main/c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
|
||||||
* [tetris](https://github.com/goplus/lib/tree/main/c/raylib/_demo/tetris/tetris.go): a tetris game based on raylib
|
* [tetris](https://github.com/goplus/lib/tree/main/c/raylib/_demo/tetris/tetris.go): a tetris game based on raylib
|
||||||
@@ -252,11 +252,11 @@ Here are some examples related to them:
|
|||||||
|
|
||||||
All Go syntax (including `cgo`) is already supported. Here are some examples:
|
All Go syntax (including `cgo`) is already supported. Here are some examples:
|
||||||
|
|
||||||
* [concat](_demo/concat/concat.go): define a variadic function
|
* [concat](_demo/c/concat/concat.go): define a variadic function
|
||||||
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
|
* [genints](_demo/c/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
|
||||||
* [errors](_cmptest/errors/errors.go): demo to implement error interface
|
* [errors](_cmptest/errors/errors.go): demo to implement error interface
|
||||||
* [defer](_cmptest/defer/defer.go): defer demo
|
* [defer](_cmptest/defer/defer.go): defer demo
|
||||||
* [goroutine](_demo/goroutine/goroutine.go): goroutine demo
|
* [goroutine](_demo/go/goroutine/goroutine.go): goroutine demo
|
||||||
|
|
||||||
|
|
||||||
### Defer
|
### Defer
|
||||||
|
|||||||
@@ -106,8 +106,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goplus/llgo/_demo/cgofull/pymod1"
|
"github.com/goplus/llgo/_demo/c/cgofull/pymod1"
|
||||||
"github.com/goplus/llgo/_demo/cgofull/pymod2"
|
"github.com/goplus/llgo/_demo/c/cgofull/pymod2"
|
||||||
)
|
)
|
||||||
|
|
||||||
//export go_callback_not_use_in_go
|
//export go_callback_not_use_in_go
|
||||||
@@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/goplus/lib/c"
|
"github.com/goplus/lib/c"
|
||||||
"github.com/goplus/lib/c/math"
|
"github.com/goplus/lib/c/math"
|
||||||
"github.com/goplus/llgo/_demo/cppintf/foo"
|
"github.com/goplus/llgo/_demo/c/cppintf/foo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bar struct {
|
type Bar struct {
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/goplus/lib/c"
|
"github.com/goplus/lib/c"
|
||||||
"github.com/goplus/lib/c/math"
|
"github.com/goplus/lib/c/math"
|
||||||
"github.com/goplus/llgo/_demo/cppmintf/foo"
|
"github.com/goplus/llgo/_demo/c/cppmintf/foo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bar struct {
|
type Bar struct {
|
||||||
5
_demo/c/go.mod
Normal file
5
_demo/c/go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/goplus/llgo/_demo/c
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/goplus/lib v0.3.0
|
||||||
2
_demo/c/go.sum
Normal file
2
_demo/c/go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/goplus/lib v0.3.0 h1:y0ZGb5Q/RikW1oMMB4Di7XIZIpuzh/7mlrR8HNbxXCA=
|
||||||
|
github.com/goplus/lib v0.3.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
||||||
|
Before Width: | Height: | Size: 183 KiB After Width: | Height: | Size: 183 KiB |
@@ -36,7 +36,6 @@ loop: // parse command line arguments
|
|||||||
// build the Tokenizer via the tokenizer .bin file
|
// build the Tokenizer via the tokenizer .bin file
|
||||||
var tokenizer llama2.Tokenizer
|
var tokenizer llama2.Tokenizer
|
||||||
llama2.BuildTokenizer(&tokenizer, tokenizerPath, transformer.Config.VocabSize)
|
llama2.BuildTokenizer(&tokenizer, tokenizerPath, transformer.Config.VocabSize)
|
||||||
|
|
||||||
// build the Sampler
|
// build the Sampler
|
||||||
var sampler llama2.Sampler
|
var sampler llama2.Sampler
|
||||||
llama2.BuildSampler(&sampler, transformer.Config.VocabSize, temperature, topp, rngSeed)
|
llama2.BuildSampler(&sampler, transformer.Config.VocabSize, temperature, topp, rngSeed)
|
||||||
5
_demo/embed/go.mod
Normal file
5
_demo/embed/go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/goplus/llgo/_demo/embed
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/goplus/lib v0.3.0
|
||||||
2
_demo/embed/go.sum
Normal file
2
_demo/embed/go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/goplus/lib v0.3.0 h1:y0ZGb5Q/RikW1oMMB4Di7XIZIpuzh/7mlrR8HNbxXCA=
|
||||||
|
github.com/goplus/lib v0.3.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
||||||
16
_demo/embed/hello-esp32/main.go
Normal file
16
_demo/embed/hello-esp32/main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
13
_demo/embed/hello-esp32/uart.go
Normal file
13
_demo/embed/hello-esp32/uart.go
Normal file
@@ -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)
|
||||||
@@ -111,7 +111,7 @@ if [ $# -eq 1 ]; then
|
|||||||
done < "$target_file"
|
done < "$target_file"
|
||||||
else
|
else
|
||||||
# Use targets from *.json files
|
# Use targets from *.json files
|
||||||
for target_file in ../../targets/*.json; do
|
for target_file in ../../../targets/*.json; do
|
||||||
# Extract target name from filename (remove path and .json extension)
|
# Extract target name from filename (remove path and .json extension)
|
||||||
target=$(basename "$target_file" .json)
|
target=$(basename "$target_file" .json)
|
||||||
targets_to_build+=("$target")
|
targets_to_build+=("$target")
|
||||||
@@ -127,9 +127,9 @@ for target in "${targets_to_build[@]}"; do
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
output=$(../../llgo.sh build -target $target -o hello.out . 2>&1)
|
output=$(../../../llgo.sh build -target $target -o hello.elf . 2>&1)
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo ✅ $target `file hello.out`
|
echo ✅ $target `file hello.elf`
|
||||||
successful_targets+=("$target")
|
successful_targets+=("$target")
|
||||||
else
|
else
|
||||||
# Check if output contains warning messages
|
# Check if output contains warning messages
|
||||||
6
_demo/embed/targetsbuild/empty.go
Normal file
6
_demo/embed/targetsbuild/empty.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import _ "github.com/goplus/llgo/_demo/embed/targetsbuild/C"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
||||||
31
_demo/embed/write-esp32/main.go
Normal file
31
_demo/embed/write-esp32/main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goplus/llgo/_demo/async/async"
|
"github.com/goplus/llgo/_demo/go/async/async"
|
||||||
"github.com/goplus/llgo/_demo/async/timeout"
|
"github.com/goplus/llgo/_demo/go/async/timeout"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Sleep(i int, d time.Duration) async.Future[int] {
|
func Sleep(i int, d time.Duration) async.Future[int] {
|
||||||
@@ -3,7 +3,7 @@ package timeout
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goplus/llgo/_demo/async/async"
|
"github.com/goplus/llgo/_demo/go/async/async"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Timeout(d time.Duration) async.Future[async.Void] {
|
func Timeout(d time.Duration) async.Future[async.Void] {
|
||||||
3
_demo/go/go.mod
Normal file
3
_demo/go/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module github.com/goplus/llgo/_demo/go
|
||||||
|
|
||||||
|
go 1.20
|
||||||
0
_demo/go/go.sum
Normal file
0
_demo/go/go.sum
Normal file
@@ -1,4 +1,4 @@
|
|||||||
module github.com/goplus/llgo/_demo
|
module github.com/goplus/llgo/_demo/py
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import _ "github.com/goplus/llgo/_demo/targetsbuild/C"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
module github.com/goplus/llgo/_pydemo
|
|
||||||
|
|
||||||
go 1.20
|
|
||||||
|
|
||||||
require github.com/goplus/lib v0.2.0
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g=
|
|
||||||
github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
|
||||||
Reference in New Issue
Block a user