Closed yesudeep closed 4 years ago
I'm closing this, like you said people can always check closed issues but I have no interest in Bazel or any specific build system.
Update. Build fails when using BoringSSL with uSockets on Windows with MSVC++. The fix is to define WIN32_LEAN_AND_MEAN=1 and link with the appropriate libraries based on this discussion.
Updated build file:
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
licenses(["notice"]) # MIT
exports_files(["LICENSE"])
config_setting(
name = "platform_linux",
constraint_values = [
"@platforms//os:linux",
],
)
config_setting(
name = "platform_macos",
constraint_values = [
"@platforms//os:macos",
],
)
config_setting(
name = "platform_windows",
constraint_values = [
"@platforms//os:windows",
],
)
DEFAULT_DEPS = [
# Prefer BoringSSL to OpenSSL.
"@boringssl//:crypto",
"@boringssl//:ssl",
# "@openssl",
]
cc_library(
name = "usockets",
srcs = glob([
"src/**/*.c",
]),
hdrs = glob([
"src/**/*.h",
]),
copts = select({
":platform_linux": [
"-std=c11",
"-flto",
],
":platform_macos": [
"-std=c11",
"-flto",
],
"//conditions:default": [],
}),
defines = select({
":platform_linux": [
"LIBUS_USE_OPENSSL=1",
"LIBUS_USE_EPOLL=1",
],
":platform_macos": [
"LIBUS_USE_OPENSSL=1",
"LIBUS_USE_KQUEUE=1",
],
":platform_windows": [
"LIBUS_USE_OPENSSL=1",
"LIBUS_USE_LIBUV=1",
"WIN32_LEAN_AND_MEAN=1",
],
"//conditions:default": [
"LIBUS_USE_OPENSSL=1",
"LIBUS_USE_LIBUV=1",
],
}),
includes = [
"src",
],
linkopts = select({
":platform_linux": [
"-pthread",
"-ldl",
],
":platform_macos": [
"-pthread",
],
":platform_windows": [
"-DEFAULTLIB:Ws2_32.lib",
"-DEFAULTLIB:Iphlpapi.lib",
"-DEFAULTLIB:Psapi.lib",
"-DEFAULTLIB:User32.lib",
"-DEFAULTLIB:Userenv.lib",
],
"//conditions:default": [],
}),
linkstatic = True,
strip_include_prefix = "src",
visibility = ["//visibility:public"],
deps = select({
":platform_linux": DEFAULT_DEPS,
":platform_macos": DEFAULT_DEPS,
"//conditions:default": DEFAULT_DEPS + [
"@libuv",
],
}),
)
cc_binary(
name = "example_echo_server",
srcs = [
"examples/echo_server.c",
],
visibility = ["//visibility:public"],
deps = [
":usockets",
],
)
cc_binary(
name = "example_http_server",
srcs = [
"examples/http_server.c",
],
visibility = ["//visibility:public"],
deps = [
":usockets",
],
)
cc_binary(
name = "example_http_load_test",
srcs = [
"examples/http_load_test.c",
],
visibility = ["//visibility:public"],
deps = [
":usockets",
],
)
cc_binary(
name = "example_hammer_test",
srcs = [
"examples/hammer_test.c",
],
visibility = ["//visibility:public"],
deps = [
":usockets",
],
)
cc_binary(
name = "example_peer_verify_test",
srcs = [
"examples/peer_verify_test.c",
],
visibility = ["//visibility:public"],
deps = [
":usockets",
],
)
Since autotools don't work as well as I'd hoped on Windows (PowerShell), I've ported the libuv.BUILD to explicitly build using only Bazel on Windows. A fallback rule that uses autotools is also available.
libuv.BUILD:
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make")
config_setting(
name = "platform_linux",
constraint_values = [
"@platforms//os:linux",
],
)
config_setting(
name = "platform_macos",
constraint_values = [
"@platforms//os:macos",
],
)
config_setting(
name = "platform_windows",
constraint_values = [
"@platforms//os:windows",
],
)
config_setting(
name = "platform_ios",
constraint_values = [
"@platforms//os:ios",
],
)
config_setting(
name = "platform_android",
constraint_values = [
"@platforms//os:android",
],
)
filegroup(
name = "all",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
# Use this if nothing else works--it results in slower builds.
configure_make(
name = "libuv_autotools",
autogen = True,
# see @rules_foreign_cc#315
configure_env_vars = select({
":platform_macos": {"AR": ""},
"//conditions:default": {},
}),
configure_in_place = True,
lib_source = "@libuv//:all",
linkopts = select({
":platform_linux": [
"-pthread",
"-ldl",
],
"//conditions:default": [
"-pthread",
],
}),
out_lib_dir = "lib",
static_libraries = ["libuv.a"],
visibility = ["//visibility:public"],
)
# Based on https://github.com/grpc/grpc/blob/master/third_party/libuv.BUILD
COMMON_LIBUV_HEADERS = [
"include/uv.h",
"include/uv/errno.h",
"include/uv/threadpool.h",
"include/uv/version.h",
"include/uv/tree.h",
]
UNIX_LIBUV_HEADERS = [
"include/uv/unix.h",
"src/unix/atomic-ops.h",
"src/unix/internal.h",
"src/unix/spinlock.h",
]
LINUX_LIBUV_HEADERS = [
"include/uv/linux.h",
"src/unix/linux-syscalls.h",
]
ANDROID_LIBUV_HEADERS = [
"include/uv/android-ifaddrs.h",
]
DARWIN_LIBUV_HEADERS = [
"include/uv/darwin.h",
"src/unix/darwin-stub.h",
]
WINDOWS_LIBUV_HEADERS = [
"include/uv/win.h",
"src/win/atomicops-inl.h",
"src/win/handle-inl.h",
"src/win/internal.h",
"src/win/req-inl.h",
"src/win/stream-inl.h",
"src/win/winapi.h",
"src/win/winsock.h",
]
COMMON_LIBUV_SOURCES = [
"src/fs-poll.c",
"src/heap-inl.h",
"src/idna.c",
"src/idna.h",
"src/inet.c",
"src/queue.h",
"src/strscpy.c",
"src/strscpy.h",
"src/threadpool.c",
"src/timer.c",
"src/uv-data-getter-setters.c",
"src/uv-common.c",
"src/uv-common.h",
"src/version.c",
]
UNIX_LIBUV_SOURCES = [
"src/unix/async.c",
"src/unix/atomic-ops.h",
"src/unix/core.c",
"src/unix/dl.c",
"src/unix/fs.c",
"src/unix/getaddrinfo.c",
"src/unix/getnameinfo.c",
"src/unix/internal.h",
"src/unix/loop.c",
"src/unix/loop-watcher.c",
"src/unix/pipe.c",
"src/unix/poll.c",
"src/unix/process.c",
"src/unix/signal.c",
"src/unix/spinlock.h",
"src/unix/stream.c",
"src/unix/tcp.c",
"src/unix/thread.c",
"src/unix/tty.c",
"src/unix/udp.c",
]
LINUX_LIBUV_SOURCES = [
"src/unix/linux-core.c",
"src/unix/linux-inotify.c",
"src/unix/linux-syscalls.c",
"src/unix/linux-syscalls.h",
"src/unix/procfs-exepath.c",
"src/unix/proctitle.c",
"src/unix/sysinfo-loadavg.c",
"src/unix/sysinfo-memory.c",
]
ANDROID_LIBUV_SOURCES = [
"src/unix/android-ifaddrs.c",
"src/unix/pthread-fixes.c",
]
DARWIN_LIBUV_SOURCES = [
"src/unix/bsd-ifaddrs.c",
"src/unix/darwin.c",
"src/unix/fsevents.c",
"src/unix/kqueue.c",
"src/unix/darwin-proctitle.c",
"src/unix/proctitle.c",
]
WINDOWS_LIBUV_SOURCES = [
"src/win/async.c",
"src/win/atomicops-inl.h",
"src/win/core.c",
"src/win/detect-wakeup.c",
"src/win/dl.c",
"src/win/error.c",
"src/win/fs-event.c",
"src/win/fs.c",
"src/win/getaddrinfo.c",
"src/win/getnameinfo.c",
"src/win/handle.c",
"src/win/handle-inl.h",
"src/win/internal.h",
"src/win/loop-watcher.c",
"src/win/pipe.c",
"src/win/poll.c",
"src/win/process-stdio.c",
"src/win/process.c",
"src/win/req-inl.h",
"src/win/signal.c",
"src/win/stream.c",
"src/win/stream-inl.h",
"src/win/tcp.c",
"src/win/thread.c",
"src/win/tty.c",
"src/win/udp.c",
"src/win/util.c",
"src/win/winapi.c",
"src/win/winapi.h",
"src/win/winsock.c",
"src/win/winsock.h",
]
COPTS_COMMON = [
"-D_LARGEFILE_SOURCE",
"-D_FILE_OFFSET_BITS=64",
"-D_GNU_SOURCE",
]
COPTS_CC = [
"--std=gnu89",
"-pedantic",
"-O2",
"-Wno-error",
"-Wno-strict-aliasing",
"-Wstrict-aliasing",
"-Wno-implicit-function-declaration",
"-Wno-unused-function",
"-Wno-unused-variable",
"-pthread",
]
COPTS_LINUX = COPTS_CC + [
"-Wno-tree-vrp",
"-Wno-omit-frame-pointer",
]
COPTS_MACOS = COPTS_CC + [
"-D_DARWIN_USE_64_BIT_INODE=1",
"-D_DARWIN_UNLIMITED_SELECT=1",
]
# TODO: We haven't build something for iOS yet.
COPTS_IOS = COPTS_CC
cc_library(
name = "libuv",
srcs = select({
":platform_android": COMMON_LIBUV_SOURCES + UNIX_LIBUV_SOURCES + LINUX_LIBUV_SOURCES + ANDROID_LIBUV_SOURCES,
":platform_ios": COMMON_LIBUV_SOURCES + UNIX_LIBUV_SOURCES + DARWIN_LIBUV_SOURCES,
":platform_macos": COMMON_LIBUV_SOURCES + UNIX_LIBUV_SOURCES + DARWIN_LIBUV_SOURCES,
":platform_windows": COMMON_LIBUV_SOURCES + WINDOWS_LIBUV_SOURCES,
"//conditions:default": COMMON_LIBUV_SOURCES + UNIX_LIBUV_SOURCES + LINUX_LIBUV_SOURCES,
}),
hdrs = select({
":platform_android": COMMON_LIBUV_HEADERS + UNIX_LIBUV_HEADERS + LINUX_LIBUV_HEADERS + ANDROID_LIBUV_HEADERS,
":platform_macos": COMMON_LIBUV_HEADERS + UNIX_LIBUV_HEADERS + DARWIN_LIBUV_HEADERS,
":platform_ios": COMMON_LIBUV_HEADERS + UNIX_LIBUV_HEADERS + DARWIN_LIBUV_HEADERS,
":platform_windows": COMMON_LIBUV_HEADERS + WINDOWS_LIBUV_HEADERS,
"//conditions:default": COMMON_LIBUV_HEADERS + UNIX_LIBUV_HEADERS + LINUX_LIBUV_HEADERS,
}),
copts = COPTS_COMMON + select({
":platform_macos": COPTS_MACOS,
":platform_ios": COPTS_IOS,
":platform_linux": COPTS_LINUX,
":platform_windows": [
"-O2",
"-DWIN32_LEAN_AND_MEAN",
"-D_WIN32_WINNT=0x0600",
],
"//conditions:default": COPTS_CC,
}),
includes = [
"include",
"src",
"src/unix",
],
linkopts = select({
":platform_windows": [
"-DEFAULTLIB:Ws2_32.lib",
"-DEFAULTLIB:Iphlpapi.lib",
"-DEFAULTLIB:Psapi.lib",
"-DEFAULTLIB:User32.lib",
"-DEFAULTLIB:Userenv.lib",
],
"//conditions:default": [
"-lpthread",
"-ldl",
],
}),
linkstatic = True,
visibility = [
"//visibility:public",
],
deps = select({
":platform_windows": [
"@pthread_windows//:pthread",
],
"//conditions:default": [],
}),
)
pthreads-w32 ported to Bazel is available here: pthread_windows.
Hi,
Thank you for this amazing library.
I use Bazel to build
uSockets
and I understand the project wants to maintain only aMakefile
. However, I'm making myBUILD.bazel
file and configuration available so that anybody who needs it can use it. The users of the library don't need to know how to build the library and its dependencies—the rules are part of the remote BUILD file. Here's example usage from the perspective of the library user (simpler one):A more complex example:
To run the example:
I include
uSockets
in my workspace using:usockets.BUILD
file:libuv.BUILD
:To build and run the example with AddressSanitizer, for example, I just type:
Bazel fetches the correct commit from github for each dependency without needing to use submodules and builds everything into your workspace only if you use it—you don't pay for what you don't use. Build files for openssl and boringssl can be found here.
--config=asan
is defined here and goes into a workspace level.bazelrc
file in more recent versions of Bazel.Please feel free to close the bug. Just reporting this here so it helps anybody who needs it.
Cheers.