1+ load ("//misc/bazel:os.bzl" , "os_select" )
2+
3+ # TODO: make a shared library with the internal repos for transitions
4+ # unfortunately github.com/fmeum/rules_meta doesn't work any more with latest bazel
5+
6+ def _transition_impl (settings , attr ):
7+ return {
8+ "macos" : {
9+ "//command_line_option:copt" : [
10+ "-fno-rtti" ,
11+ # we currently cannot built the swift extractor for ARM
12+ "-arch" ,
13+ "x86_64" ,
14+ ],
15+ "//command_line_option:cxxopt" : [
16+ "-std=c++20" ,
17+ # we currently cannot built the swift extractor for ARM
18+ "-arch" ,
19+ "x86_64" ,
20+ ],
21+ "//command_line_option:linkopt" : [
22+ # we currently cannot built the swift extractor for ARM
23+ "-arch" ,
24+ "x86_64" ,
25+ ],
26+ },
27+ "linux" : {
28+ "//command_line_option:copt" : [
29+ "-fno-rtti" ,
30+ ],
31+ "//command_line_option:cxxopt" : [
32+ "-std=c++20" ,
33+ ],
34+ "//command_line_option:linkopt" : [],
35+ },
36+ "windows" : {
37+ "//command_line_option:copt" : [],
38+ "//command_line_option:cxxopt" : [
39+ "/std:c++20" ,
40+ "--cxxopt=/Zc:preprocessor" ,
41+ ],
42+ "//command_line_option:linkopt" : [],
43+ },
44+ }[attr .os ]
45+
46+ _transition = transition (
47+ implementation = _transition_impl ,
48+ inputs = [],
49+ outputs = ["//command_line_option:copt" , "//command_line_option:cxxopt" , "//command_line_option:linkopt" ],
50+ )
51+
52+ def _cc_transition_impl (ctx ):
53+ src = ctx .attr .src [0 ]
54+ default_info = src [DefaultInfo ]
55+ executable = default_info .files_to_run .executable
56+ runfiles = default_info .default_runfiles
57+ direct = []
58+ if executable :
59+ original_executable = executable
60+ executable = ctx .actions .declare_file (original_executable .basename )
61+ command = "cp %s %s" % (original_executable .path , executable .path )
62+
63+ ctx .actions .run_shell (
64+ inputs = [original_executable ],
65+ outputs = [executable ],
66+ command = command ,
67+ )
68+
69+ # costly, but no other way to remove the internal exe from the runfiles
70+ files = runfiles .files .to_list ()
71+ files .remove (original_executable )
72+ files .append (executable )
73+ runfiles = ctx .runfiles (files )
74+
75+ direct = [executable ]
76+
77+ providers = [
78+ DefaultInfo (
79+ files = depset (direct ),
80+ runfiles = runfiles ,
81+ executable = executable ,
82+ ),
83+ ]
84+ for p in (OutputGroupInfo , CcInfo ):
85+ if p in src :
86+ providers .append (src [p ])
87+
88+ return providers
89+
90+ _cc_transition = rule (
91+ implementation = _cc_transition_impl ,
92+ attrs = {
93+ "_allowlist_function_transition" : attr .label (
94+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist" ,
95+ ),
96+ "src" : attr .label (mandatory = True , cfg = _transition ),
97+ "os" : attr .string (),
98+ },
99+ )
100+
1101def _add_args (kwargs , kwarg , value ):
2102 kwargs [kwarg ] = kwargs .get (kwarg , []) + value
3103
4104def _wrap_cc (rule , kwargs ):
5- _add_args (kwargs , "copts" , [
6- # Required by LLVM/Swift
7- "-fno-rtti" ,
8- ])
105+ name = kwargs .pop ("name" )
106+ visibility = kwargs .pop ("visibility" , None )
9107 _add_args (kwargs , "features" , [
10108 # temporary, before we do universal merging
11109 "-universal_binaries" ,
@@ -17,7 +115,13 @@ def _wrap_cc(rule, kwargs):
17115 "@platforms//os:macos" : [],
18116 "//conditions:default" : ["@platforms//:incompatible" ],
19117 }))
20- rule (** kwargs )
118+ rule (name = "internal/" + name , visibility = ["//visibility:private" ], ** kwargs )
119+ _cc_transition (
120+ name = name ,
121+ visibility = visibility ,
122+ src = ":internal/" + name ,
123+ os = os_select (linux = "linux" , macos = "macos" , windows = "windows" ),
124+ )
21125
22126def swift_cc_binary (** kwargs ):
23127 _wrap_cc (native .cc_binary , kwargs )
0 commit comments