#!/bin/sh
#
# Copyright © 2020 – 2024 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.
# 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.
#


if [ "$#" -ne 7 ]; then
    echo "go-build-wrapper: wrong arguments" >&2
    echo "Usage: go-build-wrapper [SOURCE DIR]" >&2
    echo "                        [OUTPUT ROOT DIR]" >&2
    echo "                        [OUTPUT FILE]" >&2
    echo "                        [VERSION]" >&2
    echo "                        [C COMPILER]" >&2
    echo "                        [DYNAMIC LINKER]" >&2
    echo "                        [MIGRATION PATH FOR COREOS/TOOLBOX]" >&2
    exit 1
fi

if ! cd "$1"; then
    echo "go-build-wrapper: failed to enter source directory $1"
    exit 1
fi

tags=""
if $7; then
    tags="-tags migration_path_for_coreos_toolbox"
fi

if ! libc_dir=$("$5" --print-file-name=libc.so); then
    echo "go-build-wrapper: failed to read the path to libc.so" >&2
    exit 1
fi

if ! libc_dir_canonical=$(readlink --canonicalize "$libc_dir"); then
    echo "go-build-wrapper: failed to canonicalize the path to libc.so" >&2
    exit 1
fi

if ! libc_dir_canonical_dirname=$(dirname "$libc_dir_canonical"); then
    echo "go-build-wrapper: failed to read the dirname of the canonicalized path to libc.so" >&2
    exit 1
fi

if ! dynamic_linker_basename=$(basename "$6"); then
    echo "go-build-wrapper: failed to read the basename of dynamic linker $6" >&2
    exit 1
fi

if ! dynamic_linker_canonical=$(readlink --canonicalize "$6"); then
    echo "go-build-wrapper: failed to canonicalize dynamic linker $6" >&2
    exit 1
fi

if ! dynamic_linker_canonical_dirname=$(dirname "$dynamic_linker_canonical"); then
    echo "go-build-wrapper: failed to read the dirname of the canonicalized dynamic linker $dynamic_linker_canonical" >&2
    exit 1
fi

dynamic_linker="/run/host$dynamic_linker_canonical_dirname/$dynamic_linker_basename"

# Note for distributors:
#
# The '-z now' flag, which is the opposite of '-z lazy', is unsupported as an
# external linker flag [1], because of how the NVIDIA Container Toolkit stack
# uses dlopen(3) to load libcuda.so.1 and libnvidia-ml.so.1 at runtime [2,3].
#
# The NVIDIA Container Toolkit stack doesn't use dlsym(3) to obtain the address
# of a symbol at runtime before using it.  It links against undefined symbols
# at build-time available through a CUDA API definition embedded directly in
# the CGO code or a copy of nvml.h.  It relies upon lazily deferring function
# call resolution to the point when dlopen(3) is able to load the shared
# libraries at runtime, instead of doing it when toolbox(1) is started.
#
# This is unlike how Toolbx itself uses dlopen(3) and dlsym(3) to load
# libsubid.so at runtime.
#
# Compare the output of:
#   $ nm /path/to/toolbox | grep ' subid_init'
#
# ... with those from:
#   $ nm /path/to/toolbox | grep ' nvmlGpuInstanceGetComputeInstanceProfileInfoV'
#           U nvmlGpuInstanceGetComputeInstanceProfileInfoV
#   $ nm /path/to/toolbox | grep ' nvmlDeviceGetAccountingPids'
#           U nvmlDeviceGetAccountingPids
#
# Using '-z now' as an external linker flag forces the dynamic linker to
# resolve all symbols when toolbox(1) is started, and leads to:
#   $ toolbox
#   toolbox: symbol lookup error: toolbox: undefined symbol:
#       nvmlGpuInstanceGetComputeInstanceProfileInfoV
#
# [1] NVIDIA Container Toolkit commit 1407ace94ab7c150
#     https://github.com/NVIDIA/nvidia-container-toolkit/commit/1407ace94ab7c150
#     https://github.com/NVIDIA/go-nvml/issues/18
#     https://github.com/NVIDIA/nvidia-container-toolkit/issues/49
#
# [2] https://github.com/NVIDIA/nvidia-container-toolkit/tree/main/internal/cuda
#
# [3] https://github.com/NVIDIA/go-nvml/blob/main/README.md
#     https://github.com/NVIDIA/go-nvml/tree/main/pkg/dl
#     https://github.com/NVIDIA/go-nvml/tree/main/pkg/nvml

# shellcheck disable=SC2086
go build \
        $tags \
        -trimpath \
        -ldflags "-extldflags '-Wl,-dynamic-linker,$dynamic_linker -Wl,-rpath,/run/host$libc_dir_canonical_dirname -Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-in-object-files' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$4" \
        -o "$2/$3"

exit "$?"
