summaryrefslogtreecommitdiff
path: root/xmake/scripts/base/compiler.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2015-06-11 17:41:29 +0800
committerruki <[email protected]>2015-06-11 17:45:48 +0800
commita731ed4d6ef2c3936d3af791e3d228fa6775eb35 (patch)
tree0e1ba672990df66eac4eafc7a3405d6d68ae6169 /xmake/scripts/base/compiler.lua
parent1e8ddd3262352cc199b51493501a4ce5f48e69d2 (diff)
add platform configure to compiler and linker
Diffstat (limited to 'xmake/scripts/base/compiler.lua')
-rw-r--r--xmake/scripts/base/compiler.lua322
1 files changed, 180 insertions, 142 deletions
diff --git a/xmake/scripts/base/compiler.lua b/xmake/scripts/base/compiler.lua
index 23378f061..d9c3a7c5e 100644
--- a/xmake/scripts/base/compiler.lua
+++ b/xmake/scripts/base/compiler.lua
@@ -111,112 +111,65 @@ function compiler._getflags(module, names, flags)
return flags_mapped
end
--- get the flag names from the given compiler name
-function compiler._flagnames(name)
+-- add flags from the compiler
+function compiler._addflags_from_compiler(module, flags, flagnames)
-- check
- assert(name)
+ assert(module and flags and flagnames)
- -- the flag names
- local flagnames = nil
- if name == "cc" then flagnames = { "cxflags", "cflags" }
- elseif name == "cxx" then flagnames = { "cxflags", "cxxflags" }
- elseif name == "mm" then flagnames = { "mxflags", "mflags" }
- elseif name == "mxx" then flagnames = { "mxflags", "mxxflags" }
- elseif name == "as" then flagnames = { "asflags" }
- -- error
- utils.error("unknown compiler: %s", name)
- return
+ -- done
+ for _, flagname in ipairs(flagnames) do
+ table.join2(flags, module, module[flagname])
end
-
- -- ok
- return flagnames
end
--- get the compiler kind from the source file type
-function compiler._kind(srcfile)
+-- add flags from the configure
+function compiler._addflags_from_config(module, flags, flagnames)
- -- 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()
+ -- check
+ assert(module and flags and flagnames)
- -- get the compiler kind
- local kind = nil
- if filetype == ".c" then kind = "cc"
- elseif filetype == ".cpp" or filetype == ".cc" then kind = "cxx"
- elseif filetype == ".m" then kind = "mm"
- elseif filetype == ".mm" then kind = "mxx"
- elseif filetype == ".s" or filetype == ".asm" then kind = "as"
+ -- done
+ for _, flagname in ipairs(flagnames) do
+ table.join2(flags, config.get(flagname))
end
-
- -- ok
- return kind
end
-
--- get the compiler from the given source file
-function compiler.get(srcfile)
- -- get the compiler kind
- local kind = compiler._kind(srcfile)
- if not kind then
- return
- end
+-- add flags from the platform
+function compiler._addflags_from_platform(module, flags, flagnames)
- -- get compiler from the source file type
- local module = tools.get(kind)
- if module then
-
- -- invalid compiler
- if not module.command_compile then
- return
- end
+ -- check
+ assert(module and flags and flagnames)
- -- save kind
- module._KIND = kind
+ -- done
+ for _, flagname in ipairs(flagnames) do
+ table.join2(flags, compiler._mapflags(module, platform.get(flagname)))
end
-
- -- ok?
- return module
end
--- make the compile command
-function compiler.make(module, target, srcfile, objfile)
+-- add flags from the target
+function compiler._addflags_from_target(module, flags, flagnames, target)
-- check
- assert(module and target)
-
- -- the flag names
- local flagnames = compiler._flagnames(module._KIND)
- assert(flagnames)
-
- -- append the common flags from the current compiler
- local flags = {}
- for _, flagname in ipairs(flagnames) do
- table.join2(flags, module[flagname])
- end
+ assert(module and flags and flagnames and target)
- -- append the target flags from the current project
+ -- add the target flags from the current project
for _, flagname in ipairs(flagnames) do
table.join2(flags, compiler._mapflags(module, target[flagname]))
end
- -- append the symbols flags from the current project
+ -- add the symbols flags from the current project
table.join2(flags, compiler._getflags(module, target.symbols, { debug = "-g"
, hidden = "-fvisibility=hidden"
}))
- -- append the warning flags from the current project
+ -- add the warning flags from the current project
table.join2(flags, compiler._getflags(module, target.warnings, { none = "-w"
, all = "-Wall"
, error = "-Werror"
}))
- -- append the optimize flags from the current project
+ -- add the optimize flags from the current project
table.join2(flags, compiler._getflags(module, target.optimize, { none = "-O0"
, fast = "-O1"
, faster = "-O2"
@@ -225,7 +178,7 @@ function compiler.make(module, target, srcfile, objfile)
, aggressive = "-Ofast"
}))
- -- append the vector extensions flags from the current project
+ -- add the vector extensions flags from the current project
table.join2(flags, compiler._getflags(module, target.vectorexts, { mmx = "-mmmx"
, sse = "-msse"
, sse2 = "-msse2"
@@ -236,7 +189,7 @@ function compiler.make(module, target, srcfile, objfile)
, neon = "-mfpu=neon"
}))
- -- append the language flags from the current project
+ -- add the language flags from the current project
table.join2(flags, compiler._getflags(module, target.language, { ansi = "-ansi"
, c89 = "-std=c89"
, gnu89 = "-std=gnu89"
@@ -251,14 +204,14 @@ function compiler.make(module, target, srcfile, objfile)
}))
- -- append the includedirs flags from the current project
+ -- add the includedirs flags from the current project
if module.flag_includedir then
for _, includedir in ipairs(utils.wrap(target.includedirs)) do
table.join2(flags, module:flag_includedir(includedir))
end
end
- -- append the defines flags from the current project
+ -- add the defines flags from the current project
if module.flag_define then
for _, define in ipairs(utils.wrap(target.defines)) do
table.join2(flags, module:flag_define(define))
@@ -281,19 +234,19 @@ function compiler.make(module, target, srcfile, objfile)
if config.get(name) then opt = config.get("__" .. name) end
if nil ~= opt then
- -- append the flags from the option
+ -- add the flags from the option
for _, flagname in ipairs(flagnames) do
table.join2(flags, compiler._mapflags(module, opt[flagname]))
end
- -- append the includedirs flags from the option
+ -- add the includedirs flags from the option
if module.flag_includedir then
for _, includedir in ipairs(utils.wrap(opt.includedirs)) do
table.join2(flags, module:flag_includedir(includedir))
end
end
- -- append the defines flags from the option
+ -- add the defines flags from the option
if module.flag_define then
local defines = {}
@@ -305,7 +258,7 @@ function compiler.make(module, target, srcfile, objfile)
end
end
- -- append the undefines flags from the option
+ -- add the undefines flags from the option
if module.flag_undefine then
local undefines = {}
@@ -319,12 +272,139 @@ function compiler.make(module, target, srcfile, objfile)
end
end
end
+end
- -- append the flags from the configure
+-- add flags from the option
+function compiler._addflags_from_option(module, flags, flagnames, opt)
+
+ -- check
+ assert(module and flags and flagnames and opt)
+
+ -- append the option flags
for _, flagname in ipairs(flagnames) do
- table.join2(flags, compiler._mapflags(module, config.get(flagname)))
+ table.join2(flags, compiler._mapflags(module, opt[flagname]))
end
+ -- append the defines flags
+ if opt.defines and module.flag_define then
+ local defines = utils.wrap(opt.defines)
+ for _, define in ipairs(defines) do
+ table.join2(flags, module:flag_define(define))
+ end
+ end
+
+ -- append the undefines flags
+ if opt.undefines and module.flag_undefine then
+ local undefines = utils.wrap(opt.undefines)
+ for _, undefine in ipairs(undefines) do
+ table.join2(flags, module:flag_undefine(undefine))
+ end
+ end
+
+ -- append the includedirs flags
+ if opt.includedirs and module.flag_includedir then
+ for _, includedir in ipairs(utils.wrap(opt.includedirs)) do
+ table.join2(flags, module:flag_includedir(includedir))
+ end
+ end
+end
+
+-- get the flag names from the given compiler name
+function compiler._flagnames(name)
+
+ -- check
+ assert(name)
+
+ -- the flag names
+ local flagnames = nil
+ if name == "cc" then flagnames = { "cxflags", "cflags" }
+ elseif name == "cxx" then flagnames = { "cxflags", "cxxflags" }
+ elseif name == "mm" then flagnames = { "mxflags", "mflags" }
+ elseif name == "mxx" then flagnames = { "mxflags", "mxxflags" }
+ elseif name == "as" then flagnames = { "asflags" }
+ -- error
+ utils.error("unknown compiler: %s", name)
+ return
+ end
+
+ -- ok
+ return flagnames
+end
+
+-- get the compiler kind from the source file type
+function compiler._kind(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 kind
+ local kind = nil
+ if filetype == ".c" then kind = "cc"
+ elseif filetype == ".cpp" or filetype == ".cc" then kind = "cxx"
+ elseif filetype == ".m" then kind = "mm"
+ elseif filetype == ".mm" then kind = "mxx"
+ elseif filetype == ".s" or filetype == ".asm" then kind = "as"
+ end
+
+ -- ok
+ return kind
+end
+
+-- get the compiler from the given source file
+function compiler.get(srcfile)
+
+ -- get the compiler kind
+ local kind = compiler._kind(srcfile)
+ if not kind then
+ return
+ end
+
+ -- get compiler from the source file type
+ local module = tools.get(kind)
+ if module then
+
+ -- invalid compiler
+ if not module.command_compile then
+ return
+ end
+
+ -- save kind
+ module._KIND = kind
+ end
+
+ -- ok?
+ return module
+end
+
+-- make the compile command
+function compiler.make(module, target, srcfile, objfile)
+
+ -- check
+ assert(module and target)
+
+ -- the flag names
+ local flagnames = compiler._flagnames(module._KIND)
+ assert(flagnames)
+
+ -- add flags from the compiler
+ local flags = {}
+ compiler._addflags_from_compiler(module, flags, flagnames)
+
+ -- add flags from the platform
+ compiler._addflags_from_platform(module, flags, flagnames)
+
+ -- add flags from the target
+ compiler._addflags_from_target(module, flags, flagnames, target)
+
+ -- add flags from the configure
+ compiler._addflags_from_config(module, flags, flagnames)
+
-- make the compile command
return module:command_compile(srcfile, objfile, table.concat(flags, " "):trim())
end
@@ -361,39 +441,18 @@ function compiler.check_include(opt, include, srcpath, objpath)
local flagnames = compiler._flagnames(module._KIND)
assert(flagnames)
- -- append the common flags
+ -- add flags from the compiler
local flags = {}
- for _, flagname in ipairs(flagnames) do
- table.join2(flags, module[flagname])
- end
+ compiler._addflags_from_compiler(module, flags, flagnames)
- -- append the option flags
- for _, flagname in ipairs(flagnames) do
- table.join2(flags, compiler._mapflags(module, opt[flagname]))
- end
+ -- add flags from the platform
+ compiler._addflags_from_platform(module, flags, flagnames)
- -- append the defines flags
- if opt.defines and module.flag_define then
- local defines = utils.wrap(opt.defines)
- for _, define in ipairs(defines) do
- table.join2(flags, module:flag_define(define))
- end
- end
+ -- add flags from the option
+ compiler._addflags_from_option(module, flags, flagnames, opt)
- -- append the undefines flags
- if opt.undefines and module.flag_undefine then
- local undefines = utils.wrap(opt.undefines)
- for _, undefine in ipairs(undefines) do
- table.join2(flags, module:flag_undefine(undefine))
- end
- end
-
- -- append the includedirs flags
- if opt.includedirs and module.flag_includedir then
- for _, includedir in ipairs(utils.wrap(opt.includedirs)) do
- table.join2(flags, module:flag_includedir(includedir))
- end
- end
+ -- add flags from the configure
+ compiler._addflags_from_config(module, flags, flagnames)
-- make the compile command
local cmd = string.format("%s > %s 2>&1", module:command_compile(srcpath, objpath, table.concat(flags, " "):trim()), xmake._NULDEV)
@@ -447,39 +506,18 @@ function compiler.check_function(opt, interface, srcpath, objpath)
local flagnames = compiler._flagnames(module._KIND)
assert(flagnames)
- -- append the common flags
+ -- add flags from the compiler
local flags = {}
- for _, flagname in ipairs(flagnames) do
- table.join2(flags, module[flagname])
- end
+ compiler._addflags_from_compiler(module, flags, flagnames)
- -- append the option flags
- for _, flagname in ipairs(flagnames) do
- table.join2(flags, compiler._mapflags(module, opt[flagname]))
- end
-
- -- append the defines flags
- if opt.defines and module.flag_define then
- local defines = utils.wrap(opt.defines)
- for _, define in ipairs(defines) do
- table.join2(flags, module:flag_define(define))
- end
- end
+ -- add flags from the platform
+ compiler._addflags_from_platform(module, flags, flagnames)
- -- append the undefines flags
- if opt.undefines and module.flag_undefine then
- local undefines = utils.wrap(opt.undefines)
- for _, undefine in ipairs(undefines) do
- table.join2(flags, module:flag_undefine(undefine))
- end
- end
+ -- add flags from the option
+ compiler._addflags_from_option(module, flags, flagnames, opt)
- -- append the includedirs flags
- if opt.includedirs and module.flag_includedir then
- for _, includedir in ipairs(utils.wrap(opt.includedirs)) do
- table.join2(flags, module:flag_includedir(includedir))
- end
- end
+ -- add flags from the configure
+ compiler._addflags_from_config(module, flags, flagnames)
-- make the compile command
local cmd = string.format("%s > %s 2>&1", module:command_compile(srcpath, objpath, table.concat(flags, " "):trim()), xmake._NULDEV)