createpkg.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python3
  2. # Copyright 2017-2021 The Meson development team
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import subprocess
  16. import shutil, sys, os
  17. import xml.etree.ElementTree as ET
  18. sys.path.append(os.getcwd())
  19. from mesonbuild import coredata
  20. class PkgGenerator:
  21. def __init__(self):
  22. self.pkg_dir = 'macpkg'
  23. self.sharedir = os.path.join(self.pkg_dir, 'usr/local/share')
  24. self.bindir = os.path.join(self.pkg_dir, 'usr/local/bin')
  25. self.product_name = 'Meson Build System'
  26. self.identifier = 'com.mesonbuild.meson'
  27. self.version = coredata.version.replace('dev', '')
  28. self.mesonstashdir = os.path.join(self.sharedir, f'meson-{self.version}')
  29. self.pkgname = 'meson.pkg'
  30. self.productname = f'meson-{self.version}.pkg'
  31. self.distribution_file = 'meson-distribution.xml'
  32. self.resourcedir = 'packaging/macpages'
  33. def build_dist(self):
  34. if os.path.exists(self.pkg_dir):
  35. shutil.rmtree(self.pkg_dir)
  36. os.mkdir(self.pkg_dir)
  37. pyinstaller_bin = '/Users/jpakkane/Library/Python/3.8/bin/pyinstaller'
  38. pyinst_cmd = [pyinstaller_bin,
  39. '--clean',
  40. '--additional-hooks-dir=packaging',
  41. '--distpath',
  42. self.pkg_dir]
  43. pyinst_cmd += ['meson.py']
  44. subprocess.check_call(pyinst_cmd)
  45. tmpdir = os.path.join(self.pkg_dir, 'meson')
  46. shutil.move(tmpdir, self.mesonstashdir)
  47. os.makedirs(self.bindir)
  48. ln_base = os.path.relpath(self.mesonstashdir, self.bindir)
  49. ninja_bin = shutil.which('ninja')
  50. assert ninja_bin
  51. shutil.copy(ninja_bin, self.bindir)
  52. subprocess.check_call(['strip', os.path.join(self.bindir, 'ninja')])
  53. os.symlink(os.path.join(ln_base, 'meson'), os.path.join(self.bindir, 'meson'))
  54. def build_package(self):
  55. subprocess.check_call(['pkgbuild',
  56. '--root',
  57. self.pkg_dir,
  58. '--identifier',
  59. self.identifier,
  60. self.pkgname])
  61. self.generate_distribution()
  62. subprocess.check_call(['productbuild',
  63. '--distribution',
  64. self.distribution_file,
  65. '--resources',
  66. self.resourcedir,
  67. self.productname])
  68. def generate_distribution(self):
  69. root = ET.Element('installer-gui-script', {'minSpecVersion': '1'})
  70. ET.SubElement(root, 'welcome', {'file': 'welcome.html',
  71. 'mime-type': 'text/html'})
  72. ET.SubElement(root, 'license', {'file': 'license.html',
  73. 'mime-type': 'text/html'})
  74. ET.SubElement(root, 'conclusion', {'file': 'conclusion.html',
  75. 'mime-type': 'text/html'})
  76. ET.SubElement(root, 'pkg-ref', {'id': self.identifier})
  77. ET.SubElement(root, 'options', {'customize': 'never',
  78. 'require-scripts': 'false',
  79. 'hostArhcitectures': 'x86_64,arm64'})
  80. choices_outline = ET.SubElement(root, 'choices-outline')
  81. line = ET.SubElement(choices_outline, 'line', {'choice': 'default'})
  82. ET.SubElement(line, 'line', {'choice': self.identifier})
  83. ET.SubElement(root, 'choice', {'id': 'default'})
  84. choice = ET.SubElement(root, 'choice', {'id': self.identifier, 'visible': 'false'})
  85. ET.SubElement(choice, 'pkg-ref', {'id': self.identifier})
  86. ET.SubElement(root, 'pkg-ref', {'id': self.identifier,
  87. 'version': '0', # self.version,
  88. 'onConclusion': 'none'}).text = self.pkgname
  89. ET.ElementTree(root).write(self.distribution_file, encoding='utf-8', xml_declaration=True)
  90. # ElementTree can not do prettyprinting so do it manually
  91. import xml.dom.minidom
  92. doc = xml.dom.minidom.parse(self.distribution_file)
  93. with open(self.distribution_file, 'w') as open_file:
  94. open_file.write(doc.toprettyxml())
  95. def remove_tempfiles(self):
  96. shutil.rmtree('macpkg')
  97. os.unlink('meson-distribution.xml')
  98. os.unlink('meson.pkg')
  99. os.unlink('meson.spec')
  100. if __name__ == '__main__':
  101. if not os.path.exists('meson.py'):
  102. sys.exit(print('Run me in the top level source dir.'))
  103. subprocess.check_call(['pip3', 'install', '--user', '--upgrade', 'pyinstaller'])
  104. pg = PkgGenerator()
  105. pg.build_dist()
  106. pg.build_package()
  107. pg.remove_tempfiles()