diff options
| author | ruki <[email protected]> | 2021-08-12 07:53:06 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-12 07:53:06 +0800 |
| commit | 0a9aebf55fac34c1965fd69da212a798ed04769a (patch) | |
| tree | 49ce3b5db4c2fc8d1b97e0c39f6c7d7b0a21f046 | |
| parent | 4dfaeeb6326cbe3ffebb3355b10895b6939c53d5 (diff) | |
| parent | 3fdef81bd4c17f224065296cf71c8f18a2b35f9e (diff) | |
Merge pull request #1574 from xq114/dev
generate cmake import files for multiple targets
| -rw-r--r-- | xmake/modules/target/action/install/cmake_importfiles.lua | 93 | ||||
| -rw-r--r-- | xmake/scripts/cmake_importfiles/xxxTargets.cmake | 2 |
2 files changed, 86 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..e4e5d118e 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -21,18 +21,33 @@ -- imports import("core.project.project") +-- 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 + 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 = path.filename(target:targetfile()), + 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"} 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() @@ -48,6 +63,68 @@ function _install_cmake_importfile(target, installdir, filename, opt) -- 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 content = content:gsub("(@(.-)@)", function(_, variable) variable = variable:trim() local value = builtinvars[variable] @@ -71,13 +148,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") |
