Skip to content

Commit 0f95eac

Browse files
VertexwahnJulian Amann
authored and
Julian Amann
committed
Switch from macro to rule
GitOrigin-RevId: 1c4f115c185d0b7d68130366f7aa8080875aba59
1 parent fa6d950 commit 0f95eac

File tree

8 files changed

+36
-163
lines changed

8 files changed

+36
-163
lines changed

BUILD.bazel

-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
config_setting(
2-
name = "osx_arm64",
3-
constraint_values = [
4-
"@platforms//os:osx",
5-
"@platforms//cpu:aarch64",
6-
],
7-
)
8-
9-
config_setting(
10-
name = "osx_x86_64",
11-
constraint_values = [
12-
"@platforms//os:osx",
13-
"@platforms//cpu:x86_64",
14-
],
15-
)

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ All the magic to set up ISPC should be done by Bazel with as little effort as po
1414
## Quick start
1515

1616
This project uses [Bazel](https://bazel.build/) as a build system.
17-
The current used version of Bazel is defined in [.bazelversion](tests/.bazelversion).
17+
The current used version of Bazel to test these rules is defined in [.bazelversion](tests/.bazelversion).
18+
It is very likely that these rules work also with other Bazel versions,
19+
since only very basic features are used.
1820

1921
**Prerequisites:**
2022

2123
The following tools should be installed:
2224

2325
- [Git](https://git-scm.com/)
2426
- [Bazel](https://bazel.build/install)
25-
- A C++ compiler (GCC, Visual Studio, Clang, etc.)
27+
- A C++ compiler (GCC, Visual Studio, Clang, Apple Clang, etc.)
2628

2729
**Checkout, build, and run:**
2830

WORKSPACE.bazel

-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
11
workspace(name = "rules_ispc")
2-
3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4-
5-
http_archive(
6-
name = "platforms",
7-
sha256 = "5308fc1d8865406a49427ba24a9ab53087f17f5266a7aabbfc28823f3916e1ca",
8-
urls = [
9-
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.6/platforms-0.0.6.tar.gz",
10-
"https://github.com/bazelbuild/platforms/releases/download/0.0.6/platforms-0.0.6.tar.gz",
11-
],
12-
)

bp.patch

-75
This file was deleted.

ispc.bzl

+14-55
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,35 @@
33

44
def _ispc_cc_library_impl(ctx):
55
info = ctx.toolchains["@rules_ispc//tools:toolchain_type"].ispc_info
6+
default_target = info.default_target
67
default_target_os = info.default_target_os
8+
default_arch = info.default_arch
79
ispc_path = info.ispc_path
810

9-
generated_header_filename = ctx.attr.generated_header_filename
10-
1111
ispc_defines_list = ""
1212
if len(ctx.attr.defines) > 0:
1313
ispc_defines_list = "-D" + " -D".join(ctx.attr.defines)
1414

1515
srcs = ctx.files.srcs
16-
inputs = depset(srcs) # see https://bazel.build/extending/rules
16+
inputs = depset(srcs)
1717

1818
object = ctx.actions.declare_file(ctx.attr.name + ".o")
19-
20-
o2 = ctx.actions.declare_file("square.h")
21-
19+
2220
args = ctx.actions.args()
2321

2422
if len(ctx.attr.defines) > 0:
2523
args.add(ispc_defines_list)
2624

25+
args.add("--target=%s" % default_target)
2726
args.add("--target-os=%s" % default_target_os)
28-
29-
if default_target_os == "windows":
30-
args.add("--arch=x86-64")
31-
args.add("--target=avx2")
32-
else:
33-
args.add("--arch=aarch64")
34-
args.add("--target=neon")
35-
27+
args.add("--arch=%s" % default_arch)
3628
args.add("--addressing=64")
3729

3830
if default_target_os != "windows":
39-
args.add("--pic")
40-
31+
args.add("--pic")
32+
4133
args.add(ctx.file.ispc_main_source_file.short_path)
42-
#args.add(ctx.attr.ispc_main_source_file.package + ctx.attr.ispc_main_source_file)
43-
44-
args.add("--header-outfile=%s" % "defines/square.h") #generated_header_filename.short_path)
34+
args.add("--header-outfile=%s" % ctx.outputs.out.path)
4535
args.add("-o", object)
4636

4737
exec_requirements = {}
@@ -50,7 +40,7 @@ def _ispc_cc_library_impl(ctx):
5040

5141
ctx.actions.run(
5242
inputs = inputs,
53-
outputs = [object, ctx.outputs.generated_header_filename],
43+
outputs = [object, ctx.outputs.out],
5444
arguments = [args],
5545
executable = ispc_path,
5646
execution_requirements = exec_requirements,
@@ -66,7 +56,7 @@ ispc_library2 = rule(
6656
6757
This rule uses a precompiled version of ISPC v1.19.0 for compilation.""",
6858
attrs = {
69-
"generated_header_filename": attr.output(
59+
"out": attr.output(
7060
doc = """
7161
Name of the generated header file.
7262
""",
@@ -93,13 +83,14 @@ This rule uses a precompiled version of ISPC v1.19.0 for compilation.""",
9383
toolchains = ["@rules_ispc//tools:toolchain_type"],
9484
)
9585

96-
def ispc_cc_library2(name, generated_header_filename, ispc_main_source_file, srcs, defines = [], **kwargs):
86+
def ispc_cc_library(name, out, ispc_main_source_file, srcs, defines = [], **kwargs):
9787
ispc_library2(
9888
name = "%s_ispc_gen" % name,
99-
generated_header_filename = generated_header_filename,
89+
out = out,
10090
ispc_main_source_file = ispc_main_source_file,
10191
srcs = srcs,
10292
defines = defines,
93+
tags = ["local"],
10394
**kwargs
10495
)
10596
native.cc_library(
@@ -109,35 +100,3 @@ def ispc_cc_library2(name, generated_header_filename, ispc_main_source_file, src
109100
defines = defines,
110101
**kwargs
111102
)
112-
113-
def ispc_cc_library(name, out, ispc_main_source_file, srcs, defines = [], **kwargs):
114-
generated_header_filename = out
115-
116-
ispc_defines_list = ""
117-
if len(defines) > 0:
118-
ispc_defines_list = "-D" + " -D".join(defines)
119-
120-
native.genrule(
121-
name = "%s_ispc_gen" % name,
122-
srcs = srcs,
123-
outs = [name + ".o", generated_header_filename],
124-
cmd = select({
125-
"@platforms//os:linux": "$(location @ispc_linux_x86_64//:ispc) %s --target=avx2 --target-os=linux --arch=x86-64 --addressing=64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_defines_list, ispc_main_source_file, generated_header_filename, name),
126-
"@rules_ispc//:osx_arm64": "$(location @ispc_osx_arm64//:ispc) %s --target=neon --target-os=macos --arch=aarch64 --addressing=64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_defines_list, ispc_main_source_file, generated_header_filename, name),
127-
"@rules_ispc//:osx_x86_64": "$(location @ispc_osx_x86_64//:ispc) %s --target=sse2 --target-os=macos --arch=x86-64 --addressing=64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_defines_list, ispc_main_source_file, generated_header_filename, name),
128-
"@platforms//os:windows": "$(location @ispc_windows_x86_64//:ispc) %s --target=avx2 --target-os=windows --arch=x86-64 --addressing=64 $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_defines_list, ispc_main_source_file, generated_header_filename, name),
129-
}),
130-
tools = select({
131-
"@platforms//os:linux": ["@ispc_linux_x86_64//:ispc"],
132-
"@rules_ispc//:osx_arm64": ["@ispc_osx_arm64//:ispc"],
133-
"@rules_ispc//:osx_x86_64": ["@ispc_osx_x86_64//:ispc"],
134-
"@platforms//os:windows": ["@ispc_windows_x86_64//:ispc"],
135-
}),
136-
)
137-
native.cc_library(
138-
name = name,
139-
srcs = [name + ".o"],
140-
hdrs = [name + ".h"],
141-
defines = defines,
142-
**kwargs
143-
)

tests/defines/BUILD.bazel

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ COMMON_DEFINES = select({
1212
ispc_cc_library(
1313
name = "square",
1414
srcs = ["square.ispc"],
15-
#generated_header_filename = "square.h",
1615
out = "square.h",
17-
#defines = COMMON_DEFINES, # this is currenlty not working
16+
defines = COMMON_DEFINES,
1817
ispc_main_source_file = "square.ispc",
1918
)
2019

tools/BUILD.bazel

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ ispc_toolchain(
1010
"@ispc_linux_x86_64//:ispc",
1111
],
1212
ispc_cmd = "$(location @ispc_linux_x86_64//:ispc)",
13+
default_target = "avx2",
1314
default_target_os = "linux",
15+
default_arch = "x86-64",
1416
)
1517

1618
ispc_toolchain(
@@ -19,16 +21,20 @@ ispc_toolchain(
1921
"@ispc_windows_x86_64//:ispc",
2022
],
2123
ispc_cmd = "$(location @ispc_windows_x86_64//:ispc)",
24+
default_target = "avx2",
2225
default_target_os = "windows",
26+
default_arch = "x86-64",
2327
)
2428

2529
ispc_toolchain(
26-
name = "ispc_osx_M1",
30+
name = "ispc_osx_arm64",
2731
data = [
2832
"@ispc_osx_arm64//:ispc",
2933
],
3034
ispc_cmd = "$(location @ispc_osx_arm64//:ispc)",#"/opt/homebrew/bin/ispc",#
35+
default_target = "neon",
3136
default_target_os = "macos",
37+
default_arch = "aarch64",
3238
)
3339

3440
ispc_toolchain(
@@ -37,7 +43,9 @@ ispc_toolchain(
3743
"@ispc_osx_x86_64//:ispc",
3844
],
3945
ispc_cmd = "$(location @ispc_osx_x86_64//:ispc)",
46+
default_target = "sse2",
4047
default_target_os = "macos",
48+
default_arch = "x86-64",
4149
)
4250

4351
toolchain(
@@ -75,7 +83,7 @@ toolchain(
7583
"@platforms//os:osx",
7684
"@platforms//cpu:arm64",
7785
],
78-
toolchain = ":ispc_osx_M1",
86+
toolchain = ":ispc_osx_arm64",
7987
toolchain_type = "@rules_ispc//tools:toolchain_type",
8088
)
8189

tools/ispc_toolchain.bzl

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"""
33

44
IspcToolchainInfo = provider(
5-
doc = "Information about how to invoke ISPC compiler.",
5+
doc = "Information about how to invoke ISPC.",
66
fields = [
77
"ispc_path",
8+
"default_target",
89
"default_target_os",
10+
"default_arch",
911
],
1012
)
1113

@@ -14,7 +16,9 @@ def _ispc_toolchain_impl(ctx):
1416
toolchain_info = platform_common.ToolchainInfo(
1517
ispc_info = IspcToolchainInfo(
1618
ispc_path = expand_ispc_path,
19+
default_target = ctx.attr.default_target,
1720
default_target_os = ctx.attr.default_target_os,
21+
default_arch = ctx.attr.default_arch
1822
),
1923
)
2024
return [toolchain_info]
@@ -23,7 +27,9 @@ ispc_toolchain = rule(
2327
implementation = _ispc_toolchain_impl,
2428
attrs = {
2529
"ispc_cmd": attr.string(),
30+
"default_target": attr.string(),
2631
"default_target_os": attr.string(),
32+
"default_arch": attr.string(),
2733
"data": attr.label_list(allow_files = True),
2834
},
2935
)

0 commit comments

Comments
 (0)