@@ -33,6 +33,7 @@ import (
33
33
"os/exec"
34
34
"path"
35
35
"path/filepath"
36
+ "regexp"
36
37
"runtime"
37
38
"strings"
38
39
"time"
@@ -41,7 +42,6 @@ import (
41
42
42
43
"github.com/arduino/arduino-create-agent/utilities"
43
44
"github.com/blang/semver"
44
- "github.com/xrash/smetrics"
45
45
)
46
46
47
47
type system struct {
@@ -64,15 +64,21 @@ type index struct {
64
64
} `json:"packages"`
65
65
}
66
66
67
- var systems = map [string ]string {
68
- "linuxamd64" : "x86_64-linux-gnu" ,
69
- "linux386" : "i686-linux-gnu" ,
70
- "darwinamd64" : "i686-apple-darwin" ,
71
- "darwinarm64" : "arm64-apple-darwin" ,
72
- "windows386" : "i686-mingw32" ,
73
- "windowsamd64" : "x86_64-mingw32" ,
74
- "linuxarm" : "arm-linux-gnueabihf" ,
75
- }
67
+ // Source: https://github.com/arduino/arduino-cli/blob/master/arduino/cores/tools.go#L129-L142
68
+ var (
69
+ regexpLinuxArm = regexp .MustCompile ("arm.*-linux-gnueabihf" )
70
+ regexpLinuxArm64 = regexp .MustCompile ("(aarch64|arm64)-linux-gnu" )
71
+ regexpLinux64 = regexp .MustCompile ("x86_64-.*linux-gnu" )
72
+ regexpLinux32 = regexp .MustCompile ("i[3456]86-.*linux-gnu" )
73
+ regexpWindows32 = regexp .MustCompile ("i[3456]86-.*(mingw32|cygwin)" )
74
+ regexpWindows64 = regexp .MustCompile ("(amd64|x86_64)-.*(mingw32|cygwin)" )
75
+ regexpMac64 = regexp .MustCompile ("x86_64-apple-darwin.*" )
76
+ regexpMac32 = regexp .MustCompile ("i[3456]86-apple-darwin.*" )
77
+ regexpMacArm64 = regexp .MustCompile ("arm64-apple-darwin.*" )
78
+ regexpFreeBSDArm = regexp .MustCompile ("arm.*-freebsd[0-9]*" )
79
+ regexpFreeBSD32 = regexp .MustCompile ("i?[3456]86-freebsd[0-9]*" )
80
+ regexpFreeBSD64 = regexp .MustCompile ("amd64-freebsd[0-9]*" )
81
+ )
76
82
77
83
// public vars to allow override in the tests
78
84
var (
@@ -299,6 +305,64 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
299
305
return t .writeMap ()
300
306
}
301
307
308
+ // Source: https://github.com/arduino/arduino-cli/blob/master/arduino/cores/tools.go#L144-L176
309
+ func (s * system ) isExactMatchWith (osName , osArch string ) bool {
310
+ if s .Host == "all" {
311
+ return true
312
+ }
313
+
314
+ switch osName + "," + osArch {
315
+ case "linux,arm" , "linux,armbe" :
316
+ return regexpLinuxArm .MatchString (s .Host )
317
+ case "linux,arm64" :
318
+ return regexpLinuxArm64 .MatchString (s .Host )
319
+ case "linux,amd64" :
320
+ return regexpLinux64 .MatchString (s .Host )
321
+ case "linux,386" :
322
+ return regexpLinux32 .MatchString (s .Host )
323
+ case "windows,386" :
324
+ return regexpWindows32 .MatchString (s .Host )
325
+ case "windows,amd64" :
326
+ return regexpWindows64 .MatchString (s .Host )
327
+ case "darwin,arm64" :
328
+ return regexpMacArm64 .MatchString (s .Host )
329
+ case "darwin,amd64" :
330
+ return regexpMac64 .MatchString (s .Host )
331
+ case "darwin,386" :
332
+ return regexpMac32 .MatchString (s .Host )
333
+ case "freebsd,arm" :
334
+ return regexpFreeBSDArm .MatchString (s .Host )
335
+ case "freebsd,386" :
336
+ return regexpFreeBSD32 .MatchString (s .Host )
337
+ case "freebsd,amd64" :
338
+ return regexpFreeBSD64 .MatchString (s .Host )
339
+ }
340
+ return false
341
+ }
342
+
343
+ // Source: https://github.com/arduino/arduino-cli/blob/master/arduino/cores/tools.go#L178-L198
344
+ func (s * system ) isCompatibleWith (osName , osArch string ) (bool , int ) {
345
+ if s .isExactMatchWith (osName , osArch ) {
346
+ return true , 1000
347
+ }
348
+
349
+ switch osName + "," + osArch {
350
+ case "windows,amd64" :
351
+ return regexpWindows32 .MatchString (s .Host ), 10
352
+ case "darwin,amd64" :
353
+ return regexpMac32 .MatchString (s .Host ), 10
354
+ case "darwin,arm64" :
355
+ // Compatibility guaranteed through Rosetta emulation
356
+ if regexpMac64 .MatchString (s .Host ) {
357
+ // Prefer amd64 version if available
358
+ return true , 20
359
+ }
360
+ return regexpMac32 .MatchString (s .Host ), 10
361
+ }
362
+
363
+ return false , 0
364
+ }
365
+
302
366
func findTool (pack , name , version string , data index ) (tool , system ) {
303
367
var correctTool tool
304
368
correctTool .Version = "0.0"
@@ -325,11 +389,10 @@ func findTool(pack, name, version string, data index) (tool, system) {
325
389
326
390
// Find the url based on system
327
391
var correctSystem system
328
- maxSimilarity := 0.7
392
+ maxSimilarity := - 1
329
393
330
394
for _ , s := range correctTool .Systems {
331
- similarity := smetrics .Jaro (s .Host , systems [OS + Arch ])
332
- if similarity > maxSimilarity {
395
+ if comp , similarity := s .isCompatibleWith (OS , Arch ); comp && similarity > maxSimilarity {
333
396
correctSystem = s
334
397
maxSimilarity = similarity
335
398
}
0 commit comments