summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/make_installer.py
blob: dbca577118b02d3e2514eb8e3b2e3c1246db609f (plain) (blame)
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
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import os
import posixpath
import sys

from llnl.path import convert_to_posix_path

import spack.paths
import spack.util.executable
from spack.spec import Spec

description = "generate Windows installer"
section = "admin"
level = "long"


def txt_to_rtf(file_path):
    rtf_header = r"""{{\rtf1\ansi\deff0\nouicompat
    {{\fonttbl{{\f0\\fnil\fcharset0 Courier New;}}}}
    {{\colortbl ;\red0\green0\blue255;}}
    {{\*\generator Riched20 10.0.19041}}\viewkind4\uc1
    \f0\fs22\lang1033
    {}
    }}
    """

    def line_to_rtf(str):
        return str.replace("\n", "\\par")

    contents = ""
    with open(file_path, "r+") as f:
        for line in f.readlines():
            contents += line_to_rtf(line)
    return rtf_header.format(contents)


def setup_parser(subparser):
    spack_source_group = subparser.add_mutually_exclusive_group(required=True)
    spack_source_group.add_argument(
        "-v", "--spack-version", default="", help="download given spack version"
    )
    spack_source_group.add_argument(
        "-s", "--spack-source", default="", help="full path to spack source"
    )

    subparser.add_argument(
        "-g",
        "--git-installer-verbosity",
        default="",
        choices=["SILENT", "VERYSILENT"],
        help="level of verbosity provided by bundled git installer (default is fully verbose)",
        required=False,
        action="store",
        dest="git_verbosity",
    )

    subparser.add_argument("output_dir", help="output directory")


def make_installer(parser, args):
    """
    Use CMake to generate WIX installer in newly created build directory
    """
    if sys.platform == "win32":
        output_dir = args.output_dir
        cmake_spec = Spec("cmake")
        cmake_spec.concretize()
        cmake_path = os.path.join(cmake_spec.prefix, "bin", "cmake.exe")
        cpack_path = os.path.join(cmake_spec.prefix, "bin", "cpack.exe")
        spack_source = args.spack_source
        git_verbosity = ""
        if args.git_verbosity:
            git_verbosity = "/" + args.git_verbosity

        if spack_source:
            if not os.path.exists(spack_source):
                print("%s does not exist" % spack_source)
                return
            else:
                if not os.path.isabs(spack_source):
                    spack_source = posixpath.abspath(spack_source)
                spack_source = convert_to_posix_path(spack_source)

        spack_version = args.spack_version

        here = os.path.dirname(os.path.abspath(__file__))
        source_dir = os.path.join(here, "installer")
        posix_root = convert_to_posix_path(spack.paths.spack_root)
        spack_license = posixpath.join(posix_root, "LICENSE-APACHE")
        rtf_spack_license = txt_to_rtf(spack_license)
        spack_license = posixpath.join(source_dir, "LICENSE.rtf")

        with open(spack_license, "w") as rtf_license:
            written = rtf_license.write(rtf_spack_license)
            if written == 0:
                raise RuntimeError("Failed to generate properly formatted license file")
        spack_logo = posixpath.join(posix_root, "share/spack/logo/favicon.ico")

        try:
            spack.util.executable.Executable(cmake_path)(
                "-S",
                source_dir,
                "-B",
                output_dir,
                "-DSPACK_VERSION=%s" % spack_version,
                "-DSPACK_SOURCE=%s" % spack_source,
                "-DSPACK_LICENSE=%s" % spack_license,
                "-DSPACK_LOGO=%s" % spack_logo,
                "-DSPACK_GIT_VERBOSITY=%s" % git_verbosity,
            )
        except spack.util.executable.ProcessError:
            print("Failed to generate installer")
            return spack.util.executable.ProcessError.returncode

        try:
            spack.util.executable.Executable(cpack_path)(
                "--config", "%s/CPackConfig.cmake" % output_dir, "-B", "%s/" % output_dir
            )
        except spack.util.executable.ProcessError:
            print("Failed to generate installer")
            return spack.util.executable.ProcessError.returncode
        try:
            spack.util.executable.Executable(os.environ.get("WIX") + "/bin/candle.exe")(
                "-ext",
                "WixBalExtension",
                "%s/bundle.wxs" % output_dir,
                "-out",
                "%s/bundle.wixobj" % output_dir,
            )
        except spack.util.executable.ProcessError:
            print("Failed to generate installer chain")
            return spack.util.executable.ProcessError.returncode
        try:
            spack.util.executable.Executable(os.environ.get("WIX") + "/bin/light.exe")(
                "-sw1134",
                "-ext",
                "WixBalExtension",
                "%s/bundle.wixobj" % output_dir,
                "-out",
                "%s/Spack.exe" % output_dir,
            )
        except spack.util.executable.ProcessError:
            print("Failed to generate installer chain")
            return spack.util.executable.ProcessError.returncode
        print("Successfully generated Spack.exe in %s" % (output_dir))
    else:
        print("The make-installer command is currently only supported on Windows.")