Skip to content

Commit

Permalink
build: Consolidate the binary used to generate the shell completions
Browse files Browse the repository at this point in the history
The Toolbx source code was being repeatedly re-built with 'go run ...'
to generate each of the shell completions.  A following commit will use
CGO to link to libsubid.so, which will sufficiently complicate the build
that a simple 'go run ...' won't be enough.

Hence, it's better to use the same mechanisms in src/go-build-wrapper
that are used to generate the Toolbx binary.

However, the main Toolbx binary only works if /run/host exists [1],
which is only created after 'meson install' and may not exist during
'meson compile'.  This is solved by using a throwaway binary that lacks
the ABI protections necessary to run as a container's entry, but is
enough to generate the shell completions.

Fallout from bafbbe8

[1] Commit 6063eb2
    containers#821

containers#1074
  • Loading branch information
debarshiray committed Jan 27, 2023
1 parent b368ccf commit dec4fed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/go-build-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ go build \
-ldflags "-extldflags '-Wl,-dynamic-linker,$dynamic_linker -Wl,-rpath,/run/host$libc_dir_canonical_dirname' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$4" \
-o "$2/$3"

# shellcheck disable=SC2086
go build \
$tags \
-trimpath \
-ldflags "-X github.com/containers/toolbox/pkg/version.currentVersion=$4" \
-o "$2/$3-throwaway"

exit "$?"
8 changes: 5 additions & 3 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ toolbox_go = custom_target(
output: 'toolbox',
)

toolbox_go_throwaway = toolbox_go.full_path() + '-throwaway'

if bashcompletionsdir != ''
custom_target(
'bash-completion',
capture: true,
command: [
meson_generate_completions_program,
meson.current_source_dir(),
toolbox_go_throwaway,
'bash',
],
depends: [toolbox_go],
Expand All @@ -89,7 +91,7 @@ if fishcompletionsdir != ''
capture: true,
command: [
meson_generate_completions_program,
meson.current_source_dir(),
toolbox_go_throwaway,
'fish',
],
depends: [toolbox_go],
Expand All @@ -104,7 +106,7 @@ custom_target(
capture: true,
command: [
meson_generate_completions_program,
meson.current_source_dir(),
toolbox_go_throwaway,
'zsh',
],
depends: [toolbox_go],
Expand Down
10 changes: 4 additions & 6 deletions src/meson_generate_completions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
#
# Copyright © 2022 Ondřej Míchal
# Copyright © 2022 Red Hat Inc.
# Copyright © 2022 – 2023 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,19 +16,17 @@
# limitations under the License.
#

import os
import subprocess
import sys

if len(sys.argv) != 3:
print('{}: wrong arguments'.format(sys.argv[0]), file=sys.stderr)
print('Usage: {} [SOURCE DIR] [COMPLETION TYPE]'.format(sys.argv[0]), file=sys.stderr)
print('Usage: {} [toolbox] [COMPLETION TYPE]'.format(sys.argv[0]), file=sys.stderr)
sys.exit(1)

source_dir = sys.argv[1]
toolbox = sys.argv[1]
completion_type = sys.argv[2]

os.chdir(source_dir)
output = subprocess.run(['go', 'run', '.', 'completion', completion_type], check=True)
output = subprocess.run([toolbox, 'completion', completion_type], check=True)

sys.exit(0)

0 comments on commit dec4fed

Please sign in to comment.