From dec4fed5117390669399785bb6da02957fbf143e Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Fri, 27 Jan 2023 00:43:46 +0100 Subject: [PATCH] build: Consolidate the binary used to generate the shell completions 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 bafbbe81c9220cb3749a19a244e45a61477553a6 [1] Commit 6063eb27b9893994 https://github.com/containers/toolbox/issues/821 https://github.com/containers/toolbox/issues/1074 --- src/go-build-wrapper | 7 +++++++ src/meson.build | 8 +++++--- src/meson_generate_completions.py | 10 ++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/go-build-wrapper b/src/go-build-wrapper index c572d6dfb..722142f99 100755 --- a/src/go-build-wrapper +++ b/src/go-build-wrapper @@ -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 "$?" diff --git a/src/meson.build b/src/meson.build index 1d2b868b6..b8ca8f8be 100644 --- a/src/meson.build +++ b/src/meson.build @@ -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], @@ -89,7 +91,7 @@ if fishcompletionsdir != '' capture: true, command: [ meson_generate_completions_program, - meson.current_source_dir(), + toolbox_go_throwaway, 'fish', ], depends: [toolbox_go], @@ -104,7 +106,7 @@ custom_target( capture: true, command: [ meson_generate_completions_program, - meson.current_source_dir(), + toolbox_go_throwaway, 'zsh', ], depends: [toolbox_go], diff --git a/src/meson_generate_completions.py b/src/meson_generate_completions.py index 07e3b8024..9bc7b3095 100644 --- a/src/meson_generate_completions.py +++ b/src/meson_generate_completions.py @@ -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. @@ -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)