nsis.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2020 Red Hat, Inc.
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-or-later
  6. import argparse
  7. import glob
  8. import os
  9. import shutil
  10. import subprocess
  11. import tempfile
  12. def signcode(path):
  13. cmd = os.environ.get("SIGNCODE")
  14. if not cmd:
  15. return
  16. subprocess.run([cmd, path])
  17. def main():
  18. parser = argparse.ArgumentParser(description="QEMU NSIS build helper.")
  19. parser.add_argument("outfile")
  20. parser.add_argument("prefix")
  21. parser.add_argument("srcdir")
  22. parser.add_argument("cpu")
  23. parser.add_argument("nsisargs", nargs="*")
  24. args = parser.parse_args()
  25. destdir = tempfile.mkdtemp()
  26. try:
  27. subprocess.run(["make", "install", "DESTDIR=" + destdir + os.path.sep])
  28. with open(
  29. os.path.join(destdir + args.prefix, "system-emulations.nsh"), "w"
  30. ) as nsh, open(
  31. os.path.join(destdir + args.prefix, "system-mui-text.nsh"), "w"
  32. ) as muinsh:
  33. for exe in sorted(glob.glob(
  34. os.path.join(destdir + args.prefix, "qemu-system-*.exe")
  35. )):
  36. exe = os.path.basename(exe)
  37. arch = exe[12:-4]
  38. nsh.write(
  39. """
  40. Section "{0}" Section_{0}
  41. SetOutPath "$INSTDIR"
  42. File "${{BINDIR}}\\{1}"
  43. SectionEnd
  44. """.format(
  45. arch, exe
  46. )
  47. )
  48. if arch.endswith('w'):
  49. desc = arch[:-1] + " emulation (GUI)."
  50. else:
  51. desc = arch + " emulation."
  52. muinsh.write(
  53. """
  54. !insertmacro MUI_DESCRIPTION_TEXT ${{Section_{0}}} "{1}"
  55. """.format(arch, desc))
  56. for exe in glob.glob(os.path.join(destdir + args.prefix, "*.exe")):
  57. signcode(exe)
  58. makensis = [
  59. "makensis",
  60. "-V2",
  61. "-NOCD",
  62. "-DSRCDIR=" + args.srcdir,
  63. "-DBINDIR=" + destdir + args.prefix,
  64. ]
  65. dlldir = "w32"
  66. if args.cpu == "x86_64":
  67. dlldir = "w64"
  68. makensis += ["-DW64"]
  69. if os.path.exists(os.path.join(args.srcdir, "dll")):
  70. makensis += ["-DDLLDIR={0}/dll/{1}".format(args.srcdir, dlldir)]
  71. makensis += ["-DOUTFILE=" + args.outfile] + args.nsisargs
  72. subprocess.run(makensis)
  73. signcode(args.outfile)
  74. finally:
  75. shutil.rmtree(destdir)
  76. if __name__ == "__main__":
  77. main()