1*9bb1b549SSpandan Das# Copyright 2021 The Bazel Authors. All rights reserved. 2*9bb1b549SSpandan Das# 3*9bb1b549SSpandan Das# Licensed under the Apache License, Version 2.0 (the "License"); 4*9bb1b549SSpandan Das# you may not use this file except in compliance with the License. 5*9bb1b549SSpandan Das# You may obtain a copy of the License at 6*9bb1b549SSpandan Das# 7*9bb1b549SSpandan Das# http://www.apache.org/licenses/LICENSE-2.0 8*9bb1b549SSpandan Das# 9*9bb1b549SSpandan Das# Unless required by applicable law or agreed to in writing, software 10*9bb1b549SSpandan Das# distributed under the License is distributed on an "AS IS" BASIS, 11*9bb1b549SSpandan Das# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9bb1b549SSpandan Das# See the License for the specific language governing permissions and 13*9bb1b549SSpandan Das# limitations under the License. 14*9bb1b549SSpandan Das 15*9bb1b549SSpandan Dasload( 16*9bb1b549SSpandan Das "@bazel_skylib//lib:paths.bzl", 17*9bb1b549SSpandan Das "paths", 18*9bb1b549SSpandan Das) 19*9bb1b549SSpandan Das 20*9bb1b549SSpandan Dasdef _rpath(go, library, executable = None): 21*9bb1b549SSpandan Das """Returns the potential rpaths of a library, possibly relative to another file.""" 22*9bb1b549SSpandan Das if not executable: 23*9bb1b549SSpandan Das return [paths.dirname(library.short_path)] 24*9bb1b549SSpandan Das 25*9bb1b549SSpandan Das origin = go.mode.goos == "darwin" and "@loader_path" or "$ORIGIN" 26*9bb1b549SSpandan Das 27*9bb1b549SSpandan Das # Accomodate for two kinds of executable paths. 28*9bb1b549SSpandan Das rpaths = [] 29*9bb1b549SSpandan Das 30*9bb1b549SSpandan Das # 1. Where the executable is inside its own .runfiles directory. 31*9bb1b549SSpandan Das # This is the case for generated libraries as well as remote builds. 32*9bb1b549SSpandan Das # a) go back to the workspace root from the executable file in .runfiles 33*9bb1b549SSpandan Das depth = executable.short_path.count("/") 34*9bb1b549SSpandan Das back_to_root = paths.join(*([".."] * depth)) 35*9bb1b549SSpandan Das 36*9bb1b549SSpandan Das # b) then walk back to the library's short path 37*9bb1b549SSpandan Das rpaths.append(paths.join(origin, back_to_root, paths.dirname(library.short_path))) 38*9bb1b549SSpandan Das 39*9bb1b549SSpandan Das # 2. Where the executable is outside the .runfiles directory: 40*9bb1b549SSpandan Das # This is the case for local pre-built libraries, as well as local 41*9bb1b549SSpandan Das # generated libraries. 42*9bb1b549SSpandan Das runfiles_dir = paths.basename(executable.short_path) + ".runfiles" 43*9bb1b549SSpandan Das rpaths.append(paths.join(origin, runfiles_dir, go._ctx.workspace_name, paths.dirname(library.short_path))) 44*9bb1b549SSpandan Das 45*9bb1b549SSpandan Das return rpaths 46*9bb1b549SSpandan Das 47*9bb1b549SSpandan Dasdef _flags(go, *args, **kwargs): 48*9bb1b549SSpandan Das """Returns the rpath linker flags for a library.""" 49*9bb1b549SSpandan Das return ["-Wl,-rpath," + p for p in _rpath(go, *args, **kwargs)] 50*9bb1b549SSpandan Das 51*9bb1b549SSpandan Dasdef _install_name(f): 52*9bb1b549SSpandan Das """Returns the install name for a dylib on macOS.""" 53*9bb1b549SSpandan Das return f.short_path 54*9bb1b549SSpandan Das 55*9bb1b549SSpandan Dasrpath = struct( 56*9bb1b549SSpandan Das flags = _flags, 57*9bb1b549SSpandan Das install_name = _install_name, 58*9bb1b549SSpandan Das rpath = _rpath, 59*9bb1b549SSpandan Das) 60