Refactor building DLL example on Windows with windows_dll_library
Closes #7930.
PiperOrigin-RevId: 243611901
diff --git a/examples/windows/dll/BUILD b/examples/windows/dll/BUILD
index 588eef4..7cf1f25 100644
--- a/examples/windows/dll/BUILD
+++ b/examples/windows/dll/BUILD
@@ -1,69 +1,35 @@
+load("//examples/windows/dll:windows_dll_library.bzl", "windows_dll_library")
+
filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//examples:__pkg__"],
)
-cc_library(
- name = "hello-library-header",
+# Define the shared library
+windows_dll_library(
+ name = "hellolib",
+ srcs = ["hello-library.cpp"],
hdrs = ["hello-library.h"],
-)
-
-cc_binary(
- name = "hellolib.dll",
- srcs = [
- "hello-library.cpp",
- ],
# Define COMPILING_DLL to export symbols during compiling the DLL.
# See hello-library.h
copts = ["/DCOMPILING_DLL"],
- linkshared = 1,
- deps = [
- ":hello-library-header",
- ],
)
# **Explicitly link to hellolib.dll**
-# Declare hellolib.dll as data dependency and load it explicitly in code.
+## Declare hellolib.dll as data dependency and load it explicitly in code.
cc_binary(
name = "hello_world-load-dll-at-runtime",
- srcs = [
- "hello_world-load-dll-at-runtime.cpp",
- ],
+ srcs = ["hello_world-load-dll-at-runtime.cpp"],
data = [":hellolib.dll"],
)
# **Implicitly link to hellolib.dll**
-# Get the import library for hellolib.dll
-filegroup(
- name = "hello_lib_import_lib",
- srcs = [":hellolib.dll"],
- output_group = "interface_library",
-)
-
-# Because we cannot directly depend on cc_binary from other cc rules in deps attribute,
-# we use cc_import as a bridge to depend on hellolib.dll
-cc_import(
- name = "hellolib_dll_import",
- interface_library = ":hello_lib_import_lib",
- shared_library = ":hellolib.dll",
-)
-
-# Create a new cc_library to also include the headers needed for hellolib.dll
-cc_library(
- name = "hellolib_dll",
- deps = [
- ":hello-library-header",
- ":hellolib_dll_import",
- ],
-)
-
+## Link to hellolib.dll through its import library.
cc_binary(
name = "hello_world-link-to-dll-via-lib",
- srcs = [
- "hello_world-link-to-dll-via-lib.cpp",
- ],
- deps = [":hellolib_dll"],
+ srcs = ["hello_world-link-to-dll-via-lib.cpp"],
+ deps = [":hellolib"],
)
diff --git a/examples/windows/dll/windows_dll_library.bzl b/examples/windows/dll/windows_dll_library.bzl
new file mode 100644
index 0000000..21262ae
--- /dev/null
+++ b/examples/windows/dll/windows_dll_library.bzl
@@ -0,0 +1,58 @@
+# This is a simple windows_dll_library rule for builing a DLL Windows
+# that can be depended on by other cc rules.
+#
+# Example useage:
+# windows_dll_library(
+# name = "hellolib",
+# srcs = [
+# "hello-library.cpp",
+# ],
+# hdrs = ["hello-library.h"],
+# # Define COMPILING_DLL to export symbols during compiling the DLL.
+# copts = ["/DCOMPILING_DLL"],
+# )
+#
+
+def windows_dll_library(
+ name,
+ srcs = [],
+ hdrs = [],
+ visibility = None,
+ **kwargs):
+ """A simple windows_dll_library rule for builing a DLL Windows."""
+ dll_name = name + ".dll"
+ import_lib_name = name + "_import_lib"
+ import_target_name = name + "_dll_import"
+
+ # Build the shared library
+ native.cc_binary(
+ name = dll_name,
+ srcs = srcs + hdrs,
+ linkshared = 1,
+ **kwargs
+ )
+
+ # Get the import library for the dll
+ native.filegroup(
+ name = import_lib_name,
+ srcs = [":" + dll_name],
+ output_group = "interface_library",
+ )
+
+ # Because we cannot directly depend on cc_binary from other cc rules in deps attribute,
+ # we use cc_import as a bridge to depend on the dll.
+ native.cc_import(
+ name = import_target_name,
+ interface_library = ":" + import_lib_name,
+ shared_library = ":" + dll_name,
+ )
+
+ # Create a new cc_library to also include the headers needed for the shared library
+ native.cc_library(
+ name = name,
+ hdrs = hdrs,
+ visibility = visibility,
+ deps = [
+ ":" + import_target_name,
+ ],
+ )