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
Note that this macro only works on foreign_cc_targets in external repositories , not in the main repository . This is due to the issue described here : https : / / github . com / bazelbuild / bazel / issues / 10923
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 " , [ ] )
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 (
name = " windows_config_setting " ,
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 ( {
" :windows_config_setting " : binary + " .exe " ,
" //conditions:default " : binary ,
} ) ,
2022-11-03 21:24:31 +00:00
)
2023-01-03 11:26:05 +00:00
wrapper_cmd = """
sed s @EXECUTABLE @ $ ( rootpath { name } ) @g $ ( location @rules_foreign_cc / / foreign_cc / private : runnable_binary_wrapper . sh ) > tmp
sed s @SH_BINARY_FILENAME @ { sh_binary_filename } @g tmp > $ @
"""
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 " ] ,
2023-01-03 11:26:05 +00:00
cmd = select ( {
2023-06-09 10:29:12 +00:00
" @platforms//os:windows " : wrapper_cmd . format ( name = full_label ( name + " _fg " ) , sh_binary_filename = binary + " .exe " if match_binary_name else name ) ,
" //conditions:default " : wrapper_cmd . format ( name = full_label ( name + " _fg " ) , sh_binary_filename = binary if match_binary_name else name ) ,
2023-01-03 11:26:05 +00:00
} ) ,
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
)