aboutsummaryrefslogtreecommitdiffstats
path: root/python/servo/platform/windows.py
blob: cea8de5a5dd59d811b096e7725a9ed0ea5fd11fd (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
# Copyright 2023 The Servo Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import os
import shutil
import subprocess
import urllib
import zipfile

from distutils.version import LooseVersion

import six
from .base import Base
from ..util import extract, download_file

DEPS_URL = "https://github.com/servo/servo-build-deps/releases/download/msvc-deps/"
DEPENDENCIES = {
    "cmake": "3.14.3",
    "llvm": "15.0.5",
    "moztools": "3.2",
    "ninja": "1.7.1",
    "nuget": "08-08-2019",
    "openssl": "111.3.0+1.1.1c-vs2017-2019-09-18",
    "gstreamer-uwp": "1.16.0.5",
    "openxr-loader-uwp": "1.0",
}


class Windows(Base):
    def __init__(self):
        pass

    @staticmethod
    def cmake_already_installed(required_version: str) -> bool:
        cmake_path = shutil.which("cmake")
        if not cmake_path:
            return False

        output = subprocess.check_output([cmake_path, "--version"])
        cmake_version_output = six.ensure_str(output).splitlines()[0]
        installed_version = cmake_version_output.replace("cmake version ", "")
        return LooseVersion(installed_version) >= LooseVersion(required_version)

    @classmethod
    def prepare_file(cls, deps_dir: str, zip_path: str, full_spec: str):
        if not os.path.isfile(zip_path):
            zip_url = "{}{}.zip".format(DEPS_URL, urllib.parse.quote(full_spec))
            download_file(full_spec, zip_url, zip_path)

        print("Extracting {}...".format(full_spec), end='')
        try:
            extract(zip_path, deps_dir)
        except zipfile.BadZipfile:
            print("\nError: %s.zip is not a valid zip file, redownload..." % full_spec)
            os.remove(zip_path)
            cls.prepare_file(deps_dir, zip_path, full_spec)
        else:
            print("done")

    def _platform_bootstrap(self, cache_dir: str, _force: bool = False) -> bool:
        deps_dir = os.path.join(cache_dir, "msvc-dependencies")

        def get_package_dir(package, version) -> str:
            return os.path.join(deps_dir, package, version)

        to_install = {}
        for (package, version) in DEPENDENCIES.items():
            # Don't install CMake if it already exists in PATH
            if package == "cmake" and self.cmake_already_installed(version):
                continue

            if not os.path.isdir(get_package_dir(package, version)):
                to_install[package] = version

        if not to_install:
            return False

        print("Installing missing MSVC dependencies...")
        for (package, version) in to_install.items():
            full_spec = '{}-{}'.format(package, version)

            package_dir = get_package_dir(package, version)
            parent_dir = os.path.dirname(package_dir)
            if not os.path.isdir(parent_dir):
                os.makedirs(parent_dir)

            self.prepare_file(deps_dir, package_dir + ".zip", full_spec)

            extracted_path = os.path.join(deps_dir, full_spec)
            os.rename(extracted_path, package_dir)

        return True