Skip to content

Commit e89c9d6

Browse files
sesmith177htuch
authored andcommitted
build: Initial bazel build file changes for Windows (envoyproxy#3884)
This is the first step in breaking up envoyproxy#3786 into smaller chunks. This contains the Bazel config / compiler options to allow Envoy to build on Windows. Future PRs will address the external deps build scripts and PGV. Risk Level: Low Testing: Ran bazel build //source/exe:envoy-static and bazel test //test/... on Linux Docs Changes: None Release Notes: None Signed-off-by: Sam Smith <[email protected]>
1 parent b32eabf commit e89c9d6

File tree

17 files changed

+364
-32
lines changed

17 files changed

+364
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ SOURCE_VERSION
1515
.cache
1616
.vimrc
1717
.vscode
18+
.vs

bazel/BUILD

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ genrule(
3535
stamp = 1,
3636
)
3737

38+
config_setting(
39+
name = "windows_x86_64",
40+
values = {"cpu": "x64_windows"},
41+
)
42+
43+
config_setting(
44+
name = "windows_opt_build",
45+
values = {
46+
"cpu": "x64_windows",
47+
"compilation_mode": "opt",
48+
},
49+
)
50+
51+
config_setting(
52+
name = "windows_dbg_build",
53+
values = {
54+
"cpu": "x64_windows",
55+
"compilation_mode": "dbg",
56+
},
57+
)
58+
59+
config_setting(
60+
name = "windows_fastbuild_build",
61+
values = {
62+
"cpu": "x64_windows",
63+
"compilation_mode": "fastbuild",
64+
},
65+
)
66+
3867
config_setting(
3968
name = "opt_build",
4069
values = {"compilation_mode": "opt"},

bazel/envoy_build_system.bzl

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,39 @@ def envoy_package():
55

66
# Compute the final copts based on various options.
77
def envoy_copts(repository, test = False):
8-
return [
9-
"-Wall",
10-
"-Wextra",
11-
"-Werror",
12-
"-Wnon-virtual-dtor",
13-
"-Woverloaded-virtual",
14-
"-Wold-style-cast",
15-
"-std=c++14",
16-
] + select({
8+
posix_options = [
9+
"-Wall",
10+
"-Wextra",
11+
"-Werror",
12+
"-Wnon-virtual-dtor",
13+
"-Woverloaded-virtual",
14+
"-Wold-style-cast",
15+
"-std=c++14",
16+
]
17+
18+
msvc_options = [
19+
"-WX",
20+
"-DWIN32",
21+
"-DWIN32_LEAN_AND_MEAN",
22+
# need win8 for ntohll
23+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx
24+
"-D_WIN32_WINNT=0x0602",
25+
"-DNTDDI_VERSION=0x06020000",
26+
"-DCARES_STATICLIB",
27+
"-DNGHTTP2_STATICLIB",
28+
]
29+
30+
return select({
31+
"//bazel:windows_x86_64": msvc_options,
32+
"//conditions:default": posix_options,
33+
}) + select({
1734
# Bazel adds an implicit -DNDEBUG for opt.
1835
repository + "//bazel:opt_build": [] if test else ["-ggdb3"],
1936
repository + "//bazel:fastbuild_build": [],
2037
repository + "//bazel:dbg_build": ["-ggdb3"],
38+
repository + "//bazel:windows_opt_build": [],
39+
repository + "//bazel:windows_fastbuild_build": [],
40+
repository + "//bazel:windows_dbg_build": [],
2141
}) + select({
2242
repository + "//bazel:disable_tcmalloc": ["-DABSL_MALLOC_HOOK_MMAP_DISABLE"],
2343
"//conditions:default": ["-DTCMALLOC"],
@@ -47,6 +67,9 @@ def envoy_linkopts():
4767
"-pagezero_size 10000",
4868
"-image_base 100000000",
4969
],
70+
"//bazel:windows_x86_64": [
71+
"-DEFAULTLIB:advapi32.lib",
72+
],
5073
"//conditions:default": [
5174
"-pthread",
5275
"-lrt",
@@ -62,6 +85,7 @@ def _envoy_stamped_linkopts():
6285
#
6386
# /usr/bin/ld.gold: internal error in write_build_id, at ../../gold/layout.cc:5419
6487
"@envoy//bazel:coverage_build": [],
88+
"//bazel:windows_x86_64": [],
6589

6690
# MacOS doesn't have an official equivalent to the `.note.gnu.build-id`
6791
# ELF section, so just stuff the raw ID into a new text section.
@@ -120,6 +144,13 @@ def tcmalloc_external_deps(repository):
120144
"//conditions:default": [envoy_external_dep_path("tcmalloc_and_profiler")],
121145
})
122146

147+
# Dependencies on libevent should be wrapped with this function.
148+
def libevent_external_deps(repository):
149+
return [envoy_external_dep_path("event")] + select({
150+
repository + "//bazel:windows_x86_64": [],
151+
"//conditions:default": [envoy_external_dep_path("event_pthreads")],
152+
})
153+
123154
# Transform the package path (e.g. include/envoy/common) into a path for
124155
# exporting the package headers at (e.g. envoy/common). Source files can then
125156
# include using this path scheme (e.g. #include "envoy/common/time.h").
@@ -144,13 +175,17 @@ def envoy_cc_library(
144175
visibility = None,
145176
external_deps = [],
146177
tcmalloc_dep = None,
178+
libevent_dep = None,
147179
repository = "",
148180
linkstamp = None,
149181
tags = [],
150182
deps = [],
151183
strip_include_prefix = None):
152184
if tcmalloc_dep:
153185
deps += tcmalloc_external_deps(repository)
186+
if libevent_dep:
187+
deps += libevent_external_deps(repository)
188+
154189
native.cc_library(
155190
name = name,
156191
srcs = srcs,
@@ -168,7 +203,10 @@ def envoy_cc_library(
168203
include_prefix = envoy_include_prefix(PACKAGE_NAME),
169204
alwayslink = 1,
170205
linkstatic = 1,
171-
linkstamp = linkstamp,
206+
linkstamp = select({
207+
repository + "//bazel:windows_x86_64": None,
208+
"//conditions:default": linkstamp,
209+
}),
172210
strip_include_prefix = strip_include_prefix,
173211
)
174212

@@ -481,5 +519,6 @@ def envoy_select_force_libcpp(if_libcpp, default = None):
481519
return select({
482520
"@envoy//bazel:force_libcpp": if_libcpp,
483521
"@bazel_tools//tools/osx:darwin": [],
522+
"//bazel:windows_x86_64": [],
484523
"//conditions:default": default or [],
485524
})

bazel/external/libcircllhist.BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ cc_library(
66
],
77
includes = ["src"],
88
visibility = ["//visibility:public"],
9+
copts = select({
10+
"@envoy//bazel:windows_x86_64": ["-DWIN32"],
11+
"//conditions:default": [],
12+
}),
913
)

bazel/repositories.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "Start"
2+
@ECHO OFF
3+
%BAZEL_SH% -c "./repositories.sh %*"
4+
exit %ERRORLEVEL%

bazel/repositories.bzl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ load(":genrule_repository.bzl", "genrule_repository")
77
load(":patched_http_archive.bzl", "patched_http_archive")
88
load(":repository_locations.bzl", "REPOSITORY_LOCATIONS")
99
load(":target_recipes.bzl", "TARGET_RECIPES")
10+
load(
11+
"@bazel_tools//tools/cpp:windows_cc_configure.bzl",
12+
"find_vc_path",
13+
"setup_vc_env_vars",
14+
)
15+
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_env_var")
1016

1117
def _repository_impl(name, **kwargs):
1218
# `existing_rule_keys` contains the names of repositories that have already
@@ -67,6 +73,7 @@ def _repository_impl(name, **kwargs):
6773
def _build_recipe_repository_impl(ctxt):
6874
# Setup the build directory with links to the relevant files.
6975
ctxt.symlink(Label("//bazel:repositories.sh"), "repositories.sh")
76+
ctxt.symlink(Label("//bazel:repositories.bat"), "repositories.bat")
7077
ctxt.symlink(
7178
Label("//ci/build_container:build_and_install_deps.sh"),
7279
"build_and_install_deps.sh",
@@ -81,11 +88,25 @@ def _build_recipe_repository_impl(ctxt):
8188
ctxt.symlink(Label("//ci/prebuilt:BUILD"), "BUILD")
8289

8390
# Run the build script.
84-
environment = {}
91+
command = []
92+
env = {}
93+
if ctxt.os.name.upper().startswith("WINDOWS"):
94+
vc_path = find_vc_path(ctxt)
95+
current_path = get_env_var(ctxt, "PATH", None, False)
96+
env = setup_vc_env_vars(ctxt, vc_path)
97+
env["PATH"] += (";%s" % current_path)
98+
env["CC"] = "cl"
99+
env["CXX"] = "cl"
100+
env["CXXFLAGS"] = "-DNDEBUG"
101+
env["CFLAGS"] = "-DNDEBUG"
102+
command = ["./repositories.bat"] + ctxt.attr.recipes
103+
else:
104+
command = ["./repositories.sh"] + ctxt.attr.recipes
105+
85106
print("Fetching external dependencies...")
86107
result = ctxt.execute(
87-
["./repositories.sh"] + ctxt.attr.recipes,
88-
environment = environment,
108+
command,
109+
environment = env,
89110
quiet = False,
90111
)
91112
print(result.stdout)

bazel/repository_locations.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ REPOSITORY_LOCATIONS = dict(
1818
remote = "https://github.com/bombela/backward-cpp",
1919
),
2020
com_github_circonus_labs_libcircllhist = dict(
21-
commit = "476687ac9cc636fc92ac3070246d757ae6854547", # 2018-05-08
21+
commit = "050da53a44dede7bda136b93a9aeef47bd91fa12", # 2018-07-02
2222
remote = "https://github.com/circonus-labs/libcircllhist",
2323
),
2424
com_github_cyan4973_xxhash = dict(

ci/build_setup.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
$ErrorActionPreference = "Stop";
2+
trap { $host.SetShouldExit(1) }
3+
4+
if ("$env:NUM_CPUS" -eq "") {
5+
$env:NUM_CPUS = (Get-WmiObject -class Win32_computersystem).NumberOfLogicalProcessors
6+
}
7+
8+
if ("$env:ENVOY_BAZEL_ROOT" -eq "") {
9+
Write-Host "ENVOY_BAZEL_ROOT must be set!"
10+
throw
11+
}
12+
13+
mkdir -force "$env:ENVOY_BAZEL_ROOT" > $nul
14+
15+
$env:ENVOY_SRCDIR = [System.IO.Path]::GetFullPath("$PSScriptRoot\..")
16+
17+
echo "ENVOY_BAZEL_ROOT: $env:ENVOY_BAZEL_ROOT"
18+
echo "ENVOY_SRCDIR: $env:ENVOY_SRCDIR"
19+
20+
$env:BAZEL_BASE_OPTIONS="--nomaster_bazelrc --output_base=$env:ENVOY_BAZEL_ROOT --bazelrc=$env:ENVOY_SRCDIR\windows\tools\bazel.rc"
21+
$env:BAZEL_BUILD_OPTIONS="--strategy=Genrule=standalone --spawn_strategy=standalone --verbose_failures --jobs=$env:NUM_CPUS --show_task_finish $env:BAZEL_BUILD_EXTRA_OPTIONS"
22+
$env:BAZEL_TEST_OPTIONS="$env:BAZEL_BUILD_OPTIONS --cache_test_results=no --test_output=all $env:BAZEL_EXTRA_TEST_OPTIONS"

ci/do_ci.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$ErrorActionPreference = "Stop";
2+
trap { $host.SetShouldExit(1) }
3+
4+
. "$PSScriptRoot\build_setup.ps1"
5+
Write-Host "building using $env:NUM_CPUS CPUs"
6+
7+
function bazel_debug_binary_build() {
8+
echo "Building..."
9+
pushd "$env:ENVOY_SRCDIR"
10+
bazel $env:BAZEL_BASE_OPTIONS.Split(" ") build $env:BAZEL_BUILD_OPTIONS.Split(" ") -c dbg "//source/exe:envoy-static"
11+
$exit = $LASTEXITCODE
12+
if ($exit -ne 0) {
13+
popd
14+
exit $exit
15+
}
16+
popd
17+
}
18+
19+
echo "bazel debug build..."
20+
bazel_debug_binary_build

ci/prebuilt/BUILD

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,37 @@ licenses(["notice"]) # Apache 2
22

33
package(default_visibility = ["//visibility:public"])
44

5+
config_setting(
6+
name = "windows_x86_64",
7+
values = {"cpu": "x64_windows"},
8+
)
9+
510
cc_library(
611
name = "ares",
7-
srcs = ["thirdparty_build/lib/libcares.a"],
12+
srcs = select({
13+
":windows_x86_64": ["thirdparty_build/lib/cares.lib"],
14+
"//conditions:default": ["thirdparty_build/lib/libcares.a"],
15+
}),
816
hdrs = glob(["thirdparty_build/include/ares*.h"]),
917
includes = ["thirdparty_build/include"],
1018
)
1119

1220
cc_library(
1321
name = "benchmark",
14-
srcs = ["thirdparty_build/lib/libbenchmark.a"],
22+
srcs = select({
23+
":windows_x86_64": ["thirdparty_build/lib/benchmark.lib"],
24+
"//conditions:default": ["thirdparty_build/lib/libbenchmark.a"],
25+
}),
1526
hdrs = ["thirdparty_build/include/testing/base/public/benchmark.h"],
1627
includes = ["thirdparty_build/include"],
1728
)
1829

1930
cc_library(
2031
name = "event",
21-
srcs = ["thirdparty_build/lib/libevent.a"],
32+
srcs = select({
33+
":windows_x86_64": ["thirdparty_build/lib/event.lib"],
34+
"//conditions:default": ["thirdparty_build/lib/libevent.a"],
35+
}),
2236
hdrs = glob(["thirdparty_build/include/event2/**/*.h"]),
2337
includes = ["thirdparty_build/include"],
2438
)
@@ -31,7 +45,10 @@ cc_library(
3145

3246
cc_library(
3347
name = "luajit",
34-
srcs = ["thirdparty_build/lib/libluajit-5.1.a"],
48+
srcs = select({
49+
":windows_x86_64": ["thirdparty_build/lib/luajit.lib"],
50+
"//conditions:default": ["thirdparty_build/lib/libluajit-5.1.a"],
51+
}),
3552
hdrs = glob(["thirdparty_build/include/luajit-2.0/*"]),
3653
includes = ["thirdparty_build/include"],
3754
# TODO(mattklein123): We should strip luajit-2.0 here for consumers. However, if we do that
@@ -40,7 +57,10 @@ cc_library(
4057

4158
cc_library(
4259
name = "nghttp2",
43-
srcs = ["thirdparty_build/lib/libnghttp2.a"],
60+
srcs = select({
61+
":windows_x86_64": ["thirdparty_build/lib/nghttp2.lib"],
62+
"//conditions:default": ["thirdparty_build/lib/libnghttp2.a"],
63+
}),
4464
hdrs = glob(["thirdparty_build/include/nghttp2/**/*.h"]),
4565
includes = ["thirdparty_build/include"],
4666
)
@@ -54,14 +74,20 @@ cc_library(
5474

5575
cc_library(
5676
name = "yaml_cpp",
57-
srcs = ["thirdparty_build/lib/libyaml-cpp.a"],
77+
srcs = select({
78+
":windows_x86_64": glob(["thirdparty_build/lib/libyaml-cpp*.lib"]),
79+
"//conditions:default": ["thirdparty_build/lib/libyaml-cpp.a"],
80+
}),
5881
hdrs = glob(["thirdparty_build/include/yaml-cpp/**/*.h"]),
5982
includes = ["thirdparty_build/include"],
6083
)
6184

6285
cc_library(
6386
name = "zlib",
64-
srcs = ["thirdparty_build/lib/libz.a"],
87+
srcs = select({
88+
":windows_x86_64": glob(["thirdparty_build/lib/zlibstaticd.lib"]),
89+
"//conditions:default": ["thirdparty_build/lib/libz.a"],
90+
}),
6591
hdrs = [
6692
"thirdparty_build/include/zconf.h",
6793
"thirdparty_build/include/zlib.h",

0 commit comments

Comments
 (0)