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