Skip to content

Commit 6d7b716

Browse files
authored
Merge pull request #270 from kcl-lang/feat-kcl-linux-musl
feat: kcl lib musl build and release
2 parents 12659aa + 66c788a commit 6d7b716

File tree

16 files changed

+361
-176
lines changed

16 files changed

+361
-176
lines changed

.github/workflows/go-test.yaml

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,98 @@ jobs:
2424
test:
2525
strategy:
2626
matrix:
27-
os: [macos-13, macos-13, macos-13-xlarge, macos-14, macos-latest, ubuntu-22.04, ubuntu-latest, windows-latest]
28-
runs-on: ${{ matrix.os }}
27+
include:
28+
# macOS Intel/ARM configurations
29+
30+
- os: macos-13
31+
runner: macos-13
32+
cgo_enabled: 0
33+
34+
- os: macos-13-xlarge
35+
runner: macos-13
36+
cgo_enabled: 0
37+
38+
- os: macos-14
39+
runner: macos-14
40+
cgo_enabled: 0
41+
42+
- os: macos-latest
43+
runner: macos-latest
44+
cgo_enabled: 0
45+
46+
# Ubuntu configurations
47+
48+
- os: ubuntu-22.04
49+
runner: ubuntu-22.04
50+
cgo_enabled: 0
51+
52+
- os: ubuntu-latest
53+
runner: ubuntu-latest
54+
cgo_enabled: 0
55+
56+
# Windows configurations
57+
58+
- os: windows-latest
59+
runner: windows-latest
60+
cgo_enabled: 0
61+
62+
# Alpine Linux container configurations
63+
- os: alpine-latest
64+
runner: ubuntu-latest # Host runner for the container
65+
container: golang:1.23-alpine3.19
66+
cgo_enabled: 1
67+
68+
runs-on: ${{ matrix.runner }}
69+
container: ${{ matrix.container }}
2970
defaults:
3071
run:
3172
working-directory: "go"
73+
3274
steps:
3375
- name: Git checkout
3476
uses: actions/checkout@v6
35-
- name: Set up Go
77+
78+
- name: Set up Go (non-musl)
79+
if: matrix.container == null
3680
uses: actions/setup-go@v6
3781
with:
3882
go-version: 1.23
39-
- name: Go code test
40-
run: go test ./...
83+
cache: true
84+
85+
- name: Install musl dependencies (Alpine container)
86+
if: matrix.container != null
87+
run: |
88+
apk add --no-cache \
89+
musl-dev \
90+
gcc \
91+
git \
92+
make
93+
94+
- name: Go mod tidy
95+
run: go mod tidy
96+
97+
- name: Setup build tags for Alpine
98+
if: matrix.container != null
99+
run: |
100+
echo "GO_BUILD_TAGS=musl netgo static osusergo" >> $GITHUB_ENV
101+
echo "GO_LDFLAGS=-linkmode external -extldflags '-static'" >> $GITHUB_ENV
102+
103+
- name: Go code test (CGO_ENABLED=${{ matrix.cgo_enabled }})
104+
if: matrix.os != 'windows-latest'
105+
run: |
106+
if [ -n "${{ matrix.container }}" ]; then
107+
BUILD_TAGS="$GO_BUILD_TAGS"
108+
EXTRA_LDFLAGS="$GO_LDFLAGS"
109+
fi
110+
111+
CGO_ENABLED=${{ matrix.cgo_enabled }} \
112+
go test ./... -v \
113+
-tags="${BUILD_TAGS}" \
114+
-ldflags="${EXTRA_LDFLAGS}"
115+
env:
116+
CGO_LDFLAGS: ${{ matrix.cgo_enabled == '1' && '-static' || '' }}
117+
118+
- name: Go code test (CGO_ENABLED=${{ matrix.cgo_enabled }}) on Windows
119+
if: matrix.os == 'windows-latest'
120+
run: |
121+
go test ./... -v

go/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
!lib/**/*.dylib
32
!lib/**/*.dll
43
!lib/**/*.lib

go/include/kcl.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef _KCL_H
2+
#define _KCL_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
#include <stdbool.h>
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif // __cplusplus
11+
12+
typedef uintptr_t KclServiceHandle;
13+
14+
KclServiceHandle kcl_service_new(uint64_t plugin_agent);
15+
void kcl_service_delete(KclServiceHandle svc);
16+
uint8_t* kcl_service_call_with_length(
17+
KclServiceHandle svc,
18+
const char* method,
19+
const char* args,
20+
uint32_t args_len,
21+
uint32_t* out_len
22+
);
23+
void kcl_free(uint8_t* ptr, uint32_t len);
24+
25+
#ifdef __cplusplus
26+
} // extern "C"
27+
#endif // __cplusplus
28+
29+
#endif /* _KCL_H */

go/install/install.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package install
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"path/filepath"
87
"runtime"
98

@@ -12,13 +11,6 @@ import (
1211

1312
const KCL_VERSION = "v0.12.1"
1413

15-
func findPath(name string) string {
16-
if path, err := exec.LookPath(name); err == nil {
17-
return path
18-
}
19-
return ""
20-
}
21-
2214
func getVersion() string {
2315
return fmt.Sprintf("%s-%s-%s", KCL_VERSION, runtime.GOOS, runtime.GOARCH)
2416
}

go/install/install_bin_unix.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

go/install/install_bin_windows.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

go/lib/linux-musl-amd64/libkcl.a

36.2 MB
Binary file not shown.

go/lib/linux-musl-arm64/libkcl.a

43.4 MB
Binary file not shown.

go/native/client.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,18 @@ package native
33
import (
44
"bytes"
55
"errors"
6-
"runtime"
76
"strings"
8-
"sync"
97
"unsafe"
108

11-
"github.com/ebitengine/purego"
129
"google.golang.org/protobuf/proto"
1310
"google.golang.org/protobuf/reflect/protoreflect"
1411
"kcl-lang.io/lib/go/api"
15-
"kcl-lang.io/lib/go/plugin"
16-
)
17-
18-
var libInit sync.Once
19-
20-
var (
21-
client *NativeServiceClient
22-
lib uintptr
23-
serviceNew func(uint64) uintptr
24-
serviceDelete func(uintptr)
25-
serviceCall func(uintptr, string, string, uint, *uint) uintptr
26-
free func(uintptr, uint)
2712
)
2813

2914
type validator interface {
3015
Validate() error
3116
}
3217

33-
type NativeServiceClient struct {
34-
svc uintptr
35-
}
36-
37-
func initClient(pluginAgent uint64) {
38-
libInit.Do(func() {
39-
lib, err := loadServiceNativeLib()
40-
if err != nil {
41-
panic(err)
42-
}
43-
purego.RegisterLibFunc(&serviceNew, lib, "kcl_service_new")
44-
purego.RegisterLibFunc(&serviceDelete, lib, "kcl_service_delete")
45-
purego.RegisterLibFunc(&serviceCall, lib, "kcl_service_call_with_length")
46-
purego.RegisterLibFunc(&free, lib, "kcl_free")
47-
client = new(NativeServiceClient)
48-
client.svc = serviceNew(pluginAgent)
49-
runtime.SetFinalizer(client, func(x *NativeServiceClient) {
50-
if x != nil {
51-
x.Close()
52-
}
53-
})
54-
})
55-
}
56-
57-
func NewNativeServiceClient() api.ServiceClient {
58-
return NewNativeServiceClientWithPluginAgent(plugin.GetInvokeJsonProxyPtr())
59-
}
60-
61-
func NewNativeServiceClientWithPluginAgent(pluginAgent uint64) *NativeServiceClient {
62-
initClient(pluginAgent)
63-
return client
64-
}
65-
66-
func (x *NativeServiceClient) Close() {
67-
serviceDelete(x.svc)
68-
closeLibrary(lib)
69-
}
70-
7118
func cApiCall[I interface {
7219
*TI
7320
proto.Message
@@ -131,16 +78,6 @@ func (c *NativeServiceClient) ExecProgram(in *api.ExecProgramArgs) (*api.ExecPro
13178
return cApiCall[*api.ExecProgramArgs, *api.ExecProgramResult](c, "KclService.ExecProgram", in)
13279
}
13380

134-
// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.12.1.
135-
func (c *NativeServiceClient) BuildProgram(in *api.BuildProgramArgs) (*api.BuildProgramResult, error) {
136-
return cApiCall[*api.BuildProgramArgs, *api.BuildProgramResult](c, "KclService.BuildProgram", in)
137-
}
138-
139-
// Depreciated: Please use the env.EnableFastEvalMode() and c.ExecutProgram method and will be removed in v0.12.1.
140-
func (c *NativeServiceClient) ExecArtifact(in *api.ExecArtifactArgs) (*api.ExecProgramResult, error) {
141-
return cApiCall[*api.ExecArtifactArgs, *api.ExecProgramResult](c, "KclService.ExecArtifact", in)
142-
}
143-
14481
func (c *NativeServiceClient) ParseFile(in *api.ParseFileArgs) (*api.ParseFileResult, error) {
14582
return cApiCall[*api.ParseFileArgs, *api.ParseFileResult](c, "KclService.ParseFile", in)
14683
}

go/native/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ n = Name {name = "name"}` // Sample KCL source code
6161
}
6262
}
6363

64-
func ParseFileASTJson(filename string, src interface{}) (result string, err error) {
64+
func ParseFileASTJson(filename string, src any) (result string, err error) {
6565
var code string
6666
if src != nil {
6767
switch src := src.(type) {

0 commit comments

Comments
 (0)