diff options
| author | ruki <[email protected]> | 2015-06-02 09:35:37 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-06-02 09:35:37 +0800 |
| commit | f6177ffb405aa474e5d8e8af787778bd8ed8439c (patch) | |
| tree | c4520100c7b617a4ef54f137f2f8acb13d98f5ab | |
| parent | 52b7de2f4d73e08aba2fea79aa04bb06a9af449d (diff) | |
add linker and compiler common flags
| -rw-r--r-- | xmake/scripts/base/main.lua | 16 | ||||
| -rw-r--r-- | xmake/scripts/base/makefile.lua | 81 | ||||
| -rw-r--r-- | xmake/scripts/base/project.lua | 1 | ||||
| -rw-r--r-- | xmake/scripts/compiler/_clang.lua | 26 | ||||
| -rw-r--r-- | xmake/scripts/compiler/compiler.lua | 54 | ||||
| -rw-r--r-- | xmake/scripts/linker/_ar.lua | 9 | ||||
| -rw-r--r-- | xmake/scripts/linker/_clang.lua | 26 | ||||
| -rw-r--r-- | xmake/scripts/linker/linker.lua | 52 | ||||
| -rw-r--r-- | xmake/scripts/platform/macosx/_macosx.lua | 2 | ||||
| -rw-r--r-- | xmake/scripts/platform/platform.lua | 59 |
10 files changed, 182 insertions, 144 deletions
diff --git a/xmake/scripts/base/main.lua b/xmake/scripts/base/main.lua index 2d4e0ea10..28e9ca14b 100644 --- a/xmake/scripts/base/main.lua +++ b/xmake/scripts/base/main.lua @@ -197,13 +197,21 @@ local menu = , {nil, "cxxflags", "kv", nil, "The C++ Compiler Flags" } , {} - , {nil, "ld", "kv", nil, "The Linker" } - , {nil, "ldflags", "kv", nil, "The Linker Flags" } - - , {} , {nil, "as", "kv", nil, "The Assembler" } , {nil, "asflags", "kv", nil, "The Assembler Flags" } + , {} + , {nil, "ld", "kv", nil, "The Linker" } + , {nil, "ldflags", "kv", nil, "The Binary Linker Flags" } + + , {} + , {nil, "ar", "kv", nil, "The Static Library Linker" } + , {nil, "arflags", "kv", nil, "The Static Library Linker Flags" } + + , {} + , {nil, "sh", "kv", nil, "The Shared Library Linker" } + , {nil, "shflags", "kv", nil, "The Shared Library Linker Flags" } + -- the options for all platforms , function () return platform.menu("config") end diff --git a/xmake/scripts/base/makefile.lua b/xmake/scripts/base/makefile.lua index 7d873665b..808ff819c 100644 --- a/xmake/scripts/base/makefile.lua +++ b/xmake/scripts/base/makefile.lua @@ -32,67 +32,6 @@ local config = require("base/config") local project = require("base/project") local platform = require("platform/platform") --- get the linker from the given kind -function makefile._linker(kind) - - -- init linkers - makefile._LINKERS = makefile._LINKERS or {} - local linkers = makefile._LINKERS - - -- return it directly if the linker has been cached - local l = linkers[kind] - if l then return l end - - -- get compiler from the current platform - l = platform.linker(kind) - - -- cache this compiler - linkers[kind] = l - - -- ok - return l -end - --- get the compiler from the given source file -function makefile._compiler(srcfile) - - -- get the source file type - local filetype = path.extension(srcfile) - if not filetype then - return nil, string.format("cannot get the source file type: %s", srcfile) - end - - -- get the lower file type - filetype = filetype:lower() - - -- get the compiler name - local name = nil - if filetype == ".c" then name = "cc" - elseif filetype == ".cpp" or filetype == ".cc" then name = "cxx" - elseif filetype == ".m" then name = "mm" - elseif filetype == ".mm" then name = "mxx" - elseif filetype == ".s" or filetype == ".asm" then name = "as" - else return nil, string.format("unknown file type: %s", filetype) - end - - -- init compilers - makefile._COMPILERS = makefile._COMPILERS or {} - local compilers = makefile._COMPILERS - - -- return it directly if the compiler has been cached - local c = compilers[name] - if c then return c end - - -- get compiler from the current platform - c = platform.compiler(name) - - -- cache this compiler - compilers[name] = c - - -- ok - return c -end - -- get the source files from the given target function makefile._srcfiles(target) @@ -189,11 +128,11 @@ function makefile._make_object(file, target, srcfile, objfile) -- check assert(file and target and srcfile and objfile) - -- get the compiler - local c, errors = makefile._compiler(srcfile); + -- get the compiler and filetype + local c, filetype = platform.compiler(srcfile); if not c then -- error - utils.error(errors) + utils.error("unknown source file: %s", srcfile) return false end @@ -206,7 +145,7 @@ function makefile._make_object(file, target, srcfile, objfile) -- make body file:write(string.format("\t@echo [%s]: compiling %s\n", config.get("mode"), srcfile)) file:write(string.format("\t@xmake l mkdir %s\n", path.directory(objfile))) - file:write(string.format("\t@%s\n", c:make(srcfile, objfile, ""))) + file:write(string.format("\t@%s\n", c:make(filetype, srcfile, objfile, ""))) -- make tail file:write("\n") @@ -254,17 +193,9 @@ function makefile._make_target(file, name, target) assert(targetfile) -- the linker - local l = makefile._linker(target.kind) + local l = platform.linker(target.kind) assert(l) - -- the make command - local make_command = l["make_" .. target.kind] - if not make_command then - -- error - utils.error("the target kind: %s in not surpported now!", target.kind) - return false - end - -- make head file:write(string.format("%s:", name)) @@ -287,7 +218,7 @@ function makefile._make_target(file, name, target) -- make body file:write(string.format("\t@echo [%s]: linking %s\n", config.get("mode"), path.filename(targetfile))) file:write(string.format("\t@xmake l mkdir %s\n", path.directory(targetfile))) - file:write(string.format("\t@%s\n", make_command(l, objfiles, targetfile, ""))) + file:write(string.format("\t@%s\n", l:make(target.kind, objfiles, targetfile, ""))) -- make tail file:write("\n") diff --git a/xmake/scripts/base/project.lua b/xmake/scripts/base/project.lua index 0b7783e2c..1ed00372b 100644 --- a/xmake/scripts/base/project.lua +++ b/xmake/scripts/base/project.lua @@ -163,6 +163,7 @@ function project.loadxproj(file) , "mxflags" , "mxxflags" , "ldflags" + , "shflags" , "defines"} -- init filter diff --git a/xmake/scripts/compiler/_clang.lua b/xmake/scripts/compiler/_clang.lua index 6bfa83d69..b275f23b8 100644 --- a/xmake/scripts/compiler/_clang.lua +++ b/xmake/scripts/compiler/_clang.lua @@ -31,6 +31,32 @@ local config = require("base/config") -- init the compiler function _clang._init(configs) + -- the architecture + local arch = config.get("arch") + assert(arch) + + -- init flags for architecture + local flags_arch = "" + if arch == "x86" then flags_arch = "-m32" + elseif arch == "x64" then flags_arch = "-m64" + else flags_arch = "-arch " .. arch + end + + -- init cflags + configs.cflags = flags_arch + + -- init cxxflags + configs.cxxflags = flags_arch + + -- init mflags + configs.mflags = flags_arch + + -- init mxxflags + configs.mxxflags = flags_arch + + -- init asflags + configs.asflags = flags_arch + end -- make the compiler command diff --git a/xmake/scripts/compiler/compiler.lua b/xmake/scripts/compiler/compiler.lua index 9a6192b26..7b52bd951 100644 --- a/xmake/scripts/compiler/compiler.lua +++ b/xmake/scripts/compiler/compiler.lua @@ -24,6 +24,7 @@ local compiler = compiler or {} -- load modules +local path = require("base/path") local utils = require("base/utils") local string = require("base/string") local config = require("base/config") @@ -58,17 +59,35 @@ function compiler._mapflags(self, flags) end -- make the compile command -function compiler._make(self, srcfile, objfile, flags) +function compiler._make(self, filetype, srcfile, objfile, flags) -- check - assert(self); + assert(self and filetype); -- the configure local configs = self._CONFIGS assert(configs) + -- get the common flags string for the current compiler + local flags_common = nil + if filetype == "cc" then flags_common = configs.cflags + elseif filetype == "cxx" then flags_common = configs.cxxflags + elseif filetype == "mm" then flags_common = configs.mflags + elseif filetype == "mxx" then flags_common = configs.mxxflags + elseif filetype == "as" then flags_common = configs.asflags + end + flags_common = flags_common or "" + + -- map flags + flags = compiler._mapflags(self, utils.wrap(flags)) + assert(flags) + + -- make flags string + flags = table.concat(flags) + assert(flags) + -- get it - return self._make(configs, srcfile, objfile, compiler._mapflags(self, flags)) + return self._make(configs, srcfile, objfile, flags_common .. " " .. flags) end -- load the given compiler @@ -85,7 +104,9 @@ function compiler.load(name) -- cl.exe? elseif name:find("cl.exe", 1, true) then module = "msvc" -- icc? - elseif name:find("icc", 1, true) then module = "intel" + elseif name:find("icc", 1, true) then module = "icc" + -- as? + elseif name:find("as", 1, true) then module = "as" -- unknown? else -- error @@ -120,6 +141,31 @@ function compiler.load(name) -- ok? return c end + +-- get the source file type +function compiler.filetype(srcfile) + + -- get the source file type + local filetype = path.extension(srcfile) + if not filetype then + return nil + end + + -- get the lower file type + filetype = filetype:lower() + + -- get the compiler name + local name = nil + if filetype == ".c" then name = "cc" + elseif filetype == ".cpp" or filetype == ".cc" then name = "cxx" + elseif filetype == ".m" then name = "mm" + elseif filetype == ".mm" then name = "mxx" + elseif filetype == ".s" or filetype == ".asm" then name = "as" + end + + -- ok + return name +end -- return module: compiler return compiler diff --git a/xmake/scripts/linker/_ar.lua b/xmake/scripts/linker/_ar.lua index 39aeadc56..c65fa41f2 100644 --- a/xmake/scripts/linker/_ar.lua +++ b/xmake/scripts/linker/_ar.lua @@ -31,13 +31,16 @@ local config = require("base/config") -- init the linker function _ar._init(configs) + -- init arflags + configs.arflags = "-crs" + end --- make the static library command -function _ar._make_static(configs, objfiles, targetfile, flags) +-- make the command +function _ar._make(configs, objfiles, targetfile, flags) -- make it - return string.format("%s -crs %s %s %s", configs.name, flags, targetfile, table.concat(objfiles)) + return string.format("%s %s %s %s", configs.name, flags, targetfile, objfiles) end -- map gcc flag to the current linker flag diff --git a/xmake/scripts/linker/_clang.lua b/xmake/scripts/linker/_clang.lua index 062bbc455..0f9ce3fff 100644 --- a/xmake/scripts/linker/_clang.lua +++ b/xmake/scripts/linker/_clang.lua @@ -31,20 +31,30 @@ local config = require("base/config") -- init the linker function _clang._init(configs) -end + -- the architecture + local arch = config.get("arch") + assert(arch) --- make the binary command -function _clang._make_binary(configs, objfiles, targetfile, flags) + -- init flags for architecture + local flags_arch = "" + if arch == "x86" then flags_arch = "-m32" + elseif arch == "x64" then flags_arch = "-m64" + else flags_arch = "-arch " .. arch + end + + -- init ldflags + configs.ldflags = flags_arch + + -- init shflags + configs.shflags = flags_arch .. " -dynamiclib" - -- make it - return string.format("%s -o%s %s %s", configs.name, table.concat(objfiles), targetfile, flags) end --- make the shared library command -function _clang._make_shared(configs, objfiles, targetfile, flags) +-- make the command +function _clang._make(configs, objfiles, targetfile, flags) -- make it - return string.format("%s -dynamiclib -o%s %s %s", configs.name, table.concat(objfiles), targetfile, flags) + return string.format("%s %s -o%s %s", configs.name, flags, objfiles, targetfile) end -- map gcc flag to the current linker flag diff --git a/xmake/scripts/linker/linker.lua b/xmake/scripts/linker/linker.lua index 5f909857c..d2e4ec9ec 100644 --- a/xmake/scripts/linker/linker.lua +++ b/xmake/scripts/linker/linker.lua @@ -57,46 +57,34 @@ function linker._mapflags(self, flags) return flags_mapped end --- make the binary command -function linker._make_binary(self, objfiles, targetfile, flags) +-- make the command +function linker._make(self, kind, objfiles, targetfile, flags) -- check - assert(self and self._make_binary) + assert(self and self._make) -- the configure local configs = self._CONFIGS assert(configs) - -- make it - return self._make_binary(configs, objfiles, targetfile, linker._mapflags(self, flags)) -end - --- make the static library command -function linker._make_static(self, objfiles, targetfile, flags) - - -- check - assert(self and self._make_static) - - -- the configure - local configs = self._CONFIGS - assert(configs) - - -- make it - return self._make_static(configs, objfiles, targetfile, linker._mapflags(self, flags)) -end - --- make the shared library command -function linker._make_shared(self, objfiles, targetfile, flags) - - -- check - assert(self and self._make_shared) + -- get the common flags string for the current linker + local flags_common = nil + if kind == "binary" then flags_common = configs.ldflags + elseif kind == "static" then flags_common = configs.arflags + elseif kind == "shared" then flags_common = configs.shflags + end + flags_common = flags_common or "" - -- the configure - local configs = self._CONFIGS - assert(configs) + -- map flags + flags = linker._mapflags(self, utils.wrap(flags)) + assert(flags) + + -- make flags string + flags = table.concat(flags) + assert(flags) -- make it - return self._make_shared(configs, objfiles, targetfile, linker._mapflags(self, flags)) + return self._make(configs, table.concat(objfiles), targetfile, flags_common .. " " .. flags) end -- load the given linker @@ -143,9 +131,7 @@ function linker.load(name) l._init(configs) -- init interfaces - l["make_binary"] = linker._make_binary - l["make_static"] = linker._make_static - l["make_shared"] = linker._make_shared + l["make"] = linker._make -- ok? return l diff --git a/xmake/scripts/platform/macosx/_macosx.lua b/xmake/scripts/platform/macosx/_macosx.lua index b23204224..532a540df 100644 --- a/xmake/scripts/platform/macosx/_macosx.lua +++ b/xmake/scripts/platform/macosx/_macosx.lua @@ -55,7 +55,7 @@ function _macosx.make(configs) configs.linker = {} configs.linker.binary = config.get("ld") or "xcrun -sdk macosx clang++" configs.linker.static = config.get("ar") or "xcrun -sdk macosx ar" - configs.linker.shared = config.get("ld") or "xcrun -sdk macosx clang++" + configs.linker.shared = config.get("sh") or "xcrun -sdk macosx clang++" -- init xcode sdk directory configs.xcode_sdkdir = config.get("xcode_dir") .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. config.get("xcode_sdkver") .. ".sdk" diff --git a/xmake/scripts/platform/platform.lua b/xmake/scripts/platform/platform.lua index 57f50576c..ec21f0300 100644 --- a/xmake/scripts/platform/platform.lua +++ b/xmake/scripts/platform/platform.lua @@ -105,37 +105,64 @@ function platform.filename(name, kind) end -- get the linker from the given name -function platform.linker(name) +function platform.linker(kind) -- check - assert(name) + assert(kind) + + -- init linkers + platform._LINKERS = platform._LINKERS or {} + local linkers = platform._LINKERS + + -- return it directly if the linker has been cached + local l = linkers[kind] + if l then return l end -- get linker local c = platform.get("linker") assert(c) -- load linker - c = c[name] - if c then - return linker.load(c) - end + c = c[kind] + if c then return linker.load(c) end + + -- cache this linker + linkers[kind] = l + + -- ok + return l end --- get the compiler from the given name -function platform.compiler(name) +-- get the compiler from the given source file +function platform.compiler(srcfile) - -- check - assert(name) + -- get the source file type + local filetype = compiler.filetype(srcfile) + if not filetype then + return + end + + -- init compilers + platform._COMPILERS = platform._COMPILERS or {} + local compilers = platform._COMPILERS - -- get compiler - local c = platform.get("compiler") + -- return it directly if the compiler has been cached + local c = compilers[filetype] + if c then return c, filetype end + + -- get compiler from the current platform + c = platform.get("compiler") assert(c) -- load compiler - c = c[name] - if c then - return compiler.load(c) - end + c = c[filetype] + if c then c = compiler.load(c) end + + -- cache this compiler + compilers[filetype] = c + + -- ok + return c, filetype end -- dump the platform configure |
