-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake.py
More file actions
606 lines (505 loc) · 16.1 KB
/
make.py
File metadata and controls
606 lines (505 loc) · 16.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/env python3
# Copyright (c) 2025 ADBC Drivers Contributors
#
# 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.
"""
A build script for ADBC drivers using doit.
See: https://pydoit.org/
"""
import os
import platform
import shlex
import subprocess
import sys
from pathlib import Path
import doit
import packaging.version
match platform.system():
case "Darwin":
EXT = "dylib"
PLATFORM = "macos"
case "Linux":
EXT = "so"
PLATFORM = "linux"
case "Windows":
EXT = "dll"
PLATFORM = "windows"
case _:
raise RuntimeError(f"Unsupported platform: {platform.system()}")
DOIT_CONFIG = {
"default_tasks": ["build"],
}
SMUGGLE_VARS = {"CGO_CFLAGS", "CGO_LDFLAGS", "PROTOC"}
def to_bool(value: str | bool) -> bool:
if value is None:
return False
elif isinstance(value, bool):
return value
value = value.lower()
if value in {"1", "true", "yes"}:
return True
elif value in {"0", "false", "no"}:
return False
raise ValueError(f"Cannot convert {value!r} to bool")
def is_verbose() -> bool:
return to_bool(get_var("VERBOSE", "False"))
def append_flags(env: dict[str, str], var: str, flags: str) -> None:
if var in env:
env[var] += " " + flags
else:
env[var] = flags
def architecture() -> str:
match platform.machine():
case "AMD64":
return "amd64"
case "aarch64":
return "arm64"
case "arm64v8":
return "arm64"
case "x86_64":
return "amd64"
case _:
raise ValueError(f"{platform.machine()} is not a recognized architecture")
def _check_call(f, *args, **kwargs) -> str:
extra_env = kwargs.pop("env", {})
if extra_env:
env = os.environ.copy()
for k, v in extra_env.items():
if k in {"CGO_CFLAGS", "CGO_LDFLAGS"}:
if k in env:
env[k] += " " + v
else:
env[k] = v
elif k in {
"ADBC_DRIVER_BUILD_VERSION",
"ARCH",
"MACOSX_DEPLOYMENT_TARGET",
"SOURCE_ROOT",
}:
env[k] = v
else:
raise TypeError(f"Unsupported env var override {k}")
env.update(extra_env)
kwargs["env"] = env
if is_verbose():
# TODO: use log, color
if kwargs.get("cwd") is not None:
cwd = kwargs["cwd"]
else:
cwd = "."
print(
"*",
f"[{cwd}]",
" ".join(shlex.quote(arg) for arg in args[0]),
file=sys.stderr,
)
if extra_env:
for k, v in extra_env.items():
print("*", "[env]", f"{k}={v}", file=sys.stderr)
return f(*args, **kwargs, text=True)
def check_call(*args, **kwargs) -> str:
return _check_call(subprocess.check_call, *args, **kwargs)
def check_output(*args, **kwargs) -> str:
return _check_call(subprocess.check_output, *args, **kwargs).strip()
def info(*args, **kwargs):
print("!", *args, **kwargs, file=sys.stderr)
def detect_version(
driver_root: Path,
*,
strict: bool = False,
) -> str:
repo_root = driver_root
while not (repo_root / ".git").is_dir():
if repo_root.parent == repo_root:
raise ValueError(f"{driver_root} is not in a git repository")
repo_root = repo_root.parent
prefix = str(driver_root.relative_to(repo_root))
if prefix == ".":
prefix = "v"
else:
prefix = f"{prefix}/v"
tags = check_output(
[
"git",
"tag",
"-l",
"--no-column",
"--no-format",
"--no-color",
"--sort",
"-v:refname",
f"{prefix}*",
],
cwd=repo_root,
).splitlines()
if not tags:
if strict:
raise ValueError(f"No tags found for driver {driver_root}")
version = "unknown"
else:
tag = tags[0]
version = tag[len(prefix) - 1 :]
# If we are not on the tag, append the commit count and hash
count = int(
check_output(["git", "rev-list", f"{tag}..HEAD", "--count"], cwd=repo_root)
)
if count > 0:
if strict:
raise ValueError(
f"Driver {driver_root} is not on tag {tag}, but has {count} commits since"
)
rev = check_output(["git", "rev-parse", "--short", "HEAD"], cwd=repo_root)
version += f"-dev.{count}.{rev}"
# Append -dirty if there are uncommitted changes
dirty = check_output(["git", "status", "--porcelain"], cwd=repo_root).splitlines()
# Ignore untracked files
if any(not line.startswith("?? ") for line in dirty):
if strict:
info(repo_root, "has uncommitted changes. `git status --porcelain`:")
for line in dirty:
info("> ", line)
raise ValueError(f"{repo_root} has uncommitted changes")
version += "-dirty"
return version
def get_var(name: str, default: str) -> str:
value = os.environ.get(name)
if value is not None:
return value
value = doit.get_var(name, default)
return value
def maybe_build_docker(
*,
repo_root: Path,
driver_root: Path,
env: dict[str, str],
args: list[str],
ci: bool,
) -> None:
if not ci or platform.system() != "Linux":
check_call(args, cwd=driver_root, env=env)
return
env = env.copy()
env["SOURCE_ROOT"] = str(repo_root)
env["ARCH"] = architecture()
volumes = get_var("ADDITIONAL_VOLUMES", "")
if volumes:
volumes = volumes.split(",")
# Some env vars need to be explicitly propagated into Docker
smuggle_env = ""
for var in SMUGGLE_VARS:
if var in env:
smuggle_env += f'{var}="{shlex.quote(env[var])}" '
elif var in os.environ:
smuggle_env += f'{var}="{shlex.quote(os.environ[var])}" '
command = [
"docker",
"compose",
"run",
"--rm",
"--user",
str(os.getuid()),
]
for volume in volumes:
command.extend(["-v", volume])
command.extend(
[
"manylinux-rust",
"--",
"bash",
"-c",
f"cd /source/{driver_root.relative_to(repo_root)} && env {smuggle_env} {' '.join(args)}",
]
)
check_call(command, cwd=Path(__file__).parent, env=env)
def build_go(
repo_root: Path,
driver_root: Path,
driver: str,
target: str,
*,
ci: bool = False,
) -> None:
version = detect_version(driver_root)
(repo_root / "build").mkdir(exist_ok=True)
# Embed the version in the library
prop = "github.com/adbc-drivers/driverbase-go/driverbase.infoDriverVersion"
ldflags = " ".join(
[
"-s",
"-w",
f"-X {prop}={version}",
]
)
tags = ["driverlib"]
if to_bool(get_var("DEBUG", "False")):
tags.append("assert")
extra_tags = get_var("BUILD_TAGS", "")
if extra_tags:
extra_tags = extra_tags.split(",")
extra_tags = [tag.strip() for tag in extra_tags]
extra_tags = [tag for tag in extra_tags if tag]
tags.extend(extra_tags)
tags = ",".join(tags)
tags = "-tags=" + tags
info("Building", target, "version", version)
env = {}
for var in SMUGGLE_VARS:
if var in os.environ:
env[var] = os.environ[var]
if platform.system() == "Darwin":
append_flags(env, "CGO_CFLAGS", "-mmacosx-version-min=11.0")
append_flags(env, "CGO_LDFLAGS", "-mmacosx-version-min=11.0")
if ci and platform.system() == "Linux":
check_call(["go", "mod", "vendor"], cwd=driver_root)
ldflags += (
" -linkmode external -extldflags=-Wl,--version-script=/only-export-adbc.ld"
)
# Command differs under Docker so don't invoke this otherwise
maybe_build_docker(
repo_root=repo_root,
driver_root=driver_root,
env=env,
args=[
"go",
"build",
"-buildmode=c-shared",
tags,
"-o",
f"/source/build/{target}",
"-ldflags",
ldflags,
"./pkg",
],
ci=ci,
)
else:
check_call(
[
"go",
"build",
"-buildmode=c-shared",
tags,
"-o",
f"{repo_root / 'build' / target}",
"-ldflags",
ldflags,
"./pkg",
],
cwd=driver_root,
env=env,
)
output = (repo_root / "build" / target).resolve()
output.chmod(0o755)
header = output.with_suffix(".h")
header.unlink(missing_ok=True)
def build_rust(
repo_root: Path,
driver_root: Path,
driver: str,
target: str,
*,
ci: bool = False,
) -> None:
version = detect_version(driver_root)
(repo_root / "build").mkdir(exist_ok=True)
debug = to_bool(get_var("DEBUG", "False"))
# Note: version embedded in library is determined by Cargo.toml
# TODO: check that it matches git tag?
args = []
if not debug:
args.append("--release")
features = []
extra_features = get_var("FEATURES", "")
if extra_features:
extra_features = extra_features.split(",")
extra_features = [tag.strip() for tag in extra_features]
extra_features = [tag for tag in extra_features if tag]
features.extend(extra_features)
if features:
args.append("--features")
args.append(",".join(features))
info("Building", target, "version", version, "features", features)
env = {}
if platform.system() == "Darwin":
# https://doc.rust-lang.org/nightly/rustc/platform-support/apple-darwin.html#os-version
env["MACOSX_DEPLOYMENT_TARGET"] = "11.0"
maybe_build_docker(
repo_root=repo_root,
driver_root=driver_root,
env=env,
args=["cargo", "build", *args],
ci=ci,
)
lib = driver_root / "target"
if debug:
lib = lib / "debug"
else:
lib = lib / "release"
source_target = target
if platform.system() == "Windows":
source_target = target.removeprefix("lib")
lib = lib / source_target
lib.rename(repo_root / "build" / target)
output = (repo_root / "build" / target).resolve()
output.chmod(0o755)
def build_custom(
repo_root: Path,
driver_root: Path,
driver: str,
target: str,
*,
ci: bool = False,
) -> None:
version = detect_version(driver_root)
(repo_root / "build").mkdir(exist_ok=True)
debug = to_bool(get_var("DEBUG", "False"))
args = []
if debug:
args.append("release")
else:
args.append("test")
args.append(PLATFORM)
args.append(architecture())
info("Building", target, "version", version)
env = {}
if platform.system() == "Darwin":
env["MACOSX_DEPLOYMENT_TARGET"] = "11.0"
maybe_build_docker(
repo_root=repo_root,
driver_root=driver_root,
env=env,
args=["./ci/scripts/build.sh", *args],
ci=ci,
)
output = (repo_root / "build" / target).resolve()
output.chmod(0o755)
def check_linux(binary: Path) -> None:
symbols = check_output(
[
"nm",
"--demangle",
"--dynamic",
str(binary),
]
).splitlines()
# TODO(https://github.com/adbc-drivers/dev/issues/36): check exported symbols
bad_symbols = []
for symbol in symbols:
if " T " not in symbol:
continue
_, _, name = symbol.partition(" T ")
if not name.startswith("Adbc"):
bad_symbols.append(name)
if bad_symbols:
raise RuntimeError(
f"{', '.join(bad_symbols[:3])}... ({len(bad_symbols)} symbols total) should not be exported from {binary}"
)
# Like upstream. Match manylinux2014's versions.
# https://peps.python.org/pep-0599/#the-manylinux2014-policy
glibc_max = "2.17"
glibcxx_max = "3.14.19"
for symbol in symbols:
if "@GLIBC_" in symbol:
version = packaging.version.Version(symbol.partition("@")[2][6:])
if version > packaging.version.Version(glibc_max):
raise RuntimeError(
f"{symbol} requires too new a glibc (max {glibc_max})"
)
elif "@GLIBCXX_" in symbol:
version = packaging.version.Version(symbol.partition("@")[2][8:])
if version > packaging.version.Version(glibcxx_max):
raise RuntimeError(
f"{symbol} requires too new a glibcxx (max {glibcxx_max})"
)
def check_macos(binary: Path) -> None:
output = check_output(["otool", "-l", str(binary)]).splitlines()
minos = None
for line in output:
line = line.strip()
if not line.startswith("minos"):
continue
_, _, minos = line.partition(" ")
break
if minos is None:
raise RuntimeError("Could not determine minimum macOS version")
minos = packaging.version.Version(minos)
maxos = packaging.version.Version("11.0")
if minos > maxos:
raise RuntimeError(
f"{binary} requires macOS {minos} but {maxos} was expected at most"
)
def check(binary: Path) -> None:
if platform.system() == "Linux":
check_linux(binary)
elif platform.system() == "Darwin":
check_macos(binary)
def task_build():
driver = get_var("DRIVER", "")
if not driver:
raise ValueError("Must specify DRIVER=driver")
ci = to_bool(get_var("CI", False))
lang = get_var("IMPL_LANG", "go").strip().lower()
repo_root = Path(".").resolve().absolute()
driver_root = Path(driver)
if driver_root.is_dir():
driver_root = driver_root.resolve()
elif (
Path("./go.mod").is_file() or Path("./Cargo.toml").is_file() or lang == "custom"
):
driver_root = Path(".").resolve()
# Compute dependencies
file_deps = []
extensions = [".go", ".c", ".cc", ".cpp", ".h", ".rs"]
for dirname, _, filenames in driver_root.walk():
for filename in filenames:
if filename in {"go.mod", "go.sum", "Cargo.toml", "Cargo.lock"}:
file_deps.append(Path(dirname) / filename)
elif any(filename.endswith(ext) for ext in extensions):
file_deps.append(Path(dirname) / filename)
target = f"libadbc_driver_{driver}.{EXT}"
if lang == "go":
actions = [
lambda: build_go(repo_root, driver_root, driver, target, ci=ci),
]
elif lang == "rust":
actions = [
lambda: build_rust(repo_root, driver_root, driver, target, ci=ci),
]
elif lang == "custom":
actions = [
lambda: build_custom(repo_root, driver_root, driver, target, ci=ci),
]
else:
raise ValueError(f"Unsupported LANG={lang}")
return {
"actions": actions,
"file_dep": [str(p) for p in file_deps],
"targets": [repo_root / "build" / target],
}
def task_check():
driver = get_var("DRIVER", "")
if not driver:
raise ValueError("Must specify DRIVER=driver")
repo_root = Path(".").resolve()
target = repo_root / "build" / f"libadbc_driver_{driver}.{EXT}"
return {
"actions": [
lambda: check(target),
],
"file_dep": [target],
"targets": [],
}
def main():
doit.run(globals())
if __name__ == "__main__":
main()