diff options
| author | ruki <[email protected]> | 2017-02-26 21:43:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-02-26 21:43:33 +0800 |
| commit | 6b1e196b60c27c0ecd96f3a309861f66d8f3ae12 (patch) | |
| tree | 40ad8ff909eda242a60fcb62abcb40093c876392 | |
| parent | f79a6bc543ebc146550e241713609924d3c20208 (diff) | |
build static library for rust
| -rw-r--r-- | tests/console_rust/src/main.rs | 2 | ||||
| -rw-r--r-- | tests/static_library_rust/src/interfaces.rs | 5 | ||||
| -rw-r--r-- | tests/static_library_rust/src/main.rs | 7 | ||||
| -rwxr-xr-x | tests/static_library_rust/xmake.lua | 48 | ||||
| -rwxr-xr-x | xmake/actions/build/kinds/binary.lua | 54 | ||||
| -rwxr-xr-x | xmake/actions/build/kinds/shared.lua | 54 | ||||
| -rwxr-xr-x | xmake/actions/build/kinds/static.lua | 54 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/tool/compiler.lua | 40 | ||||
| -rw-r--r-- | xmake/core/tool/compiler.lua | 14 | ||||
| -rwxr-xr-x | xmake/languages/rust/xmake.lua | 19 | ||||
| -rw-r--r-- | xmake/tools/cl.lua | 6 | ||||
| -rwxr-xr-x | xmake/tools/dmd.lua | 2 | ||||
| -rwxr-xr-x | xmake/tools/gcc.lua | 6 | ||||
| -rwxr-xr-x | xmake/tools/go.lua | 2 | ||||
| -rwxr-xr-x | xmake/tools/ml.lua | 6 | ||||
| -rwxr-xr-x | xmake/tools/rustc.lua | 42 | ||||
| -rw-r--r-- | xmake/tools/swiftc.lua | 6 |
18 files changed, 306 insertions, 63 deletions
diff --git a/tests/console_rust/src/main.rs b/tests/console_rust/src/main.rs index 8915e16ad..e8d2ae9b2 100644 --- a/tests/console_rust/src/main.rs +++ b/tests/console_rust/src/main.rs @@ -1,4 +1,4 @@ fn main() { - println!("hello xmake!!"); + println!("hello xmake!"); } diff --git a/tests/static_library_rust/src/interfaces.rs b/tests/static_library_rust/src/interfaces.rs new file mode 100644 index 000000000..277008ba4 --- /dev/null +++ b/tests/static_library_rust/src/interfaces.rs @@ -0,0 +1,5 @@ +pub fn add(a: i32, b: i32) -> i32 +{ + return a + b; +} + diff --git a/tests/static_library_rust/src/main.rs b/tests/static_library_rust/src/main.rs new file mode 100644 index 000000000..0cd4449d1 --- /dev/null +++ b/tests/static_library_rust/src/main.rs @@ -0,0 +1,7 @@ +extern crate interfaces; + +fn main() +{ + println!("hello xmake!"); + println!("add: {}", interfaces::add(1, 1)); +} diff --git a/tests/static_library_rust/xmake.lua b/tests/static_library_rust/xmake.lua new file mode 100755 index 000000000..0d2c3accc --- /dev/null +++ b/tests/static_library_rust/xmake.lua @@ -0,0 +1,48 @@ +-- the debug mode +if is_mode("debug") then + + -- enable the debug symbols + set_symbols("debug") + + -- disable optimization + set_optimize("none") +end + +-- the release mode +if is_mode("release") then + + -- set the symbols visibility: hidden + set_symbols("hidden") + + -- enable fastest optimization + set_optimize("fastest") + + -- strip all symbols + set_strip("all") +end + +-- add target +target("interfaces") + + -- set kind + set_kind("static") + + -- add files + add_files("src/interfaces.rs") + +-- add target +target("test") + + -- set kind + set_kind("binary") + + -- add deps + add_deps("interfaces") + + -- add files + add_files("src/main.rs") + + -- add link directory + add_linkdirs("$(buildir)") + + diff --git a/xmake/actions/build/kinds/binary.lua b/xmake/actions/build/kinds/binary.lua index 728babe5a..b7dd5f512 100755 --- a/xmake/actions/build/kinds/binary.lua +++ b/xmake/actions/build/kinds/binary.lua @@ -25,10 +25,11 @@ -- imports import("core.base.option") import("core.tool.linker") +import("core.tool.compiler") import("object") --- build binary target -function build(target, buildinfo) +-- build target from sources +function _build_from_objects(target, buildinfo) -- build objects object.build(target, buildinfo) @@ -69,3 +70,52 @@ function build(target, buildinfo) linker.link(objectfiles, targetfile, target) end +-- build target from sources +function _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + + -- the target file + local targetfile = target:targetfile() + + -- is verbose? + local verbose = option.get("verbose") + + -- trace percent into + cprintf("${green}[%02d%%]:${clear} ", (buildinfo.targetindex + 1) * 100 / buildinfo.targetcount) + if verbose then + cprint("${dim magenta}linking.$(mode) %s", path.filename(targetfile)) + else + cprint("${magenta}linking.$(mode) %s", path.filename(targetfile)) + end + + -- trace verbose info + if verbose then + print(compiler.buildcmd(sourcebatch.sourcefiles, targetfile, target, sourcekind)) + end + + -- build it + compiler.build(sourcebatch.sourcefiles, targetfile, target, sourcekind) +end + +-- build binary target +function build(target, buildinfo) + + -- only one source kind? + local kindcount = 0 + local sourcekind = nil + local sourcebatch = nil + for kind, batch in pairs(target:sourcebatches()) do + sourcekind = kind + sourcebatch = batch + kindcount = kindcount + 1 + if kindcount > 1 then + break + end + end + + -- build target + if kindcount == 1 and sourcekind and compiler.feature(sourcekind, "binary:sources") then + _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + else + _build_from_objects(target, buildinfo) + end +end diff --git a/xmake/actions/build/kinds/shared.lua b/xmake/actions/build/kinds/shared.lua index ebd54dec9..ab2cd40b0 100755 --- a/xmake/actions/build/kinds/shared.lua +++ b/xmake/actions/build/kinds/shared.lua @@ -25,10 +25,11 @@ -- imports import("core.base.option") import("core.tool.linker") +import("core.tool.compiler") import("object") --- build binary target -function build(target, buildinfo) +-- build target from objects +function _build_from_objects(target, buildinfo) -- build objects object.build(target, buildinfo) @@ -82,3 +83,52 @@ function build(target, buildinfo) linker.link(objectfiles, targetfile, target) end +-- build target from sources +function _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + + -- the target file + local targetfile = target:targetfile() + + -- is verbose? + local verbose = option.get("verbose") + + -- trace percent into + cprintf("${green}[%02d%%]:${clear} ", (buildinfo.targetindex + 1) * 100 / buildinfo.targetcount) + if verbose then + cprint("${dim magenta}linking.$(mode) %s", path.filename(targetfile)) + else + cprint("${magenta}linking.$(mode) %s", path.filename(targetfile)) + end + + -- trace verbose info + if verbose then + print(compiler.buildcmd(sourcebatch.sourcefiles, targetfile, target, sourcekind)) + end + + -- build it + compiler.build(sourcebatch.sourcefiles, targetfile, target, sourcekind) +end + +-- build shared target +function build(target, buildinfo) + + -- only one source kind? + local kindcount = 0 + local sourcekind = nil + local sourcebatch = nil + for kind, batch in pairs(target:sourcebatches()) do + sourcekind = kind + sourcebatch = batch + kindcount = kindcount + 1 + if kindcount > 1 then + break + end + end + + -- build target + if kindcount == 1 and sourcekind and compiler.feature(sourcekind, "shared:sources") then + _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + else + _build_from_objects(target, buildinfo) + end +end diff --git a/xmake/actions/build/kinds/static.lua b/xmake/actions/build/kinds/static.lua index 15b461c53..6dcacbd30 100755 --- a/xmake/actions/build/kinds/static.lua +++ b/xmake/actions/build/kinds/static.lua @@ -25,10 +25,11 @@ -- imports import("core.base.option") import("core.tool.linker") +import("core.tool.compiler") import("object") --- build binary target -function build(target, buildinfo) +-- build target from objects +function _build_from_objects(target, buildinfo) -- build objects object.build(target, buildinfo) @@ -82,3 +83,52 @@ function build(target, buildinfo) linker.link(objectfiles, targetfile, target) end +-- build target from sources +function _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + + -- the target file + local targetfile = target:targetfile() + + -- is verbose? + local verbose = option.get("verbose") + + -- trace percent into + cprintf("${green}[%02d%%]:${clear} ", (buildinfo.targetindex + 1) * 100 / buildinfo.targetcount) + if verbose then + cprint("${dim magenta}archiving.$(mode) %s", path.filename(targetfile)) + else + cprint("${magenta}archiving.$(mode) %s", path.filename(targetfile)) + end + + -- trace verbose info + if verbose then + print(compiler.buildcmd(sourcebatch.sourcefiles, targetfile, target, sourcekind)) + end + + -- build it + compiler.build(sourcebatch.sourcefiles, targetfile, target, sourcekind) +end + +-- build static target +function build(target, buildinfo) + + -- only one source kind? + local kindcount = 0 + local sourcekind = nil + local sourcebatch = nil + for kind, batch in pairs(target:sourcebatches()) do + sourcekind = kind + sourcebatch = batch + kindcount = kindcount + 1 + if kindcount > 1 then + break + end + end + + -- build target + if kindcount == 1 and sourcekind and compiler.feature(sourcekind, "static:sources") then + _build_from_sources(target, buildinfo, sourcebatch, sourcekind) + else + _build_from_objects(target, buildinfo) + end +end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 5f0a775ff..b78584b74 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -547,7 +547,7 @@ function target:sourcebatches() -- this batch support to compile multiple objects at the same time? local instance = compiler.load(sourcekind) - if instance and instance:feature("compile:multifiles") then + if instance and instance:feature("object:sources") then -- get the first source file local sourcefile = sourcebatch.sourcefiles[1] diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua index 7e93570b1..fb753aed3 100644 --- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua +++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua @@ -102,5 +102,45 @@ function sandbox_core_tool_compiler.compflags(sourcefiles, target, sourcekind) return instance:compflags(target) end +-- make command for building source file +function sandbox_core_tool_compiler.buildcmd(sourcefiles, targetfile, target, sourcekind) + + -- get source kind if only one source file + if not sourcekind and type(sourcefiles) == "string" then + sourcekind = language.sourcekind_of(sourcefiles) + end + + -- get the compiler instance + local instance, errors = compiler.load(sourcekind) + if not instance then + raise(errors) + end + + -- make command + return instance:buildcmd(sourcefiles, target:targetkind(), targetfile, target) +end + +-- build source files +function sandbox_core_tool_compiler.build(sourcefiles, targetfile, target, sourcekind) + + -- get source kind if only one source file + if not sourcekind and type(sourcefiles) == "string" then + sourcekind = language.sourcekind_of(sourcefiles) + end + + -- get the compiler instance + local instance, errors = compiler.load(sourcekind) + if not instance then + raise(errors) + end + + -- build it + local ok, errors = instance:build(sourcefiles, target:targetkind(), targetfile, target) + if not ok then + raise(errors) + end +end + + -- return module return sandbox_core_tool_compiler diff --git a/xmake/core/tool/compiler.lua b/xmake/core/tool/compiler.lua index b932a9ed3..98c258d6b 100644 --- a/xmake/core/tool/compiler.lua +++ b/xmake/core/tool/compiler.lua @@ -107,6 +107,20 @@ function compiler.load(sourcekind) return instance end +-- build the source files +function compiler:build(sourcefiles, targetkind, targetfile, target) + + -- get it + return sandbox.load(self:_tool().build, sourcefiles, targetkind, targetfile, (self:compflags(target)) .. " " .. (target:linkflags())) +end + +-- get the build command +function compiler:buildcmd(sourcefiles, targetkind, targetfile, target) + + -- get it + return self:_tool().buildcmd(sourcefiles, targetkind, targetfile, (self:compflags(target) .. " " .. (target:linkflags()))) +end + -- compile the source files function compiler:compile(sourcefiles, objectfile, incdepfile, target) diff --git a/xmake/languages/rust/xmake.lua b/xmake/languages/rust/xmake.lua index 9f42c5f64..acb0d8ff3 100755 --- a/xmake/languages/rust/xmake.lua +++ b/xmake/languages/rust/xmake.lua @@ -48,17 +48,10 @@ language("rust") { object = { - "config.includedirs" - , "target.symbols" + "target.symbols" , "target.warnings" , "target.optimize:check" , "target.vectorexts:check" - , "target.defines" - , "target.undefines" - , "target.includedirs" - , "platform.includedirs" - , "platform.defines" - , "platform.undefines" } , binary = { @@ -68,10 +61,6 @@ language("rust") , "target.symbols" , "option.linkdirs" , "platform.linkdirs" - , "config.links" - , "target.links" - , "option.links" - , "platform.links" } , shared = { @@ -81,10 +70,6 @@ language("rust") , "target.symbols" , "option.linkdirs" , "platform.linkdirs" - , "config.links" - , "target.links" - , "option.links" - , "platform.links" } , static = { @@ -104,9 +89,7 @@ language("rust") , {nil, "rc-sh", "kv", nil, "The Rust Shared Library Linker" } , { } - , {nil, "links", "kv", nil, "The Link Libraries" } , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } - , {nil, "includedirs","kv", nil, "The Include Search Directories" } } } diff --git a/xmake/tools/cl.lua b/xmake/tools/cl.lua index b61fa31db..b74f93ea3 100644 --- a/xmake/tools/cl.lua +++ b/xmake/tools/cl.lua @@ -82,7 +82,7 @@ function init(shellname) -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false } end @@ -297,7 +297,7 @@ end function compcmd(sourcefiles, objectfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file return _compcmd1(sourcefiles, objectfile, flags) @@ -307,7 +307,7 @@ end function compile(sourcefiles, objectfile, incdepfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file _compile1(sourcefiles, objectfile, incdepfile, flags) diff --git a/xmake/tools/dmd.lua b/xmake/tools/dmd.lua index fec9e9e70..844c94e92 100755 --- a/xmake/tools/dmd.lua +++ b/xmake/tools/dmd.lua @@ -50,7 +50,7 @@ function init(shellname, kind) -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false } end diff --git a/xmake/tools/gcc.lua b/xmake/tools/gcc.lua index 281064a2b..8b0795f9d 100755 --- a/xmake/tools/gcc.lua +++ b/xmake/tools/gcc.lua @@ -69,7 +69,7 @@ function init(shellname, kind) -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false } end @@ -357,7 +357,7 @@ end function compcmd(sourcefiles, objectfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file return _compcmd1(sourcefiles, objectfile, flags) @@ -367,7 +367,7 @@ end function compile(sourcefiles, objectfile, incdepfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file _compile1(sourcefiles, objectfile, incdepfile, flags) diff --git a/xmake/tools/go.lua b/xmake/tools/go.lua index 4d25792a3..0d9bd00f4 100755 --- a/xmake/tools/go.lua +++ b/xmake/tools/go.lua @@ -47,7 +47,7 @@ function init(shellname, kind) -- init features _g.features = { - ["compile:multifiles"] = true + ["object:sources"] = true } end diff --git a/xmake/tools/ml.lua b/xmake/tools/ml.lua index 95cff1d95..705c05dbd 100755 --- a/xmake/tools/ml.lua +++ b/xmake/tools/ml.lua @@ -58,7 +58,7 @@ function init(shellname) -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false } end @@ -128,7 +128,7 @@ end function compcmd(sourcefiles, objectfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file return _compcmd1(sourcefiles, objectfile, flags) @@ -138,7 +138,7 @@ end function compile(sourcefiles, objectfile, incdepfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file _compile1(sourcefiles, objectfile, incdepfile, flags) diff --git a/xmake/tools/rustc.lua b/xmake/tools/rustc.lua index effda258f..5d008d7a3 100755 --- a/xmake/tools/rustc.lua +++ b/xmake/tools/rustc.lua @@ -43,10 +43,20 @@ function init(shellname, kind) -- init shflags _g.shflags = { "--crate-type=dylib" } + -- init ldflags + _g.ldflags = { "--crate-type=bin" } + + -- init the file formats + _g.formats = {} + _g.formats.static = {"lib", ".rlib"} + -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false -- compile multiple source filess to the single object + , ["binary:sources"] = true -- build multiple source files to the binary target file + , ["static:sources"] = true -- build multiple source files to the static target file + , ["shared:sources"] = true -- build multiple source files to the shared target file } end @@ -89,42 +99,28 @@ function nf_symbol(level) return maps[level] or "" end --- make the includedir flag -function nf_includedir(dir) - - -- make it - return "" -end - --- make the link flag -function nf_link(lib) - - -- make it - return "" -end - -- make the linkdir flag function nf_linkdir(dir) -- make it - return "" + return "-L " .. dir end --- make the link command -function linkcmd(objectfiles, targetkind, targetfile, flags) +-- make the build command +function buildcmd(sourcefiles, targetkind, targetfile, flags) -- make it - return format("%s %s -o %s %s", _g.shellname, flags, targetfile, objectfiles) + return format("%s %s -o %s %s", _g.shellname, flags, targetfile, table.concat(sourcefiles, " ")) end --- link the target file -function link(objectfiles, targetkind, targetfile, flags) +-- build the target file +function build(objectfiles, targetkind, targetfile, flags) -- ensure the target directory os.mkdir(path.directory(targetfile)) - -- link it - os.run(linkcmd(objectfiles, targetkind, targetfile, flags)) + -- build it + os.run(buildcmd(objectfiles, targetkind, targetfile, flags)) end -- make the complie command diff --git a/xmake/tools/swiftc.lua b/xmake/tools/swiftc.lua index f006b4c14..d320fb2e9 100644 --- a/xmake/tools/swiftc.lua +++ b/xmake/tools/swiftc.lua @@ -65,7 +65,7 @@ function init(shellname, kind) -- init features _g.features = { - ["compile:multifiles"] = false + ["object:sources"] = false } end @@ -250,7 +250,7 @@ end function compcmd(sourcefiles, objectfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file return _compcmd1(sourcefiles, objectfile, flags) @@ -260,7 +260,7 @@ end function compile(sourcefiles, objectfile, incdepfile, flags) -- only support single source file now - assert(type(sourcefiles) ~= "table", "'compile:multifiles' not support!") + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") -- for only single source file _compile1(sourcefiles, objectfile, incdepfile, flags) |
