| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright 2017-2021 The Meson development team |
| 4 |
|
| 5 |
import subprocess |
| 6 |
import shutil, sys, os |
| 7 |
from glob import glob |
| 8 |
|
| 9 |
import xml.etree.ElementTree as ET |
| 10 |
|
| 11 |
sys.path.append(os.getcwd()) |
| 12 |
from mesonbuild import coredata |
| 13 |
|
| 14 |
class PkgGenerator: |
| 15 |
|
| 16 |
def __init__(self): |
| 17 |
self.pkg_dir = 'macpkg' |
| 18 |
self.sharedir = os.path.join(self.pkg_dir, 'usr/local/share') |
| 19 |
self.bindir = os.path.join(self.pkg_dir, 'usr/local/bin') |
| 20 |
self.product_name = 'Meson Build System' |
| 21 |
self.identifier = 'com.mesonbuild.meson' |
| 22 |
self.version = coredata.version.replace('dev', '') |
| 23 |
self.mesonstashdir = os.path.join(self.sharedir, f'meson-{self.version}') |
| 24 |
self.pkgname = 'meson.pkg' |
| 25 |
self.productname = f'meson-{self.version}.pkg' |
| 26 |
self.distribution_file = 'meson-distribution.xml' |
| 27 |
self.resourcedir = 'packaging/macpages' |
| 28 |
|
| 29 |
def build_dist(self): |
| 30 |
if os.path.exists(self.pkg_dir): |
| 31 |
shutil.rmtree(self.pkg_dir) |
| 32 |
os.mkdir(self.pkg_dir) |
| 33 |
pyinstaller_bin = glob('/Users/jpakkane/Library/Python/*/bin/pyinstaller') |
| 34 |
if len(pyinstaller_bin) != 1: |
| 35 |
sys.exit('Could not determine unique installer.') |
| 36 |
pyinstaller_bin = pyinstaller_bin[0] |
| 37 |
pyinst_cmd = [pyinstaller_bin, |
| 38 |
'--clean', |
| 39 |
'--additional-hooks-dir=packaging', |
| 40 |
'--distpath', |
| 41 |
self.pkg_dir] |
| 42 |
pyinst_cmd += ['meson.py'] |
| 43 |
subprocess.check_call(pyinst_cmd) |
| 44 |
tmpdir = os.path.join(self.pkg_dir, 'meson') |
| 45 |
shutil.move(tmpdir, self.mesonstashdir) |
| 46 |
os.makedirs(self.bindir) |
| 47 |
ln_base = os.path.relpath(self.mesonstashdir, self.bindir) |
| 48 |
ninja_bin = shutil.which('ninja') |
| 49 |
assert ninja_bin |
| 50 |
shutil.copy(ninja_bin, self.bindir) |
| 51 |
subprocess.check_call(['strip', os.path.join(self.bindir, 'ninja')]) |
| 52 |
os.symlink(os.path.join(ln_base, 'meson'), os.path.join(self.bindir, 'meson')) |
| 53 |
|
| 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 |
|
| 69 |
def generate_distribution(self): |
| 70 |
root = ET.Element('installer-gui-script', {'minSpecVersion': '1'}) |
| 71 |
ET.SubElement(root, 'welcome', {'file': 'welcome.html', |
| 72 |
'mime-type': 'text/html'}) |
| 73 |
ET.SubElement(root, 'license', {'file': 'license.html', |
| 74 |
'mime-type': 'text/html'}) |
| 75 |
ET.SubElement(root, 'conclusion', {'file': 'conclusion.html', |
| 76 |
'mime-type': 'text/html'}) |
| 77 |
ET.SubElement(root, 'pkg-ref', {'id': self.identifier}) |
| 78 |
ET.SubElement(root, 'options', {'customize': 'never', |
| 79 |
'require-scripts': 'false', |
| 80 |
'hostArchitectures': 'x86_64,arm64'}) |
| 81 |
choices_outline = ET.SubElement(root, 'choices-outline') |
| 82 |
line = ET.SubElement(choices_outline, 'line', {'choice': 'default'}) |
| 83 |
ET.SubElement(line, 'line', {'choice': self.identifier}) |
| 84 |
ET.SubElement(root, 'choice', {'id': 'default'}) |
| 85 |
choice = ET.SubElement(root, 'choice', {'id': self.identifier, 'visible': 'false'}) |
| 86 |
ET.SubElement(choice, 'pkg-ref', {'id': self.identifier}) |
| 87 |
ET.SubElement(root, 'pkg-ref', {'id': self.identifier, |
| 88 |
'version': '0', # self.version, |
| 89 |
'onConclusion': 'none'}).text = self.pkgname |
| 90 |
ET.ElementTree(root).write(self.distribution_file, encoding='utf-8', xml_declaration=True) |
| 91 |
# ElementTree cannot do pretty-printing, so do it manually |
| 92 |
import xml.dom.minidom |
| 93 |
doc = xml.dom.minidom.parse(self.distribution_file) |
| 94 |
with open(self.distribution_file, 'w', encoding='utf-8') as open_file: |
| 95 |
open_file.write(doc.toprettyxml()) |
| 96 |
|
| 97 |
def remove_tempfiles(self): |
| 98 |
shutil.rmtree('macpkg') |
| 99 |
os.unlink('meson-distribution.xml') |
| 100 |
os.unlink('meson.pkg') |
| 101 |
os.unlink('meson.spec') |
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
if not os.path.exists('meson.py'): |
| 105 |
sys.exit(print('Run me in the top level source dir.')) |
| 106 |
subprocess.check_call(['pip3', 'install', '--user', '--upgrade', 'pyinstaller']) |
| 107 |
|
| 108 |
pg = PkgGenerator() |
| 109 |
pg.build_dist() |
| 110 |
pg.build_package() |
| 111 |
pg.remove_tempfiles() |