From 02879d82807aa8812a7e4967b1dd3597996ab23b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:33:05 +0800 Subject: install nuget package --- .../modules/package/manager/nuget/find_package.lua | 35 +++++++++++ .../package/manager/nuget/install_package.lua | 70 ++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 xmake/modules/package/manager/nuget/find_package.lua create mode 100644 xmake/modules/package/manager/nuget/install_package.lua diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua new file mode 100644 index 000000000..f766139e0 --- /dev/null +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -0,0 +1,35 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_package.lua +-- + +-- imports +import("lib.detect.find_file") +import("lib.detect.find_tool") +import("core.base.option") +import("core.project.config") +import("core.project.target") + +-- find package from the nuget package manager +-- +-- @param name the package name, e.g. zlib, pcre +-- @param opt the options, e.g. {verbose = true) +-- +function main(name, opt) + opt = opt or {} +end diff --git a/xmake/modules/package/manager/nuget/install_package.lua b/xmake/modules/package/manager/nuget/install_package.lua new file mode 100644 index 000000000..281fa2f16 --- /dev/null +++ b/xmake/modules/package/manager/nuget/install_package.lua @@ -0,0 +1,70 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file install_package.lua +-- + +-- imports +import("core.base.option") +import("core.base.json") +import("core.base.semver") +import("lib.detect.find_tool") + +-- dotnet add package libpapki --version 1.0.147 +function _install(dotnet, name, opt) + + -- init argv + local argv = {"add", "package", name} + local require_version = opt.require_version + if require_version == "latest" then + require_version = nil + end + if require_version then + table.insert(argv, "--version") + table.insert(argv, require_version) + end + + local installdir = assert(opt.installdir, "installdir not found!") + if not os.isdir(installdir) then + os.mkdir(installdir) + end + local stubdir = path.join(installdir, "stub") + if not os.isdir(stubdir) then + os.vrunv(dotnet, {"new", "console", "-n", "stub"}, {curdir = installdir}) + end + + -- install package + os.vrunv(dotnet, argv, {curdir = stubdir}) + + local manifestfile = path.join(stubdir, "obj", "project.assets.json") + io.cat(manifestfile) +end + +-- install package +-- +-- @param name the package name, e.g. pcre2 +-- @param opt the options, e.g. {verbose = true} +-- +-- @return true or false +-- +function main(name, opt) + local dotnet = find_tool("dotnet") + if not dotnet then + raise("dotnet not found!") + end + _install(dotnet.program, name, opt or {}) +end -- cgit v1.3.1 From 4fe52e2dff1b781fa73c3a4119bc750b8b242db3 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:34:31 +0800 Subject: improve to find nuget package --- xmake/modules/package/manager/nuget/find_package.lua | 8 ++++++++ xmake/modules/package/manager/nuget/install_package.lua | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index f766139e0..1dfd5a64d 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -22,6 +22,7 @@ import("lib.detect.find_file") import("lib.detect.find_tool") import("core.base.option") +import("core.base.json") import("core.project.config") import("core.project.target") @@ -32,4 +33,11 @@ import("core.project.target") -- function main(name, opt) opt = opt or {} + + -- load manifest info + local installdir = assert(opt.installdir, "installdir not found!") + local stubdir = path.join(installdir, "stub") + local manifestfile = path.join(stubdir, "obj", "project.assets.json") + local manifest = json.loadfile(manifestfile) + print(manifest) end diff --git a/xmake/modules/package/manager/nuget/install_package.lua b/xmake/modules/package/manager/nuget/install_package.lua index 281fa2f16..902c6ac2b 100644 --- a/xmake/modules/package/manager/nuget/install_package.lua +++ b/xmake/modules/package/manager/nuget/install_package.lua @@ -20,7 +20,6 @@ -- imports import("core.base.option") -import("core.base.json") import("core.base.semver") import("lib.detect.find_tool") @@ -49,9 +48,6 @@ function _install(dotnet, name, opt) -- install package os.vrunv(dotnet, argv, {curdir = stubdir}) - - local manifestfile = path.join(stubdir, "obj", "project.assets.json") - io.cat(manifestfile) end -- install package -- cgit v1.3.1 From f28059827a165f1f9f31bf84968bb39e3d0ebb7b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:38:14 +0800 Subject: get nuget package info --- .../modules/package/manager/nuget/find_package.lua | 43 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index 1dfd5a64d..7ab369733 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -26,6 +26,22 @@ import("core.base.json") import("core.project.config") import("core.project.target") +function _find_package(name, targets, libraries, result) + local libinfo = libraries[name] + if libinfo then + print("find", name, libinfo) + end + local targetinfo = targets[name] + if targetinfo then + local dependencies = targetinfo.dependencies + if dependencies then + for k, v in pairs(dependencies) do + _find_package(k .. "/" .. v, targets, libraries, result) + end + end + end +end + -- find package from the nuget package manager -- -- @param name the package name, e.g. zlib, pcre @@ -38,6 +54,31 @@ function main(name, opt) local installdir = assert(opt.installdir, "installdir not found!") local stubdir = path.join(installdir, "stub") local manifestfile = path.join(stubdir, "obj", "project.assets.json") + if not os.isfile(manifestfile) then + return + end local manifest = json.loadfile(manifestfile) - print(manifest) + local targets + for k, v in pairs(manifest.targets) do + targets = v + break + end + local target_root + if targets then + for k, v in pairs(targets) do + if k:startswith(name) then + target_root = k + break + end + end + end + local libraries = manifest.libraries + if target_root and libraries then + local result = {} + _find_package(target_root, targets, libraries, result) + if result.links or result.includedirs then + return result + end + end end + -- cgit v1.3.1 From 55fc56d1dd04a1a913cf788da2a478d04bb25f06 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:40:33 +0800 Subject: get package file path --- .../modules/package/manager/nuget/find_package.lua | 24 ++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index 7ab369733..fa8b55221 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -26,17 +26,20 @@ import("core.base.json") import("core.project.config") import("core.project.target") -function _find_package(name, targets, libraries, result) - local libinfo = libraries[name] +function _find_package(name, metainfo, result) + local libinfo = metainfo.libraries[name] if libinfo then - print("find", name, libinfo) + for _, file in ipairs(libinfo.files) do + local filepath = path.join(metainfo.packagesdir, name, file) + print(filepath, os.isfile(filepath)) + end end - local targetinfo = targets[name] + local targetinfo = metainfo.targets[name] if targetinfo then local dependencies = targetinfo.dependencies if dependencies then for k, v in pairs(dependencies) do - _find_package(k .. "/" .. v, targets, libraries, result) + _find_package(k .. "/" .. v, metainfo, result) end end end @@ -72,10 +75,15 @@ function main(name, opt) end end end - local libraries = manifest.libraries - if target_root and libraries then + local metainfo = {} + metainfo.targets = targets + metainfo.libraries = manifest.libraries + if manifest.project and manifest.project.restore then + metainfo.packagesdir = manifest.project.restore.packagesPath + end + if target_root and metainfo.targets and metainfo.libraries and metainfo.packagesdir then local result = {} - _find_package(target_root, targets, libraries, result) + _find_package(target_root, metainfo, result) if result.links or result.includedirs then return result end -- cgit v1.3.1 From b504cb32e1c05f69ab2e5ea38a6c13e667d4c335 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:44:09 +0800 Subject: improve to get links --- .../modules/package/manager/nuget/find_package.lua | 75 ++++++++++++++++++++-- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index fa8b55221..55b83a6f8 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -26,20 +26,78 @@ import("core.base.json") import("core.project.config") import("core.project.target") -function _find_package(name, metainfo, result) - local libinfo = metainfo.libraries[name] +-- find native package files +-- e.g. +--[[ + "build/native/include/crc32.h", + "build/native/include/deflate.h", + "build/native/include/gzguts.h", + "build/native/include/inffast.h", + "build/native/include/inffixed.h", + "build/native/include/inflate.h", + "build/native/include/inftrees.h", + "build/native/include/trees.h", + "build/native/include/zconf.h", + "build/native/include/zlib.h", + "build/native/include/zutil.h", + "build/native/lib/Win32/v140/Debug/zlib.lib", + "build/native/lib/Win32/v140/Release/zlib.lib", + "build/native/lib/Win32/v141/Debug/zlib.lib", + "build/native/lib/Win32/v141/Release/zlib.lib", + "build/native/lib/Win32/v142/Debug/MultiThreadedDebug/zlib.lib", + "build/native/lib/Win32/v142/Debug/MultiThreadedDebugDLL/zlib.lib", + "build/native/lib/Win32/v142/Release/MultiThreaded/zlib.lib", + "build/native/lib/Win32/v142/Release/MultiThreadedDLL/zlib.lib", + "build/native/lib/x64/v140/Debug/zlib.lib", + "build/native/lib/x64/v140/Release/zlib.lib", + "build/native/lib/x64/v141/Debug/zlib.lib", + "build/native/lib/x64/v141/Release/zlib.lib", + "build/native/lib/x64/v142/Debug/MultiThreadedDebug/zlib.lib", + "build/native/lib/x64/v142/Debug/MultiThreadedDebugDLL/zlib.lib", + "build/native/lib/x64/v142/Release/MultiThreaded/zlib.lib", + "build/native/lib/x64/v142/Release/MultiThreadedDLL/zlib.lib", +]] +function _find_package(name, result, opt) + local arch = opt.arch + local plat = opt.plat + local mode = opt.mode + local libinfo = opt.libraries[name] if libinfo then + local installdir = path.join(opt.packagesdir, name) + local libdir = "build/native/lib" + local runtime = "MultiThreadedDLL" + local toolset = "v142" + local libmode = mode == "debug" and "Debug" or "Release" for _, file in ipairs(libinfo.files) do - local filepath = path.join(metainfo.packagesdir, name, file) - print(filepath, os.isfile(filepath)) + local filepath = path.join(installdir, file) + file = file:lower():trim() + + -- get includedirs + if file:find("/include/", 1, true) then + result.includedirs = result.includedirs or {} + table.insert(result.includedirs, path.join(installdir, "include")) + end + + -- get linkdirs and links + if file:endswith(".lib") then + local libfile = path.unix(path.join(libdir, toolset, libmode, runtime)) + if file:startswith(libfile) then + result.links = result.links or {} + result.linkdirs = result.linkdirs or {} + result.libfiles = result.libfiles or {} + table.insert(result.linkdirs, path.directory(filepath)) + table.insert(result.links, target.linkname(path.filename(filepath), {plat = plat})) + table.insert(result.libfiles, filepath) + end + end end end - local targetinfo = metainfo.targets[name] + local targetinfo = opt.targets[name] if targetinfo then local dependencies = targetinfo.dependencies if dependencies then for k, v in pairs(dependencies) do - _find_package(k .. "/" .. v, metainfo, result) + _find_package(k .. "/" .. v, result, opt) end end end @@ -76,6 +134,9 @@ function main(name, opt) end end local metainfo = {} + metainfo.plat = opt.plat + metainfo.arch = opt.arch + metainfo.mode = opt.mode metainfo.targets = targets metainfo.libraries = manifest.libraries if manifest.project and manifest.project.restore then @@ -83,7 +144,7 @@ function main(name, opt) end if target_root and metainfo.targets and metainfo.libraries and metainfo.packagesdir then local result = {} - _find_package(target_root, metainfo, result) + _find_package(target_root, result, metainfo) if result.links or result.includedirs then return result end -- cgit v1.3.1 From f113930e9714a5b2b9227f008a08d66c0e725a2a Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:46:52 +0800 Subject: improve to find nuget packages --- .../modules/package/manager/nuget/find_package.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index 55b83a6f8..896eeb243 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -60,17 +60,24 @@ import("core.project.target") function _find_package(name, result, opt) local arch = opt.arch local plat = opt.plat - local mode = opt.mode + local configs = opt.configs or {} local libinfo = opt.libraries[name] if libinfo then + local libarchs = {x64 = "x64", x86 = "Win32", arm64 = "arm64"} + local runtimes = { + MT = "MultiThreaded", + MTd = "MultiThreadedDebug", + MD = "MultiThreadedDLL", + MDd = "MultiThreadedDebugDLL"} local installdir = path.join(opt.packagesdir, name) local libdir = "build/native/lib" - local runtime = "MultiThreadedDLL" + local runtime = assert(runtimes[configs.runtimes], "unknown runtimes %s", configs.runtimes) local toolset = "v142" - local libmode = mode == "debug" and "Debug" or "Release" + local libarch = libarchs[arch] or "x64" + local libmode = configs.debug and "Debug" or "Release" for _, file in ipairs(libinfo.files) do local filepath = path.join(installdir, file) - file = file:lower():trim() + file = file:trim() -- get includedirs if file:find("/include/", 1, true) then @@ -80,8 +87,8 @@ function _find_package(name, result, opt) -- get linkdirs and links if file:endswith(".lib") then - local libfile = path.unix(path.join(libdir, toolset, libmode, runtime)) - if file:startswith(libfile) then + local libfile = path.unix(path.join(libdir, libarch, toolset, libmode, runtime)) + if file:startswith(libfile .. "/") then result.links = result.links or {} result.linkdirs = result.linkdirs or {} result.libfiles = result.libfiles or {} @@ -137,6 +144,7 @@ function main(name, opt) metainfo.plat = opt.plat metainfo.arch = opt.arch metainfo.mode = opt.mode + metainfo.configs = opt.configs metainfo.targets = targets metainfo.libraries = manifest.libraries if manifest.project and manifest.project.restore then @@ -151,3 +159,4 @@ function main(name, opt) end end + -- cgit v1.3.1 From b31c6509a8707ce7847e0ce9ea1322286edf1d30 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 00:46:59 +0800 Subject: update comments --- xmake/modules/package/manager/nuget/install_package.lua | 2 -- xmake/modules/package/manager/vcpkg/install_package.lua | 2 -- 2 files changed, 4 deletions(-) diff --git a/xmake/modules/package/manager/nuget/install_package.lua b/xmake/modules/package/manager/nuget/install_package.lua index 902c6ac2b..371dd4391 100644 --- a/xmake/modules/package/manager/nuget/install_package.lua +++ b/xmake/modules/package/manager/nuget/install_package.lua @@ -55,8 +55,6 @@ end -- @param name the package name, e.g. pcre2 -- @param opt the options, e.g. {verbose = true} -- --- @return true or false --- function main(name, opt) local dotnet = find_tool("dotnet") if not dotnet then diff --git a/xmake/modules/package/manager/vcpkg/install_package.lua b/xmake/modules/package/manager/vcpkg/install_package.lua index 6cd92573c..b82739367 100644 --- a/xmake/modules/package/manager/vcpkg/install_package.lua +++ b/xmake/modules/package/manager/vcpkg/install_package.lua @@ -139,8 +139,6 @@ end -- @param name the package name, e.g. pcre2, pcre2/libpcre2-8 -- @param opt the options, e.g. {verbose = true} -- --- @return true or false --- function main(name, opt) -- attempt to find vcpkg -- cgit v1.3.1 From cd00417c43706fb44b07fc6ca3da1786ea93e2ea Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 23:20:12 +0800 Subject: improve toolset --- xmake/modules/package/manager/nuget/find_package.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index 896eeb243..5af474c69 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -23,8 +23,10 @@ import("lib.detect.find_file") import("lib.detect.find_tool") import("core.base.option") import("core.base.json") +import("core.tool.toolchain") import("core.project.config") import("core.project.target") +import("private.utils.toolchain", {alias = "toolchain_utils"}) -- find native package files -- e.g. @@ -72,7 +74,7 @@ function _find_package(name, result, opt) local installdir = path.join(opt.packagesdir, name) local libdir = "build/native/lib" local runtime = assert(runtimes[configs.runtimes], "unknown runtimes %s", configs.runtimes) - local toolset = "v142" + local toolset = toolchain_utils.get_vs_toolset_ver(toolchain.load("msvc", {plat = plat, arch = arch}):config("vs_toolset") or config.get("vs_toolset")) local libarch = libarchs[arch] or "x64" local libmode = configs.debug and "Debug" or "Release" for _, file in ipairs(libinfo.files) do -- cgit v1.3.1 From dc88fe879795cc5fcebe32244628a59dca9dad09 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Dec 2024 23:21:17 +0800 Subject: add tests --- tests/projects/package/nuget/.gitignore | 8 ++++++++ tests/projects/package/nuget/src/main.cpp | 8 ++++++++ tests/projects/package/nuget/xmake.lua | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/projects/package/nuget/.gitignore create mode 100644 tests/projects/package/nuget/src/main.cpp create mode 100644 tests/projects/package/nuget/xmake.lua diff --git a/tests/projects/package/nuget/.gitignore b/tests/projects/package/nuget/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/package/nuget/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/package/nuget/src/main.cpp b/tests/projects/package/nuget/src/main.cpp new file mode 100644 index 000000000..e64ad9bc2 --- /dev/null +++ b/tests/projects/package/nuget/src/main.cpp @@ -0,0 +1,8 @@ +#include + +using namespace std; + +int main(int argc, char** argv) { + cout << "hello world!" << endl; + return 0; +} diff --git a/tests/projects/package/nuget/xmake.lua b/tests/projects/package/nuget/xmake.lua new file mode 100644 index 000000000..591aa2439 --- /dev/null +++ b/tests/projects/package/nuget/xmake.lua @@ -0,0 +1,7 @@ +add_requires("nuget::zlib_static", {alias = "zlib"}) + +target("test") + set_kind("binary") + add_files("src/*.cpp") + add_packages("zlib") + -- cgit v1.3.1