From 82839efc0adea5d263e2c19711efe3f1bf2e2681 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 19:49:15 +0800 Subject: generate cmake import files for multiple targets --- .../target/action/install/cmake_importfiles.lua | 78 +++++++++++++++++++--- xmake/scripts/cmake_importfiles/xxxTargets.cmake | 2 +- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index 12d044251..d311f19ce 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -25,14 +25,14 @@ import("core.project.project") function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = path.filename(target:targetfile()), + TARGETFILENAME = path.filename(target:targetfile()):replace("dll", "lib"), TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} end --- install cmake import file -function _install_cmake_importfile(target, installdir, filename, opt) +-- install cmake config file +function _install_cmake_configfile(target, installdir, filename, opt) -- get import file path local projectname = project.name() or target:name() @@ -45,6 +45,68 @@ function _install_cmake_importfile(target, installdir, filename, opt) -- get the builtin variables local builtinvars = _get_builtinvars(target, installdir) + -- copy and replace builtin variables + local content = io.readfile(importfile_src) + if content then + content = content:split("#######################+#")[1] + content = content:gsub("(@(.-)@)", function(_, variable) + variable = variable:trim() + local value = builtinvars[variable] + return type(value) == "function" and value() or value + end) + io.writefile(importfile_dst, content) + end +end + +-- append target to cmake config file +function _append_cmake_configfile(target, installdir, filename, opt) + + -- get import file path + local projectname = project.name() or target:name() + local importfile_src = path.join(os.programdir(), "scripts", "cmake_importfiles", filename) + local importfile_dst = path.join(installdir, opt and opt.libdir or "lib", "cmake", projectname, (filename:gsub("xxx", projectname))) + + -- get the builtin variables + local builtinvars = _get_builtinvars(target, installdir) + + -- generate the file if not exist / file is outdated + if not os.isfile(importfile_dst) or os.mtime(importfile_dst) < os.mtime(target:targetfile()) then + _install_cmake_configfile(target, installdir, filename, opt) + end + + -- copy and replace builtin variables + local content = io.readfile(importfile_src) + local dst_content = io.readfile(importfile_dst) + if content then + content = content:split("#######################+#")[2] + content = content:gsub("(@(.-)@)", function(_, variable) + variable = variable:trim() + local value = builtinvars[variable] + return type(value) == "function" and value() or value + end) + content = content:trim() + + -- check if the target already exists + if not dst_content:match(format("%sTargets.cmake", target:name())) then + io.writefile(importfile_dst, dst_content:trim() .. "\n\n" .. content .. "\n") + end + end +end + +-- install cmake target file +function _install_cmake_targetfile(target, installdir, filename, opt) + + -- get import file path + local projectname = project.name() or target:name() + local importfile_src = path.join(os.programdir(), "scripts", "cmake_importfiles", filename) + local importfile_dst = path.join(installdir, opt and opt.libdir or "lib", "cmake", projectname, (filename:gsub("xxx", target:name()))) + + -- trace + vprint("generating %s ..", importfile_dst) + + -- get the builtin variables + local builtinvars = _get_builtinvars(target, installdir) + -- copy and replace builtin variables local content = io.readfile(importfile_src) if content then @@ -71,13 +133,13 @@ function main(target, opt) end -- do install - _install_cmake_importfile(target, installdir, "xxxConfig.cmake", opt) - _install_cmake_importfile(target, installdir, "xxxConfigVersion.cmake", opt) - _install_cmake_importfile(target, installdir, "xxxTargets.cmake", opt) + _append_cmake_configfile(target, installdir, "xxxConfig.cmake", opt) + _install_cmake_configfile(target, installdir, "xxxConfigVersion.cmake", opt) + _install_cmake_targetfile(target, installdir, "xxxTargets.cmake", opt) if is_mode("debug") then - _install_cmake_importfile(target, installdir, "xxxTargets-debug.cmake", opt) + _install_cmake_targetfile(target, installdir, "xxxTargets-debug.cmake", opt) else - _install_cmake_importfile(target, installdir, "xxxTargets-release.cmake", opt) + _install_cmake_targetfile(target, installdir, "xxxTargets-release.cmake", opt) end end diff --git a/xmake/scripts/cmake_importfiles/xxxTargets.cmake b/xmake/scripts/cmake_importfiles/xxxTargets.cmake index 7ade9f919..bdf2774e9 100644 --- a/xmake/scripts/cmake_importfiles/xxxTargets.cmake +++ b/xmake/scripts/cmake_importfiles/xxxTargets.cmake @@ -1,4 +1,4 @@ -# Generated by CMake +# Generated by XMake if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5) message(FATAL_ERROR "CMake >= 2.6.0 required") -- cgit v1.3.1 From f75a7e64e5d370e0da29ea394d9a91c4436ce3c2 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 20:47:15 +0800 Subject: constrain replace --- xmake/modules/target/action/install/cmake_importfiles.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index d311f19ce..1fb9ca5a6 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -25,7 +25,7 @@ import("core.project.project") function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = path.filename(target:targetfile()):replace("dll", "lib"), + TARGETFILENAME = is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub(".dll$", ".lib") or path.filename(target:targetfile()), TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} -- cgit v1.3.1 From ef9e86536b0c707ea6c6b0d515f88b1dc4b3385e Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 20:48:25 +0800 Subject: fix typo --- xmake/modules/target/action/install/cmake_importfiles.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index 1fb9ca5a6..40ff77295 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -25,7 +25,7 @@ import("core.project.project") function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub(".dll$", ".lib") or path.filename(target:targetfile()), + TARGETFILENAME = is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub("%.dll$", ".lib") or path.filename(target:targetfile()), TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} -- cgit v1.3.1 From 9c5d90b1f884b600fbc7e9327ba34ad6328fe5c5 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 21:47:03 +0800 Subject: add target: prefix --- xmake/modules/target/action/install/cmake_importfiles.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index 40ff77295..35479d3c1 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -25,7 +25,7 @@ import("core.project.project") function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub("%.dll$", ".lib") or path.filename(target:targetfile()), + TARGETFILENAME = target:is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub("%.dll$", ".lib") or path.filename(target:targetfile()), TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} -- cgit v1.3.1 From 9a29212b893502d685282660ab34646e167be1e9 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 22:32:08 +0800 Subject: fix libfile on mingw --- xmake/modules/target/action/install/cmake_importfiles.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index 35479d3c1..0b6a8c276 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -23,9 +23,15 @@ import("core.project.project") -- get the builtin variables function _get_builtinvars(target, installdir) + local libfile = path.filename(target:targetfile()) + if target:is_plat("windows") then + libfile = libfile:gsub("%.dll$", ".lib") + elseif target:is_plat("mingw") then + libfile = libfile:gsub("%.dll$", ".dll.a") + end return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = target:is_plat("windows", "mingw") and path.filename(target:targetfile()):gsub("%.dll$", ".lib") or path.filename(target:targetfile()), + TARGETFILENAME = libfile, TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} -- cgit v1.3.1 From 3fdef81bd4c17f224065296cf71c8f18a2b35f9e Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 11 Aug 2021 23:25:27 +0800 Subject: improve libfile finding on mingw --- .../modules/target/action/install/cmake_importfiles.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index 0b6a8c276..e4e5d118e 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -21,17 +21,26 @@ -- imports import("core.project.project") --- get the builtin variables -function _get_builtinvars(target, installdir) +-- get the lib file of the target +function _get_libfile(target, installdir) local libfile = path.filename(target:targetfile()) if target:is_plat("windows") then libfile = libfile:gsub("%.dll$", ".lib") elseif target:is_plat("mingw") then - libfile = libfile:gsub("%.dll$", ".dll.a") + if os.isfile(path.join(installdir, "lib", libfile:gsub("%.dll$", ".dll.a"))) then + libfile = libfile:gsub("%.dll$", ".dll.a") + else + libfile = libfile:gsub("%.dll$", ".lib") + end end + return libfile +end + +-- get the builtin variables +function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), - TARGETFILENAME = libfile, + TARGETFILENAME = _get_libfile(target, installdir), TARGETKIND = target:is_shared() and "SHARED" or "STATIC", PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} -- cgit v1.3.1