summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-08-29 00:35:20 +0800
committerruki <[email protected]>2020-08-29 00:35:20 +0800
commit9965dfcab2d32d7d5c72831f1a838028bde66e7c (patch)
tree66b9245d6c4017f04e38f26fb82a3feb105ccb9b
parentefb8e8c1ef851be738d69aa563c9a4097cd42246 (diff)
improve cmake generator
-rw-r--r--xmake/modules/detect/sdks/find_vcpkgdir.lua2
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua52
2 files changed, 47 insertions, 7 deletions
diff --git a/xmake/modules/detect/sdks/find_vcpkgdir.lua b/xmake/modules/detect/sdks/find_vcpkgdir.lua
index 988d8c0df..0c2f50d45 100644
--- a/xmake/modules/detect/sdks/find_vcpkgdir.lua
+++ b/xmake/modules/detect/sdks/find_vcpkgdir.lua
@@ -100,7 +100,5 @@ function main(sdkdir, opt)
-- save to cache
cacheinfo.vcpkg = vcpkg or false
cache.save(key, cacheinfo)
-
- -- ok?
return vcpkg
end
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua
index 3a06e8f6f..643c97f8c 100644
--- a/xmake/plugins/project/cmake/cmakelists.lua
+++ b/xmake/plugins/project/cmake/cmakelists.lua
@@ -21,6 +21,24 @@
-- imports
import("core.project.project")
import("core.tool.compiler")
+import("core.base.semver")
+import("lib.detect.find_tool")
+
+-- get minimal cmake version
+function _get_cmake_minver()
+ local cmake_minver = _g.cmake_minver
+ if not cmake_minver then
+ local cmake = find_tool("cmake", {version = true})
+ if cmake and cmake.version then
+ cmake_minver = semver.new(cmake.version)
+ end
+ if not cmake_minver or cmake_minver:gt("3.13.0") then
+ cmake_minver = semver.new("3.13.0")
+ end
+ _g.cmake_minver = cmake_minver
+ end
+ return cmake_minver
+end
-- get unix path
function _get_unix_path(filepath)
@@ -42,13 +60,20 @@ end
-- add project info
function _add_project(cmakelists)
+
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 3.13.0)")
+ cmakelists:print("cmake_minimum_required(VERSION %s)", _get_cmake_minver())
local project_name = project.name()
+ if not project_name then
+ for _, target in pairs(project.targets()) do
+ project_name = target:name()
+ break
+ end
+ end
if project_name then
local project_info = ""
local project_version = project.version()
@@ -326,11 +351,28 @@ end
function _add_target_link_directories(cmakelists, target)
local linkdirs = _get_configs_from_target(target, "linkdirs")
if #linkdirs > 0 then
- cmakelists:print("target_link_directories(%s PRIVATE", target:name())
- for _, linkdir in ipairs(linkdirs) do
- cmakelists:print(" " .. _get_unix_path(linkdir))
+ local cmake_minver = _get_cmake_minver()
+ if cmake_minver:ge("3.13.0") then
+ cmakelists:print("target_link_directories(%s PRIVATE", target:name())
+ for _, linkdir in ipairs(linkdirs) do
+ cmakelists:print(" " .. _get_unix_path(linkdir))
+ end
+ cmakelists:print(")")
+ else
+ cmakelists:print("if(MSVC)")
+ cmakelists:print(" target_link_options(%s PRIVATE", target:name())
+ for _, linkdir in ipairs(linkdirs) do
+ cmakelists:print(" -libpath:" .. _get_unix_path(linkdir))
+ end
+ cmakelists:print(" )")
+ cmakelists:print("else()")
+ cmakelists:print(" target_link_options(%s PRIVATE", target:name())
+ for _, linkdir in ipairs(linkdirs) do
+ cmakelists:print(" -L" .. _get_unix_path(linkdir))
+ end
+ cmakelists:print(" )")
+ cmakelists:print("endif()")
end
- cmakelists:print(")")
end
end