Skip to content

Commit db82d8f

Browse files
committed
Use Bazel to expand archive tree
1 parent 7e0dc4f commit db82d8f

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

elisp/private/tools/build_emacs.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import argparse
22+
from collections.abc import Iterable
2223
import glob
2324
import os
2425
import os.path
@@ -35,32 +36,35 @@ def main() -> None:
3536
parser.add_argument('--release', action='store_true')
3637
parser.add_argument('--readme', type=pathlib.Path, required=True)
3738
parser.add_argument('--install', type=pathlib.Path, required=True)
39+
parser.add_argument(
40+
'--srcs', type=argparse.FileType('rt', encoding='utf-8'), required=True)
3841
parser.add_argument('--bash', type=pathlib.Path)
3942
parser.add_argument('--cc', type=pathlib.Path)
4043
parser.add_argument('--cflags')
4144
parser.add_argument('--ldflags')
4245
parser.add_argument('--module-header', type=pathlib.Path)
4346
args = parser.parse_args()
44-
source = args.readme.resolve().parent
47+
source = args.readme.parent
4548
install = args.install.resolve()
4649

4750
if args.release:
48-
_unpack(source=source, install=install)
51+
_unpack(source=source, install=install, lines=args.srcs)
4952
else:
50-
_build(source=source, install=install, bash=args.bash,
53+
_build(source=source, install=install, lines=args.srcs, bash=args.bash,
5154
cc=args.cc, cflags=args.cflags, ldflags=args.ldflags)
5255

5356
if args.module_header:
5457
# Copy emacs-module.h to the desired location.
5558
shutil.copy(install / 'include' / 'emacs-module.h', args.module_header)
5659

5760

58-
def _build(*, source: pathlib.Path, install: pathlib.Path, bash: pathlib.Path,
61+
def _build(*, source: pathlib.Path, install: pathlib.Path,
62+
lines: Iterable[str], bash: pathlib.Path,
5963
cc: pathlib.Path, cflags: str, ldflags: str) -> None:
6064
windows = platform.system() == 'Windows'
6165
temp = pathlib.Path(tempfile.mkdtemp(prefix='emacs-build-'))
6266
build = temp / 'build'
63-
shutil.copytree(source, build)
67+
_unpack(source=source, install=build, lines=lines)
6468

6569
def run(*command: str) -> None:
6670
env = None
@@ -121,15 +125,14 @@ def run(*command: str) -> None:
121125
_rename(shared / 'lisp', install / 'lisp')
122126

123127

124-
def _unpack(*, source: pathlib.Path, install: pathlib.Path) -> None:
125-
try:
126-
# Bazel sometimes creates the output directory; remove it so that
127-
# _unpack_archive works correctly.
128-
install.rmdir()
129-
except FileNotFoundError:
130-
pass
131-
132-
shutil.copytree(source, install)
128+
def _unpack(*, source: pathlib.Path, install: pathlib.Path,
129+
lines: Iterable[str]) -> None:
130+
for line in lines:
131+
src = pathlib.Path(line.rstrip('\n'))
132+
rel = src.relative_to(source)
133+
dest = install / rel
134+
dest.parent.mkdir(parents=True, exist_ok=True)
135+
shutil.copy2(src, dest)
133136

134137

135138
def _glob_unique(pattern: pathlib.PurePath) -> pathlib.Path:

elisp/toolchains/elisp_emacs_binary.bzl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ def _install(ctx, shell_toolchain, cc_toolchain, readme):
221221
omit_if_empty = False,
222222
expand_directories = False,
223223
)
224+
srcs = ctx.actions.args()
225+
srcs.use_param_file("--srcs=%s", use_always = True)
226+
srcs.set_param_file_format("multiline")
227+
srcs.add_all(ctx.files.srcs, uniquify = True)
224228
secondary_outs = []
225229
if ctx.outputs.module_header:
226230
args.add(ctx.outputs.module_header, format = "--module-header=%s")
@@ -232,7 +236,7 @@ def _install(ctx, shell_toolchain, cc_toolchain, readme):
232236
transitive = [cc_toolchain.all_files],
233237
),
234238
executable = ctx.executable._build,
235-
arguments = [args],
239+
arguments = [args, srcs],
236240
mnemonic = "EmacsInstall",
237241
progress_message = "Installing Emacs into %{output}",
238242
env = env,
@@ -255,11 +259,15 @@ def _unpack(ctx, readme):
255259
args.add("--release")
256260
args.add(readme, format = "--readme=%s")
257261
args.add(install.path, format = "--install=%s")
262+
srcs = ctx.actions.args()
263+
srcs.use_param_file("--srcs=%s", use_always = True)
264+
srcs.set_param_file_format("multiline")
265+
srcs.add_all(ctx.files.srcs, uniquify = True)
258266
ctx.actions.run(
259267
outputs = [install],
260268
inputs = ctx.files.srcs,
261269
executable = ctx.executable._build,
262-
arguments = [args],
270+
arguments = [args, srcs],
263271
mnemonic = "EmacsInstall",
264272
progress_message = "Unpacking Emacs into %{output}",
265273
toolchain = None,

0 commit comments

Comments
 (0)