aboutsummaryrefslogtreecommitdiff
path: root/msi/createmsi.py
blob: bb6492c3b146de9141d30a18a6fefa80a6ada3f7 (plain)
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
#!/usr/bin/env python3

# Copyright 2017 The Meson development team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, os, subprocess, shutil, uuid
from glob import glob
import platform
import xml.etree.ElementTree as ET

sys.path.append(os.getcwd())
from mesonbuild import coredata

xml_templ = '''<?xml version='1.0' encoding='windows-1252'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
  <Product Name='Meson Build System' Manufacturer='The Meson Development Team'
           Id='%s' 
           UpgradeCode='%s'
           Language='1033' Codepage='1252' Version='%s'>
    <Package Id='*' Keywords='Installer' Description="Meson %s installer"
             Comments='Meson is a high performance build system' Manufacturer='Meson development team'
             InstallerVersion='200' Languages='1033' Compressed='yes' SummaryCodepage='1252'
             %s />

    <Media Id="1" Cabinet="meson.cab" EmbedCab="yes" />

    <Directory Id='TARGETDIR' Name='SourceDir'>
      <Directory Id="%s">
        <Directory Id="INSTALLDIR" Name="Meson">
'''

xml_footer_templ = '''
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="DefaultFeature" Level="1">
%s
    </Feature>

  <Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
  <UIRef Id="WixUI_InstallDir" />

  </Product>
</Wix>
'''

file_templ = '''<File Id='%s' Name='%s' DiskId='1' Source='%s'  />
'''

comp_ref_templ = '''<ComponentRef Id="%s" />
'''

path_addition_xml = '''<Environment Id="Environment" Name="PATH" Part="last" System="yes" Action="set" Value="[INSTALLDIR]"/>
'''


def gen_guid():
    return str(uuid.uuid4()).upper()

class Node:
    def __init__(self, dirs, files):
        assert(isinstance(dirs, list))
        assert(isinstance(files, list))
        self.dirs = dirs
        self.files = files

class PackageGenerator:

    def __init__(self):
        self.product_name = 'Meson Build System'
        self.manufacturer = 'The Meson Development Team'
        self.version = coredata.version.replace('dev', '')
        self.guid = 'DF5B3ECA-4A31-43E3-8CE4-97FC8A97212E'
        self.update_guid = '141527EE-E28A-4D14-97A4-92E6075D28B2'
        self.main_xml = 'meson.wxs'
        self.main_o = 'meson.wixobj'
        self.bytesize = '32' if '32' in platform.architecture()[0] else '64'
        self.final_output = 'meson-%s-%s.msi' % (self.version, self.bytesize)
        self.staging_dir = 'dist'
        if self.bytesize == '64':
            self.platform_str = 'Platform="x64"'
            self.progfile_dir = 'ProgramFiles64Folder'
            self.component_platform = 'Win64="yes"'
        else:
            self.platform_str = ''
            self.progfile_dir = 'ProgramFilesFolder'
            self.component_platform = ''

    def build_dist(self):
        if os.path.exists(self.staging_dir):
            shutil.rmtree(self.staging_dir)
        modules = [os.path.splitext(os.path.split(x)[1])[0] for x in glob(os.path.join('mesonbuild/modules/*'))]
        modules = ['mesonbuild.modules.' + x for x in modules if not x.startswith('_')]
        modulestr = ','.join(modules)
        subprocess.check_call(['c:\\Python\python.exe',
                               'c:\\Python\Scripts\\cxfreeze',
                               '--target-dir',
                               self.staging_dir,
                               '--include-modules',
                               modulestr,
                               'meson.py'])
        shutil.copy(shutil.which('ninja'), self.staging_dir)
        if not os.path.exists(os.path.join(self.staging_dir, 'meson.exe')):
            sys.exit('Meson exe missing from staging dir.')
        if not os.path.exists(os.path.join(self.staging_dir, 'ninja.exe')):
            sys.exit('Ninja exe missing from staging dir.')


    def generate_files(self):
        self.root = ET.Element('Wix', {'xmlns': 'http://schemas.microsoft.com/wix/2006/wi'})
        product = ET.SubElement(self.root, 'Product', {
            'Name': self.product_name,
            'Manufacturer': 'The Meson Development Team',
            'Id': self.guid,
            'UpgradeCode': self.update_guid,
            'Language': '1033',
            'Codepage':  '1252',
            'Version': self.version,
            })

        ET.SubElement(product, 'Package',  {
            'Id': '*',
            'Keywords': 'Installer',
            'Description': 'Meson %s installer' % self.version,
            'Comments': 'Meson is a high performance build system',
            'Manufacturer': 'The Meson Development Team',
            'InstallerVersion': '100',
            'Languages': '1033',
            'Compressed': 'yes',
            'SummaryCodepage': '1252',
            })
        ET.SubElement(product, 'Media', {
            'Id': '1',
            'Cabinet': 'meson.cab',
            'EmbedCab': 'yes',
            })
        targetdir = ET.SubElement(product, 'Directory', {
            'Id': 'TARGETDIR',
            'Name': 'SourceDir',
            })
        progfiledir = ET.SubElement(targetdir, 'Directory', {
            'Id' : self.progfile_dir,
            })
        installdir = ET.SubElement(progfiledir, 'Directory', {
            'Id': 'INSTALLDIR',
            'Name': 'Meson'})

        ET.SubElement(product, 'Property', {
            'Id': 'WIXUI_INSTALLDIR',
            'Value': 'INSTALLDIR',
            })
        ET.SubElement(product, 'UIRef', {
            'Id': 'WixUI_InstallDir',
            })
        assert(os.path.isdir(self.staging_dir))
        comp_ref_xml = ''
        nodes = {}
        with open(self.main_xml, 'w') as ofile:
            for root, dirs, files in os.walk(self.staging_dir):
                cur_node = Node(dirs, files)
                nodes[root] = cur_node
            ofile.write(xml_templ % (self.guid, self.update_guid, self.version, self.version,
                                     self.platform_str, self.progfile_dir))
            self.component_num = 0
            self.create_xml(nodes, ofile, self.staging_dir, installdir)
            feature = ET.SubElement(product, 'Feature', {
                'Id': 'DefaultFeature',
                'Level': '1',
                })
            
            for i in range(self.component_num):
                ET.SubElement(feature, 'ComponentRef', {
                    'Id': 'ApplicationFiles%d' % i,
                    })
                comp_ref_xml += comp_ref_templ % ('ApplicationFiles%d' % i)
            ofile.write(xml_footer_templ % comp_ref_xml)

        ET.ElementTree(self.root).write(self.main_xml, encoding='utf-8',xml_declaration=True)

    def create_xml(self, nodes, ofile, current_dir, parent_xml_node):
        cur_node = nodes[current_dir]
        if cur_node.files:
            comp_xml_node = ET.SubElement(parent_xml_node, 'Component', {
                'Id': 'ApplicationFiles%d' % self.component_num,
                'Guid': gen_guid(),
                })
            if self.bytesize == 64:
                comp_xml_node.set('Win64', 'yes')
            if self.component_num == 0:
                ET.SubElement(comp_xml_node, 'Environment', {
                    'Id': 'Environment',
                    'Name': 'PATH',
                    'Part': 'last',
                    'System': 'yes',
                    'Action': 'set',
                    'Value': '[INSTALLDIR]',
                })
            ofile.write("<Component Id='ApplicationFiles%d' Guid='%s' %s>\n" % (self.component_num, gen_guid(), self.component_platform))
            if self.component_num == 0:
                ofile.write(path_addition_xml)
            self.component_num += 1
            for f in cur_node.files:
                file_source = os.path.join(current_dir, f).replace('\\', '\\\\')
                file_id = os.path.join(current_dir, f).replace('\\', '_').replace('#', '_').replace('-', '_')
                ET.SubElement(comp_xml_node, 'File', {
                    'Id': file_id,
                    'Name': f,
                    'Source': os.path.join(current_dir, f),
                    })
                ofile.write(file_templ % (file_id, f, file_source))
            ofile.write('</Component>\n')

        for dirname in cur_node.dirs:
            dir_id = os.path.join(current_dir, dirname).replace('\\', '_').replace('/', '_')
            dir_node = ET.SubElement(parent_xml_node, 'Directory', {
                'Id': dir_id,
                'Name': dirname,
                })
            ofile.write('''<Directory Id="%s" Name="%s">\n''' % (dir_id, dirname))
            self.create_xml(nodes, ofile, os.path.join(current_dir, dirname), dir_node)
            ofile.write('</Directory>\n')
        


    def build_package(self):
        wixdir = 'c:\\Program Files\\Wix Toolset v3.11\\bin'
        if not os.path.isdir(wixdir):
            wixdir = 'c:\\Program Files (x86)\\Wix Toolset v3.11\\bin'
        subprocess.check_call([os.path.join(wixdir, 'candle'), self.main_xml])
        subprocess.check_call([os.path.join(wixdir, 'light'),
                               '-ext', 'WixUIExtension',
                               '-cultures:en-us',
                               '-dWixUILicenseRtf=msi\\License.rtf',
                               '-out', self.final_output,
                               self.main_o])

if __name__ == '__main__':
    if not os.path.exists('meson.py'):
        sys.exit(print('Run me in the top level source dir.'))

    p = PackageGenerator()
    p.build_dist()
    p.generate_files()
    p.build_package()