diff --git a/c10/BUILD.bazel b/c10/BUILD.bazel index f64bce7b78e6..77f97d82da4f 100644 --- a/c10/BUILD.bazel +++ b/c10/BUILD.bazel @@ -1,6 +1,5 @@ load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") -load("//third_party:substitution.bzl", "header_template_rule") load("//tools/config:defs.bzl", "if_cuda") # The bool_flag targets allow configuring the build from the @@ -27,28 +26,17 @@ config_setting( flag_values = {":use_glog": "true"}, ) -header_template_rule( - name = "cuda_cmake_macros_h", - src = "cuda/impl/cuda_cmake_macros.h.in", - out = "cuda/impl/cuda_cmake_macros.h", - substitutions = { - "cmakedefine": "define", - }, -) - cc_library( name = "headers", hdrs = [ "//c10/core:headers", + "//c10/cuda:headers", "//c10/mobile:headers", "//c10/util:headers", - ] + glob([ - "cuda/*.h", - "cuda/impl/*.h", - ]), + ], deps = [ - ":cuda_cmake_macros_h", "//c10/core:alignment", + "//c10/cuda:Macros", "//c10/macros", ] + select({ ":using_gflags": ["@com_github_gflags_gflags//:gflags"], @@ -62,14 +50,6 @@ cc_library( cc_library( name = "c10", - srcs = if_cuda( - glob([ - "cuda/*.cpp", - "cuda/impl/*.cpp", - ]), - [], - ), - local_defines = ["C10_BUILD_MAIN_LIB"], deps = [ ":headers", "//c10/core:CPUAllocator", @@ -82,7 +62,7 @@ cc_library( "//c10/util:base", "//c10/util:typeid", ] + if_cuda( - ["@cuda"], + ["//c10/cuda"], [], ), alwayslink = True, diff --git a/c10/cuda/BUILD.bazel b/c10/cuda/BUILD.bazel new file mode 100644 index 000000000000..d1a0db360d23 --- /dev/null +++ b/c10/cuda/BUILD.bazel @@ -0,0 +1,4 @@ +load("//:tools/bazel.bzl", "rules") +load(":build.bzl", "define_targets") + +define_targets(rules = rules) diff --git a/c10/cuda/build.bzl b/c10/cuda/build.bzl new file mode 100644 index 000000000000..9ee16a418e30 --- /dev/null +++ b/c10/cuda/build.bzl @@ -0,0 +1,65 @@ +def define_targets(rules): + rules.cc_library( + name = "cuda", + srcs = rules.glob( + [ + "*.cpp", + "impl/*.cpp", + ], + exclude = [ + "test/**/*.cpp", + ], + ), + hdrs = rules.glob( + [ + "*.h", + "impl/*.h", + ], + exclude = [ + "CUDAMacros.h", + ], + ), + # This library uses registration. Don't let registered + # entities be removed. + alwayslink = True, + linkstatic = True, + local_defines = ["C10_BUILD_MAIN_LIB"], + visibility = ["//visibility:public"], + deps = [ + ":Macros", + "@cuda", + "//c10/core:base", + "//c10/macros", + "//c10/util:base", + ], + target_compatible_with = rules.requires_cuda_enabled(), + ) + + rules.cc_library( + name = "Macros", + srcs = [":cuda_cmake_macros"], + hdrs = ["CUDAMacros.h"], + linkstatic = True, + local_defines = ["C10_BUILD_MAIN_LIB"], + visibility = ["//visibility:public"], + ) + + rules.cmake_configure_file( + name = "cuda_cmake_macros", + src = "impl/cuda_cmake_macros.h.in", + out = "impl/cuda_cmake_macros.h", + definitions = [], + ) + + rules.filegroup( + name = "headers", + srcs = rules.glob( + [ + "*.h", + "impl/*.h", + ], + exclude = [ + ], + ), + visibility = ["//c10:__pkg__"], + ) diff --git a/tools/bazel.bzl b/tools/bazel.bzl index ad7b8432ad23..04d690dbdf65 100644 --- a/tools/bazel.bzl +++ b/tools/bazel.bzl @@ -1,4 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_cuda//cuda:defs.bzl", "requires_cuda_enabled") load("//c10/macros:cmake_configure_file.bzl", "cmake_configure_file") # Rules implementation for the Bazel build system. Since the common @@ -9,5 +10,6 @@ rules = struct( cmake_configure_file = cmake_configure_file, filegroup = native.filegroup, glob = native.glob, + requires_cuda_enabled = requires_cuda_enabled, select = select, )