-
Notifications
You must be signed in to change notification settings - Fork 440
/
_env.py
150 lines (119 loc) · 4.58 KB
/
_env.py
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
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from snapcraft import formatting_utils, project
from snapcraft.internal import common, elf, pluginhandler
from typing import Dict, List
def env_for_classic(base: str, arch_triplet: str) -> List[str]:
"""Set the required environment variables for a classic confined build."""
env = []
core_path = common.get_core_path(base)
paths = common.get_library_paths(core_path, arch_triplet, existing_only=False)
env.append(
formatting_utils.format_path_variable(
"LD_LIBRARY_PATH", paths, prepend="", separator=":"
)
)
return env
def runtime_env(root: str, arch_triplet: str) -> List[str]:
"""Set the environment variables required for running binaries."""
env = []
env.append(
'PATH="'
+ ":".join(
["{0}/usr/sbin", "{0}/usr/bin", "{0}/sbin", "{0}/bin", "$PATH"]
).format(root)
+ '"'
)
# Add the default LD_LIBRARY_PATH
paths = common.get_library_paths(root, arch_triplet)
# Add more specific LD_LIBRARY_PATH from staged packages if necessary
paths += elf.determine_ld_library_path(root)
if paths:
env.append(
formatting_utils.format_path_variable(
"LD_LIBRARY_PATH", paths, prepend="", separator=":"
)
)
return env
def build_env(root: str, snap_name: str, arch_triplet: str) -> List[str]:
"""Set the environment variables required for building.
This is required for the current parts installdir due to stage-packages
and also to setup the stagedir.
"""
env = []
paths = common.get_include_paths(root, arch_triplet)
if paths:
for envvar in ["CPPFLAGS", "CFLAGS", "CXXFLAGS"]:
env.append(
formatting_utils.format_path_variable(
envvar, paths, prepend="-I", separator=" "
)
)
paths = common.get_library_paths(root, arch_triplet)
if paths:
env.append(
formatting_utils.format_path_variable(
"LDFLAGS", paths, prepend="-L", separator=" "
)
)
paths = common.get_pkg_config_paths(root, arch_triplet)
if paths:
env.append(
formatting_utils.format_path_variable(
"PKG_CONFIG_PATH", paths, prepend="", separator=":"
)
)
return env
def build_env_for_stage(stagedir: str, snap_name: str, arch_triplet: str) -> List[str]:
env = build_env(stagedir, snap_name, arch_triplet)
env.append('PERL5LIB="{0}/usr/share/perl5/"'.format(stagedir))
return env
def snapcraft_global_environment(project: project.Project) -> Dict[str, str]:
if project.info.name:
name = project.info.name
else:
name = ""
if project.info.version:
version = project.info.version
else:
version = ""
if project.info.grade:
grade = project.info.grade
else:
grade = ""
return {
"SNAPCRAFT_ARCH_TRIPLET": project.arch_triplet,
"SNAPCRAFT_PARALLEL_BUILD_COUNT": str(project.parallel_build_count),
"SNAPCRAFT_PROJECT_NAME": name,
"SNAPCRAFT_PROJECT_VERSION": version,
"SNAPCRAFT_PROJECT_GRADE": grade,
"SNAPCRAFT_STAGE": project.stage_dir,
"SNAPCRAFT_PRIME": project.prime_dir,
"SNAPCRAFT_EXTENSIONS_DIR": common.get_extensionsdir(),
}
def snapcraft_part_environment(part: pluginhandler.PluginHandler) -> Dict[str, str]:
return {
"SNAPCRAFT_PART_SRC": part.plugin.sourcedir,
"SNAPCRAFT_PART_BUILD": part.plugin.builddir,
"SNAPCRAFT_PART_INSTALL": part.plugin.installdir,
}
def environment_to_replacements(environment: Dict[str, str]) -> Dict[str, str]:
replacements = {} # type: Dict[str, str]
for variable, value in environment.items():
# Support both $VAR and ${VAR} syntax
replacements["${}".format(variable)] = value
replacements["${{{}}}".format(variable)] = value
return replacements