2022-11-03 21:24:31 +00:00
""" This file contains useful utilities """
2023-06-09 10:29:12 +00:00
def full_label ( label ) :
2022-11-03 21:24:31 +00:00
return native . repository_name ( ) + " // " + native . package_name ( ) + " : " + label
2022-11-09 11:54:39 +00:00
def runnable_binary ( name , binary , foreign_cc_target , match_binary_name = False , * * kwargs ) :
2022-11-03 21:24:31 +00:00
"""
Macro that provides a wrapper script around a binary generated by a rules_foreign_cc rule that can be run using " bazel run " .
The wrapper script also facilitates the running of binaries that are dynamically linked to shared libraries also built by rules_foreign_cc . The runnable bin could be used as a tool in a dependent bazel target
2023-10-14 13:24:09 +00:00
Note that this macro requires bazel 5.4 .0 due to the use of the rlocationpath variable ( see https : / / github . com / bazelbuild / bazel / issues / 10923 for context )
2022-11-03 21:24:31 +00:00
Also note that the macro requires the ` - - enable_runfiles ` option to be set on Windows .
Args :
name : The target name
2022-11-09 11:54:39 +00:00
binary : The name of the binary generated by rules_foreign_cc , should not include . exe extension
2022-11-03 21:24:31 +00:00
foreign_cc_target : The target that generates the binary
2022-11-09 11:54:39 +00:00
match_binary_name : True if the generated runnable file should have the same name as the provided " binary " argument . This is useful when the runnable_binary is used with tools that expect a certain filename , e . g tools like CMake and Meson expect " pkg-config " to be on the PATH
2022-11-03 21:24:31 +00:00
* * kwargs : Remaining keyword arguments
"""
tags = kwargs . pop ( " tags " , [ ] )
2023-08-25 22:23:56 +00:00
config_setting_name = name + " _windows_config_setting "
2022-11-09 11:54:39 +00:00
# filegroups cannot select on constraint_values in before Bazel 5.1. Add this config_setting as a workaround. See https://github.com/bazelbuild/bazel/issues/13047
native . config_setting (
2023-08-25 22:23:56 +00:00
name = config_setting_name ,
2022-11-09 11:54:39 +00:00
constraint_values = [
" @platforms//os:windows " ,
] ,
)
2022-11-03 21:24:31 +00:00
native . filegroup (
name = name + " _fg " ,
srcs = [ foreign_cc_target ] ,
tags = tags + [ " manual " ] ,
2022-11-09 11:54:39 +00:00
output_group = select ( {
2023-08-25 22:23:56 +00:00
" : " + config_setting_name : binary + " .exe " ,
2022-11-09 11:54:39 +00:00
" //conditions:default " : binary ,
} ) ,
2022-11-03 21:24:31 +00:00
)
2023-01-03 11:26:05 +00:00
wrapper_cmd = """
2023-10-14 13:24:09 +00:00
sed s @EXECUTABLE @ $ ( rlocationpath { name } ) @g $ ( location @rules_foreign_cc / / foreign_cc / private : runnable_binary_wrapper . sh ) > tmp
2024-09-04 18:31:03 +00:00
cp tmp $ @
2023-01-03 11:26:05 +00:00
"""
2024-08-12 15:41:56 +00:00
if hasattr ( native , " package_relative_label " ) :
fg_label = native . package_relative_label ( name + " _fg " )
else :
# pre Bazel 6.1.0
fg_label = full_label ( name + " _fg " )
2022-11-03 21:24:31 +00:00
native . genrule (
name = name + " _wrapper " ,
srcs = [ " @rules_foreign_cc//foreign_cc/private:runnable_binary_wrapper.sh " , name + " _fg " ] ,
outs = [ name + " _wrapper.sh " ] ,
2024-09-04 18:31:03 +00:00
cmd = wrapper_cmd . format ( name = fg_label ) ,
2022-11-03 21:24:31 +00:00
tags = tags + [ " manual " ] ,
)
native . sh_binary (
2022-11-09 11:54:39 +00:00
name = binary if match_binary_name else name ,
2022-11-03 21:24:31 +00:00
deps = [ " @bazel_tools//tools/bash/runfiles " ] ,
2022-11-09 11:54:39 +00:00
data = [ name + " _fg " , foreign_cc_target ] ,
2022-11-03 21:24:31 +00:00
srcs = [ name + " _wrapper " ] ,
2022-11-09 11:54:39 +00:00
tags = tags + [ " manual " ] ,
2022-11-03 21:24:31 +00:00
* * kwargs
)
2022-11-09 11:54:39 +00:00
if match_binary_name :
native . alias (
name = name ,
actual = binary ,
tags = tags ,
* * kwargs
)