forked from emzeat/mz-cmaketools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conan_package.py
executable file
·210 lines (171 loc) · 8.29 KB
/
conan_package.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
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
#!/usr/bin/env python3
"""
conan_package.py
Copyright (c) 2022 Marius Zwicker
All rights reserved.
SPDX-License-Identifier: Apache-2.0
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 argparse
import subprocess
from pathlib import Path
from shutil import rmtree
import os
import sys
import re
def discover_profile(cwd, name_expr):
'''Helper to glob for the first matching directory or file'''
for profile in cwd.glob(name_expr):
return profile
return None
# the name by which conan can be invoked
CONAN = 'conan'
# the name by which cmake can be invoked
CMAKE = 'cmake'
# the remote to which packages get uploaded
REMOTE = os.environ.get('MZ_CONAN_REMOTE_NAME', 'emzeat')
# tracks verbosity settings
VERBOSE_ENV = 'VERBOSE'
VERBOSE = os.environ.get(VERBOSE_ENV, False)
# working directory assuming this gets invoked from within ./build
DEFAULT_WKDIR = Path(__file__).parent.parent
# default recipe
DEFAULT_RECIPE = DEFAULT_WKDIR / 'conanfile.py'
# default channel name
DEFAULT_CHANNEL = 'emzeat/oss'
# default host profile
DEFAULT_PROFILE = discover_profile(DEFAULT_WKDIR / 'build', '*/profile.conan')
# default build profile
DEFAULT_BUILD_PROFILE = discover_profile(DEFAULT_WKDIR / 'build', '*/build_profile.conan')
# default directory to test in
DEFAULT_TEST_DIR = DEFAULT_PROFILE.parent / 'conan_package' if DEFAULT_PROFILE else None
def log_fatal(msg: str) -> None:
'''Helper to log a message and abort'''
log_info('!! ' + msg)
sys.exit(1)
def log_info(msg: str) -> None:
'''Helper to log a message'''
sys.stderr.write(f'conan-package: {msg}\n')
def log_debug(msg: str) -> None:
'''Helper to log a debug message'''
if VERBOSE:
log_info(msg)
def invoke_conan(with_args, cwd=DEFAULT_WKDIR, failure_ok=False) -> None:
'''Invokes conan using the given arguments'''
with_args = [str(arg) for arg in with_args]
log_debug(f"Invoking {CONAN} with args={with_args} cwd={cwd}")
try:
subprocess.check_call([CONAN] + with_args, encoding='utf8', cwd=cwd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
if failure_ok:
log_info(f"conan failed but not fatal: {error}")
else:
log_fatal(f"conan failed: {error}")
except FileNotFoundError as error:
log_fatal(f"Failed to invoke conan: {error}")
def invoke_cmake(with_args, cwd=DEFAULT_WKDIR) -> None:
'''Invokes cmake using the given arguments'''
log_debug(f"Invoking {CMAKE} with args={with_args} cwd={cwd}")
try:
subprocess.check_call([CMAKE] + with_args, encoding='utf8', cwd=cwd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
log_fatal(f"cmake failed: {error}")
except FileNotFoundError as error:
log_fatal(f"Failed to invoke cmake: {error}")
parser = argparse.ArgumentParser(description='Helper to test and deploy conan packages')
parser.add_argument('--test', default=False, help='Test the conan recipe step-by-step', action='store_true')
parser.add_argument('--create', default=False,
help='Create the conan package including a build for the given profile.', action='store_true')
parser.add_argument('--build', default=False,
help='Create a build for the given profile using the previously created package.', action='store_true')
parser.add_argument('--upload', default=False, help='Upload the conan package', action='store_true')
parser.add_argument('--verbose', default=VERBOSE, help='Enable verbose logging', action='store_true')
parser.add_argument('--recipe', default=DEFAULT_RECIPE,
help=f'Specifies conan recipe to be processed Default: {DEFAULT_RECIPE}', type=Path)
parser.add_argument('--profile', default=DEFAULT_PROFILE,
help=f'Configures the conan profile to be used. Default: {DEFAULT_PROFILE}', type=Path)
parser.add_argument('--build-profile', default=DEFAULT_BUILD_PROFILE,
help=f'Configures the conan build profile to be used for cross building. Default: {DEFAULT_BUILD_PROFILE}', type=Path)
parser.add_argument('--test-dir', default=DEFAULT_TEST_DIR,
help=f'Specifies the directory to test the package in. Default: {DEFAULT_TEST_DIR}', type=Path)
parser.add_argument('--version', default=None,
help='Specifies the version which will be parsed from git elsewise.', type=str)
args = parser.parse_args()
VERBOSE = args.verbose
if args.recipe.exists():
log_info(f"Processing '{args.recipe}'")
else:
log_fatal(f"No such recipe: {args.recipe}")
rmtree(args.test_dir, ignore_errors=True)
args.test_dir.mkdir(parents=True, exist_ok=True)
log_debug(f"Testing below '{args.test_dir}'")
name = re.search(r'name\s*=\s*[\'"]([^\'"]+)', args.recipe.read_text(), re.MULTILINE)
if name:
args.name = name.group(1)
else:
log_fatal("Failed to match name from recipe")
log_info(f"Package name '{args.name}'")
if args.version is None:
version_txt = args.test_dir / 'version.txt'
invoke_cmake([f'-DMZ_SEMVER_TO_FILE={version_txt}', '-P', 'build/semver.cmake'])
args.version = version_txt.read_text().strip()
log_info(f"Package version '{args.version}'")
if args.test:
source_dir = args.test_dir / 'source'
source_dir.mkdir(parents=True, exist_ok=True)
build_dir = args.test_dir / 'build'
build_dir.mkdir(parents=True, exist_ok=True)
install_dir = args.test_dir / 'install'
install_dir.mkdir(parents=True, exist_ok=True)
package_dir = args.test_dir / 'package'
package_dir.mkdir(parents=True, exist_ok=True)
invoke_conan(['source', '-sf', source_dir, args.recipe])
if args.build_profile:
invoke_conan(['install', '-if', install_dir, '-pr:h', args.profile, '-pr:b', args.build_profile, args.recipe])
else:
invoke_conan(['install', '-if', install_dir, '-pr', args.profile, args.recipe])
invoke_conan(['build', '-bf', build_dir, '-if', install_dir, '-pf', package_dir, '-sf', source_dir, args.recipe])
invoke_conan(['package', '-bf', build_dir, '-if', install_dir, '-pf', package_dir, '-sf', source_dir, args.recipe])
elif args.create:
reference = f'{args.name}/{args.version}@{DEFAULT_CHANNEL}'
test_package = args.recipe.parent / 'test_package'
if not test_package.exists():
log_fatal(f"Missing 'test_package' dir at '{test_package}' - cannot verify recipe so aborting")
log_info("Cleaning local cache")
invoke_conan(['remove', '--force', reference], failure_ok=True)
log_info(f"Creating package as '{reference}'")
if args.build_profile:
invoke_conan(['create', '-tbf', args.test_dir, '-pr:h', args.profile,
'-pr:b', args.build_profile, args.recipe, reference])
else:
invoke_conan(['create', '-tbf', args.test_dir, '-pr', args.profile, args.recipe, reference])
if args.upload:
log_info(f"Uploading to '{REMOTE}'")
invoke_conan(['upload', '-r', REMOTE, '--all', '--check', '--confirm', reference])
elif args.build:
reference = f'{args.name}/{args.version}@{DEFAULT_CHANNEL}'
out_dir = args.test_dir / 'out'
out_dir.mkdir(parents=True, exist_ok=True)
install_dir = args.test_dir / 'install'
install_dir.mkdir(parents=True, exist_ok=True)
log_info("Cleaning local cache")
invoke_conan(['remove', '--force', reference], failure_ok=True)
log_info(f"Building package '{reference}'")
if args.build_profile:
invoke_conan(['install', '-if', install_dir, '-of', out_dir, '-pr:h', args.profile,
'-pr:b', args.build_profile, '-b', args.name, reference])
else:
invoke_conan(['install', '-if', install_dir, '-of', out_dir, '-pr', args.profile, '-b', args.name, reference])
if args.upload:
log_info(f"Uploading to '{REMOTE}'")
invoke_conan(['upload', '-r', REMOTE, '--all', '--check', '--confirm', reference])
else:
log_fatal("Please pass --test, --create or --build")