From d7f6d8de9f103c44448e952d9d1b88c0fdaab0bb Mon Sep 17 00:00:00 2001 From: Morris Date: Wed, 18 Nov 2026 19:15:05 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E9=BB=98=E8=AE=A4=E7=BC=96=20Flutter?= =?UTF-8?q?=20=E5=89=8D=E7=AB=AF=EF=BC=8C=E8=A1=A5=20Linux=20=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=B9=B6=E6=8C=89=E4=B8=BB=E6=9C=BA=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E4=BA=A7=E7=89=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此前 build.sh 在非 Windows 宿主上把前端当「可选项」:flutter 缺失只告警、 交叉编译失败只告警、macOS 前端在 Linux 上直接跳过。结果是包照出,用户拿到 的是 Avalonia 回退界面,而构建日志里一行红字很容易被忽略。 改为默认必需: - build_flutter_ui / build_flutter_ui_cross 缺 flutter 或编译失败一律返回非 0, build.sh 立即中断并给出原因(交叉编译失败时带日志尾巴) - 确实要出降级包时显式声明 ./build.sh build --allow-no-ui 目标平台按构建主机决定,每条产物都带 Flutter: macOS 主机 → mac-arm64(Xcode 原生)+ win-amd64/win-arm64(mingw 交叉) Linux 主机 → linux-amd64(原生)+ win-amd64/win-arm64(mingw 交叉) Windows 主机 → win-amd64/win-arm64(原生) Linux 目标此前根本不存在,这次一并补上: - GC-UI/frontend/linux/ 由 flutter create 生成,BINARY_NAME=GC-UI、 APPLICATION_ID=com.xhmcl.GC、窗口标题 "Morris GC" - build_flutter_ui 增加 linux 分支,bundle(GC-UI + lib/ + data/)铺进 dist/linux-amd64/,pack_launcher 增加 Linux 整目录打包 - linux/CMakeLists.txt 强制 CMAKE_INSTALL_PREFIX 指向 build/.../bundle: 某些环境下它会被初始化成 /usr/local,导致 flutter 报「构建成功」但 bundle 是空的、产物装进了系统目录(本机实测踩到) 顺带修正两处: - 生成 settings.json 的默认 updateRepository 还是 gitcode 地址,与只保留 xhmcl 源的现状不符 - 进度条权重改为按实际目标动态计算,新平台不会再把总进度算过 100% 验证(Linux 主机,flutter 3.24.5): - ./build.sh build 出 win-amd64 109 MB、win-arm64 105 MB、linux-amd64 65 MB, 三份均含 Flutter 前端,清单签名 Verified - Linux 包在 Xvfb + llvmpipe 下实跑:后端 /api/health 返回 ok,GC-UI 窗口 正常渲染出模型库首页 - macOS 目标在 Linux 上不再产出 Avalonia 空壳包(要它得回 macOS 编) --- GC-UI/CI.md | 24 +++- GC-UI/frontend/.metadata | 3 + GC-UI/frontend/linux/.gitignore | 1 + GC-UI/frontend/linux/CMakeLists.txt | 146 ++++++++++++++++++++ GC-UI/frontend/linux/flutter/CMakeLists.txt | 88 ++++++++++++ GC-UI/frontend/linux/main.cc | 6 + GC-UI/frontend/linux/my_application.cc | 124 +++++++++++++++++ GC-UI/frontend/linux/my_application.h | 18 +++ build.sh | 4 +- buildsh/build.sh | 87 +++++++++--- buildsh/lib/build_funcs.sh | 58 ++++++-- 11 files changed, 526 insertions(+), 33 deletions(-) create mode 100644 GC-UI/frontend/linux/.gitignore create mode 100644 GC-UI/frontend/linux/CMakeLists.txt create mode 100644 GC-UI/frontend/linux/flutter/CMakeLists.txt create mode 100644 GC-UI/frontend/linux/main.cc create mode 100644 GC-UI/frontend/linux/my_application.cc create mode 100644 GC-UI/frontend/linux/my_application.h diff --git a/GC-UI/CI.md b/GC-UI/CI.md index 09962ae..4512829 100644 --- a/GC-UI/CI.md +++ b/GC-UI/CI.md @@ -64,10 +64,16 @@ Windows 还需要 **VS Build Tools 的「使用 C++ 的桌面开发」工作负 ## 本地构建(不经 CI) ```bash -# Linux / macOS:三平台一次出齐。Windows 前端用 mingw-w64 + wine 交叉编译, -# 不需要 Windows 机器。 +# 按构建主机自动选目标,每条产物都带 Flutter 前端: +# macOS 主机 → mac-arm64(原生)+ win-amd64 / win-arm64(mingw 交叉) +# Linux 主机 → linux-amd64(原生)+ win-amd64 / win-arm64(mingw 交叉) +# Windows 主机 → win-amd64 / win-arm64(原生) ./build.sh build +# 前端是必需项:flutter 缺失或编译失败会直接中断, +# 确需降级成只有 Avalonia 回退界面的包时才用: +./build.sh build --allow-no-ui + # 只编 macOS 前端 ./GC-UI/build-ui.sh macos @@ -75,6 +81,20 @@ Windows 还需要 **VS Build Tools 的「使用 C++ 的桌面开发」工作负 GC-UI\build-ui.cmd ``` +### Linux 前端(原生) + +Flutter 的 Linux 桌面端只能在 Linux 上编,需要 GTK3 开发库与 C++ 工具链: + +```bash +sudo apt install clang cmake ninja-build pkg-config libgtk-3-dev libstdc++-14-dev +``` + +`libstdc++-14-dev` 容易漏:clang 18 会去 `/usr/lib/gcc/x86_64-linux-gnu/14/` 找 +`-lstdc++`,系统只装了 gcc-13 的开发包时链接会失败,报 `cannot find -lstdc++`。 + +产物布局:`GC-UI` + `lib/libflutter_linux_gtk.so` + `data/`,与官方 `flutter build +linux` 的 bundle 一致,直接铺进 `dist/linux-amd64/`。 + ### Windows 前端交叉编译 Flutter 官方不支持在非 Windows 主机上 `flutter build windows`,但 Windows 嵌入层 diff --git a/GC-UI/frontend/.metadata b/GC-UI/frontend/.metadata index b3d77c5..cbf0cf3 100644 --- a/GC-UI/frontend/.metadata +++ b/GC-UI/frontend/.metadata @@ -15,6 +15,9 @@ migration: - platform: root create_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 base_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 + - platform: linux + create_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 + base_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 - platform: macos create_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 base_revision: dec2ee5c1f98f8e84a7d5380c05eb8a3d0a81668 diff --git a/GC-UI/frontend/linux/.gitignore b/GC-UI/frontend/linux/.gitignore new file mode 100644 index 0000000..d3896c9 --- /dev/null +++ b/GC-UI/frontend/linux/.gitignore @@ -0,0 +1 @@ +flutter/ephemeral diff --git a/GC-UI/frontend/linux/CMakeLists.txt b/GC-UI/frontend/linux/CMakeLists.txt new file mode 100644 index 0000000..234d0dd --- /dev/null +++ b/GC-UI/frontend/linux/CMakeLists.txt @@ -0,0 +1,146 @@ +# Project-level configuration. +cmake_minimum_required(VERSION 3.10) +project(runner LANGUAGES CXX) + +# The name of the executable created for the application. Change this to change +# the on-disk name of your application. +set(BINARY_NAME "GC-UI") +# The unique GTK application identifier for this application. See: +# https://wiki.gnome.org/HowDoI/ChooseApplicationID +set(APPLICATION_ID "com.xhmcl.GC") + +# Explicitly opt in to modern CMake behaviors to avoid warnings with recent +# versions of CMake. +cmake_policy(SET CMP0063 NEW) + +# Load bundled libraries from the lib/ directory relative to the binary. +set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") + +# Root filesystem for cross-building. +if(FLUTTER_TARGET_PLATFORM_SYSROOT) + set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +endif() + +# Define build configuration options. +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE "Debug" CACHE + STRING "Flutter build mode" FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Profile" "Release") +endif() + +# Compilation settings that should be applied to most targets. +# +# Be cautious about adding new options here, as plugins use this function by +# default. In most cases, you should add new options to specific targets instead +# of modifying this function. +function(APPLY_STANDARD_SETTINGS TARGET) + target_compile_features(${TARGET} PUBLIC cxx_std_14) + target_compile_options(${TARGET} PRIVATE -Wall -Werror) + target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") + target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") +endfunction() + +# Flutter library and tool build rules. +set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") +add_subdirectory(${FLUTTER_MANAGED_DIR}) + +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) + +add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") + +# Define the application target. To change its name, change BINARY_NAME above, +# not the value here, or `flutter run` will no longer work. +# +# Any new source files that you add to the application should be added here. +add_executable(${BINARY_NAME} + "main.cc" + "my_application.cc" + "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" +) + +# Apply the standard set of build settings. This can be removed for applications +# that need different build settings. +apply_standard_settings(${BINARY_NAME}) + +# Add dependency libraries. Add any application-specific dependencies here. +target_link_libraries(${BINARY_NAME} PRIVATE flutter) +target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) + +# Run the Flutter tool portions of the build. This must not be removed. +add_dependencies(${BINARY_NAME} flutter_assemble) + +# Only the install-generated bundle's copy of the executable will launch +# correctly, since the resources must in the right relative locations. To avoid +# people trying to run the unbundled copy, put it in a subdirectory instead of +# the default top-level location. +set_target_properties(${BINARY_NAME} + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run" +) + + +# Generated plugin build rules, which manage building the plugins and adding +# them to the application. +include(flutter/generated_plugins.cmake) + + +# === Installation === +# By default, "installing" just makes a relocatable bundle in the build +# directory. +set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") +# 无条件强制安装到 build 目录下的 bundle:某些环境下 CMAKE_INSTALL_PREFIX +# 会被初始化成 /usr/local,导致 flutter build linux 声称成功、却把产物装进 +# 系统目录,build/.../bundle 是空的。 +set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "Flutter bundle 输出目录" FORCE) + +# Start with a clean build bundle directory every time. +install(CODE " + file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") + " COMPONENT Runtime) + +set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") +set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES}) + install(FILES "${bundled_library}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endforeach(bundled_library) + +# Copy the native assets provided by the build.dart from all packages. +set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") +install(DIRECTORY "${NATIVE_ASSETS_DIR}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +# Fully re-copy the assets directory on each build to avoid having stale files +# from a previous install. +set(FLUTTER_ASSET_DIR_NAME "flutter_assets") +install(CODE " + file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") + " COMPONENT Runtime) +install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" + DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) + +# Install the AOT library on non-Debug builds only. +if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") + install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endif() diff --git a/GC-UI/frontend/linux/flutter/CMakeLists.txt b/GC-UI/frontend/linux/flutter/CMakeLists.txt new file mode 100644 index 0000000..d5bd016 --- /dev/null +++ b/GC-UI/frontend/linux/flutter/CMakeLists.txt @@ -0,0 +1,88 @@ +# This file controls Flutter-level build steps. It should not be edited. +cmake_minimum_required(VERSION 3.10) + +set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") + +# Configuration provided via flutter tool. +include(${EPHEMERAL_DIR}/generated_config.cmake) + +# TODO: Move the rest of this into files in ephemeral. See +# https://github.com/flutter/flutter/issues/57146. + +# Serves the same purpose as list(TRANSFORM ... PREPEND ...), +# which isn't available in 3.10. +function(list_prepend LIST_NAME PREFIX) + set(NEW_LIST "") + foreach(element ${${LIST_NAME}}) + list(APPEND NEW_LIST "${PREFIX}${element}") + endforeach(element) + set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) +endfunction() + +# === Flutter Library === +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) +pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) +pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) + +set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") + +# Published to parent scope for install step. +set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) +set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) +set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) +set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) + +list(APPEND FLUTTER_LIBRARY_HEADERS + "fl_basic_message_channel.h" + "fl_binary_codec.h" + "fl_binary_messenger.h" + "fl_dart_project.h" + "fl_engine.h" + "fl_json_message_codec.h" + "fl_json_method_codec.h" + "fl_message_codec.h" + "fl_method_call.h" + "fl_method_channel.h" + "fl_method_codec.h" + "fl_method_response.h" + "fl_plugin_registrar.h" + "fl_plugin_registry.h" + "fl_standard_message_codec.h" + "fl_standard_method_codec.h" + "fl_string_codec.h" + "fl_value.h" + "fl_view.h" + "flutter_linux.h" +) +list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") +add_library(flutter INTERFACE) +target_include_directories(flutter INTERFACE + "${EPHEMERAL_DIR}" +) +target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") +target_link_libraries(flutter INTERFACE + PkgConfig::GTK + PkgConfig::GLIB + PkgConfig::GIO +) +add_dependencies(flutter flutter_assemble) + +# === Flutter tool backend === +# _phony_ is a non-existent file to force this command to run every time, +# since currently there's no way to get a full input/output list from the +# flutter tool. +add_custom_command( + OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} + ${CMAKE_CURRENT_BINARY_DIR}/_phony_ + COMMAND ${CMAKE_COMMAND} -E env + ${FLUTTER_TOOL_ENVIRONMENT} + "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" + ${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE} + VERBATIM +) +add_custom_target(flutter_assemble DEPENDS + "${FLUTTER_LIBRARY}" + ${FLUTTER_LIBRARY_HEADERS} +) diff --git a/GC-UI/frontend/linux/main.cc b/GC-UI/frontend/linux/main.cc new file mode 100644 index 0000000..e7c5c54 --- /dev/null +++ b/GC-UI/frontend/linux/main.cc @@ -0,0 +1,6 @@ +#include "my_application.h" + +int main(int argc, char** argv) { + g_autoptr(MyApplication) app = my_application_new(); + return g_application_run(G_APPLICATION(app), argc, argv); +} diff --git a/GC-UI/frontend/linux/my_application.cc b/GC-UI/frontend/linux/my_application.cc new file mode 100644 index 0000000..d4cc6fd --- /dev/null +++ b/GC-UI/frontend/linux/my_application.cc @@ -0,0 +1,124 @@ +#include "my_application.h" + +#include +#ifdef GDK_WINDOWING_X11 +#include +#endif + +#include "flutter/generated_plugin_registrant.h" + +struct _MyApplication { + GtkApplication parent_instance; + char** dart_entrypoint_arguments; +}; + +G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) + +// Implements GApplication::activate. +static void my_application_activate(GApplication* application) { + MyApplication* self = MY_APPLICATION(application); + GtkWindow* window = + GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); + + // Use a header bar when running in GNOME as this is the common style used + // by applications and is the setup most users will be using (e.g. Ubuntu + // desktop). + // If running on X and not using GNOME then just use a traditional title bar + // in case the window manager does more exotic layout, e.g. tiling. + // If running on Wayland assume the header bar will work (may need changing + // if future cases occur). + gboolean use_header_bar = TRUE; +#ifdef GDK_WINDOWING_X11 + GdkScreen* screen = gtk_window_get_screen(window); + if (GDK_IS_X11_SCREEN(screen)) { + const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); + if (g_strcmp0(wm_name, "GNOME Shell") != 0) { + use_header_bar = FALSE; + } + } +#endif + if (use_header_bar) { + GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); + gtk_widget_show(GTK_WIDGET(header_bar)); + gtk_header_bar_set_title(header_bar, "Morris GC"); + gtk_header_bar_set_show_close_button(header_bar, TRUE); + gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); + } else { + gtk_window_set_title(window, "Morris GC"); + } + + gtk_window_set_default_size(window, 1280, 720); + gtk_widget_show(GTK_WIDGET(window)); + + g_autoptr(FlDartProject) project = fl_dart_project_new(); + fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments); + + FlView* view = fl_view_new(project); + gtk_widget_show(GTK_WIDGET(view)); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); + + fl_register_plugins(FL_PLUGIN_REGISTRY(view)); + + gtk_widget_grab_focus(GTK_WIDGET(view)); +} + +// Implements GApplication::local_command_line. +static gboolean my_application_local_command_line(GApplication* application, gchar*** arguments, int* exit_status) { + MyApplication* self = MY_APPLICATION(application); + // Strip out the first argument as it is the binary name. + self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); + + g_autoptr(GError) error = nullptr; + if (!g_application_register(application, nullptr, &error)) { + g_warning("Failed to register: %s", error->message); + *exit_status = 1; + return TRUE; + } + + g_application_activate(application); + *exit_status = 0; + + return TRUE; +} + +// Implements GApplication::startup. +static void my_application_startup(GApplication* application) { + //MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application startup. + + G_APPLICATION_CLASS(my_application_parent_class)->startup(application); +} + +// Implements GApplication::shutdown. +static void my_application_shutdown(GApplication* application) { + //MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application shutdown. + + G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application); +} + +// Implements GObject::dispose. +static void my_application_dispose(GObject* object) { + MyApplication* self = MY_APPLICATION(object); + g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); + G_OBJECT_CLASS(my_application_parent_class)->dispose(object); +} + +static void my_application_class_init(MyApplicationClass* klass) { + G_APPLICATION_CLASS(klass)->activate = my_application_activate; + G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line; + G_APPLICATION_CLASS(klass)->startup = my_application_startup; + G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown; + G_OBJECT_CLASS(klass)->dispose = my_application_dispose; +} + +static void my_application_init(MyApplication* self) {} + +MyApplication* my_application_new() { + return MY_APPLICATION(g_object_new(my_application_get_type(), + "application-id", APPLICATION_ID, + "flags", G_APPLICATION_NON_UNIQUE, + nullptr)); +} diff --git a/GC-UI/frontend/linux/my_application.h b/GC-UI/frontend/linux/my_application.h new file mode 100644 index 0000000..72271d5 --- /dev/null +++ b/GC-UI/frontend/linux/my_application.h @@ -0,0 +1,18 @@ +#ifndef FLUTTER_MY_APPLICATION_H_ +#define FLUTTER_MY_APPLICATION_H_ + +#include + +G_DECLARE_FINAL_TYPE(MyApplication, my_application, MY, APPLICATION, + GtkApplication) + +/** + * my_application_new: + * + * Creates a new Flutter-based application. + * + * Returns: a new #MyApplication. + */ +MyApplication* my_application_new(); + +#endif // FLUTTER_MY_APPLICATION_H_ diff --git a/build.sh b/build.sh index f64657b..04ececf 100755 --- a/build.sh +++ b/build.sh @@ -3,6 +3,7 @@ # GC 构建入口(薄封装) # # ./build.sh build 完整发布(原 publish),带 apt 风格进度条 +# 默认要求每个平台都带 Flutter 前端;--allow-no-ui 允许降级 # ./build.sh check 验证:编译 + 自检 + 离屏渲染 + 产物 # ./build.sh models 编译模型 dll + 封面 + 版本戳 # ./build.sh stamp 重新生成 version.json @@ -33,11 +34,12 @@ VERSION=$(sed -n 's/.*\(.*\)<\/Version>.*/\1/p' Directory.Build.props | TASK="${1:-build}" shift 2>/dev/null || true -# 解析 -version=<版本号>(也接受 --version=) +# 解析 -version=<版本号>(也接受 --version=)与 --allow-no-ui CLI_VERSION="" for arg in "$@"; do case "$arg" in -version=*|--version=*) CLI_VERSION="${arg#*=}" ;; + -allow-no-ui|--allow-no-ui) export GC_ALLOW_NO_UI=1 ;; esac done if [ -n "$CLI_VERSION" ]; then diff --git a/buildsh/build.sh b/buildsh/build.sh index 6c8d54f..f306832 100644 --- a/buildsh/build.sh +++ b/buildsh/build.sh @@ -1,13 +1,41 @@ #!/bin/sh # buildsh/build.sh —— 完整发布(原 publish) -# 步骤及权重按实测耗时设定,供进度条估算总进度与剩余时间: -# 清理(2) 版本(3) Mac(35) WinX64(25) WinArm(25) 骨架(3) 打包(5) 收尾(2) = 100 +# +# 目标平台按构建主机决定,每条产物都带 Flutter 前端: +# macOS 主机 : mac-arm64(原生)+ win-amd64 / win-arm64(mingw 交叉) +# Linux 主机 : linux-amd64(原生)+ win-amd64 / win-arm64(mingw 交叉) +# Windows 主机: win-amd64 / win-arm64(原生) +# +# 前端是默认必需项:flutter 缺失或编译失败一律中断,不再悄悄产出只带 Avalonia +# 回退界面的包。确实要降级时用 ./build.sh build --allow-no-ui。 +# +# 进度权重按实测耗时设定,供进度条估算总进度与剩余时间。 bs_build() { ROOT_DIR="$1" : "${APP_PROJ:?}" "${SLN:?}" "${VERSION:?}" - progress_init 100 2 3 35 25 25 3 5 2 + HOST=$(uname -s) + BUILD_MAC=0 + BUILD_LINUX=0 + case "$HOST" in + Darwin) BUILD_MAC=1 ;; + MINGW*|MSYS*|CYGWIN*) : ;; # Windows 主机只能编 Windows + *) BUILD_LINUX=1 ;; + esac + # 显式降级:允许在没有该平台 Flutter 宿主的情况下仍出 Avalonia 版包 + ALLOW_NO_UI="${GC_ALLOW_NO_UI:-0}" + + # 权重:清理 版本 Mac WinX64 WinArm64 Linux 前端 骨架 打包 收尾 + WEIGHTS="2 3" + [ "$BUILD_MAC" = 1 ] && WEIGHTS="$WEIGHTS 35" + WEIGHTS="$WEIGHTS 25 25" + [ "$BUILD_LINUX" = 1 ] && WEIGHTS="$WEIGHTS 30" + WEIGHTS="$WEIGHTS 25 3 5 2" + TOTAL=0 + for _w in $WEIGHTS; do TOTAL=$((TOTAL + _w)); done + + progress_init "$TOTAL" $WEIGHTS progress_step "清理旧产物"; progress_tick 100 rm -rf "$ROOT_DIR/dist"; mkdir -p "$ROOT_DIR/dist" @@ -37,12 +65,20 @@ bs_build() { progress_tick 100 } - progress_step "编译 Mac" - _run_publish mac 90 publish_rid "osx-arm64" "dist/mac-arm64" false || return 1 - publish_backend "osx-arm64" "dist/mac-arm64" || return 1 - # Flutter 的 macOS 产物只能在 macOS 上编;交叉构建时跳过,包内回退内置界面 - if [ "$(uname -s)" = "Darwin" ]; then - build_flutter_ui macos "dist/mac-arm64" || return 1 + if [ "$BUILD_MAC" = 1 ]; then + progress_step "编译 Mac" + _run_publish mac 90 publish_rid "osx-arm64" "dist/mac-arm64" false || return 1 + publish_backend "osx-arm64" "dist/mac-arm64" || return 1 + # Flutter 的 macOS 产物必须用 Xcode 在本机编,Linux 上无法替代 + build_flutter_ui macos "dist/mac-arm64" || { + echo "!! macOS 前端编译失败。macOS 版 Flutter 只能在 macOS 上编。" + return 1 + } + elif [ "$ALLOW_NO_UI" = 1 ]; then + progress_step "编译 Mac" + _run_publish mac 90 publish_rid "osx-arm64" "dist/mac-arm64" false || return 1 + publish_backend "osx-arm64" "dist/mac-arm64" || return 1 + echo "!! --allow-no-ui:macOS 包内不含 Flutter 前端,启动器将回退 Avalonia" fi progress_step "编译 Win-x64" @@ -53,25 +89,38 @@ bs_build() { _run_publish winarm 70 publish_rid "win-arm64" "dist/win-arm64" true || return 1 publish_backend "win-arm64" "dist/win-arm64" || return 1 + if [ "$BUILD_LINUX" = 1 ]; then + progress_step "编译 Linux" + _run_publish linux 70 publish_rid "linux-x64" "dist/linux-amd64" false || return 1 + publish_backend "linux-x64" "dist/linux-amd64" || return 1 + build_flutter_ui linux "dist/linux-amd64" || return 1 + fi + # Windows 前端:Windows 主机走官方 toolchain;Linux/macOS 主机用 mingw-w64 + # wine 交叉编译(buildsh/cross)。只产出 x64 一份,两个包共用, # ARM64 上由系统模拟 x64 进程运行前端。 - case "$(uname -s)" in + progress_step "编译 Win 前端" + case "$HOST" in MINGW*|MSYS*|CYGWIN*) build_flutter_ui windows "dist/win-flutter" || return 1 ;; - *) build_flutter_ui_cross "dist/win-flutter" ;; + *) build_flutter_ui_cross "dist/win-flutter" || return 1 ;; esac - if [ -d "$ROOT_DIR/dist/win-flutter" ]; then - for label in win-amd64 win-arm64; do - cp -R "$ROOT_DIR/dist/win-flutter/." "$ROOT_DIR/dist/$label/" - done - rm -rf "$ROOT_DIR/dist/win-flutter" - fi + for label in win-amd64 win-arm64; do + cp -R "$ROOT_DIR/dist/win-flutter/." "$ROOT_DIR/dist/$label/" + done + rm -rf "$ROOT_DIR/dist/win-flutter" progress_step "运行骨架"; progress_tick 100 - package_mac_app + if [ -d "$ROOT_DIR/dist/mac-arm64" ]; then + package_mac_app + fi prepare_runtime_layout "dist/win-amd64" prepare_runtime_layout "dist/win-arm64" - prepare_runtime_layout "dist/mac-arm64/GC.app/Contents/MacOS" + if [ "$BUILD_LINUX" = 1 ]; then + prepare_runtime_layout "dist/linux-amd64" + fi + if [ -d "$ROOT_DIR/dist/mac-arm64/GC.app/Contents/MacOS" ]; then + prepare_runtime_layout "dist/mac-arm64/GC.app/Contents/MacOS" + fi progress_step "打包 zip"; progress_tick 100 pack_launcher diff --git a/buildsh/lib/build_funcs.sh b/buildsh/lib/build_funcs.sh index c28f241..3656fd2 100644 --- a/buildsh/lib/build_funcs.sh +++ b/buildsh/lib/build_funcs.sh @@ -94,7 +94,7 @@ TXT "memoryMb": 2048, "autoUpdateOnStartup": true, "requireSignedManifest": true, - "updateRepository": "https://gitcode.com/Morris_1/GC", + "updateRepository": "https://git.xhmcl.com/Morris/GC", "updateManifestUrl": "", "updateLocalRoot": "", "installedModelVersions": {}, @@ -190,18 +190,24 @@ publish_backend() { -o "$ROOT_DIR/$OUT" || return 1 } -# 当前主机原生构建 Flutter 前端(Windows 主机编 Windows,macOS 主机编 macOS)。 -# macOS 版 Flutter 桌面只能在 macOS 上编;Windows 版则由 build_flutter_ui_cross -# 在 Linux/macOS 上交叉编译。 +# 当前主机原生构建 Flutter 前端(Windows 主机编 Windows,macOS 主机编 macOS, +# Linux 主机编 Linux)。跨平台的 Windows 版由 build_flutter_ui_cross 负责。 +# +# 默认「必须有前端」:缺 flutter 或编译失败直接返回非 0,让 build.sh 停下来说明 +# 原因,而不是悄悄出一个只有 Avalonia 回退界面的包。确实需要降级时用 +# ./build.sh build --allow-no-ui 显式声明。 build_flutter_ui() { PLAT="$1"; DEST="$2" UI_SRC="$ROOT_DIR/GC-UI/frontend" - [ -d "$UI_SRC" ] || return 0 + if [ ! -d "$UI_SRC" ]; then + echo "!! 找不到 Flutter 工程:$UI_SRC" + return 1 + fi FLUTTER_BIN="${FLUTTER_BIN:-flutter}" if ! command -v "$FLUTTER_BIN" >/dev/null 2>&1; then - echo "!! 未找到 flutter(可用 FLUTTER_BIN=<路径> 指定),本次包内不含 Flutter 界面" - return 0 + echo "!! 未找到 flutter(可用 FLUTTER_BIN=<路径> 指定)" + return 1 fi echo "==> 构建 Flutter 前端 (${PLAT})" @@ -217,6 +223,14 @@ build_flutter_ui() { rm -rf "$ROOT_DIR/$DEST/GC-UI.app" cp -R "$UI_SRC/build/macos/Build/Products/Release/GC-UI.app" "$ROOT_DIR/$DEST/" ;; + linux) + # bundle/ 里是 GC-UI、lib/*.so 与 data/,整体铺到包的根目录 + cp -R "$UI_SRC/build/linux/x64/release/bundle/." "$ROOT_DIR/$DEST/" + ;; + *) + echo "!! 不支持的 Flutter 目标平台:$PLAT" + return 1 + ;; esac echo " -> $DEST(Flutter ${PLAT})" } @@ -231,8 +245,8 @@ build_flutter_ui_cross() { FLUTTER_BIN="${FLUTTER_BIN:-flutter}" if ! command -v "$FLUTTER_BIN" >/dev/null 2>&1; then - echo "!! 未找到 flutter(可用 FLUTTER_BIN=<路径> 指定),本次包内不含 Flutter 界面" - return 0 + echo "!! 未找到 flutter(可用 FLUTTER_BIN=<路径> 指定)" + return 1 fi mkdir -p "$ROOT_DIR/build/logs" @@ -240,8 +254,9 @@ build_flutter_ui_cross() { if ! FLUTTER_BIN="$FLUTTER_BIN" bash "$ROOT_DIR/buildsh/cross/build-ui-win-cross.sh" \ "$UI_SRC/build/windows/x64/runner/Release" \ >"$ROOT_DIR/build/logs/flutter-win-cross.log" 2>&1; then - echo "!! 交叉编译失败,本次包内不含 Flutter 界面(日志:build/logs/flutter-win-cross.log)" - return 0 + echo "!! 交叉编译失败(日志:build/logs/flutter-win-cross.log)" + tail -5 "$ROOT_DIR/build/logs/flutter-win-cross.log" 2>/dev/null | sed 's/^/ /' + return 1 fi mkdir -p "$ROOT_DIR/$DEST" @@ -284,6 +299,27 @@ pack_launcher() { echo " -> dist/$LABEL/GC-${VERSION}-${LABEL}.zip" done + # --- Linux:整目录打包(启动器 + 守护 + 后端 + Flutter 前端)--- + SRC="$ROOT_DIR/dist/linux-amd64" + if [ -d "$SRC" ]; then + TMP="$ROOT_DIR/dist/.linuxpkg" + rm -rf "$TMP"; mkdir -p "$TMP" + cp -R "$SRC"/. "$TMP"/ + rm -f "$TMP"/*.zip + if [ -f "$TMP/GC" ]; then + mv "$TMP/GC" "$TMP/GC-${VERSION}-linux-amd64" + chmod +x "$TMP/GC-${VERSION}-linux-amd64" 2>/dev/null || true + fi + if [ ! -f "$TMP/GC-UI" ]; then + echo " !! 包内没有 Flutter 前端,启动器将使用内置 Avalonia 界面" + fi + ZIP="$ROOT_DIR/dist/linux-amd64/GC-${VERSION}-linux-amd64.zip" + rm -f "$ZIP" + ( cd "$TMP" && zip -qry "$ZIP" . ) + rm -rf "$TMP" + echo " -> dist/linux-amd64/GC-${VERSION}-linux-amd64.zip" + fi + # --- macOS:先得到真实 .app 目录(改名 + 同步 Info.plist)--- MACAPP="$ROOT_DIR/dist/mac-arm64/GC.app" if [ -d "$MACAPP" ]; then