aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/project/ci/dirs.py
blob: 6513455ca0bb19cf2f933bb21a945fe9d53cbeab (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
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
# Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "cmake-common" project.
# For details, see https://github.com/egor-tensin/cmake-common.
# Distributed under the MIT License.

import abc
import os
import os.path

from project.boost.version import Version
from project.ci.appveyor.generator import Generator, Image
from project.configuration import Configuration
import project.os
from project.platform import Platform
from project.toolchain import ToolchainType
from project.utils import env


class Dirs(abc.ABC):
    @staticmethod
    def detect():
        matching = [ci for ci in _ALL_CI_LIST if ci.this_one()]
        if len(matching) == 0:
            raise RuntimeError('no CI system was detected')
        if len(matching) > 1:
            names = ', '.join(ci.get_name() for ci in matching)
            raise RuntimeError(f"can't select a single CI system out of these: {names}")
        return matching[0]

    def __init__(self):
        pass

    @staticmethod
    @abc.abstractmethod
    def get_name():
        pass

    @abc.abstractmethod
    def this_one(self):
        pass

    def get_toolset(self):
        if 'toolset' in os.environ:
            return ToolchainType.parse(os.environ['toolset'])
        return None

    @abc.abstractmethod
    def get_platform(self):
        pass

    @abc.abstractmethod
    def get_configuration(self):
        pass

    @abc.abstractmethod
    def get_src_dir(self):
        pass

    def get_build_dir(self):
        return os.path.join(os.path.dirname(self.get_src_dir()), 'build')

    @abc.abstractmethod
    def get_prebuilt_boost_dir(self):
        pass

    def get_boost_version(self):
        return Version.from_string(env('boost_version'))

    def get_boost_dir(self):
        return os.path.join(self.get_build_dir(), 'boost')

    def get_cmake_dir(self):
        return os.path.join(self.get_build_dir(), 'cmake')

    def get_install_dir(self):
        return os.path.join(self.get_build_dir(), 'install')

    @abc.abstractmethod
    def get_cmake_args(self):
        pass

    @staticmethod
    def get_boost_help():
        names = ', '.join(ci.get_name() for ci in _ALL_CI_LIST)
        return f'''Download & build Boost during a CI run.

This is similar to running both project.boost.download & project.boost.build,
but auto-fills some parameters from environment variables.

The supported CI systems are: {names}.
'''

    @staticmethod
    def get_cmake_help():
        names = ', '.join(ci.get_name() for ci in _ALL_CI_LIST)
        return f'''Build a CMake project during a CI run.

This is similar to running project.cmake.build, but auto-fills some parameters
from environment variables.

The supported CI systems are: {names}.
'''


class Travis(Dirs):
    @staticmethod
    def get_name():
        return 'Travis'

    def this_one(self):
        return 'TRAVIS' in os.environ

    def get_platform(self):
        return Platform.parse(env('platform'))

    def get_configuration(self):
        return Configuration.parse(env('configuration'))

    def get_src_dir(self):
        return env('TRAVIS_BUILD_DIR')

    def get_prebuilt_boost_dir(self):
        # Travis doesn't have pre-built Boost (available for installation from
        # the official Ubuntu repositories though).
        return None

    def get_cmake_args(self):
        return []


class AppVeyor(Dirs):
    @staticmethod
    def get_name():
        return 'AppVeyor'

    def this_one(self):
        return 'APPVEYOR' in os.environ

    def get_platform(self):
        return Platform.parse(env('PLATFORM'))

    def get_configuration(self):
        return Configuration.parse(env('CONFIGURATION'))

    def get_src_dir(self):
        return env('APPVEYOR_BUILD_FOLDER')

    def get_prebuilt_boost_dir(self):
        return Image.get().get_prebuilt_boost_dir()

    def get_cmake_args(self):
        return ['-G', str(Generator.from_image(Image.get()))]


class GitHub(Dirs):
    @staticmethod
    def get_name():
        return 'GitHub Actions'

    def this_one(self):
        return 'GITHUB_ACTIONS' in os.environ

    def get_platform(self):
        return Platform.parse(env('platform'))

    def get_configuration(self):
        return Configuration.parse(env('configuration'))

    def get_src_dir(self):
        return env('GITHUB_WORKSPACE')

    def get_prebuilt_boost_dir(self):
        # As of 2021-01-25, Boost 1.72.0 is pre-built (on all images except for
        # ubuntu-20.04 for some reason).  The path is stored in environment
        # variable BOOST_ROOT_1_72_0.
        var_name = 'BOOST_ROOT_1_72_0'
        if var_name in os.environ:
            return os.environ[var_name]
        return None

    def get_cmake_args(self):
        return []


_ALL_CI_LIST = (Travis(), AppVeyor(), GitHub())