#!/usr/bin/env python3 """生成 ios/QFT.xcodeproj,不依赖 XcodeGen 等外部工具。 生成结果已提交到仓库,正常情况下双击 QFT.xcodeproj 即可。 只有在**增删源文件**之后才需要重跑: python3 ios/tools/generate_xcodeproj.py 对象 ID 由文件路径哈希得到,因此同样的文件集合总是产出同样的工程, diff 干净,不会每次生成都全量变动。 """ import hashlib import os HERE = os.path.dirname(os.path.abspath(__file__)) IOS_ROOT = os.path.dirname(HERE) APP_NAME = "QFT" BUNDLE_ID = "com.qft.transfer" DEPLOYMENT_TARGET = "17.0" MARKETING_VERSION = "0.4.6" SOURCES = [ "QFT/App/QFTApp.swift", "QFT/App/HomeView.swift", "QFT/App/ScannerView.swift", "QFT/App/TransferModel.swift", "QFT/Core/Base64URL.swift", "QFT/Core/PairingProtocol.swift", "QFT/Core/HostDiscovery.swift", "QFT/Core/DownloadClient.swift", "QFT/Core/UploadClient.swift", "QFT/Core/PeerPairing.swift", "QFT/Core/PeerLink.swift", ] RESOURCES = ["QFT/Assets.xcassets"] OTHER_FILES = ["QFT/Info.plist"] def oid(*parts): """由稳定字符串派生 24 位十六进制对象 ID。""" digest = hashlib.sha1("::".join(parts).encode("utf-8")).hexdigest() return digest[:24].upper() def file_type(path): if path.endswith(".swift"): return "sourcecode.swift" if path.endswith(".xcassets"): return "folder.assetcatalog" if path.endswith(".plist"): return "text.plist.xml" return "text" def build_tree(paths): """把扁平路径列表折成 {目录: {...}} 的嵌套结构。""" tree = {} for path in paths: node = tree segments = path.split("/") for segment in segments[:-1]: node = node.setdefault(segment, {}) node[segments[-1]] = None return tree def emit_groups(tree, prefix, lines): """深度优先输出 PBXGroup,返回本层 group 的 ID。""" children = [] for name in sorted(tree.keys()): path = "%s/%s" % (prefix, name) if prefix else name if tree[name] is None: children.append((oid("file", path), name)) else: children.append((emit_groups(tree[name], path, lines), name)) group_id = oid("group", prefix or "") lines.append("\t\t%s /* %s */ = {" % (group_id, prefix.split("/")[-1] if prefix else APP_NAME)) lines.append("\t\t\tisa = PBXGroup;") lines.append("\t\t\tchildren = (") for child_id, child_name in children: lines.append("\t\t\t\t%s /* %s */," % (child_id, child_name)) lines.append("\t\t\t);") if prefix: lines.append("\t\t\tpath = %s;" % prefix.split("/")[-1]) lines.append("\t\t\tsourceTree = \"\";") lines.append("\t\t};") return group_id def generate(): all_files = SOURCES + RESOURCES + OTHER_FILES project_id = oid("project") target_id = oid("target", APP_NAME) product_id = oid("product", APP_NAME) products_group_id = oid("group", "") sources_phase_id = oid("phase", "sources") resources_phase_id = oid("phase", "resources") frameworks_phase_id = oid("phase", "frameworks") project_config_list = oid("configlist", "project") target_config_list = oid("configlist", "target") lines = [] add = lines.append add("// !$*UTF8*$!") add("{") add("\tarchiveVersion = 1;") add("\tclasses = {") add("\t};") add("\tobjectVersion = 56;") add("\tobjects = {") # ---------------- PBXBuildFile add("") add("/* Begin PBXBuildFile section */") for path in SOURCES + RESOURCES: name = path.split("/")[-1] add("\t\t%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };" % (oid("buildfile", path), name, "Resources" if path in RESOURCES else "Sources", oid("file", path), name)) add("/* End PBXBuildFile section */") # ---------------- PBXFileReference add("") add("/* Begin PBXFileReference section */") add("\t\t%s /* %s.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; " "includeInIndex = 0; path = %s.app; sourceTree = BUILT_PRODUCTS_DIR; };" % (product_id, APP_NAME, APP_NAME)) for path in all_files: name = path.split("/")[-1] add("\t\t%s /* %s */ = {isa = PBXFileReference; lastKnownFileType = %s; path = %s; " "sourceTree = \"\"; };" % (oid("file", path), name, file_type(path), name)) add("/* End PBXFileReference section */") # ---------------- PBXFrameworksBuildPhase add("") add("/* Begin PBXFrameworksBuildPhase section */") add("\t\t%s /* Frameworks */ = {" % frameworks_phase_id) add("\t\t\tisa = PBXFrameworksBuildPhase;") add("\t\t\tbuildActionMask = 2147483647;") add("\t\t\tfiles = (") add("\t\t\t);") add("\t\t\trunOnlyForDeploymentPostprocessing = 0;") add("\t\t};") add("/* End PBXFrameworksBuildPhase section */") # ---------------- PBXGroup add("") add("/* Begin PBXGroup section */") group_lines = [] app_group_id = emit_groups(build_tree(all_files)[APP_NAME], APP_NAME, group_lines) lines.extend(group_lines) root_group_id = oid("group", "") add("\t\t%s = {" % root_group_id) add("\t\t\tisa = PBXGroup;") add("\t\t\tchildren = (") add("\t\t\t\t%s /* %s */," % (app_group_id, APP_NAME)) add("\t\t\t\t%s /* Products */," % products_group_id) add("\t\t\t);") add("\t\t\tsourceTree = \"\";") add("\t\t};") add("\t\t%s /* Products */ = {" % products_group_id) add("\t\t\tisa = PBXGroup;") add("\t\t\tchildren = (") add("\t\t\t\t%s /* %s.app */," % (product_id, APP_NAME)) add("\t\t\t);") add("\t\t\tname = Products;") add("\t\t\tsourceTree = \"\";") add("\t\t};") add("/* End PBXGroup section */") # ---------------- PBXNativeTarget add("") add("/* Begin PBXNativeTarget section */") add("\t\t%s /* %s */ = {" % (target_id, APP_NAME)) add("\t\t\tisa = PBXNativeTarget;") add("\t\t\tbuildConfigurationList = %s;" % target_config_list) add("\t\t\tbuildPhases = (") add("\t\t\t\t%s /* Sources */," % sources_phase_id) add("\t\t\t\t%s /* Frameworks */," % frameworks_phase_id) add("\t\t\t\t%s /* Resources */," % resources_phase_id) add("\t\t\t);") add("\t\t\tbuildRules = (") add("\t\t\t);") add("\t\t\tdependencies = (") add("\t\t\t);") add("\t\t\tname = %s;" % APP_NAME) add("\t\t\tproductName = %s;" % APP_NAME) add("\t\t\tproductReference = %s /* %s.app */;" % (product_id, APP_NAME)) add("\t\t\tproductType = \"com.apple.product-type.application\";") add("\t\t};") add("/* End PBXNativeTarget section */") # ---------------- PBXProject add("") add("/* Begin PBXProject section */") add("\t\t%s /* Project object */ = {" % project_id) add("\t\t\tisa = PBXProject;") add("\t\t\tattributes = {") add("\t\t\t\tBuildIndependentTargetsInParallel = 1;") add("\t\t\t\tLastSwiftUpdateCheck = 1600;") add("\t\t\t\tLastUpgradeCheck = 1600;") add("\t\t\t\tTargetAttributes = {") add("\t\t\t\t\t%s = {" % target_id) add("\t\t\t\t\t\tCreatedOnToolsVersion = 16.0;") add("\t\t\t\t\t};") add("\t\t\t\t};") add("\t\t\t};") add("\t\t\tbuildConfigurationList = %s;" % project_config_list) add("\t\t\tcompatibilityVersion = \"Xcode 14.0\";") add("\t\t\tdevelopmentRegion = en;") add("\t\t\thasScannedForEncodings = 0;") add("\t\t\tknownRegions = (") add("\t\t\t\ten,") add("\t\t\t\tBase,") add("\t\t\t\t\"zh-Hans\",") add("\t\t\t);") add("\t\t\tmainGroup = %s;" % root_group_id) add("\t\t\tproductRefGroup = %s /* Products */;" % products_group_id) add("\t\t\tprojectDirPath = \"\";") add("\t\t\tprojectRoot = \"\";") add("\t\t\ttargets = (") add("\t\t\t\t%s /* %s */," % (target_id, APP_NAME)) add("\t\t\t);") add("\t\t};") add("/* End PBXProject section */") # ---------------- PBXResourcesBuildPhase add("") add("/* Begin PBXResourcesBuildPhase section */") add("\t\t%s /* Resources */ = {" % resources_phase_id) add("\t\t\tisa = PBXResourcesBuildPhase;") add("\t\t\tbuildActionMask = 2147483647;") add("\t\t\tfiles = (") for path in RESOURCES: add("\t\t\t\t%s /* %s in Resources */," % (oid("buildfile", path), path.split("/")[-1])) add("\t\t\t);") add("\t\t\trunOnlyForDeploymentPostprocessing = 0;") add("\t\t};") add("/* End PBXResourcesBuildPhase section */") # ---------------- PBXSourcesBuildPhase add("") add("/* Begin PBXSourcesBuildPhase section */") add("\t\t%s /* Sources */ = {" % sources_phase_id) add("\t\t\tisa = PBXSourcesBuildPhase;") add("\t\t\tbuildActionMask = 2147483647;") add("\t\t\tfiles = (") for path in SOURCES: add("\t\t\t\t%s /* %s in Sources */," % (oid("buildfile", path), path.split("/")[-1])) add("\t\t\t);") add("\t\t\trunOnlyForDeploymentPostprocessing = 0;") add("\t\t};") add("/* End PBXSourcesBuildPhase section */") # ---------------- XCBuildConfiguration shared_project = [ "ALWAYS_SEARCH_USER_PATHS = NO", "CLANG_ANALYZER_NONNULL = YES", "CLANG_ENABLE_MODULES = YES", "CLANG_ENABLE_OBJC_ARC = YES", "CLANG_WARN_DOCUMENTATION_COMMENTS = YES", "CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE", "COPY_PHASE_STRIP = NO", "ENABLE_STRICT_OBJC_MSGSEND = YES", "GCC_NO_COMMON_BLOCKS = YES", "IPHONEOS_DEPLOYMENT_TARGET = %s" % DEPLOYMENT_TARGET, "MTL_FAST_MATH = YES", "SDKROOT = iphoneos", "SWIFT_VERSION = 5.0", ] shared_target = [ "ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon", "ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor", "CODE_SIGN_STYLE = Automatic", "CURRENT_PROJECT_VERSION = 46", "ENABLE_PREVIEWS = YES", "GENERATE_INFOPLIST_FILE = NO", "INFOPLIST_FILE = QFT/Info.plist", "LD_RUNPATH_SEARCH_PATHS = \"$(inherited) @executable_path/Frameworks\"", "MARKETING_VERSION = %s" % MARKETING_VERSION, "PRODUCT_BUNDLE_IDENTIFIER = %s" % BUNDLE_ID, "PRODUCT_NAME = \"$(TARGET_NAME)\"", "SWIFT_EMIT_LOC_STRINGS = NO", "TARGETED_DEVICE_FAMILY = \"1,2\"", ] variants = { "Debug": { "project": ["DEBUG_INFORMATION_FORMAT = dwarf", "ENABLE_TESTABILITY = YES", "GCC_OPTIMIZATION_LEVEL = 0", "GCC_PREPROCESSOR_DEFINITIONS = \"DEBUG=1 $(inherited)\"", "MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE", "ONLY_ACTIVE_ARCH = YES", "SWIFT_ACTIVE_COMPILATION_CONDITIONS = \"DEBUG $(inherited)\"", "SWIFT_OPTIMIZATION_LEVEL = \"-Onone\""], "target": [], }, "Release": { "project": ["DEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\"", "ENABLE_NS_ASSERTIONS = NO", "MTL_ENABLE_DEBUG_INFO = NO", "SWIFT_COMPILATION_MODE = wholemodule", "VALIDATE_PRODUCT = YES"], "target": [], }, } add("") add("/* Begin XCBuildConfiguration section */") for name, extra in variants.items(): for scope, base in (("project", shared_project), ("target", shared_target)): config_id = oid("config", scope, name) add("\t\t%s /* %s */ = {" % (config_id, name)) add("\t\t\tisa = XCBuildConfiguration;") add("\t\t\tbuildSettings = {") for setting in sorted(base + extra[scope]): add("\t\t\t\t%s;" % setting) add("\t\t\t};") add("\t\t\tname = %s;" % name) add("\t\t};") add("/* End XCBuildConfiguration section */") # ---------------- XCConfigurationList add("") add("/* Begin XCConfigurationList section */") for scope, list_id in (("project", project_config_list), ("target", target_config_list)): add("\t\t%s = {" % list_id) add("\t\t\tisa = XCConfigurationList;") add("\t\t\tbuildConfigurations = (") for name in variants: add("\t\t\t\t%s /* %s */," % (oid("config", scope, name), name)) add("\t\t\t);") add("\t\t\tdefaultConfigurationIsVisible = 0;") add("\t\t\tdefaultConfigurationName = Release;") add("\t\t};") add("/* End XCConfigurationList section */") add("\t};") add("\trootObject = %s /* Project object */;" % project_id) add("}") return "\n".join(lines) + "\n" def main(): project_dir = os.path.join(IOS_ROOT, "%s.xcodeproj" % APP_NAME) os.makedirs(project_dir, exist_ok=True) target = os.path.join(project_dir, "project.pbxproj") with open(target, "w", encoding="utf-8") as fh: fh.write(generate()) print("已生成 %s" % target) missing = [p for p in SOURCES + RESOURCES + OTHER_FILES if not os.path.exists(os.path.join(IOS_ROOT, p))] if missing: print("警告:以下文件在工程里被引用但不存在:") for path in missing: print(" " + path) return 1 return 0 if __name__ == "__main__": raise SystemExit(main())