@@ -27,13 +27,28 @@ def _expand_path(path, platform):
2727 return ("arch" , path )
2828 return ("generic" , path )
2929
30- def _detect_platform (ctx ):
31- if ctx .target_platform_has_constraint (ctx .attr ._windows [platform_common .ConstraintValueInfo ]):
32- return "win64"
33- elif ctx .target_platform_has_constraint (ctx .attr ._macos [platform_common .ConstraintValueInfo ]):
34- return "osx64"
30+ def _platform_select (
31+ ctx = None ,
32+ * ,
33+ linux ,
34+ windows ,
35+ macos ):
36+ if ctx :
37+ if ctx .target_platform_has_constraint (ctx .attr ._windows [platform_common .ConstraintValueInfo ]):
38+ return windows
39+ elif ctx .target_platform_has_constraint (ctx .attr ._macos [platform_common .ConstraintValueInfo ]):
40+ return macos
41+ else :
42+ return linux
3543 else :
36- return "linux64"
44+ return select ({
45+ "@platforms//os:linux" : linux ,
46+ "@platforms//os:macos" : macos ,
47+ "@platforms//os:windows" : windows ,
48+ })
49+
50+ def _detect_platform (ctx = None ):
51+ return _platform_select (ctx , linux = "linux64" , macos = "osx64" , windows = "win64" )
3752
3853def codeql_pkg_files (
3954 * ,
@@ -160,11 +175,11 @@ def _zipmerge_impl(ctx):
160175 platform = _detect_platform (ctx )
161176 filename = "%s-%s.zip" % (ctx .attr .zip_name , platform if ctx .attr .kind == "arch" else "generic" )
162177 output = ctx .actions .declare_file (filename )
163- args = [output .path , "--prefix=%s" % ctx .attr .zip_prefix , ctx .file .base .path ]
178+ args = [output .path , "--prefix=%s" % ctx .attr .prefix , ctx .file .base .path ]
164179 for zip , prefix in ctx .attr .zips .items ():
165180 zip_kind , expanded_prefix = _expand_path (prefix , platform )
166181 if zip_kind == ctx .attr .kind :
167- args .append ("--prefix=%s/%s" % (ctx .attr .zip_prefix , expanded_prefix .rstrip ("/" )))
182+ args .append ("--prefix=%s/%s" % (ctx .attr .prefix , expanded_prefix .rstrip ("/" )))
168183 args += [f .path for f in zip .files .to_list ()]
169184 zips .append (zip .files )
170185 ctx .actions .run (
@@ -200,7 +215,7 @@ _zipmerge = rule(
200215 ),
201216 "zip_name" : attr .string (doc = "Prefix to use for the output file name" ),
202217 "kind" : attr .string (doc = "Which zip kind to consider" , values = ["generic" , "arch" ]),
203- "zip_prefix " : attr .string (doc = "Prefix posix path to add to the zip contents in the archive" ),
218+ "prefix " : attr .string (doc = "Prefix posix path to add to the zip contents in the archive" ),
204219 "_zipmerge" : attr .label (default = "//misc/bazel/internal/zipmerge" , executable = True , cfg = "exec" ),
205220 } | _PLAT_DETECTION_ATTRS ,
206221)
@@ -245,25 +260,35 @@ def codeql_pack(
245260 kind = kind ,
246261 visibility = ["//visibility:private" ],
247262 )
248- pkg_zip (
249- name = internal (kind + "-zip-base" ),
250- srcs = [internal (kind )],
251- visibility = ["//visibility:private" ],
252- )
253- _zipmerge (
254- name = internal (kind + "-zip" ),
255- base = internal (kind + "-zip-base" ),
263+ if zips :
264+ pkg_zip (
265+ name = internal (kind + "-zip-base" ),
266+ srcs = [internal (kind )],
267+ visibility = ["//visibility:private" ],
268+ )
269+ _zipmerge (
270+ name = internal (kind + "-zip" ),
271+ base = internal (kind + "-zip-base" ),
272+ zips = zips ,
273+ zip_name = zip_filename ,
274+ kind = kind ,
275+ prefix = name ,
276+ visibility = visibility ,
277+ )
278+ else :
279+ pkg_zip (
280+ name = internal (kind + "-zip" ),
281+ srcs = [internal (kind )],
282+ visibility = ["//visibility:private" ],
283+ package_dir = name ,
284+ package_file_name = name + "-" + (_detect_platform () if kind == "arch" else "generic" ) + ".zip" ,
285+ )
286+ if zips :
287+ _imported_zips_manifest (
288+ name = internal ("zip-manifest" ),
256289 zips = zips ,
257- zip_name = zip_filename ,
258- zip_prefix = name , # this is prefixing the zip contents with the pack name
259- kind = kind ,
260- visibility = visibility ,
290+ visibility = ["//visibility:private" ],
261291 )
262- _imported_zips_manifest (
263- name = internal ("zip-manifest" ),
264- zips = zips ,
265- visibility = ["//visibility:private" ],
266- )
267292
268293 pkg_install (
269294 name = internal ("script" ),
@@ -283,18 +308,20 @@ def codeql_pack(
283308 data = [
284309 internal ("build-file" ),
285310 internal ("script" ),
311+ ] + ([
286312 internal ("zip-manifest" ),
287313 "//misc/bazel/internal/ripunzip" ,
288- ],
314+ ] if zips else []) ,
289315 deps = ["@rules_python//python/runfiles" ],
290316 args = [
291317 "--build-file=$(rlocationpath %s)" % internal ("build-file" ),
292318 "--pkg-install-script=$(rlocationpath %s)" % internal ("script" ),
293319 "--destdir" ,
294320 install_dest ,
321+ ] + ([
295322 "--ripunzip=$(rlocationpath //misc/bazel/internal/ripunzip)" ,
296323 "--zip-manifest=$(rlocationpath %s)" % internal ("zip-manifest" ),
297- ],
324+ ] if zips else []) ,
298325 visibility = visibility ,
299326 )
300327 native .filegroup (
0 commit comments