diff --git a/.bazelrc b/.bazelrc index 67b5bfe76b92..1e847054613e 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,5 +1,9 @@ build --copt=--std=c++14 build --copt=-I. +# Bazel does not support including its cc_library targets as system +# headers. We work around this for generated code +# (e.g. c10/macros/cmake_macros.h) by making the generated directory a +# system include path. build --copt=-isystem --copt bazel-out/k8-fastbuild/bin build --experimental_ui_max_stdouterr_bytes=2048576 @@ -12,6 +16,9 @@ build:no-tty --show_progress_rate_limit 10 build:gpu --define=cuda=true # define a separate build folder for faster switching between configs build:gpu --platform_suffix=-gpu +# See the note on the config-less build for details about why we are +# doing this. We must also do it for the "-gpu" platform suffix. +build --copt=-isystem --copt=bazel-out/k8-fastbuild-gpu/bin # rules_cuda configuration build:gpu --@rules_cuda//cuda:enable_cuda build:gpu --@rules_cuda//cuda:cuda_targets=sm_52 diff --git a/c10/BUILD.bazel b/c10/BUILD.bazel index e5f3c81d839c..317d14edf674 100644 --- a/c10/BUILD.bazel +++ b/c10/BUILD.bazel @@ -38,14 +38,14 @@ header_template_rule( cc_library( name = "headers", - hdrs = glob([ + hdrs = [ + "//c10/util:headers", + ] + glob([ "core/*.h", "core/impl/*.h", "cuda/*.h", "cuda/impl/*.h", "mobile/*.h", - "util/*.h", - "util/*.hpp", ]), deps = [ ":cuda_cmake_macros_h", @@ -62,11 +62,12 @@ cc_library( cc_library( name = "c10", - srcs = glob([ + srcs = [ + "//c10/util:sources", + ] + glob([ "core/*.cpp", "core/impl/*.cpp", "mobile/*.cpp", - "util/*.cpp", ]) + if_cuda( glob([ "cuda/*.cpp", @@ -77,7 +78,7 @@ cc_library( local_defines = ["C10_BUILD_MAIN_LIB"], deps = [ ":headers", - "@fmt", + "//c10/util:base", ] + if_cuda( ["@cuda"], [], diff --git a/c10/util/BUILD.bazel b/c10/util/BUILD.bazel new file mode 100644 index 000000000000..d1a0db360d23 --- /dev/null +++ b/c10/util/BUILD.bazel @@ -0,0 +1,4 @@ +load("//:tools/bazel.bzl", "rules") +load(":build.bzl", "define_targets") + +define_targets(rules = rules) diff --git a/c10/util/build.bzl b/c10/util/build.bzl new file mode 100644 index 000000000000..b2a68620759d --- /dev/null +++ b/c10/util/build.bzl @@ -0,0 +1,53 @@ +def define_targets(rules): + rules.cc_library( + name = "base", + srcs = rules.glob( + ["*.cpp"], + exclude = [ + "TypeCast.cpp", + "typeid.cpp", + ], + ), + hdrs = rules.glob( + ["*.h"], + exclude = [ + "TypeCast.h", + "typeid.h", + ], + ), + # This library uses flags and registration. Do not let the + # linker remove them. + alwayslink = True, + linkstatic = True, + local_defines = ["C10_BUILD_MAIN_LIB"], + visibility = ["//visibility:public"], + deps = [ + "@fmt", + "//c10/macros", + ] + rules.select({ + "//c10:using_gflags": ["@com_github_gflags_gflags//:gflags"], + "//conditions:default": [], + }) + rules.select({ + "//c10:using_glog": ["@com_github_glog//:glog"], + "//conditions:default": [], + }), + ) + + rules.filegroup( + name = "headers", + srcs = rules.glob( + ["*.h"], + exclude = [ + ], + ), + visibility = ["//c10:__pkg__"], + ) + + rules.filegroup( + name = "sources", + srcs = [ + "TypeCast.cpp", + "typeid.cpp", + ], + visibility = ["//c10:__pkg__"], + ) diff --git a/tools/bazel.bzl b/tools/bazel.bzl index c7785f639690..ad7b8432ad23 100644 --- a/tools/bazel.bzl +++ b/tools/bazel.bzl @@ -7,5 +7,7 @@ load("//c10/macros:cmake_configure_file.bzl", "cmake_configure_file") rules = struct( cc_library = cc_library, cmake_configure_file = cmake_configure_file, + filegroup = native.filegroup, + glob = native.glob, select = select, )