summaryrefslogtreecommitdiff
path: root/xmake/modules/private/tools/go/goenv.lua
blob: 1a448e6f39e102d273c8fad80efe8238c523eb04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        goenv.lua
--

-- imports
import("lib.detect.find_tool")

-- Map xmake platform to Go OS
function GOOS(plat)
    local goos_map = {
        windows = "windows",
        mingw = "windows",
        msys = "windows",
        cygwin = "windows",
        linux = "linux",
        macosx = "darwin",
        android = "android",
        ios = "ios",
        freebsd = "freebsd",
        netbsd = "netbsd",
        openbsd = "openbsd",
        dragonfly = "dragonfly",
        solaris = "solaris",
        aix = "aix",
        plan9 = "plan9"
    }
    return goos_map[plat]
end

-- Map xmake architecture to Go architecture
function GOARCH(arch)
    local goarch_map = {
        x86 = "386",
        i386 = "386",
        x64 = "amd64",
        x86_64 = "amd64",
        amd64 = "amd64",
        arm = "arm",
        armv7 = "arm",
        armv7s = "arm",
        arm64 = "arm64",
        aarch64 = "arm64",
        mips = "mips",
        mips64 = "mips64",
        mips64le = "mips64le",
        mipsle = "mipsle",
        ppc = "ppc",
        ppc64 = "ppc64",
        ppc64le = "ppc64le",
        riscv64 = "riscv64",
        s390x = "s390x",
        wasm = "wasm"
    }
    
    -- try direct match first
    if goarch_map[arch] then
        return goarch_map[arch]
    end
    
    -- try pattern matching for arm variants
    if arch:match("^arm") then
        if arch:match("64") or arch:match("aarch64") then
            return "arm64"
        else
            return "arm"
        end
    end
    
    -- try pattern matching for x86 variants
    if arch:match("^x86") or arch:match("^i386") or arch:match("^i686") then
        return "386"
    end
    
    if arch:match("^x64") or arch:match("^amd64") or arch:match("^x86_64") then
        return "amd64"
    end
    
    return nil
end

-- Get GOROOT from Go installation
function GOROOT(toolchain)
    local go = find_tool("go")
    if go then
        local gorootdir = try { 
            function() 
                return os.iorunv(go.program, {"env", "GOROOT"}, {envs = toolchain and toolchain:get("runenvs") or nil}) 
            end 
        }
        if gorootdir then
            return gorootdir:trim()
        end
    end
end