summaryrefslogtreecommitdiff
path: root/xmake/modules/core/tools/go.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-05 22:36:39 +0800
committerruki <[email protected]>2025-12-05 22:36:39 +0800
commite688a7bd853c49ec10aa8656e704c2115ea5e14c (patch)
treeea514a602e65bec18b18e4037df605357bd95443 /xmake/modules/core/tools/go.lua
parent6d803e27d205bc7282597ac4db2a105f2a79d5ad (diff)
rewrite golang support
Diffstat (limited to 'xmake/modules/core/tools/go.lua')
-rw-r--r--xmake/modules/core/tools/go.lua93
1 files changed, 52 insertions, 41 deletions
diff --git a/xmake/modules/core/tools/go.lua b/xmake/modules/core/tools/go.lua
index 70e667ad9..90431320c 100644
--- a/xmake/modules/core/tools/go.lua
+++ b/xmake/modules/core/tools/go.lua
@@ -18,6 +18,7 @@
-- @file go.lua
--
+
-- imports
import("core.base.option")
import("core.project.config")
@@ -26,45 +27,39 @@ import("core.language.language")
-- init it
function init(self)
- self:set("gcarflags", "grc")
+ self:set("gcarflags", "")
end
-- make the optimize flag
function nf_optimize(self, level)
- -- only for source kind
- local kind = self:kind()
- if language.sourcekinds()[kind] then
- local maps = {
- none = "-N"
- }
- return maps[level]
- end
+ -- Go build flags for optimization
+ local maps = {
+ none = "-gcflags=-N"
+ }
+ return maps[level]
end
-- make the symbol flag
function nf_symbol(self, level, opt)
- local targetkind = opt.targetkind
- if targetkind ~= "object" then
- return
- end
- local maps = {
- debug = "-E"
- }
- return maps[level]
+ -- Go doesn't use separate symbol flags like C/C++
+ return
end
-- make the strip flag
function nf_strip(self, level)
+ -- Go uses -ldflags="-s -w" for stripping
+ -- -ldflags needs to be passed as separate arguments: -ldflags and the value
local maps = {
- debug = "-s"
- , all = "-s"
+ debug = {"-ldflags", "-s"}
+ , all = {"-ldflags", "-s -w"}
}
return maps[level]
end
-- make the includedir flag
function nf_includedir(self, dir)
- return {"-I", dir}
+ -- Go doesn't use -I flags, but we can use build tags or build constraints
+ return
end
-- make the sysincludedir flag
@@ -74,34 +69,50 @@ end
-- make the linkdir flag
function nf_linkdir(self, dir)
- return {"-L", dir}
+ -- Go uses -L flag for linker search paths via -ldflags
+ -- -ldflags needs to be passed as separate arguments: -ldflags and the value
+ -- Note: -L must be inside the -ldflags value, not as a separate argument
+ return {"-ldflags", "-L " .. dir}
end
--- make the link arguments list
-function linkargv(self, objectfiles, targetkind, targetfile, flags)
+-- make the build arguments list
+-- Modern Go uses "go build" command for both compilation and linking
+function buildargv(self, sourcefiles, targetkind, targetfile, flags)
+ local argv = {"build"}
+
+ -- add build flags
+ if flags then
+ table.join2(argv, flags)
+ end
+
+ -- set output file
+ table.insert(argv, "-o")
+ table.insert(argv, targetfile)
+
+ -- for static library, use buildmode=archive
if targetkind == "static" then
- return self:program(), table.join("tool", "pack", flags, targetfile, objectfiles)
+ table.insert(argv, "-buildmode=archive")
+ end
+
+ -- add source files (Go supports building from source file list)
+ -- The caller should change to the source directory before calling build
+ if sourcefiles and #sourcefiles > 0 then
+ -- convert source files to relative paths from current directory
+ -- or use absolute paths if needed
+ for _, sourcefile in ipairs(sourcefiles) do
+ table.insert(argv, sourcefile)
+ end
else
- return self:program(), table.join("tool", "link", flags, "-o", targetfile, objectfiles)
+ -- fallback to current package if no source files
+ table.insert(argv, ".")
end
+
+ return self:program(), argv
end
--- link the target file
-function link(self, objectfiles, targetkind, targetfile, flags)
+-- build the target file (compile and link in one step)
+function build(self, sourcefiles, targetkind, targetfile, flags)
os.mkdir(path.directory(targetfile))
- local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags)
- os.runv(program, argv, {envs = self:runenvs()})
-end
-
--- make the compile arguments list
-function compargv(self, sourcefiles, objectfile, flags)
- return self:program(), table.join("tool", "compile", flags, "-o", objectfile, sourcefiles)
-end
-
--- compile the source file
-function compile(self, sourcefiles, objectfile, dependinfo, flags)
- os.mkdir(path.directory(objectfile))
- local program, argv = compargv(self, sourcefiles, objectfile, flags)
+ local program, argv = buildargv(self, sourcefiles, targetkind, targetfile, flags)
os.runv(program, argv, {envs = self:runenvs()})
end
-