Skip to content

Commit e127a6d

Browse files
fix(go): align bindings and installer versions
1 parent b56b267 commit e127a6d

10 files changed

Lines changed: 105 additions & 39 deletions

File tree

.github/workflows/publish-go-sdk.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ jobs:
4848
// NativeLibReleaseTag is the GitHub release tag used to fetch native artifacts.
4949
const NativeLibReleaseTag = "${C_SDK_VERSION}"
5050
EOF
51-
cat > sdks/go/tools/install/release.go <<EOF
52-
package main
53-
54-
const defaultReleaseTag = "${C_SDK_VERSION}"
55-
EOF
5651
5752
- name: Publish source-only module tags
5853
run: ./sdks/go/scripts/publish-sdk-module-tags.sh "${{ github.event.inputs.go_version }}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
libmoss.a
2+
.moss-install-checksum
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
libmoss.a
2+
.moss-install-checksum
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
libmoss.a
2+
.moss-install-checksum
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
moss.lib
22
moss.dll
33
moss.dll.lib
4+
.moss-install-checksum

sdks/go/bindings/libmoss.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build cgo
1+
//go:build cgo && ((linux && (amd64 || arm64)) || (darwin && arm64) || (windows && amd64))
22

33
package mosscore
44

sdks/go/bindings/stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !cgo
1+
//go:build !cgo || !((linux && (amd64 || arm64)) || (darwin && arm64) || (windows && amd64))
22

33
package mosscore
44

sdks/go/tools/install/main.go

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
const (
2525
defaultRepo = "usemoss/moss"
2626
bindingsModulePath = "github.com/usemoss/moss/sdks/go/bindings"
27+
installReceiptName = ".moss-install-checksum"
2728
)
2829

2930
type platform struct {
@@ -54,7 +55,7 @@ var platforms = []platform{
5455

5556
func main() {
5657
all := flag.Bool("all", false, "install libraries for all supported platforms")
57-
releaseTag := flag.String("release", defaultReleaseTag, "C SDK GitHub release tag")
58+
releaseTag := flag.String("release", "", "C SDK GitHub release tag (default: target bindings metadata)")
5859
repo := flag.String("repo", defaultRepo, "GitHub repository (owner/name)")
5960
bindingsDir := flag.String("bindings", "", "bindings directory (default: auto-detect)")
6061
vendor := flag.Bool("vendor", false, "run go mod vendor before installing into a downloaded module")
@@ -65,6 +66,12 @@ func main() {
6566
if err != nil {
6667
fatal(err)
6768
}
69+
if *releaseTag == "" {
70+
*releaseTag, err = nativeReleaseTag(root)
71+
if err != nil {
72+
fatal(err)
73+
}
74+
}
6875

6976
version := strings.TrimPrefix(*releaseTag, "c-sdk-")
7077
version = strings.TrimPrefix(version, "v")
@@ -182,6 +189,31 @@ func goEnv(name string) (string, error) {
182189
return strings.TrimSpace(string(out)), nil
183190
}
184191

192+
func nativeReleaseTag(bindingsRoot string) (string, error) {
193+
f, err := os.Open(filepath.Join(bindingsRoot, "version.go"))
194+
if err != nil {
195+
return "", fmt.Errorf("read bindings native version: %w", err)
196+
}
197+
defer f.Close()
198+
199+
const prefix = "const NativeLibReleaseTag = \""
200+
scanner := bufio.NewScanner(f)
201+
for scanner.Scan() {
202+
line := strings.TrimSpace(scanner.Text())
203+
if !strings.HasPrefix(line, prefix) || !strings.HasSuffix(line, "\"") {
204+
continue
205+
}
206+
tag := strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\"")
207+
if tag != "" {
208+
return tag, nil
209+
}
210+
}
211+
if err := scanner.Err(); err != nil {
212+
return "", fmt.Errorf("read bindings native version: %w", err)
213+
}
214+
return "", errors.New("NativeLibReleaseTag not found in bindings/version.go; pass -release explicitly")
215+
}
216+
185217
func selectPlatforms(all bool) ([]platform, error) {
186218
if all {
187219
return platforms, nil
@@ -245,17 +277,9 @@ func installPlatform(bindingsRoot, libDir string, installHeader bool, baseURL, v
245277
destLib := filepath.Join(destDir, p.libFile)
246278
headerPath := filepath.Join(bindingsRoot, "include", "libmoss.h")
247279

248-
if !force {
249-
if err := verifyFileChecksum(destLib, wantChecksum); err == nil {
250-
if !installHeader {
251-
fmt.Printf("skip %s (already installed)\n", p.id)
252-
return nil
253-
}
254-
if _, err := os.Stat(headerPath); err == nil {
255-
fmt.Printf("skip %s (already installed)\n", p.id)
256-
return nil
257-
}
258-
}
280+
if !force && fileExists(destLib) && (!installHeader || fileExists(headerPath)) && receiptMatches(filepath.Join(destDir, installReceiptName), archive, wantChecksum) {
281+
fmt.Printf("skip %s (already installed)\n", p.id)
282+
return nil
259283
}
260284

261285
if err := os.MkdirAll(destDir, 0o755); err != nil {
@@ -291,11 +315,45 @@ func installPlatform(bindingsRoot, libDir string, installHeader bool, baseURL, v
291315
if err := copyFile(filepath.Join(extractedRoot, p.srcLib), destLib); err != nil {
292316
return err
293317
}
318+
if err := writeReceipt(filepath.Join(destDir, installReceiptName), archive, wantChecksum); err != nil {
319+
return err
320+
}
294321

295322
fmt.Printf("installed %s -> %s\n", p.id, destLib)
296323
return nil
297324
}
298325

326+
func fileExists(path string) bool {
327+
info, err := os.Stat(path)
328+
return err == nil && !info.IsDir()
329+
}
330+
331+
func receiptMatches(path, archive, checksum string) bool {
332+
contents, err := os.ReadFile(path)
333+
if err != nil {
334+
return false
335+
}
336+
return strings.TrimSpace(string(contents)) == checksum+" "+archive
337+
}
338+
339+
func writeReceipt(path, archive, checksum string) error {
340+
tmp, err := os.CreateTemp(filepath.Dir(path), ".moss-install-*")
341+
if err != nil {
342+
return err
343+
}
344+
tmpPath := tmp.Name()
345+
defer os.Remove(tmpPath)
346+
347+
if _, err := fmt.Fprintf(tmp, "%s %s\n", checksum, archive); err != nil {
348+
tmp.Close()
349+
return err
350+
}
351+
if err := tmp.Close(); err != nil {
352+
return err
353+
}
354+
return os.Rename(tmpPath, path)
355+
}
356+
299357
func extractTarGz(archivePath, destRoot, version, triple string) error {
300358
f, err := os.Open(archivePath)
301359
if err != nil {
@@ -412,23 +470,6 @@ func downloadFile(url, dest, wantChecksum string) error {
412470
return os.Rename(tmpPath, dest)
413471
}
414472

415-
func verifyFileChecksum(path, want string) error {
416-
f, err := os.Open(path)
417-
if err != nil {
418-
return err
419-
}
420-
defer f.Close()
421-
422-
hasher := sha256.New()
423-
if _, err := io.Copy(hasher, f); err != nil {
424-
return err
425-
}
426-
if hex.EncodeToString(hasher.Sum(nil)) != want {
427-
return fmt.Errorf("checksum mismatch")
428-
}
429-
return nil
430-
}
431-
432473
func copyFile(src, dest string) error {
433474
in, err := os.Open(src)
434475
if err != nil {

sdks/go/tools/install/main_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"os"
45
"path/filepath"
56
"testing"
67
)
@@ -23,3 +24,31 @@ func TestSafeArchiveTarget(t *testing.T) {
2324
}
2425
}
2526
}
27+
28+
func TestNativeReleaseTag(t *testing.T) {
29+
root := t.TempDir()
30+
if err := os.WriteFile(filepath.Join(root, "version.go"), []byte("package mosscore\n\nconst NativeLibReleaseTag = \"c-sdk-v1.2.3\"\n"), 0o644); err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
tag, err := nativeReleaseTag(root)
35+
if err != nil {
36+
t.Fatalf("nativeReleaseTag returned an error: %v", err)
37+
}
38+
if tag != "c-sdk-v1.2.3" {
39+
t.Fatalf("nativeReleaseTag() = %q, want c-sdk-v1.2.3", tag)
40+
}
41+
}
42+
43+
func TestInstallReceipt(t *testing.T) {
44+
path := filepath.Join(t.TempDir(), installReceiptName)
45+
if err := writeReceipt(path, "libmoss-v0.9.0-x86_64-unknown-linux-gnu.tar.gz", "abc123"); err != nil {
46+
t.Fatalf("writeReceipt returned an error: %v", err)
47+
}
48+
if !receiptMatches(path, "libmoss-v0.9.0-x86_64-unknown-linux-gnu.tar.gz", "abc123") {
49+
t.Fatal("receiptMatches() = false, want true")
50+
}
51+
if receiptMatches(path, "libmoss-v0.9.0-x86_64-unknown-linux-gnu.tar.gz", "different") {
52+
t.Fatal("receiptMatches() = true for a different checksum, want false")
53+
}
54+
}

sdks/go/tools/install/release.go

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

0 commit comments

Comments
 (0)