diff options
| author | ruki <[email protected]> | 2018-12-29 22:45:04 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-12-29 22:45:04 +0800 |
| commit | d563d8d01f7f2aa1daa6e177dfe483e52ac786f8 (patch) | |
| tree | c4a0bd3112911d98e6c94a30195399c307721198 | |
| parent | 5a516124fe04f2526cadebf0cc6c96057d9af553 (diff) | |
uses singleton for platform
| -rw-r--r-- | xmake/core/base/singleton.lua | 22 | ||||
| -rw-r--r-- | xmake/platforms/android/config.lua | 12 | ||||
| -rw-r--r-- | xmake/platforms/cross/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/iphoneos/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/linux/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/macosx/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/mingw/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/watchos/config.lua | 10 | ||||
| -rw-r--r-- | xmake/platforms/windows/config.lua | 10 |
9 files changed, 33 insertions, 71 deletions
diff --git a/xmake/core/base/singleton.lua b/xmake/core/base/singleton.lua index 4579cf6fe..ed90286ab 100644 --- a/xmake/core/base/singleton.lua +++ b/xmake/core/base/singleton.lua @@ -29,7 +29,12 @@ local singleton = singleton or {} -- -- e.g. -- --- local instance = singleton.get("key") +-- function get_xxx() +-- return {xxx = 1} +-- end +-- local instance = singleton.get("key", get_xxx) +-- +-- Or -- -- function get_xxx() -- @@ -44,7 +49,7 @@ local singleton = singleton or {} -- return instance -- end -- -function singleton.get(key) +function singleton.get(key, init) -- get key key = tostring(key) @@ -57,11 +62,16 @@ function singleton.get(key) local instance = instances[key] if instance == nil then - -- mark as not inited - inited = false - -- init instance - instance = {} + if init then + instance = init() + else + -- mark as not inited + inited = false + + -- init instance + instance = {} + end -- save this instance instances[key] = instance diff --git a/xmake/platforms/android/config.lua b/xmake/platforms/android/config.lua index 5efaee97b..11cdcef1e 100644 --- a/xmake/platforms/android/config.lua +++ b/xmake/platforms/android/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("detect.sdks.find_ndk") import("private.platform.toolchain") import("private.platform.check_arch") @@ -49,11 +50,6 @@ end -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- get cross local cross = config.get("cross") @@ -103,8 +99,6 @@ function _toolchains() rc_sh:add("$(env RC)", "rustc") rc_ar:add("$(env RC)", "rustc") - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -113,7 +107,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("android.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end @@ -126,7 +120,7 @@ function main(platform, name) _check_ndk() -- check ld and sh, @note toolchains must be initialized after calling check_ndk() - local toolchains = _toolchains() + local toolchains = singleton.get("android.toolchains", _toolchains) check_toolchain(config, "ld", toolchains.ld) check_toolchain(config, "sh", toolchains.sh) end diff --git a/xmake/platforms/cross/config.lua b/xmake/platforms/cross/config.lua index 802aa0e41..5c57c8538 100644 --- a/xmake/platforms/cross/config.lua +++ b/xmake/platforms/cross/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("detect.sdks.find_cross_toolchain") import("private.platform.toolchain") import("private.platform.check_arch") @@ -42,11 +43,6 @@ end -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- find cross toolchain local cross = "" local cross_toolchain = find_cross_toolchain(config.get("sdk") or config.get("bin"), {bindir = config.get("bin"), cross = config.get("cross")}) @@ -105,8 +101,6 @@ function _toolchains() -- init the static library extractor ex:add("$(env AR)", {name = "ar", cross = cross}) - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -115,7 +109,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("cross.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/iphoneos/config.lua b/xmake/platforms/iphoneos/config.lua index db815ee3a..6b3063551 100644 --- a/xmake/platforms/iphoneos/config.lua +++ b/xmake/platforms/iphoneos/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("private.platform.toolchain") import("private.platform.check_arch") import("private.platform.check_xcode") @@ -32,11 +33,6 @@ import("private.platform.check_toolchain") -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- init architecture local arch = config.get("arch") local simulator = (arch == "i386" or arch == "x86_64") @@ -100,8 +96,6 @@ function _toolchains() sc_ld:add({name = "swiftc", cross = cross}) sc_sh:add({name = "swiftc", cross = cross}) - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -110,7 +104,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("iphoneos.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/linux/config.lua b/xmake/platforms/linux/config.lua index ed6fcf543..bbcf2df21 100644 --- a/xmake/platforms/linux/config.lua +++ b/xmake/platforms/linux/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("detect.sdks.find_cross_toolchain") import("private.platform.toolchain") import("private.platform.check_arch") @@ -48,11 +49,6 @@ end -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- find cross toolchain local cross = "" local cross_toolchain = find_cross_toolchain(config.get("sdk") or config.get("bin"), {bindir = config.get("bin"), cross = config.get("cross")}) @@ -158,8 +154,6 @@ function _toolchains() cu_ld:add("nvcc") cu_sh:add("nvcc") - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -168,7 +162,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("linux.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/macosx/config.lua b/xmake/platforms/macosx/config.lua index e114f266e..3f980d87a 100644 --- a/xmake/platforms/macosx/config.lua +++ b/xmake/platforms/macosx/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("private.platform.toolchain") import("private.platform.check_arch") import("private.platform.check_cuda") @@ -33,11 +34,6 @@ import("private.platform.check_toolchain") -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- init cross local cross = "xcrun -sdk macosx " @@ -141,8 +137,6 @@ function _toolchains() cu_ld:add("nvcc") cu_sh:add("nvcc") - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -151,7 +145,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("macosx.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/mingw/config.lua b/xmake/platforms/mingw/config.lua index d875abd21..7c245f0c3 100644 --- a/xmake/platforms/mingw/config.lua +++ b/xmake/platforms/mingw/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("detect.sdks.find_mingw") import("private.platform.toolchain") import("private.platform.check_arch") @@ -48,11 +49,6 @@ end -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- get cross local cross = config.get("cross") @@ -104,8 +100,6 @@ function _toolchains() -- init the resource compiler mrc:add({name = "windres", cross = cross}) - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -114,7 +108,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("mingw.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/watchos/config.lua b/xmake/platforms/watchos/config.lua index b791b7b65..58ae3ef79 100644 --- a/xmake/platforms/watchos/config.lua +++ b/xmake/platforms/watchos/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("private.platform.toolchain") import("private.platform.check_arch") import("private.platform.check_xcode") @@ -32,11 +33,6 @@ import("private.platform.check_toolchain") -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- init architecture local arch = config.get("arch") local simulator = (arch == "i386") @@ -100,8 +96,6 @@ function _toolchains() sc_ld:add({name = "swiftc", cross = cross}) sc_sh:add({name = "swiftc", cross = cross}) - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -110,7 +104,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("watchos.toolchains", _toolchains)[name] if toolchain then check_toolchain(config, name, toolchain) end diff --git a/xmake/platforms/windows/config.lua b/xmake/platforms/windows/config.lua index c6ccec4ef..f8db651fc 100644 --- a/xmake/platforms/windows/config.lua +++ b/xmake/platforms/windows/config.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.base.singleton") import("core.platform.environment") import("private.platform.toolchain") import("private.platform.check_arch") @@ -34,11 +35,6 @@ import("private.platform.check_toolchain") -- get toolchains function _toolchains() - -- attempt to get it from cache first - if _g.TOOLCHAINS then - return _g.TOOLCHAINS - end - -- init cross local cross = "xcrun -sdk macosx " @@ -121,8 +117,6 @@ function _toolchains() cu_ld:add("nvcc") cu_sh:add("nvcc") - -- save toolchains - _g.TOOLCHAINS = toolchains return toolchains end @@ -131,7 +125,7 @@ function main(platform, name) -- only check the given config name? if name then - local toolchain = _toolchains()[name] + local toolchain = singleton.get("windows.toolchains", _toolchains)[name] if toolchain then environment.enter("toolchains") check_toolchain(config, name, toolchain) |
