Skip to content

Commit

Permalink
Switch from macro to rule
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 1c4f115c185d0b7d68130366f7aa8080875aba59
  • Loading branch information
Vertexwahn authored and Julian Amann committed Mar 28, 2023
1 parent fa6d950 commit 0f95eac
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 163 deletions.
15 changes: 0 additions & 15 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
config_setting(
name = "osx_arm64",
constraint_values = [
"@platforms//os:osx",
"@platforms//cpu:aarch64",
],
)

config_setting(
name = "osx_x86_64",
constraint_values = [
"@platforms//os:osx",
"@platforms//cpu:x86_64",
],
)
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ All the magic to set up ISPC should be done by Bazel with as little effort as po
## Quick start

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

**Prerequisites:**

The following tools should be installed:

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

**Checkout, build, and run:**

Expand Down
11 changes: 0 additions & 11 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
workspace(name = "rules_ispc")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "platforms",
sha256 = "5308fc1d8865406a49427ba24a9ab53087f17f5266a7aabbfc28823f3916e1ca",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.6/platforms-0.0.6.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.6/platforms-0.0.6.tar.gz",
],
)
75 changes: 0 additions & 75 deletions bp.patch

This file was deleted.

69 changes: 14 additions & 55 deletions ispc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,35 @@

def _ispc_cc_library_impl(ctx):
info = ctx.toolchains["@rules_ispc//tools:toolchain_type"].ispc_info
default_target = info.default_target
default_target_os = info.default_target_os
default_arch = info.default_arch
ispc_path = info.ispc_path

generated_header_filename = ctx.attr.generated_header_filename

ispc_defines_list = ""
if len(ctx.attr.defines) > 0:
ispc_defines_list = "-D" + " -D".join(ctx.attr.defines)

srcs = ctx.files.srcs
inputs = depset(srcs) # see https://bazel.build/extending/rules
inputs = depset(srcs)

object = ctx.actions.declare_file(ctx.attr.name + ".o")

o2 = ctx.actions.declare_file("square.h")


args = ctx.actions.args()

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

args.add("--target=%s" % default_target)
args.add("--target-os=%s" % default_target_os)

if default_target_os == "windows":
args.add("--arch=x86-64")
args.add("--target=avx2")
else:
args.add("--arch=aarch64")
args.add("--target=neon")

args.add("--arch=%s" % default_arch)
args.add("--addressing=64")

if default_target_os != "windows":
args.add("--pic")

args.add("--pic")
args.add(ctx.file.ispc_main_source_file.short_path)
#args.add(ctx.attr.ispc_main_source_file.package + ctx.attr.ispc_main_source_file)

args.add("--header-outfile=%s" % "defines/square.h") #generated_header_filename.short_path)
args.add("--header-outfile=%s" % ctx.outputs.out.path)
args.add("-o", object)

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

ctx.actions.run(
inputs = inputs,
outputs = [object, ctx.outputs.generated_header_filename],
outputs = [object, ctx.outputs.out],
arguments = [args],
executable = ispc_path,
execution_requirements = exec_requirements,
Expand All @@ -66,7 +56,7 @@ ispc_library2 = rule(
This rule uses a precompiled version of ISPC v1.19.0 for compilation.""",
attrs = {
"generated_header_filename": attr.output(
"out": attr.output(
doc = """
Name of the generated header file.
""",
Expand All @@ -93,13 +83,14 @@ This rule uses a precompiled version of ISPC v1.19.0 for compilation.""",
toolchains = ["@rules_ispc//tools:toolchain_type"],
)

def ispc_cc_library2(name, generated_header_filename, ispc_main_source_file, srcs, defines = [], **kwargs):
def ispc_cc_library(name, out, ispc_main_source_file, srcs, defines = [], **kwargs):
ispc_library2(
name = "%s_ispc_gen" % name,
generated_header_filename = generated_header_filename,
out = out,
ispc_main_source_file = ispc_main_source_file,
srcs = srcs,
defines = defines,
tags = ["local"],
**kwargs
)
native.cc_library(
Expand All @@ -109,35 +100,3 @@ def ispc_cc_library2(name, generated_header_filename, ispc_main_source_file, src
defines = defines,
**kwargs
)

def ispc_cc_library(name, out, ispc_main_source_file, srcs, defines = [], **kwargs):
generated_header_filename = out

ispc_defines_list = ""
if len(defines) > 0:
ispc_defines_list = "-D" + " -D".join(defines)

native.genrule(
name = "%s_ispc_gen" % name,
srcs = srcs,
outs = [name + ".o", generated_header_filename],
cmd = select({
"@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),
"@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),
"@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),
"@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),
}),
tools = select({
"@platforms//os:linux": ["@ispc_linux_x86_64//:ispc"],
"@rules_ispc//:osx_arm64": ["@ispc_osx_arm64//:ispc"],
"@rules_ispc//:osx_x86_64": ["@ispc_osx_x86_64//:ispc"],
"@platforms//os:windows": ["@ispc_windows_x86_64//:ispc"],
}),
)
native.cc_library(
name = name,
srcs = [name + ".o"],
hdrs = [name + ".h"],
defines = defines,
**kwargs
)
3 changes: 1 addition & 2 deletions tests/defines/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ COMMON_DEFINES = select({
ispc_cc_library(
name = "square",
srcs = ["square.ispc"],
#generated_header_filename = "square.h",
out = "square.h",
#defines = COMMON_DEFINES, # this is currenlty not working
defines = COMMON_DEFINES,
ispc_main_source_file = "square.ispc",
)

Expand Down
12 changes: 10 additions & 2 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ ispc_toolchain(
"@ispc_linux_x86_64//:ispc",
],
ispc_cmd = "$(location @ispc_linux_x86_64//:ispc)",
default_target = "avx2",
default_target_os = "linux",
default_arch = "x86-64",
)

ispc_toolchain(
Expand All @@ -19,16 +21,20 @@ ispc_toolchain(
"@ispc_windows_x86_64//:ispc",
],
ispc_cmd = "$(location @ispc_windows_x86_64//:ispc)",
default_target = "avx2",
default_target_os = "windows",
default_arch = "x86-64",
)

ispc_toolchain(
name = "ispc_osx_M1",
name = "ispc_osx_arm64",
data = [
"@ispc_osx_arm64//:ispc",
],
ispc_cmd = "$(location @ispc_osx_arm64//:ispc)",#"/opt/homebrew/bin/ispc",#
default_target = "neon",
default_target_os = "macos",
default_arch = "aarch64",
)

ispc_toolchain(
Expand All @@ -37,7 +43,9 @@ ispc_toolchain(
"@ispc_osx_x86_64//:ispc",
],
ispc_cmd = "$(location @ispc_osx_x86_64//:ispc)",
default_target = "sse2",
default_target_os = "macos",
default_arch = "x86-64",
)

toolchain(
Expand Down Expand Up @@ -75,7 +83,7 @@ toolchain(
"@platforms//os:osx",
"@platforms//cpu:arm64",
],
toolchain = ":ispc_osx_M1",
toolchain = ":ispc_osx_arm64",
toolchain_type = "@rules_ispc//tools:toolchain_type",
)

Expand Down
8 changes: 7 additions & 1 deletion tools/ispc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"""

IspcToolchainInfo = provider(
doc = "Information about how to invoke ISPC compiler.",
doc = "Information about how to invoke ISPC.",
fields = [
"ispc_path",
"default_target",
"default_target_os",
"default_arch",
],
)

Expand All @@ -14,7 +16,9 @@ def _ispc_toolchain_impl(ctx):
toolchain_info = platform_common.ToolchainInfo(
ispc_info = IspcToolchainInfo(
ispc_path = expand_ispc_path,
default_target = ctx.attr.default_target,
default_target_os = ctx.attr.default_target_os,
default_arch = ctx.attr.default_arch
),
)
return [toolchain_info]
Expand All @@ -23,7 +27,9 @@ ispc_toolchain = rule(
implementation = _ispc_toolchain_impl,
attrs = {
"ispc_cmd": attr.string(),
"default_target": attr.string(),
"default_target_os": attr.string(),
"default_arch": attr.string(),
"data": attr.label_list(allow_files = True),
},
)
Expand Down

0 comments on commit 0f95eac

Please sign in to comment.