xcompile.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. #
  10. # xcompile.py — builds Materialize-specific Docker images.
  11. import argparse
  12. import os
  13. import sys
  14. from materialize import mzbuild, spawn, xcompile
  15. def main() -> int:
  16. parser = argparse.ArgumentParser(
  17. prog="xcompile",
  18. description="Facilitates cross compilation of Rust binaries.",
  19. epilog="For additional help on a subcommand, run:\n\n %(prog)s <command> -h",
  20. )
  21. parser.add_argument(
  22. "--arch",
  23. default=mzbuild.Arch.X86_64,
  24. help="the CPU architecture to build for",
  25. type=mzbuild.Arch,
  26. choices=mzbuild.Arch,
  27. )
  28. parser.add_argument(
  29. "--channel",
  30. default="nightly" if os.getenv("CI_SANITIZER", "none") else None,
  31. help="Rust compiler channel to use",
  32. )
  33. subparsers = parser.add_subparsers(
  34. dest="command", metavar="<command>", required=True
  35. )
  36. cargo_parser = subparsers.add_parser(
  37. "cargo", help="run a cross-compiling cargo command"
  38. )
  39. cargo_parser.add_argument(
  40. "--rustflags",
  41. action="append",
  42. default=[],
  43. help="override the default flags to the Rust compiler",
  44. )
  45. cargo_parser.add_argument("subcommand", help="the cargo subcommand to invoke")
  46. cargo_parser.add_argument(
  47. "subargs", nargs=argparse.REMAINDER, help="the arguments to pass to cargo"
  48. )
  49. tool_parser = subparsers.add_parser(
  50. "tool", help="run a cross-compiling binutils tool"
  51. )
  52. tool_parser.add_argument(
  53. "--name-target-prefix",
  54. default=True,
  55. action=argparse.BooleanOptionalAction,
  56. help="whether the tool name should be prefixed with the target name",
  57. )
  58. tool_parser.add_argument("tool", metavar="TOOL", help="the binutils tool to invoke")
  59. tool_parser.add_argument(
  60. "subargs", nargs=argparse.REMAINDER, help="the arguments to pass to the tool"
  61. )
  62. args = parser.parse_args()
  63. if args.command == "cargo":
  64. spawn.runv(
  65. [
  66. *xcompile.cargo(
  67. arch=args.arch,
  68. channel=args.channel,
  69. subcommand=args.subcommand,
  70. rustflags=args.rustflags,
  71. ),
  72. *args.subargs,
  73. ]
  74. )
  75. elif args.command == "tool":
  76. spawn.runv(
  77. [
  78. *xcompile.tool(
  79. args.arch, args.tool, prefix_name=args.name_target_prefix
  80. ),
  81. *args.subargs,
  82. ]
  83. )
  84. else:
  85. raise RuntimeError("unreachable")
  86. return 0
  87. if __name__ == "__main__":
  88. sys.exit(main())