Skip to content

Commit 9857c48

Browse files
committed
split default and runtime flags
1 parent 61f2e26 commit 9857c48

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

xmake/modules/package/tools/cmake.lua

+59-32
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,15 @@ function _get_configs(package, configs, opt)
754754
else
755755
_get_configs_for_generic(package, configs, opt)
756756
end
757-
_get_configs_for_default_flags(package, configs, opt)
757+
local envs = _get_envs_for_default_flags(package, configs, opt)
758+
local runtime_envs = _get_envs_for_runtime_flags(package, configs, opt)
759+
if runtime_envs then
760+
envs = envs or {}
761+
for name, value in pairs(runtime_envs) do
762+
envs[name] = (envs[name] or "") .. " " .. value
763+
end
764+
end
765+
_insert_configs_from_envs(configs, envs or {}, opt)
758766
return configs
759767
end
760768

@@ -808,7 +816,7 @@ function _get_default_flags(package, configs, buildtype, opt)
808816
return cmake_default_flags
809817
end
810818

811-
function _get_configs_for_default_flags(package, configs, opt)
819+
function _get_cmake_buildtype(package)
812820
local cmake_buildtype_map = {
813821
debug = "DEBUG",
814822
release = "RELEASE",
@@ -818,37 +826,56 @@ function _get_configs_for_default_flags(package, configs, opt)
818826
if not buildtype:startswith("release") and buildtype ~= "debug" then
819827
buildtype = "release"
820828
end
821-
buildtype = cmake_buildtype_map[buildtype]
822-
823-
local runtimes = package:runtimes()
824-
local cxx_runtimeflags
825-
local c_runtimeflags
826-
local ld_runtimeflags
827-
local sh_runtimeflags
828-
local ar_runtimeflags
829-
if runtimes then
830-
local fake_target = {is_shared = function(_) return false end,
831-
sourcekinds = function(_) return "cxx" end}
832-
c_runtimeflags = _map_compflags(fake_target, "c", "runtime", runtimes)
833-
fake_target.sourcekinds = function(_) return "cxx" end
834-
cxx_runtimeflags = _map_compflags(fake_target, "cxx", "runtime", runtimes)
835-
ld_runtimeflags = _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes)
836-
ar_runtimeflags = _map_linkflags(fake_target, "static", {"cxx"}, "runtime", runtimes)
837-
fake_target.is_shared = function(_) return true end
838-
sh_runtimeflags = _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes)
839-
end
829+
return cmake_buildtype_map[buildtype]
830+
end
831+
832+
function _get_envs_for_default_flags(package, configs, opt)
833+
local buildtype = _get_cmake_buildtype(package)
834+
local envs = {}
840835
local default_flags = _get_default_flags(package, configs, buildtype, opt)
841-
local cxx_init_flags = (opt.cxxflags or opt.cxflags) and (cxx_runtimeflags or {}) or table.join(default_flags.cxxflags, cxx_runtimeflags or {})
842-
local c_init_flags = (opt.cflags or opt.cxflags) and (c_runtimeflags or {}) or table.join(default_flags.cflags, c_runtimeflags or {})
843-
local ld_init_flags = (opt.ldflags) and (ld_runtimeflags or {}) or table.join(default_flags.ldflags, ld_runtimeflags or {})
844-
local ar_init_flags = (opt.arflags) and (ar_runtimeflags or {}) or table.join(default_flags.arflags, ar_runtimeflags or {})
845-
local sh_init_flags = (opt.shflags) and (sh_runtimeflags or {}) or table.join(default_flags.shflags, sh_runtimeflags or {})
846-
847-
table.insert(configs, format("-DCMAKE_CXX_FLAGS_%s=", buildtype) .. table.concat(cxx_init_flags, " "))
848-
table.insert(configs, format("-DCMAKE_C_FLAGS_%s=", buildtype) .. table.concat(c_init_flags, " "))
849-
table.insert(configs, format("-DCMAKE_EXE_LINKER_FLAGS_%s=", buildtype) .. table.concat(ld_init_flags, " "))
850-
table.insert(configs, format("-DCMAKE_STATIC_LINKER_FLAGS_%s=", buildtype) .. table.concat(ar_init_flags, " "))
851-
table.insert(configs, format("-DCMAKE_SHARED_LINKER_FLAGS_%s=", buildtype) .. table.concat(sh_init_flags, " "))
836+
local cxx_init_flags = (not opt.cxxflags and not opt.cxflags) and default_flags.cxxflags or {}
837+
local c_init_flags = (not opt.cflags and not opt.cxflags) and default_flags.cflags or {}
838+
local ld_init_flags = not opt.ldflags and default_flags.ldflags or {}
839+
local ar_init_flags = not opt.arflags and default_flags.arflags or {}
840+
local sh_init_flags = not opt.shflags and default_flags.shflags or {}
841+
842+
envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = table.concat(cxx_init_flags or {}, " ")
843+
envs[format("CMAKE_C_FLAGS_%s", buildtype)] = table.concat(c_init_flags or {}, " ")
844+
envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = table.concat(ld_init_flags or {}, " ")
845+
envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = table.concat(ar_init_flags or {}, " ")
846+
envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = table.concat(sh_init_flags or {}, " ")
847+
848+
return envs
849+
end
850+
851+
function _get_envs_for_runtime_flags(package, configs, opt)
852+
local buildtype = _get_cmake_buildtype(package)
853+
local runtimes = package:runtimes()
854+
local envs = {}
855+
local cxx_runtimeflags
856+
local c_runtimeflags
857+
local ld_runtimeflags
858+
local sh_runtimeflags
859+
local ar_runtimeflags
860+
if runtimes then
861+
local fake_target = {is_shared = function(_) return false end,
862+
sourcekinds = function(_) return "cxx" end}
863+
c_runtimeflags = _map_compflags(fake_target, "c", "runtime", runtimes)
864+
fake_target.sourcekinds = function(_) return "cxx" end
865+
cxx_runtimeflags = _map_compflags(fake_target, "cxx", "runtime", runtimes)
866+
ld_runtimeflags = _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes)
867+
ar_runtimeflags = _map_linkflags(fake_target, "static", {"cxx"}, "runtime", runtimes)
868+
fake_target.is_shared = function(_) return true end
869+
sh_runtimeflags = _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes)
870+
end
871+
872+
envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = table.concat(cxx_runtimeflags or {}, " ")
873+
envs[format("CMAKE_C_FLAGS_%s", buildtype)] = table.concat(c_runtimeflags or {}, " ")
874+
envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = table.concat(ld_runtimeflags or {}, " ")
875+
envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = table.concat(ar_runtimeflags or {}, " ")
876+
envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = table.concat(sh_runtimeflags or {}, " ")
877+
878+
return envs
852879
end
853880

854881
-- get build environments

0 commit comments

Comments
 (0)