Skip to content

Commit ca83b24

Browse files
committed
Stub CUDA runtime for CPU-only release builds
1 parent dcdecff commit ca83b24

1 file changed

Lines changed: 63 additions & 3 deletions

File tree

third_party/cuda/cuda.bzl

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,61 @@
1616

1717
_CUDA_DIR = "CUDA_DIR"
1818

19+
_STUB_CUDA_RUNTIME_API_H = """
20+
#ifndef CUDA_RUNTIME_API_H_
21+
#define CUDA_RUNTIME_API_H_
22+
23+
#include <stddef.h>
24+
#include <stdlib.h>
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
typedef void* cudaStream_t;
31+
typedef int cudaError_t;
32+
33+
enum cudaMemcpyKind {
34+
cudaMemcpyHostToHost = 0,
35+
cudaMemcpyHostToDevice = 1,
36+
cudaMemcpyDeviceToHost = 2,
37+
cudaMemcpyDeviceToDevice = 3,
38+
};
39+
40+
static inline cudaError_t cudaMemcpyAsync(
41+
void* dst, const void* src, size_t count, enum cudaMemcpyKind kind,
42+
cudaStream_t stream) {
43+
(void)dst;
44+
(void)src;
45+
(void)count;
46+
(void)kind;
47+
(void)stream;
48+
abort();
49+
return 1;
50+
}
51+
52+
static inline cudaError_t cudaStreamSynchronize(cudaStream_t stream) {
53+
(void)stream;
54+
abort();
55+
return 1;
56+
}
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif
61+
62+
#endif /* CUDA_RUNTIME_API_H_ */
63+
"""
64+
1965
def _impl(rctx):
2066
cuda_dir = rctx.os.environ.get(_CUDA_DIR, default = "/usr/local/cuda")
21-
rctx.symlink("{}/include".format(cuda_dir), "include")
22-
rctx.symlink("{}/lib64".format(cuda_dir), "lib64")
67+
cuda_include = rctx.path("{}/include/cuda_runtime_api.h".format(cuda_dir))
68+
cudart_static = rctx.path("{}/lib64/libcudart_static.a".format(cuda_dir))
2369
rctx.file("WORKSPACE")
24-
rctx.file("BUILD", content = """
70+
if cuda_include.exists and cudart_static.exists:
71+
rctx.symlink("{}/include".format(cuda_dir), "include")
72+
rctx.symlink("{}/lib64".format(cuda_dir), "lib64")
73+
rctx.file("BUILD", content = """
2574
package(default_visibility = ["//visibility:public"])
2675
2776
cc_library(
@@ -33,6 +82,17 @@ cc_library(
3382
]),
3483
strip_include_prefix = "include",
3584
)
85+
""")
86+
else:
87+
rctx.file("include/cuda_runtime_api.h", _STUB_CUDA_RUNTIME_API_H)
88+
rctx.file("BUILD", content = """
89+
package(default_visibility = ["//visibility:public"])
90+
91+
cc_library(
92+
name = "cudart_static",
93+
hdrs = ["include/cuda_runtime_api.h"],
94+
strip_include_prefix = "include",
95+
)
3696
""")
3797

3898
cuda_configure = repository_rule(

0 commit comments

Comments
 (0)