Browse Source

telegram-bot-api: add package. (#7855)

* telegram-bot-api: add package.

* update.

* update.

* Update xmake.lua

* Update xmake.lua

* update.

---------

Co-authored-by: star9029 <[email protected]>
RedbeanW 3 weeks ago
parent
commit
2e70cddb8a
4 changed files with 244 additions and 1 deletions
  1. 108 0
      packages/t/td/xmake.lua
  2. 74 0
      packages/t/tdtl/xmake.lua
  3. 62 0
      packages/t/telegram-bot-api/xmake.lua
  4. 0 1
      scripts/test.lua

+ 108 - 0
packages/t/td/xmake.lua

@@ -0,0 +1,108 @@
+package("td")
+    set_homepage("https://core.telegram.org/tdlib/")
+    set_description("Cross-platform library for building Telegram clients.")
+    set_license("BSL-1.0")
+
+    -- td doesn't seem to like tags, so we go directly to commit id.
+    -- @see https://github.com/tdlib/td/commits/HEAD/example/web/tdweb/package.json
+    add_urls("https://github.com/tdlib/td.git")
+    add_versions("1.8.51", "bb474a201baa798784d696d2d9d762a9d2807f96")
+
+    if is_plat("wasm") then
+        add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
+    end
+
+    add_deps("cmake")
+    add_deps("openssl3", "zlib", "gperf")
+
+    on_check("@msys", function (package)
+        -- @see https://github.com/xmake-io/xmake-repo/pull/7855#issuecomment-3176844746
+        if is_subhost("msys") and not package:is_arch64() then
+            raise("package(td): MINGW32 under MSYS will not be able to compile this package due to insufficient memory.")
+        end
+    end)
+
+    on_load(function(package)
+        if package:is_plat("linux", "bsd") then
+            package:add("syslinks", "pthread", "dl")
+        elseif package:is_plat("android") then
+            package:add("syslinks", "dl", "log")
+        elseif package:is_plat("windows", "mingw", "msys", "cygwin") then
+            package:add("syslinks", "ws2_32", "mswsock", "crypt32", "normaliz", "psapi")
+        end
+
+        if package:is_cross() then
+            package:add("deps", "tdtl " .. package:version_str())
+        end
+
+        package:add("links", "tdjson", "tdjson_static", "tdjson_private", "tdclient", "tdcore", "tdcore_part1", "tdcore_part2", "tdmtproto", "tdapi", "tddb", "tdsqlite", "tdnet", "tdactor", "tde2e", "tdutils")
+        if not package:config("shared") then
+            package:add("defines", "TDJSON_STATIC_DEFINE")
+        end
+    end)
+
+    on_install(function (package)
+        function install_header(from_dir, to_dir)
+            os.cp(from_dir .. "/**.h", package:installdir("include/" .. to_dir), {rootdir = from_dir})
+            os.cp(from_dir .. "/**.hpp", package:installdir("include/" .. to_dir), {rootdir = from_dir})
+        end
+
+        local configs = {}
+        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
+        table.insert(configs, "-DTD_INSTALL_STATIC_LIBRARIES=" .. (package:config("shared") and "OFF" or "ON"))
+        table.insert(configs, "-DTD_INSTALL_SHARED_LIBRARIES=" .. (package:config("shared") and "ON" or "OFF"))
+        table.insert(configs, "-DTD_ENABLE_LTO=" .. (package:config("lto") and "ON" or "OFF"))
+        if package:is_cross() then
+            os.cd("td/generate")
+            os.mkdir("auto")
+            print("Generate TLO files")
+            os.mkdir("auto/tlo")
+            os.vrun("tl-parser -e auto/tlo/mtproto_api.tlo scheme/mtproto_api.tl")
+            os.vrun("tl-parser -e auto/tlo/secret_api.tlo scheme/secret_api.tl")
+            os.vrun("tl-parser -e auto/tlo/e2e_api.tlo scheme/e2e_api.tl")
+            os.vrun("tl-parser -e auto/tlo/td_api.tlo scheme/td_api.tl")
+            os.vrun("tl-parser -e auto/tlo/telegram_api.tlo scheme/telegram_api.tl")
+            os.cd("auto")
+            os.mkdir("td")
+            os.mkdir("td/telegram")
+            os.mkdir("td/mtproto")
+            print("Generate MTProto API source files")
+            os.vrun("generate_mtproto")
+            print("Generate common TL source files")
+            os.vrun("generate_common")
+            print("Generate JSON TL source files")
+            os.vrun("generate_json")
+            print("Generate MIME Types source files")
+            os.cd("../../../tdutils/generate")
+            os.mkdir("auto")
+            os.vrun("generate_mime_types_gperf mime_types.txt auto/mime_type_to_extension.gperf auto/extension_to_mime_type.gperf")
+            os.vrun("gperf -m100 --output-file=auto/mime_type_to_extension.cpp auto/mime_type_to_extension.gperf")
+            os.vrun("gperf -m100 --output-file=auto/extension_to_mime_type.cpp auto/extension_to_mime_type.gperf")
+            os.cd("../..")
+        end
+        if is_plat("mingw", "msys") then
+            io.replace("td/generate/tl-parser/wgetopt.h", "#ifdef __GNU_LIBRARY__", "#if 1", {plain = true})
+            io.replace("td/generate/tl-parser/wgetopt.c", "extern char *getenv();", "#include <stdlib.h>", {plain = true})
+        end
+
+        import("package.tools.cmake").install(package, configs, {target = package:config("shared") and "tdjson" or "tdjson_static", builddir = "build"})
+
+        if not package:config("shared") then
+            install_header("td/mtproto", "td/mtproto")
+            install_header("td/telegram", "td/telegram")
+            install_header("tdnet/td/net", "td/net")
+            install_header("tddb/td/db", "td/db")
+            install_header("tdactor/td/actor", "td/actor")
+            install_header("tde2e/td/e2e", "td/e2e")
+            install_header("tdutils/td/utils", "td/utils")
+            os.cp("build/tdutils/td/utils/config.h", package:installdir("include/td/utils"))
+        end
+    end)
+
+    on_test(function (package)
+        assert(package:check_cxxsnippets({test = [[
+            void test() {
+                td_json_client_create();
+            }
+        ]]}, {configs = {languages = "c++17"}, includes = "td/telegram/td_json_client.h"}))
+    end)

+ 74 - 0
packages/t/tdtl/xmake.lua

@@ -0,0 +1,74 @@
+package("tdtl")
+    set_kind("binary")
+    set_description("Toolset for building tdlib.")
+    set_license("BSL-1.0")
+
+    add_urls("https://github.com/tdlib/td.git")
+    add_versions("1.8.51", "bb474a201baa798784d696d2d9d762a9d2807f96")
+
+    local tools = {
+        {
+            project = "tl-parser",
+            executable = "tl-parser",
+            sourcedir = "td/generate/tl-parser/"
+        },
+        {
+            project = "tl_generate_mtproto",
+            executable = "generate_mtproto",
+            sourcedir = "td/generate/"
+        },
+        {
+            project = "tl_generate_common",
+            executable = "generate_common",
+            sourcedir = "td/generate/"
+        },
+        {
+            project = "tl_generate_json",
+            executable = "generate_json",
+            sourcedir = "td/generate/"
+        },
+        {
+            project = "generate_mime_types_gperf",
+            executable = "generate_mime_types_gperf",
+            sourcedir = "tdutils/generate/"
+        }
+    }
+
+    add_deps("cmake", "zlib")
+    add_deps("gperf")
+    on_install(function (package)
+        local targets = {}
+        for _, tool in ipairs(tools) do
+            table.insert(targets, tool.project)
+        end
+        io.replace("tdutils/CMakeLists.txt", "add_dependencies(tdutils tdmime_auto)", "", {plain = true})
+        io.replace("tdutils/CMakeLists.txt", "${TDMIME_AUTO}\n", "", {plain = true})
+        io.replace("td/generate/CMakeLists.txt", "${TDMIME_AUTO}\n", "", {plain = true})
+        io.replace("td/generate/CMakeLists.txt", "COMMAND", "COMMENT", {plain = true})
+
+        if is_plat("mingw", "msys") then
+            io.replace("td/generate/tl-parser/wgetopt.h", "#ifdef __GNU_LIBRARY__", "#if 1", {plain = true})
+            io.replace("td/generate/tl-parser/wgetopt.c", "extern char *getenv();", "#include <stdlib.h>", {plain = true})
+        end
+
+        import("package.tools.cmake").build(package, {}, {target = targets, builddir = "build"})
+
+        os.cd("build")
+        for _, tool in ipairs(tools) do
+            local tooldir = tool.sourcedir .. tool.executable
+            if is_host("windows") then
+                tooldir = tooldir .. ".exe"
+            end
+            os.cp(tooldir, package:installdir("bin"))
+        end
+    end)
+
+    on_test(function (package)
+        for _, tool in ipairs(tools) do
+            local tooldir = package:installdir("bin", tool.executable)
+            if is_host("windows") then
+                tooldir = tooldir .. ".exe"
+            end
+            assert(os.isexec(tooldir), tool.executable .. " not found!")
+        end
+    end)

+ 62 - 0
packages/t/telegram-bot-api/xmake.lua

@@ -0,0 +1,62 @@
+package("telegram-bot-api")
+    set_kind("binary")
+    set_description("The server provides an HTTP API for creating Telegram Bots.")
+    set_license("BSL-1.0")
+
+    add_urls("https://github.com/tdlib/telegram-bot-api.git", {submodules = false})
+    add_versions("9.1", "2e1fb0330c93a014f723f5b5d8befe9dc9fc1b7d")
+
+    local tdlib_versions = {
+        ["9.1"] = "1.8.51"
+    }
+
+    on_load(function (package)
+        package:add("deps", "td " .. tdlib_versions[package:version_str()])
+    end)
+    
+    on_install(function (package)
+        io.replace("telegram-bot-api/ClientManager.cpp", [[#include "memprof/memprof.h"]], "", {plain = true})
+        io.replace("telegram-bot-api/ClientManager.cpp", "if (is_memprof_on()) {", "#if 0", {plain = true})
+        io.replace("telegram-bot-api/ClientManager.cpp", "success_rate());\n  }", "success_rate());\n  #endif", {plain = true})
+        
+        io.writefile("xmake.lua", ([[
+            add_rules("mode.debug", "mode.release")
+            add_requires("td %s", {configs = {shared = false}})
+            target("telegram-bot-api")
+                set_kind("binary")
+                set_languages("c++17")
+                add_files("telegram-bot-api/*.cpp")
+                add_includedirs(".")
+                add_packages("td")
+                if is_plat("windows") then
+                    add_defines("_SCL_SECURE_NO_WARNINGS", "_CRT_SECURE_NO_WARNINGS")
+                    add_cxflags("/utf-8", "/GR-", "/W4", "/wd4100", "/wd4127", "/wd4324", "/wd4505", "/wd4814", "/wd4702", "/bigobj", {tools = {"cl", "clang_cl"}})
+                    add_syslinks("shell32")
+                end
+                if is_plat("windows", "mingw", "msys") then
+                    add_defines("NTDDI_VERSION=0x06020000", "WINVER=0x0602", "_WIN32_WINNT=0x0602", "PSAPI_VERSION=1", "NOMINMAX", "UNICODE", "_UNICODE", "WIN32_LEAN_AND_MEAN")
+                end
+                if is_plat("cygwin") then
+                    add_defines("_DEFAULT_SOURCE=1", "FD_SETSIZE=4096")
+                end
+                if is_plat("macosx", "iphoneos") then
+                    add_shflags("-Wl,-dead_strip")
+                elseif is_plat("wasm") then
+                    add_shflags("-Wl,--gc-sections")
+                elseif is_plat("android") then
+                    add_shflags("-Wl,--gc-sections", "-Wl,--exclude-libs,ALL", "-Wl,--icf=safe")
+                else
+                end
+                if is_arch("x64", "x86_64") then
+                    add_shflags("-Wl,-z,max-page-size=16384")
+                end
+                add_cxflags("-ffunction-sections", "-fdata-sections", "-fno-omit-frame-pointer", "-fno-exceptions", "-fno-rtti", {tools = {"clang", "gcc"}})
+                add_shflags("-Wl,--gc-sections", "-Wl,--exclude-libs,ALL")
+                add_defines("_FILE_OFFSET_BITS=64", "_GNU_SOURCE")
+        ]]):format(tdlib_versions[package:version_str()]))
+        import("package.tools.xmake").install(package)
+    end)
+
+    on_test(function (package)
+        os.vrun("telegram-bot-api --version")
+    end)

+ 0 - 1
scripts/test.lua

@@ -291,7 +291,6 @@ end
 function _lock_packages(packages)
     local locked_packages = {
         "flashlight",
-        "telegram-bot-api",
         "systemd",
         "libxcrypt",
         "libselinux",