|
| 1 | +def _custom_toolchain_impl(ctx): |
| 2 | + toolchain_dir = ctx.actions.declare_directory(ctx.attr.toolchain_name + ".xctoolchain") |
| 3 | + toolchain_info_plist_file = ctx.actions.declare_file(ctx.attr.toolchain_name + "_ToolchainInfo.plist") |
| 4 | + info_plist_file = ctx.actions.declare_file(ctx.attr.toolchain_name + "_Info.plist") |
| 5 | + |
| 6 | + default_toolchain_path = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain" |
| 7 | + |
| 8 | + # Generate symlink creation commands dynamically, excluding ToolchainInfo.plist |
| 9 | + symlink_script = """#!/bin/bash |
| 10 | +set -e |
| 11 | +
|
| 12 | +mkdir -p "{toolchain_dir}" |
| 13 | +
|
| 14 | +find "{default_toolchain}" -type f -o -type l | while read file; do |
| 15 | + rel_path="${{file#"{default_toolchain}/"}}" |
| 16 | + if [[ "$rel_path" != "ToolchainInfo.plist" && "$rel_path" != "Info.plist" ]]; then |
| 17 | + mkdir -p "{toolchain_dir}/$(dirname "$rel_path")" |
| 18 | + ln -s "$file" "{toolchain_dir}/$rel_path" |
| 19 | + fi |
| 20 | +done |
| 21 | +
|
| 22 | +mkdir -p "{toolchain_dir}" |
| 23 | +mv "{toolchain_info_plist}" "{toolchain_dir}/ToolchainInfo.plist" |
| 24 | +mv "{info_plist}" "{toolchain_dir}/Info.plist" |
| 25 | +""".format( |
| 26 | + toolchain_dir=toolchain_dir.path, |
| 27 | + default_toolchain=default_toolchain_path, |
| 28 | + toolchain_info_plist=toolchain_info_plist_file.path, |
| 29 | + info_plist=info_plist_file.path |
| 30 | + ) |
| 31 | + |
| 32 | + script_file = ctx.actions.declare_file(ctx.attr.toolchain_name + "_setup.sh") |
| 33 | + ctx.actions.write(output=script_file, content=symlink_script, is_executable=True) |
| 34 | + |
| 35 | + # Generate ToolchainInfo.plist separately |
| 36 | + toolchain_info_plist_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 37 | +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 38 | +<plist version="1.0"> |
| 39 | +<dict> |
| 40 | + <key>Identifier</key> |
| 41 | + <string>com.example.{name}</string> |
| 42 | + <key>DisplayName</key> |
| 43 | + <string>{name}</string> |
| 44 | + <key>CompatibilityVersion</key> |
| 45 | + <string>9999</string> |
| 46 | +</dict> |
| 47 | +</plist> |
| 48 | +""".format(name=ctx.attr.toolchain_name) |
| 49 | + |
| 50 | + ctx.actions.write(output=toolchain_info_plist_file, content=toolchain_info_plist_content) |
| 51 | + |
| 52 | + # Generate Info.plist |
| 53 | + info_plist_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 54 | +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 55 | +<plist version="1.0"> |
| 56 | +<dict> |
| 57 | + <key>CFBundleIdentifier</key> |
| 58 | + <string>com.example.{name}</string> |
| 59 | + <key>CFBundleName</key> |
| 60 | + <string>{name}</string> |
| 61 | + <key>CFBundleVersion</key> |
| 62 | + <string>1.0</string> |
| 63 | + <key>DTSDKName</key> |
| 64 | + <string>macosx</string> |
| 65 | + <key>DTSDKBuild</key> |
| 66 | + <string>9999</string> |
| 67 | + <key>DTCompiler</key> |
| 68 | + <string>com.apple.compilers.llvm.clang.1_0</string> |
| 69 | +</dict> |
| 70 | +</plist> |
| 71 | +""".format(name=ctx.attr.toolchain_name) |
| 72 | + |
| 73 | + ctx.actions.write(output=info_plist_file, content=info_plist_content) |
| 74 | + |
| 75 | + # Run the generated shell script |
| 76 | + ctx.actions.run_shell( |
| 77 | + outputs=[toolchain_dir], |
| 78 | + inputs=[toolchain_info_plist_file, info_plist_file], |
| 79 | + tools=[script_file], |
| 80 | + command=script_file.path |
| 81 | + ) |
| 82 | + |
| 83 | + return [DefaultInfo(files=depset([toolchain_dir]))] |
| 84 | + |
| 85 | +custom_toolchain = rule( |
| 86 | + implementation=_custom_toolchain_impl, |
| 87 | + attrs={ |
| 88 | + "toolchain_name": attr.string(mandatory=True), |
| 89 | + }, |
| 90 | +) |
0 commit comments