1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright 2017-2021 The Meson development team
'''
This script is for generating MSI packages
for Windows users.
'''
import subprocess
import shutil
import uuid
import sys
import os
from glob import glob
import xml.etree.ElementTree as ET
sys.path.append(os.getcwd())
from mesonbuild import coredata
# Elementtree does not support CDATA. So hack it.
WINVER_CHECK = 'Installed OR (VersionNT64 > 602)>'
NUGET_INDEX = 'https://api.nuget.org/v3/index.json'
WIXEXT_TOOL = 'WixToolset.UI.wixext'
def gen_guid():
'''
Generate guid
'''
return str(uuid.uuid4()).upper()
class Node:
'''
Node to hold path and directory values
'''
def __init__(self, dirs, files):
self.check_dirs(dirs)
self.check_files(files)
self.dirs = dirs
self.files = files
@staticmethod
def check_dirs(dirs):
'''
Check to see if directory is instance of list
'''
assert isinstance(dirs, list)
@staticmethod
def check_files(files):
'''
Check to see if files is instance of list
'''
assert isinstance(files, list)
class PackageGenerator:
'''
Package generator for MSI packages
'''
def __init__(self):
self.product_name = 'Meson Build System'
self.manufacturer = 'The Meson Development Team'
self.version = coredata.version.replace('dev', '')
self.root = None
self.guid = '*'
self.update_guid = '141527EE-E28A-4D14-97A4-92E6075D28B2'
self.main_xml = 'meson.wxs'
self.main_o = 'meson.wixobj'
self.final_output = f'meson-{self.version}-64.msi'
self.staging_dirs = ['dist', 'dist2']
self.progfile_dir = 'ProgramFiles64Folder'
redist_globs = ['C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Redist\\MSVC\\v*\\MergeModules\\Microsoft_VC142_CRT_x64.msm',
'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Redist\\MSVC\\v*\\MergeModules\\Microsoft_VC143_CRT_x64.msm']
redist_path = None
for g in redist_globs:
trials = glob(g)
if len(trials) > 1:
sys.exit('MSM glob matched multiple entries:' + '\n'.join(trials))
if len(trials) == 1:
redist_path = trials[0]
break
if redist_path is None:
sys.exit('No MSMs found.')
self.redist_path = redist_path
self.component_num = 0
self.feature_properties = {
self.staging_dirs[0]: {
'Id': 'MainProgram',
'Title': 'Meson',
'Description': 'Meson executables',
'Level': '1',
'AllowAbsent': 'no',
},
self.staging_dirs[1]: {
'Id': 'NinjaProgram',
'Title': 'Ninja',
'Description': 'Ninja build tool',
'Level': '1',
}
}
self.feature_components = {}
for s_d in self.staging_dirs:
self.feature_components[s_d] = []
def build_dist(self):
'''
Build dist file from PyInstaller info
'''
for sdir in self.staging_dirs:
if os.path.exists(sdir):
shutil.rmtree(sdir)
main_stage, ninja_stage = self.staging_dirs
pyinstaller = shutil.which('pyinstaller')
if not pyinstaller:
print("ERROR: This script requires pyinstaller.")
sys.exit(1)
pyinstaller_tmpdir = 'pyinst-tmp'
if os.path.exists(pyinstaller_tmpdir):
shutil.rmtree(pyinstaller_tmpdir)
pyinst_cmd = [pyinstaller,
'--clean',
'--additional-hooks-dir=packaging',
'--distpath',
pyinstaller_tmpdir]
pyinst_cmd += ['meson.py']
subprocess.check_call(pyinst_cmd)
shutil.move(pyinstaller_tmpdir + '/meson', main_stage)
self.del_infodirs(main_stage)
if not os.path.exists(os.path.join(main_stage, 'meson.exe')):
sys.exit('Meson exe missing from staging dir.')
os.mkdir(ninja_stage)
shutil.copy(shutil.which('ninja'), ninja_stage)
if not os.path.exists(os.path.join(ninja_stage, 'ninja.exe')):
sys.exit('Ninja exe missing from staging dir.')
def del_infodirs(self, dirname):
# Starting with 3.9.something there are some
# extra metadatadirs that have a hyphen in their
# file names. This is a forbidden character in WiX
# filenames so delete them.
for d in glob(os.path.join(dirname, '*-info')):
shutil.rmtree(d)
def generate_files(self):
'''
Generate package files for MSI installer package
'''
self.root = ET.Element('Wix', {
'xmlns': 'http://wixtoolset.org/schemas/v4/wxs',
'xmlns:ui': 'http://wixtoolset.org/schemas/v4/wxs/ui'
})
package = ET.SubElement(self.root, 'Package', {
'Name': self.product_name,
'Manufacturer': 'The Meson Development Team',
'UpgradeCode': self.update_guid,
'Language': '1033',
'Codepage': '1252',
'Version': self.version,
})
ET.SubElement(package, 'SummaryInformation', {
'Keywords': 'Installer',
'Description': f'Meson {self.version} installer',
'Manufacturer': 'The Meson Development Team',
})
ET.SubElement(package,
'Launch',
{'Message': 'This application is only supported on Windows 10 or higher.',
'Condition': 'X'*len(WINVER_CHECK)})
ET.SubElement(package, 'MajorUpgrade',
{'DowngradeErrorMessage':
'A newer version of Meson is already installed.'})
ET.SubElement(package, 'Media', {
'Id': '1',
'Cabinet': 'meson.cab',
'EmbedCab': 'yes',
})
targetdir = ET.SubElement(package, 'StandardDirectory', {
'Id': 'ProgramFiles64Folder',
})
installdir = ET.SubElement(targetdir, 'Directory', {
'Id': 'INSTALLDIR',
'Name': 'Meson',
})
ET.SubElement(installdir, 'Merge', {
'Id': 'VCRedist',
'SourceFile': self.redist_path,
'DiskId': '1',
'Language': '0',
})
ET.SubElement(package, 'ui:WixUI', {
'Id': 'WixUI_FeatureTree',
})
for s_d in self.staging_dirs:
assert os.path.isdir(s_d)
top_feature = ET.SubElement(package, 'Feature', {
'Id': 'Complete',
'Title': 'Meson ' + self.version,
'Description': 'The complete package',
'Display': 'expand',
'Level': '1',
'ConfigurableDirectory': 'INSTALLDIR',
})
for s_d in self.staging_dirs:
nodes = {}
for root, dirs, files in os.walk(s_d):
cur_node = Node(dirs, files)
nodes[root] = cur_node
self.create_xml(nodes, s_d, installdir, s_d)
self.build_features(top_feature, s_d)
vcredist_feature = ET.SubElement(top_feature, 'Feature', {
'Id': 'VCRedist',
'Title': 'Visual C++ runtime',
'AllowAdvertise': 'no',
'Display': 'hidden',
'Level': '1',
})
ET.SubElement(vcredist_feature, 'MergeRef', {'Id': 'VCRedist'})
ET.ElementTree(self.root).write(self.main_xml, encoding='utf-8', xml_declaration=True)
# ElementTree cannot do pretty-printing, so do it manually
import xml.dom.minidom
doc = xml.dom.minidom.parse(self.main_xml)
with open(self.main_xml, 'w') as open_file:
open_file.write(doc.toprettyxml())
# One last fix, add CDATA.
with open(self.main_xml) as open_file:
data = open_file.read()
data = data.replace('X'*len(WINVER_CHECK), WINVER_CHECK)
with open(self.main_xml, 'w') as open_file:
open_file.write(data)
def build_features(self, top_feature, staging_dir):
'''
Generate build features
'''
feature = ET.SubElement(top_feature, 'Feature', self.feature_properties[staging_dir])
for component_id in self.feature_components[staging_dir]:
ET.SubElement(feature, 'ComponentRef', {
'Id': component_id,
})
def create_xml(self, nodes, current_dir, parent_xml_node, staging_dir):
'''
Create XML file
'''
cur_node = nodes[current_dir]
if cur_node.files:
component_id = f'ApplicationFiles{self.component_num}'
comp_xml_node = ET.SubElement(parent_xml_node, 'Component', {
'Id': component_id,
'Bitness': 'always64',
'Guid': gen_guid(),
})
self.feature_components[staging_dir].append(component_id)
if self.component_num == 0:
ET.SubElement(comp_xml_node, 'Environment', {
'Id': 'Environment',
'Name': 'PATH',
'Part': 'last',
'System': 'yes',
'Action': 'set',
'Value': '[INSTALLDIR]',
})
self.component_num += 1
for f_node in cur_node.files:
file_id = os.path.join(current_dir, f_node).replace('\\', '_').replace('#', '_').replace('-', '_')
ET.SubElement(comp_xml_node, 'File', {
'Id': file_id,
'Name': f_node,
'Source': os.path.join(current_dir, f_node),
})
for dirname in cur_node.dirs:
dir_id = os.path.join(current_dir, dirname).replace('\\', '_').replace('/', '_').replace('-', '_')
dir_node = ET.SubElement(parent_xml_node, 'Directory', {
'Id': dir_id,
'Name': dirname,
})
self.create_xml(nodes, os.path.join(current_dir, dirname), dir_node, staging_dir)
def build_package(self):
'''
Generate the Meson build MSI package.
'''
subprocess.check_call(['wix',
'build',
'-bindvariable', 'WixUILicenseRtf=packaging\\License.rtf',
'-ext', 'WixToolset.UI.wixext',
'-culture', 'en-us',
'-arch', 'x64',
'-o',
self.final_output,
self.main_xml,
])
def is_nuget_source_active():
'''
Check if nuget source is active
'''
result = subprocess.run(['dotnet', 'nuget', 'list', 'source', '--format', 'Short'], stdout=subprocess.PIPE)
return f'E {NUGET_INDEX}' in result.stdout.decode('utf-8')
def is_wixext_installed():
'''
Check if wix extension is installed
'''
result = subprocess.run(['wix', 'extension', 'list'], stdout=subprocess.PIPE)
return WIXEXT_TOOL in result.stdout.decode('utf-8')
def install_wix():
# Check if nuget source is active before trying to add it
# dotnet nuget add source returns non-zero if the source already exists
if not is_nuget_source_active():
subprocess.check_call(['dotnet',
'nuget',
'add',
'source',
NUGET_INDEX])
subprocess.check_call(['dotnet',
'tool',
'install',
'--global',
'wix'])
if __name__ == '__main__':
if not os.path.exists('meson.py'):
sys.exit(print('Run me in the top level source dir.'))
if not shutil.which('wix'):
install_wix()
# Install wixext if not installed
if not is_wixext_installed():
subprocess.check_call(['wix',
'extension',
'add',
WIXEXT_TOOL,
])
subprocess.check_call(['pip', 'install', '--upgrade', 'pyinstaller'])
p = PackageGenerator()
p.build_dist()
p.generate_files()
p.build_package()
|