Skip to content

Commit 4979b01

Browse files
committed
split default and runtime flags
1 parent 61f2e26 commit 4979b01

File tree

1 file changed

+75
-70
lines changed

1 file changed

+75
-70
lines changed

xmake/modules/package/tools/cmake.lua

+75-70
Original file line numberDiff line numberDiff line change
@@ -723,41 +723,6 @@ function _get_configs_for_install(package, configs, opt)
723723
end
724724
end
725725

726-
-- get configs
727-
function _get_configs(package, configs, opt)
728-
configs = configs or {}
729-
opt._configs_str = string.serialize(configs, {indent = false, strip = true})
730-
_get_configs_for_install(package, configs, opt)
731-
_get_configs_for_generator(package, configs, opt)
732-
if package:is_plat("windows") then
733-
_get_configs_for_windows(package, configs, opt)
734-
elseif package:is_plat("android") then
735-
_get_configs_for_android(package, configs, opt)
736-
elseif package:is_plat("iphoneos", "watchos") or
737-
-- for cross-compilation on macOS, @see https://github.com/xmake-io/xmake/issues/2804
738-
(package:is_plat("macosx") and (get_config("appledev") or not package:is_arch(os.subarch()))) then
739-
_get_configs_for_appleos(package, configs, opt)
740-
elseif package:is_plat("mingw") then
741-
_get_configs_for_mingw(package, configs, opt)
742-
elseif package:is_plat("wasm") then
743-
_get_configs_for_wasm(package, configs, opt)
744-
elseif package:is_cross() then
745-
_get_configs_for_cross(package, configs, opt)
746-
elseif package:config("toolchains") then
747-
-- we still need find system libraries,
748-
-- it just pass toolchain environments if the toolchain is compatible with host
749-
if _is_toolchain_compatible_with_host(package) then
750-
_get_configs_for_host_toolchain(package, configs, opt)
751-
else
752-
_get_configs_for_cross(package, configs, opt)
753-
end
754-
else
755-
_get_configs_for_generic(package, configs, opt)
756-
end
757-
_get_configs_for_default_flags(package, configs, opt)
758-
return configs
759-
end
760-
761726
function _get_default_flags(package, configs, buildtype, opt)
762727
local cmake_default_flags = _g.cmake_default_flags
763728
if not cmake_default_flags then
@@ -797,58 +762,98 @@ function _get_default_flags(package, configs, buildtype, opt)
797762
cmake_default_flags.arflags = outdata:match("CMAKE_STATIC_LINKER_FLAGS is (.-)\n"):split(" ")
798763
table.join2(cmake_default_flags.arflags, outdata:match(format("CMAKE_STATIC_LINKER_FLAGS_%s is (.-)\n", buildtype)):split(" "))
799764

800-
cmake_default_flags.cflags = table.unique(cmake_default_flags.cflags)
801-
cmake_default_flags.cxxflags = table.unique(cmake_default_flags.cxxflags)
802-
cmake_default_flags.ldflags = table.unique(cmake_default_flags.ldflags)
803-
cmake_default_flags.shflags = table.unique(cmake_default_flags.shflags)
804-
cmake_default_flags.arflags = table.unique(cmake_default_flags.arflags)
805-
806765
_g.cmake_default_flags = cmake_default_flags
807766
end
808767
return cmake_default_flags
809768
end
810769

811-
function _get_configs_for_default_flags(package, configs, opt)
770+
function _get_cmake_buildtype(package)
812771
local cmake_buildtype_map = {
813772
debug = "DEBUG",
814773
release = "RELEASE",
815774
releasedbg = "RELWITHDEBINFO"
816775
}
817776
local buildtype = package:mode()
818-
if not buildtype:startswith("release") and buildtype ~= "debug" then
819-
buildtype = "release"
777+
return cmake_buildtype_map[buildtype] or "release"
778+
end
779+
780+
function _get_envs_for_default_flags(package, configs, opt)
781+
local buildtype = _get_cmake_buildtype(package)
782+
local envs = {}
783+
local default_flags = _get_default_flags(package, configs, buildtype, opt)
784+
if default_flags then
785+
envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = (not opt.cxxflags and not opt.cxflags) and default_flags.cxxflags
786+
envs[format("CMAKE_C_FLAGS_%s", buildtype)] = (not opt.cflags and not opt.cxflags) and default_flags.cflags
787+
envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = not opt.ldflags and default_flags.ldflags
788+
envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = not opt.arflags and default_flags.arflags
789+
envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = not opt.shflags and default_flags.shflags
820790
end
821-
buildtype = cmake_buildtype_map[buildtype]
791+
return envs
792+
end
822793

794+
function _get_envs_for_runtime_flags(package, configs, opt)
795+
local buildtype = _get_cmake_buildtype(package)
796+
local envs = {}
823797
local runtimes = package:runtimes()
824-
local cxx_runtimeflags
825-
local c_runtimeflags
826-
local ld_runtimeflags
827-
local sh_runtimeflags
828-
local ar_runtimeflags
829798
if runtimes then
830799
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)
800+
sourcekinds = function(_) return "c" end}
801+
envs[format("CMAKE_C_FLAGS_%s", buildtype)] = _map_compflags(fake_target, "c", "runtime", runtimes)
802+
fake_target.sourcekinds = function(_) return "cxx" end
803+
envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = _map_compflags(fake_target, "cxx", "runtime", runtimes)
804+
envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes)
805+
envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = _map_linkflags(fake_target, "static", {"cxx"}, "runtime", runtimes)
806+
fake_target.is_shared = function(_) return true end
807+
envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes)
839808
end
840-
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, " "))
809+
return envs
810+
end
811+
-- get configs
812+
function _get_configs(package, configs, opt)
813+
configs = configs or {}
814+
opt._configs_str = string.serialize(configs, {indent = false, strip = true})
815+
_get_configs_for_install(package, configs, opt)
816+
_get_configs_for_generator(package, configs, opt)
817+
if package:is_plat("windows") then
818+
_get_configs_for_windows(package, configs, opt)
819+
elseif package:is_plat("android") then
820+
_get_configs_for_android(package, configs, opt)
821+
elseif package:is_plat("iphoneos", "watchos") or
822+
-- for cross-compilation on macOS, @see https://github.com/xmake-io/xmake/issues/2804
823+
(package:is_plat("macosx") and (get_config("appledev") or not package:is_arch(os.subarch()))) then
824+
_get_configs_for_appleos(package, configs, opt)
825+
elseif package:is_plat("mingw") then
826+
_get_configs_for_mingw(package, configs, opt)
827+
elseif package:is_plat("wasm") then
828+
_get_configs_for_wasm(package, configs, opt)
829+
elseif package:is_cross() then
830+
_get_configs_for_cross(package, configs, opt)
831+
elseif package:config("toolchains") then
832+
-- we still need find system libraries,
833+
-- it just pass toolchain environments if the toolchain is compatible with host
834+
if _is_toolchain_compatible_with_host(package) then
835+
_get_configs_for_host_toolchain(package, configs, opt)
836+
else
837+
_get_configs_for_cross(package, configs, opt)
838+
end
839+
else
840+
_get_configs_for_generic(package, configs, opt)
841+
end
842+
local envs = _get_envs_for_default_flags(package, configs, opt)
843+
local runtime_envs = _get_envs_for_runtime_flags(package, configs, opt)
844+
if runtime_envs then
845+
envs = envs or {}
846+
for name, value in pairs(runtime_envs) do
847+
envs[name] = table.join(envs[name], value)
848+
end
849+
end
850+
if envs then
851+
for name, value in pairs(envs) do
852+
envs[name] = table.concat(table.unique(value), " ")
853+
end
854+
end
855+
_insert_configs_from_envs(configs, envs or {}, opt)
856+
return configs
852857
end
853858

854859
-- get build environments

0 commit comments

Comments
 (0)