diff options
| author | ruki <[email protected]> | 2026-03-17 22:43:06 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-03-17 22:43:06 +0800 |
| commit | 42dec9486bb56c2f08db0c30f6b2829e1b02a1a7 (patch) | |
| tree | 7fcea3a58a9b9b0495747c255a209a81dae4bc86 /xmake/modules | |
| parent | 1c5607aa0da666cd244af2cab5fc7280e30742f1 (diff) | |
| parent | 2134b93d3f87d8f4ea63e0c49b2075ba056995d9 (diff) | |
Merge pull request #7398 from xmake-io/csharp
Add csharp support
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/core/tools/dotnet.lua | 126 | ||||
| -rw-r--r-- | xmake/modules/core/tools/dotnet/has_flags.lua | 50 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_dotnet.lua | 149 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_dotnet.lua | 53 | ||||
| -rw-r--r-- | xmake/modules/package/manager/nuget/find_package.lua | 182 | ||||
| -rw-r--r-- | xmake/modules/target/action/install/main.lua | 2 |
6 files changed, 478 insertions, 84 deletions
diff --git a/xmake/modules/core/tools/dotnet.lua b/xmake/modules/core/tools/dotnet.lua new file mode 100644 index 000000000..9a502b616 --- /dev/null +++ b/xmake/modules/core/tools/dotnet.lua @@ -0,0 +1,126 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file dotnet.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") +import("core.language.language") + +-- init it +function init(self) +end + +-- make the define flag +function nf_define(self, macro) + return {"-p:DefineConstants=" .. macro} +end + +-- make the optimize flag +function nf_optimize(self, level) + local maps = { + none = "-p:Optimize=false" + , fast = "-p:Optimize=true" + , faster = "-p:Optimize=true" + , fastest = "-p:Optimize=true" + , smallest = "-p:Optimize=true" + , aggressive = "-p:Optimize=true" + } + return maps[level] +end + +-- make the symbol flag +function nf_symbol(self, level) + local maps = { + debug = {"-p:DebugType=full", "-p:DebugSymbols=true"} + } + return maps[level] +end + +-- make the warning flag +function nf_warning(self, level) + local maps = { + none = "-p:WarningLevel=0" + , less = "-p:WarningLevel=1" + , more = "-p:WarningLevel=3" + , all = "-p:WarningLevel=5" + , allextra = "-p:WarningLevel=9999" + , error = {"-p:WarningLevel=5", "-p:TreatWarningsAsErrors=true"} + } + return maps[level] +end + +-- get the .csproj file from source files +function _get_csprojfile(opt) + local target = opt and opt.target + if target then + local csprojfile = target:data("csharp.csproj") + if csprojfile then + return csprojfile + end + end +end + +-- get dotnet verbosity level +function _get_verbosity() + if option.get("diagnosis") then + return "diagnostic" + end + return "quiet" +end + +-- get build configuration from mode +function _get_configuration() + local mode = config.mode() or "release" + if mode:lower() == "debug" then + return "Debug" + end + return "Release" +end + +-- make the build arguments list +function buildargv(self, sourcefiles, targetkind, targetfile, flags, opt) + local argv = {"build"} + + -- add .csproj file + local csprojfile = _get_csprojfile(opt) + if csprojfile then + table.insert(argv, csprojfile) + end + + -- add common options + table.join2(argv, {"--nologo", "--configuration", _get_configuration(), "--verbosity", _get_verbosity()}) + + -- add output directory + table.join2(argv, {"--output", path.directory(targetfile)}) + + -- add flags + if flags then + table.join2(argv, flags) + end + return self:program(), argv +end + +-- build the target file +function build(self, sourcefiles, targetkind, targetfile, flags, opt) + os.mkdir(path.directory(targetfile)) + local program, argv = buildargv(self, sourcefiles, targetkind, targetfile, flags, opt) + os.runv(program, argv, {envs = self:runenvs()}) +end diff --git a/xmake/modules/core/tools/dotnet/has_flags.lua b/xmake/modules/core/tools/dotnet/has_flags.lua new file mode 100644 index 000000000..eaffa7e88 --- /dev/null +++ b/xmake/modules/core/tools/dotnet/has_flags.lua @@ -0,0 +1,50 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file has_flags.lua +-- + +-- attempt to check it from known flags +function _check_from_knownargs(flags, opt) + local flag = flags[1] + -- dotnet MSBuild properties + if flag:startswith("-p:") or flag:startswith("/p:") then + return true + end + -- dotnet CLI options + if flag:startswith("--") then + return true + end + -- common csflags patterns + if flag:startswith("-") or flag:startswith("/") then + return true + end +end + +-- has_flags(flags)? +-- +-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cs|csld|cssh]"} +-- +-- @return true or false +-- +function main(flags, opt) + opt = opt or {} + if _check_from_knownargs(flags, opt) then + return true + end + return false +end diff --git a/xmake/modules/detect/sdks/find_dotnet.lua b/xmake/modules/detect/sdks/find_dotnet.lua index 2dfd2a916..f64acacd3 100644 --- a/xmake/modules/detect/sdks/find_dotnet.lua +++ b/xmake/modules/detect/sdks/find_dotnet.lua @@ -20,16 +20,73 @@ -- imports import("lib.detect.find_file") +import("lib.detect.find_tool") import("core.tool.toolchain") import("core.base.option") import("core.base.global") import("core.project.config") import("core.cache.detectcache") --- find dotnet directory -function _find_sdkdir(sdkdir) +-- find dotnet sdk info from dotnet cli +function _find_dotnet_cli(sdkdir) - -- get sdk directory from vcvars + -- find dotnet program + local paths = {} + if sdkdir then + table.insert(paths, path.join(sdkdir, "bin")) + table.insert(paths, sdkdir) + end + local dotnet = find_tool("dotnet", {version = true, paths = #paths > 0 and paths or nil}) + if not dotnet then + return nil + end + + local bindir = path.directory(dotnet.program) + local result = {bindir = bindir, version = dotnet.version} + + -- get sdk list, e.g. "8.0.100 [/usr/share/dotnet/sdk]" + local sdklist = try { function () return os.iorunv(dotnet.program, {"--list-sdks"}) end } + if sdklist then + local sdks = {} + for _, line in ipairs(sdklist:split("\n", {plain = true})) do + line = line:trim() + local ver, dir = line:match("^(%S+)%s+%[(.-)%]") + if ver and dir then + table.insert(sdks, {version = ver, directory = path.join(dir, ver)}) + end + end + if #sdks > 0 then + result.sdks = sdks + result.sdkdir = sdks[#sdks].directory + result.sdkver = sdks[#sdks].version + end + end + + -- set sdkdir from bindir if not found from sdk list + if not result.sdkdir then + result.sdkdir = sdkdir or path.directory(bindir) + end + + -- get runtime list, e.g. "Microsoft.NETCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]" + local runtimelist = try { function () return os.iorunv(dotnet.program, {"--list-runtimes"}) end } + if runtimelist then + local runtimes = {} + for _, line in ipairs(runtimelist:split("\n", {plain = true})) do + line = line:trim() + local name, ver = line:match("^(%S+)%s+(%S+)%s+%[") + if name and ver then + table.insert(runtimes, {name = name, version = ver}) + end + end + if #runtimes > 0 then + result.runtimes = runtimes + end + end + return result +end + +-- find .NET Framework SDK directory from MSVC (Windows only) +function _find_netfxsdk(sdkdir) if not sdkdir then local msvc = toolchain.load("msvc") if msvc and msvc:check() then @@ -42,55 +99,40 @@ function _find_sdkdir(sdkdir) end end end - return sdkdir -end - --- find dotnet toolchains -function _find_dotnet(sdkdir, sdkver) - - -- find dotnet directory - sdkdir = _find_sdkdir(sdkdir) if not sdkdir or not os.isdir(sdkdir) then return nil end -- get sdk version - if not sdkver then - local vers = {} - for _, dir in ipairs(os.dirs(path.join(sdkdir, "*", "Include"))) do - table.insert(vers, path.filename(path.directory(dir))) - end - for _, ver in ipairs(vers) do - if os.isdir(path.join(sdkdir, ver, "Lib", "um")) and os.isdir(path.join(sdkdir, ver, "Include", "um")) then - sdkver = ver - break - end + local sdkver + local vers = {} + for _, dir in ipairs(os.dirs(path.join(sdkdir, "*", "Include"))) do + table.insert(vers, path.filename(path.directory(dir))) + end + for _, ver in ipairs(vers) do + if os.isdir(path.join(sdkdir, ver, "Lib", "um")) and os.isdir(path.join(sdkdir, ver, "Include", "um")) then + sdkver = ver + break end end if not sdkver then return nil end - - -- get the lib directory - local libdir = path.join(sdkdir, sdkver, "Lib") - - -- get the include directory - local includedir = path.join(sdkdir, sdkver, "Include") - - -- get toolchains - return {sdkdir = sdkdir, libdir = libdir, includedir = includedir, sdkver = sdkver} + return {sdkdir = sdkdir, sdkver = sdkver, + libdir = path.join(sdkdir, sdkver, "Lib"), + includedir = path.join(sdkdir, sdkver, "Include")} end --- find dotnet toolchains +-- find dotnet sdk -- -- @param sdkdir the dotnet directory --- @param opt the argument options, e.g. {verbose = true, force = false, version = "5.9.1"} +-- @param opt the argument options, e.g. {verbose = true, force = false} -- --- @return the dotnet toolchains. e.g. {sdkver = ..., sdkdir = ..., bindir = .., libdir = ..., includedir = ..., .. } +-- @return the dotnet sdk info, e.g. {program = ..., version = ..., sdkver = ..., sdkdir = ..., sdks = {...}, runtimes = {...}} -- -- @code -- --- local toolchains = find_dotnet("~/dotnet") +-- local dotnet = find_dotnet() -- -- @endcode -- @@ -100,30 +142,51 @@ function main(sdkdir, opt) opt = opt or {} -- attempt to load cache first - local key = "detect.sdks.find_dotnet." .. (sdkdir or "") + local key = "detect.sdks.find_dotnet" local cacheinfo = detectcache:get(key) or {} - if not opt.force and cacheinfo.dotnet then + if not opt.force and cacheinfo.dotnet and cacheinfo.dotnet.sdkdir and os.isdir(cacheinfo.dotnet.sdkdir) then return cacheinfo.dotnet end - -- find dotnet - local dotnet = _find_dotnet(sdkdir or config.get("dotnet") or global.get("dotnet"), opt.version or config.get("dotnet_sdkver")) + -- find dotnet cli sdk + local dotnet = _find_dotnet_cli(sdkdir or config.get("dotnet") or global.get("dotnet")) + + -- find .NET Framework SDK on Windows + if is_host("windows") then + local netfxsdk = _find_netfxsdk(sdkdir or config.get("dotnet") or global.get("dotnet")) + if netfxsdk then + if dotnet then + dotnet.netfxsdk = netfxsdk + else + dotnet = netfxsdk + end + end + end + if dotnet then -- save to config - config.set("dotnet", dotnet.sdkdir, {force = true, readonly = true}) - config.set("dotnet_sdkver", dotnet.sdkver, {force = true, readonly = true}) + if dotnet.sdkdir then + config.set("dotnet", dotnet.sdkdir, {force = true, readonly = true}) + end + if dotnet.sdkver then + config.set("dotnet_sdkver", dotnet.sdkver, {force = true, readonly = true}) + end -- trace if opt.verbose or option.get("verbose") then - cprint("checking for .Net SDK directory ... ${color.success}%s", dotnet.sdkdir) - cprint("checking for .Net SDK version ... ${color.success}%s", dotnet.sdkver) + if dotnet.version then + cprint("checking for .NET SDK version ... ${color.success}%s", dotnet.version) + end + if dotnet.sdkdir then + cprint("checking for .NET SDK directory ... ${color.success}%s", dotnet.sdkdir) + end end else -- trace if opt.verbose or option.get("verbose") then - cprint("checking for .Net SDK directory ... ${color.nothing}${text.nothing}") + cprint("checking for .NET SDK directory ... ${color.nothing}${text.nothing}") end end diff --git a/xmake/modules/detect/tools/find_dotnet.lua b/xmake/modules/detect/tools/find_dotnet.lua new file mode 100644 index 000000000..ac17979ec --- /dev/null +++ b/xmake/modules/detect/tools/find_dotnet.lua @@ -0,0 +1,53 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file find_dotnet.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find dotnet +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local dotnet = find_dotnet() +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + opt.check = opt.check or "--version" + opt.command = opt.command or "--version" + + -- find program + local program = find_program(opt.program or "dotnet", opt) + + -- find program version + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + return program, version +end diff --git a/xmake/modules/package/manager/nuget/find_package.lua b/xmake/modules/package/manager/nuget/find_package.lua index a206eb301..4a27eed19 100644 --- a/xmake/modules/package/manager/nuget/find_package.lua +++ b/xmake/modules/package/manager/nuget/find_package.lua @@ -28,6 +28,82 @@ import("core.project.config") import("core.project.target") import("private.utils.toolchain", {alias = "toolchain_utils"}) +function _find_target_root(targets, name) + local namelower = name:lower() + for targetname in pairs(targets or {}) do + local targetpkg = targetname:match("^([^/]+)") or targetname + local targetpkglower = targetpkg:lower() + local targetnamelower = targetname:lower() + local normalized_targetpkg = targetpkglower:gsub("[._%-]", "") + local normalized_name = namelower:gsub("[._%-]", "") + if targetnamelower == namelower + or targetnamelower:startswith(namelower .. "/") + or targetpkglower == namelower + or normalized_targetpkg == normalized_name then + return targetname + end + end +end + +function _find_library_root(libraries, name) + local namelower = name:lower() + for libraryname in pairs(libraries or {}) do + local librarypkg = libraryname:match("^([^/]+)") or libraryname + local librarypkglower = librarypkg:lower() + local librarynamelower = libraryname:lower() + local normalized_librarypkg = librarypkglower:gsub("[._%-]", "") + local normalized_name = namelower:gsub("[._%-]", "") + if librarynamelower == namelower + or librarynamelower:startswith(namelower .. "/") + or librarypkglower == namelower + or normalized_librarypkg == normalized_name then + return libraryname + end + end +end + +function _find_version_in_project_dependencies(manifest, name) + local namelower = name:lower() + local normalized_name = namelower:gsub("[._%-]", "") + if manifest.project and manifest.project.frameworks then + for _, framework in pairs(manifest.project.frameworks) do + local dependencies = framework.dependencies or {} + for depname, depver in pairs(dependencies) do + local deplower = depname:lower() + local normalized_depname = deplower:gsub("[._%-]", "") + if deplower == namelower or normalized_depname == normalized_name then + return tostring(depver) + end + end + end + end +end + +function _get_packagesdir_from_manifest(manifest) + if manifest.project and manifest.project.restore and manifest.project.restore.packagesPath then + return manifest.project.restore.packagesPath + end + if manifest.packageFolders then + for folder in pairs(manifest.packageFolders) do + return folder + end + end + local packagesdir = os.getenv("NUGET_PACKAGES") + if packagesdir and #packagesdir > 0 then + return packagesdir + end + local homedir = os.homedir and os.homedir() + if homedir and #homedir > 0 then + return path.join(homedir, ".nuget", "packages") + end +end + +local function _extract_version_from_root(rootname) + if rootname then + return rootname:match("/(.+)$") + end +end + -- check if pattern matches as a complete path component -- e.g. "x86" should match "/x86/" but not "/x86_64/" function _match_path_component(file_lower, pattern) @@ -86,7 +162,7 @@ function _match_libfile(file, libarch, toolset, libmode, runtime) if file_lower:find(libmode:lower(), 1, true) then score = score + 2 end - if file_lower:find(runtime:lower(), 1, true) then + if runtime and file_lower:find(runtime:lower(), 1, true) then score = score + 1 end return score @@ -105,9 +181,10 @@ function _find_package(name, result, opt) MTd = "MultiThreadedDebug", MD = "MultiThreadedDLL", MDd = "MultiThreadedDebugDLL"} + local runtime = runtimes[configs.runtimes] local installdir = path.join(opt.packagesdir, name) - local runtime = assert(runtimes[configs.runtimes], "unknown runtimes %s", configs.runtimes) - local toolset = toolchain_utils.get_vs_toolset_ver(toolchain.load("msvc", {plat = plat, arch = arch}):config("vs_toolset") or config.get("vs_toolset")) + local msvc = toolchain.load("msvc", {plat = plat, arch = arch}) + local toolset = msvc and toolchain_utils.get_vs_toolset_ver(msvc:config("vs_toolset") or config.get("vs_toolset")) or nil local libarch = libarchs[arch] or "x64" local libmode = configs.debug and "Debug" or "Release" for _, file in ipairs(libinfo.files) do @@ -169,6 +246,22 @@ function _find_package(name, result, opt) end end +function _cleanup_result(result, version) + result._libscores = nil + if version and not result.version then + result.version = version + end + if result.links or result.includedirs then + if result.includedirs then + result.includedirs = table.unique(result.includedirs) + end + if result.linkdirs then + result.linkdirs = table.unique(result.linkdirs) + end + end + return result +end + -- find package from the nuget package manager -- -- @param name the package name, e.g. zlib, pcre @@ -185,46 +278,57 @@ function main(name, opt) return end local manifest = json.loadfile(manifestfile) - local targets - for k, v in pairs(manifest.targets) do - targets = v - break + local packagesdir = _get_packagesdir_from_manifest(manifest) + if not manifest.libraries then + return end - local target_root - if targets then - for k, v in pairs(targets) do - if k:startswith(name) then - target_root = k - break + + if manifest.targets and packagesdir then + for _, targets in pairs(manifest.targets) do + local target_root = _find_target_root(targets, name) + if target_root then + local metainfo = {} + metainfo.plat = opt.plat + metainfo.arch = opt.arch + metainfo.mode = opt.mode + metainfo.configs = opt.configs + metainfo.targets = targets + metainfo.libraries = manifest.libraries + metainfo.packagesdir = packagesdir + local result = {} + _find_package(target_root, result, metainfo) + return _cleanup_result(result, _extract_version_from_root(target_root)) end end end - local metainfo = {} - 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 - 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, result, metainfo) - -- clean up internal scoring data - result._libscores = nil - if result.links or result.includedirs then - -- deduplicate paths - if result.includedirs then - result.includedirs = table.unique(result.includedirs) - end - if result.linkdirs then - result.linkdirs = table.unique(result.linkdirs) - end - return result + + local library_root = _find_library_root(manifest.libraries, name) + if library_root then + if packagesdir then + local metainfo = {} + metainfo.plat = opt.plat + metainfo.arch = opt.arch + metainfo.mode = opt.mode + metainfo.configs = opt.configs + metainfo.targets = {} + metainfo.libraries = manifest.libraries + metainfo.packagesdir = packagesdir + local result = {} + _find_package(library_root, result, metainfo) + return _cleanup_result(result, _extract_version_from_root(library_root)) end + -- package exists in manifest but package root cannot be determined, + -- treat managed-only package as found. + return {version = _extract_version_from_root(library_root)} end -end - + -- fallback for managed package references if target/library keys are not aligned + -- with the requested package name format. + local depver = _find_version_in_project_dependencies(manifest, name) + if depver then + return {version = depver} + end + if opt.require_version then + return {version = tostring(opt.require_version)} + end +end diff --git a/xmake/modules/target/action/install/main.lua b/xmake/modules/target/action/install/main.lua index 2e92122e8..466ebe551 100644 --- a/xmake/modules/target/action/install/main.lua +++ b/xmake/modules/target/action/install/main.lua @@ -49,8 +49,6 @@ function _get_target_includedir(target, opt) return path.join(opt.installdir, opt.includedir) end - - -- copy file with symlinks function _copy_file_with_symlinks(srcfile, outputdir) if os.islink(srcfile) then |
