Skip to content

Commit

Permalink
Refactor building DLL example on Windows with windows_dll_library
Browse files Browse the repository at this point in the history
Closes #7930.

PiperOrigin-RevId: 243611901
  • Loading branch information
meteorcloudy authored and copybara-github committed Apr 15, 2019
1 parent b941e0b commit c07188a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 45 deletions.
56 changes: 11 additions & 45 deletions examples/windows/dll/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
58 changes: 58 additions & 0 deletions examples/windows/dll/windows_dll_library.bzl
Original file line number Diff line number Diff line change
@@ -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,
],
)

0 comments on commit c07188a

Please sign in to comment.