Skip to content

Commit 40a5806

Browse files
committed
split default and runtime flags
1 parent 61f2e26 commit 40a5806

File tree

1 file changed

+78
-72
lines changed

1 file changed

+78
-72
lines changed

xmake/modules/package/tools/cmake.lua

+78-72
Original file line numberDiff line numberDiff line change
@@ -723,43 +723,8 @@ 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)
762-
local cmake_default_flags = _g.cmake_default_flags
727+
local cmake_default_flags = _g.cmake_default_flags and _g.cmake_default_flags[buildtype]
763728
if not cmake_default_flags then
764729
local tmpdir = path.join(os.tmpdir(), package:name(), package:mode())
765730
local dummy_cmakelist = path.join(tmpdir, "CMakeLists.txt")
@@ -797,58 +762,99 @@ 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-
806-
_g.cmake_default_flags = cmake_default_flags
765+
_g.cmake_default_flags = _g.cmake_default_flags or {}
766+
_g.cmake_default_flags[buildtype] = cmake_default_flags
807767
end
808768
return cmake_default_flags
809769
end
810770

811-
function _get_configs_for_default_flags(package, configs, opt)
771+
function _get_cmake_buildtype(package)
812772
local cmake_buildtype_map = {
813773
debug = "DEBUG",
814774
release = "RELEASE",
815775
releasedbg = "RELWITHDEBINFO"
816776
}
817777
local buildtype = package:mode()
818-
if not buildtype:startswith("release") and buildtype ~= "debug" then
819-
buildtype = "release"
778+
return cmake_buildtype_map[buildtype] or "RELEASE"
779+
end
780+
781+
function _get_envs_for_default_flags(package, configs, opt)
782+
local buildtype = _get_cmake_buildtype(package)
783+
local envs = {}
784+
local default_flags = _get_default_flags(package, configs, buildtype, opt)
785+
if default_flags then
786+
envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = (not opt.cxxflags and not opt.cxflags) and default_flags.cxxflags
787+
envs[format("CMAKE_C_FLAGS_%s", buildtype)] = (not opt.cflags and not opt.cxflags) and default_flags.cflags
788+
envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = not opt.ldflags and default_flags.ldflags
789+
envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = not opt.arflags and default_flags.arflags
790+
envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = not opt.shflags and default_flags.shflags
820791
end
821-
buildtype = cmake_buildtype_map[buildtype]
792+
return envs
793+
end
822794

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

854860
-- get build environments

0 commit comments

Comments
 (0)