Skip to content

Commit

Permalink
improve cmake minver
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Feb 19, 2024
1 parent 49de9a9 commit a73fb13
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions xmake/plugins/project/cmake/cmakelists.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import("private.utils.batchcmds")
import("private.utils.rule_groups")
import("private.utils.target", {alias = "target_utils"})
import("plugins.project.utils.target_cmds", {rootdir = os.programdir()})
import("rules.c++.modules.modules_support.compiler_support", {alias = "module_compiler_support", rootdir = os.programdir()})

-- get cmake version
function _get_cmake_version()
Expand All @@ -45,12 +46,28 @@ function _get_cmake_version()
return cmake_version
end

-- has c++ modules sources
function _has_cxxmodules_sources()
for _, target in ipairs(project.ordertargets()) do
if module_compiler_support.contains_modules(target) then
return true
end
end
end

-- get minimal cmake version
function _get_cmake_minver()
local cmake_minver = _g.cmake_minver
if not cmake_minver then
cmake_minver = _get_cmake_version()
if not cmake_minver or cmake_minver:gt("3.15.0") then
if cmake_minver then
if _has_cxxmodules_sources() and cmake_minver:gt("3.28.0") then
cmake_minver = semver.new("3.28.0")
elseif cmake_minver:gt("3.15.0") then
cmake_minver = semver.new("3.15.0")
end
end
if not cmake_minver then
cmake_minver = semver.new("3.15.0")
end
_g.cmake_minver = cmake_minver
Expand Down Expand Up @@ -113,10 +130,10 @@ function _get_relative_unix_path_to_cmake(filepath, outputdir)
return os.args(filepath)
end

-- get enabled languages from targets
function _get_project_languages(targets)
-- get enabled languages
function _get_project_languages()
local languages = {}
for _, target in table.orderpairs(targets) do
for _, target in ipairs(project.ordertargets()) do
for _, sourcekind in ipairs(target:sourcekinds()) do
if sourcekind == "cc" then table.insert(languages, "C")
elseif sourcekind == "cxx" then table.insert(languages, "CXX")
Expand Down Expand Up @@ -146,8 +163,8 @@ end

-- Did the current cmake native support for c++ modules?
function _can_native_support_for_cxxmodules()
local cmake_version = _get_cmake_version()
if cmake_version and cmake_version:ge("3.28") then
local cmake_minver = _get_cmake_minver()
if cmake_minver and cmake_minver:ge("3.28") then
return true
end
end
Expand Down Expand Up @@ -234,16 +251,16 @@ function _get_flags_from_target(target, flagkind)
end

-- add project info
function _add_project(cmakelists, languages, outputdir)
function _add_project(cmakelists, outputdir)

local cmake_version = _get_cmake_minver()
local cmake_minver = _get_cmake_minver()
cmakelists:print([[# this is the build file for project %s
# it is autogenerated by the xmake build system.
# do not edit by hand.
]], project.name() or "")
cmakelists:print("# project")
cmakelists:print("cmake_minimum_required(VERSION %s)", cmake_version)
if cmake_version:ge("3.15.0") then
cmakelists:print("cmake_minimum_required(VERSION %s)", cmake_minver)
if cmake_minver:ge("3.15.0") then
-- for MSVC_RUNTIME_LIBRARY
cmakelists:print("cmake_policy(SET CMP0091 NEW)")
end
Expand All @@ -260,6 +277,7 @@ function _add_project(cmakelists, languages, outputdir)
if project_version then
project_info = project_info .. " VERSION " .. project_version
end
local languages = _get_project_languages()
if languages then
cmakelists:print("project(%s%s LANGUAGES %s)", project_name, project_info, table.concat(languages, " "))
else
Expand Down Expand Up @@ -840,7 +858,7 @@ function _add_target_link_libraries(cmakelists, target, outputdir)
cmakelists:print(")")
end

-- ignore c++ modules rules
-- get c++ modules rules
local cxxmodules_rules
if _can_native_support_for_cxxmodules() then
cxxmodules_rules = _get_cxxmodules_rules()
Expand Down Expand Up @@ -1130,7 +1148,7 @@ end
function _generate_cmakelists(cmakelists, outputdir)

-- add project info
_add_project(cmakelists, _get_project_languages(project.targets()), outputdir)
_add_project(cmakelists, outputdir)

-- add targets
for _, target in table.orderpairs(project.targets()) do
Expand Down

0 comments on commit a73fb13

Please sign in to comment.