Skip to content

Commit 6f43c59

Browse files
authored
feat: adjust make.py for ad-hoc builds (#73)
1 parent eb8f394 commit 6f43c59

File tree

2 files changed

+130
-82
lines changed

2 files changed

+130
-82
lines changed

adbc_drivers_dev/make.py

Lines changed: 130 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@
3232
match platform.system():
3333
case "Darwin":
3434
EXT = "dylib"
35+
PLATFORM = "macos"
3536
case "Linux":
3637
EXT = "so"
38+
PLATFORM = "linux"
3739
case "Windows":
3840
EXT = "dll"
41+
PLATFORM = "windows"
3942
case _:
4043
raise RuntimeError(f"Unsupported platform: {platform.system()}")
4144

4245

4346
DOIT_CONFIG = {
4447
"default_tasks": ["build"],
4548
}
49+
SMUGGLE_VARS = {"CGO_CFLAGS", "CGO_LDFLAGS", "PROTOC"}
4650

4751

4852
def to_bool(value: str | bool) -> bool:
@@ -140,9 +144,6 @@ def detect_version(
140144
*,
141145
strict: bool = False,
142146
) -> str:
143-
if not any((driver_root / name).is_file() for name in ("Cargo.toml", "go.mod")):
144-
raise ValueError(f"{driver_root} does not contain a Cargo.toml or go.mod")
145-
146147
repo_root = driver_root
147148
while not (repo_root / ".git").is_dir():
148149
if repo_root.parent == repo_root:
@@ -211,6 +212,58 @@ def get_var(name: str, default: str) -> str:
211212
return value
212213

213214

215+
def maybe_build_docker(
216+
*,
217+
repo_root: Path,
218+
driver_root: Path,
219+
env: dict[str, str],
220+
args: list[str],
221+
ci: bool,
222+
) -> None:
223+
if not ci or platform.system() != "Linux":
224+
check_call(args, cwd=driver_root, env=env)
225+
return
226+
227+
env = env.copy()
228+
env["SOURCE_ROOT"] = str(repo_root)
229+
env["ARCH"] = architecture()
230+
231+
volumes = get_var("ADDITIONAL_VOLUMES", "")
232+
if volumes:
233+
volumes = volumes.split(",")
234+
235+
# Some env vars need to be explicitly propagated into Docker
236+
smuggle_env = ""
237+
for var in SMUGGLE_VARS:
238+
if var in env:
239+
smuggle_env += f'{var}="{shlex.quote(env[var])}" '
240+
elif var in os.environ:
241+
smuggle_env += f'{var}="{shlex.quote(os.environ[var])}" '
242+
243+
command = [
244+
"docker",
245+
"compose",
246+
"run",
247+
"--rm",
248+
"--user",
249+
str(os.getuid()),
250+
]
251+
252+
for volume in volumes:
253+
command.extend(["-v", volume])
254+
255+
command.extend(
256+
[
257+
"manylinux-rust",
258+
"--",
259+
"bash",
260+
"-c",
261+
f"cd /source/{driver_root.relative_to(repo_root)} && env {smuggle_env} {' '.join(args)}",
262+
]
263+
)
264+
check_call(command, cwd=Path(__file__).parent, env=env)
265+
266+
214267
def build_go(
215268
repo_root: Path,
216269
driver_root: Path,
@@ -249,9 +302,7 @@ def build_go(
249302
info("Building", target, "version", version)
250303

251304
env = {}
252-
253-
smuggle_vars = ("CGO_CFLAGS", "CGO_LDFLAGS")
254-
for var in smuggle_vars:
305+
for var in SMUGGLE_VARS:
255306
if var in os.environ:
256307
env[var] = os.environ[var]
257308

@@ -260,33 +311,29 @@ def build_go(
260311
append_flags(env, "CGO_LDFLAGS", "-mmacosx-version-min=11.0")
261312

262313
if ci and platform.system() == "Linux":
263-
env["SOURCE_ROOT"] = str(repo_root)
264-
env["ARCH"] = architecture()
265-
266314
check_call(["go", "mod", "vendor"], cwd=driver_root)
267-
268-
smuggle_env = ""
269-
for var in smuggle_vars:
270-
if var in env:
271-
smuggle_env += f'{var}="{shlex.quote(env[var])}" '
272-
273315
ldflags += (
274316
" -linkmode external -extldflags=-Wl,--version-script=/only-export-adbc.ld"
275317
)
276-
command = [
277-
"docker",
278-
"compose",
279-
"run",
280-
"--rm",
281-
"--user",
282-
str(os.getuid()),
283-
"manylinux",
284-
"--",
285-
"bash",
286-
"-c",
287-
f'cd /source/{driver_root.relative_to(repo_root)} && env {smuggle_env} go build -buildmode=c-shared {tags} -o /source/build/{target} -ldflags "{ldflags}" ./pkg',
288-
]
289-
check_call(command, cwd=Path(__file__).parent, env=env)
318+
319+
# Command differs under Docker so don't invoke this otherwise
320+
maybe_build_docker(
321+
repo_root=repo_root,
322+
driver_root=driver_root,
323+
env=env,
324+
args=[
325+
"go",
326+
"build",
327+
"-buildmode=c-shared",
328+
tags,
329+
"-o",
330+
f"/source/build/{target}",
331+
"-ldflags",
332+
ldflags,
333+
"./pkg",
334+
],
335+
ci=ci,
336+
)
290337
else:
291338
check_call(
292339
[
@@ -344,60 +391,17 @@ def build_rust(
344391
info("Building", target, "version", version, "features", features)
345392

346393
env = {}
347-
# Some env vars need to be explicitly propagated into Docker
348-
smuggle_vars = {"PROTOC"}
349-
350394
if platform.system() == "Darwin":
351395
# https://doc.rust-lang.org/nightly/rustc/platform-support/apple-darwin.html#os-version
352396
env["MACOSX_DEPLOYMENT_TARGET"] = "11.0"
353397

354-
if ci and platform.system() == "Linux":
355-
env["SOURCE_ROOT"] = str(repo_root)
356-
env["ARCH"] = architecture()
357-
358-
volumes = get_var("ADDITIONAL_VOLUMES", "")
359-
if volumes:
360-
volumes = volumes.split(",")
361-
362-
smuggle_env = ""
363-
for var in smuggle_vars:
364-
if var in env:
365-
smuggle_env += f'{var}="{shlex.quote(env[var])}" '
366-
elif var in os.environ:
367-
smuggle_env += f'{var}="{shlex.quote(os.environ[var])}" '
368-
369-
command = [
370-
"docker",
371-
"compose",
372-
"run",
373-
"--rm",
374-
"--user",
375-
str(os.getuid()),
376-
]
377-
378-
for volume in volumes:
379-
command.extend(["-v", volume])
380-
381-
command.extend(
382-
[
383-
"manylinux-rust",
384-
"--",
385-
"bash",
386-
"-c",
387-
f"cd /source/{driver_root.relative_to(repo_root)} && env {smuggle_env} cargo build {' '.join(args)}",
388-
]
389-
)
390-
check_call(command, cwd=Path(__file__).parent, env=env)
391-
else:
392-
check_call(
393-
[
394-
"cargo",
395-
"build",
396-
*args,
397-
],
398-
cwd=driver_root,
399-
env=env,
400-
)
398+
maybe_build_docker(
399+
repo_root=repo_root,
400+
driver_root=driver_root,
401+
env=env,
402+
args=["cargo", "build", *args],
403+
ci=ci,
404+
)
401405

402406
lib = driver_root / "target"
403407
if debug:
@@ -415,6 +419,45 @@ def build_rust(
415419
output.chmod(0o755)
416420

417421

422+
def build_custom(
423+
repo_root: Path,
424+
driver_root: Path,
425+
driver: str,
426+
target: str,
427+
*,
428+
ci: bool = False,
429+
) -> None:
430+
version = detect_version(driver_root)
431+
(repo_root / "build").mkdir(exist_ok=True)
432+
433+
debug = to_bool(get_var("DEBUG", "False"))
434+
435+
args = []
436+
if debug:
437+
args.append("release")
438+
else:
439+
args.append("test")
440+
args.append(PLATFORM)
441+
args.append(architecture())
442+
443+
info("Building", target, "version", version)
444+
445+
env = {}
446+
if platform.system() == "Darwin":
447+
env["MACOSX_DEPLOYMENT_TARGET"] = "11.0"
448+
449+
maybe_build_docker(
450+
repo_root=repo_root,
451+
driver_root=driver_root,
452+
env=env,
453+
args=["./ci/scripts/build.sh", *args],
454+
ci=ci,
455+
)
456+
457+
output = (repo_root / "build" / target).resolve()
458+
output.chmod(0o755)
459+
460+
418461
def check_linux(binary: Path) -> None:
419462
symbols = check_output(
420463
[
@@ -492,14 +535,16 @@ def task_build():
492535
if not driver:
493536
raise ValueError("Must specify DRIVER=driver")
494537

495-
ci = get_var("CI", False)
538+
ci = to_bool(get_var("CI", False))
496539
lang = get_var("IMPL_LANG", "go").strip().lower()
497540

498541
repo_root = Path(".").resolve().absolute()
499542
driver_root = Path(driver)
500543
if driver_root.is_dir():
501544
driver_root = driver_root.resolve()
502-
elif Path("./go.mod").is_file() or Path("./Cargo.toml").is_file():
545+
elif (
546+
Path("./go.mod").is_file() or Path("./Cargo.toml").is_file() or lang == "custom"
547+
):
503548
driver_root = Path(".").resolve()
504549

505550
# Compute dependencies
@@ -522,6 +567,10 @@ def task_build():
522567
actions = [
523568
lambda: build_rust(repo_root, driver_root, driver, target, ci=ci),
524569
]
570+
elif lang == "custom":
571+
actions = [
572+
lambda: build_custom(repo_root, driver_root, driver, target, ci=ci),
573+
]
525574
else:
526575
raise ValueError(f"Unsupported LANG={lang}")
527576

adbc_drivers_dev/templates/test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ jobs:
377377
fi
378378
set +a
379379
pixi run adbc-make build DEBUG=true VERBOSE=true DRIVER=<{driver}> IMPL_LANG=<{lang}> <{' '.join(lang_config.build.additional_make_args) }>
380-
381380
- name: Start Test Dependencies
382381
# Can't use Docker on macOS AArch64 runners, and Windows containers
383382
# work but often the container doesn't support Windows

0 commit comments

Comments
 (0)