diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index baa4e5aee19..248be4ad4f4 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -754,7 +754,15 @@ function _get_configs(package, configs, opt) else _get_configs_for_generic(package, configs, opt) end - _get_configs_for_default_flags(package, configs, opt) + local envs = _get_envs_for_default_flags(package, configs, opt) + local runtime_envs = _get_envs_for_runtime_flags(package, configs, opt) + if runtime_envs then + envs = envs or {} + for name, value in pairs(runtime_envs) do + envs[name] = (envs[name] or "") .. " " .. value + end + end + _insert_configs_from_envs(configs, envs or {}, opt) return configs end @@ -808,7 +816,7 @@ function _get_default_flags(package, configs, buildtype, opt) return cmake_default_flags end -function _get_configs_for_default_flags(package, configs, opt) +function _get_cmake_buildtype(package) local cmake_buildtype_map = { debug = "DEBUG", release = "RELEASE", @@ -818,37 +826,56 @@ function _get_configs_for_default_flags(package, configs, opt) if not buildtype:startswith("release") and buildtype ~= "debug" then buildtype = "release" end - buildtype = cmake_buildtype_map[buildtype] - - local runtimes = package:runtimes() - local cxx_runtimeflags - local c_runtimeflags - local ld_runtimeflags - local sh_runtimeflags - local ar_runtimeflags - if runtimes then - local fake_target = {is_shared = function(_) return false end, - sourcekinds = function(_) return "cxx" end} - c_runtimeflags = _map_compflags(fake_target, "c", "runtime", runtimes) - fake_target.sourcekinds = function(_) return "cxx" end - cxx_runtimeflags = _map_compflags(fake_target, "cxx", "runtime", runtimes) - ld_runtimeflags = _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes) - ar_runtimeflags = _map_linkflags(fake_target, "static", {"cxx"}, "runtime", runtimes) - fake_target.is_shared = function(_) return true end - sh_runtimeflags = _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes) - end + return cmake_buildtype_map[buildtype] +end + +function _get_envs_for_default_flags(package, configs, opt) + local buildtype = _get_cmake_buildtype(package) + local envs = {} local default_flags = _get_default_flags(package, configs, buildtype, opt) - local cxx_init_flags = (opt.cxxflags or opt.cxflags) and (cxx_runtimeflags or {}) or table.join(default_flags.cxxflags, cxx_runtimeflags or {}) - local c_init_flags = (opt.cflags or opt.cxflags) and (c_runtimeflags or {}) or table.join(default_flags.cflags, c_runtimeflags or {}) - local ld_init_flags = (opt.ldflags) and (ld_runtimeflags or {}) or table.join(default_flags.ldflags, ld_runtimeflags or {}) - local ar_init_flags = (opt.arflags) and (ar_runtimeflags or {}) or table.join(default_flags.arflags, ar_runtimeflags or {}) - local sh_init_flags = (opt.shflags) and (sh_runtimeflags or {}) or table.join(default_flags.shflags, sh_runtimeflags or {}) - - table.insert(configs, format("-DCMAKE_CXX_FLAGS_%s=", buildtype) .. table.concat(cxx_init_flags, " ")) - table.insert(configs, format("-DCMAKE_C_FLAGS_%s=", buildtype) .. table.concat(c_init_flags, " ")) - table.insert(configs, format("-DCMAKE_EXE_LINKER_FLAGS_%s=", buildtype) .. table.concat(ld_init_flags, " ")) - table.insert(configs, format("-DCMAKE_STATIC_LINKER_FLAGS_%s=", buildtype) .. table.concat(ar_init_flags, " ")) - table.insert(configs, format("-DCMAKE_SHARED_LINKER_FLAGS_%s=", buildtype) .. table.concat(sh_init_flags, " ")) + local cxx_init_flags = (not opt.cxxflags and not opt.cxflags) and default_flags.cxxflags or {} + local c_init_flags = (not opt.cflags and not opt.cxflags) and default_flags.cflags or {} + local ld_init_flags = not opt.ldflags and default_flags.ldflags or {} + local ar_init_flags = not opt.arflags and default_flags.arflags or {} + local sh_init_flags = not opt.shflags and default_flags.shflags or {} + + envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = table.concat(cxx_init_flags or {}, " ") + envs[format("CMAKE_C_FLAGS_%s", buildtype)] = table.concat(c_init_flags or {}, " ") + envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = table.concat(ld_init_flags or {}, " ") + envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = table.concat(ar_init_flags or {}, " ") + envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = table.concat(sh_init_flags or {}, " ") + + return envs +end + +function _get_envs_for_runtime_flags(package, configs, opt) + local buildtype = _get_cmake_buildtype(package) + local runtimes = package:runtimes() + local envs = {} + local cxx_runtimeflags + local c_runtimeflags + local ld_runtimeflags + local sh_runtimeflags + local ar_runtimeflags + if runtimes then + local fake_target = {is_shared = function(_) return false end, + sourcekinds = function(_) return "cxx" end} + c_runtimeflags = _map_compflags(fake_target, "c", "runtime", runtimes) + fake_target.sourcekinds = function(_) return "cxx" end + cxx_runtimeflags = _map_compflags(fake_target, "cxx", "runtime", runtimes) + ld_runtimeflags = _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes) + ar_runtimeflags = _map_linkflags(fake_target, "static", {"cxx"}, "runtime", runtimes) + fake_target.is_shared = function(_) return true end + sh_runtimeflags = _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes) + end + + envs[format("CMAKE_CXX_FLAGS_%s", buildtype)] = table.concat(cxx_runtimeflags or {}, " ") + envs[format("CMAKE_C_FLAGS_%s", buildtype)] = table.concat(c_runtimeflags or {}, " ") + envs[format("CMAKE_EXE_LINKER_FLAGS_%s", buildtype)] = table.concat(ld_runtimeflags or {}, " ") + envs[format("CMAKE_STATIC_LINKER_FLAGS_%s", buildtype)] = table.concat(ar_runtimeflags or {}, " ") + envs[format("CMAKE_SHARED_LINKER_FLAGS_%s", buildtype)] = table.concat(sh_runtimeflags or {}, " ") + + return envs end -- get build environments