Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Initial bazel build file changes for Windows #3884

Merged
merged 4 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ SOURCE_VERSION
.cache
.vimrc
.vscode
.vs
29 changes: 29 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ genrule(
stamp = 1,
)

config_setting(
name = "windows_x86_64",
values = {"cpu": "x64_windows"},
)

config_setting(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you define 3 different config_setting rules for Windows for the compilation_mode options? They are all used the same way, you could substitute them with //bazel:windows_x86_64.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the 3 different config_setting rules for this select here: https://github.com/greenhouse-org/envoy/blob/windows-build-pr/bazel/envoy_build_system.bzl#L36-L41

Specifically, we want to never append -ggdb3 to the copts on Windows, but we want to keep the selection mechanism on Linux. If we just add //bazel:windows_x86_64 to that select, it will fail since multiple options will be true (e.g. //bazel:windows_x86_64 and //bazel:dbg_build).

As far as we know, this is the best way to select on multiple conditions -- did we miss some other way to do this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. No, I think you're right. Thanks for the explanation!

name = "windows_opt_build",
values = {
"cpu": "x64_windows",
"compilation_mode": "opt",
},
)

config_setting(
name = "windows_dbg_build",
values = {
"cpu": "x64_windows",
"compilation_mode": "dbg",
},
)

config_setting(
name = "windows_fastbuild_build",
values = {
"cpu": "x64_windows",
"compilation_mode": "fastbuild",
},
)

config_setting(
name = "opt_build",
values = {"compilation_mode": "opt"},
Expand Down
59 changes: 49 additions & 10 deletions bazel/envoy_build_system.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,39 @@ def envoy_package():

# Compute the final copts based on various options.
def envoy_copts(repository, test = False):
return [
"-Wall",
"-Wextra",
"-Werror",
"-Wnon-virtual-dtor",
"-Woverloaded-virtual",
"-Wold-style-cast",
"-std=c++14",
] + select({
posix_options = [
"-Wall",
"-Wextra",
"-Werror",
"-Wnon-virtual-dtor",
"-Woverloaded-virtual",
"-Wold-style-cast",
"-std=c++14",
]

msvc_options = [
"-WX",
"-DWIN32",
"-DWIN32_LEAN_AND_MEAN",
# need win8 for ntohll
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx
"-D_WIN32_WINNT=0x0602",
"-DNTDDI_VERSION=0x06020000",
"-DCARES_STATICLIB",
"-DNGHTTP2_STATICLIB",
]

return select({
"//bazel:windows_x86_64": msvc_options,
"//conditions:default": posix_options,
}) + select({
# Bazel adds an implicit -DNDEBUG for opt.
repository + "//bazel:opt_build": [] if test else ["-ggdb3"],
repository + "//bazel:fastbuild_build": [],
repository + "//bazel:dbg_build": ["-ggdb3"],
repository + "//bazel:windows_opt_build": [],
repository + "//bazel:windows_fastbuild_build": [],
repository + "//bazel:windows_dbg_build": [],
}) + select({
repository + "//bazel:disable_tcmalloc": ["-DABSL_MALLOC_HOOK_MMAP_DISABLE"],
"//conditions:default": ["-DTCMALLOC"],
Expand Down Expand Up @@ -47,6 +67,9 @@ def envoy_linkopts():
"-pagezero_size 10000",
"-image_base 100000000",
],
"//bazel:windows_x86_64": [
"-DEFAULTLIB:advapi32.lib",
],
"//conditions:default": [
"-pthread",
"-lrt",
Expand All @@ -62,6 +85,7 @@ def _envoy_stamped_linkopts():
#
# /usr/bin/ld.gold: internal error in write_build_id, at ../../gold/layout.cc:5419
"@envoy//bazel:coverage_build": [],
"//bazel:windows_x86_64": [],

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

# Dependencies on libevent should be wrapped with this function.
def libevent_external_deps(repository):
return [envoy_external_dep_path("event")] + select({
repository + "//bazel:windows_x86_64": [],
"//conditions:default": [envoy_external_dep_path("event_pthreads")],
})

# Transform the package path (e.g. include/envoy/common) into a path for
# exporting the package headers at (e.g. envoy/common). Source files can then
# include using this path scheme (e.g. #include "envoy/common/time.h").
Expand All @@ -144,13 +175,17 @@ def envoy_cc_library(
visibility = None,
external_deps = [],
tcmalloc_dep = None,
libevent_dep = None,
repository = "",
linkstamp = None,
tags = [],
deps = [],
strip_include_prefix = None):
if tcmalloc_dep:
deps += tcmalloc_external_deps(repository)
if libevent_dep:
deps += libevent_external_deps(repository)

native.cc_library(
name = name,
srcs = srcs,
Expand All @@ -168,7 +203,10 @@ def envoy_cc_library(
include_prefix = envoy_include_prefix(PACKAGE_NAME),
alwayslink = 1,
linkstatic = 1,
linkstamp = linkstamp,
linkstamp = select({
repository + "//bazel:windows_x86_64": None,
"//conditions:default": linkstamp,
}),
strip_include_prefix = strip_include_prefix,
)

Expand Down Expand Up @@ -481,5 +519,6 @@ def envoy_select_force_libcpp(if_libcpp, default = None):
return select({
"@envoy//bazel:force_libcpp": if_libcpp,
"@bazel_tools//tools/osx:darwin": [],
"//bazel:windows_x86_64": [],
"//conditions:default": default or [],
})
4 changes: 4 additions & 0 deletions bazel/external/libcircllhist.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ cc_library(
],
includes = ["src"],
visibility = ["//visibility:public"],
copts = select({
"@envoy//bazel:windows_x86_64": ["-DWIN32"],
"//conditions:default": [],
}),
)
4 changes: 4 additions & 0 deletions bazel/repositories.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "Start"
@ECHO OFF
%BAZEL_SH% -c "./repositories.sh %*"
exit %ERRORLEVEL%
27 changes: 24 additions & 3 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ load(":genrule_repository.bzl", "genrule_repository")
load(":patched_http_archive.bzl", "patched_http_archive")
load(":repository_locations.bzl", "REPOSITORY_LOCATIONS")
load(":target_recipes.bzl", "TARGET_RECIPES")
load(
"@bazel_tools//tools/cpp:windows_cc_configure.bzl",
"find_vc_path",
"setup_vc_env_vars",
)
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_env_var")

def _repository_impl(name, **kwargs):
# `existing_rule_keys` contains the names of repositories that have already
Expand Down Expand Up @@ -67,6 +73,7 @@ def _repository_impl(name, **kwargs):
def _build_recipe_repository_impl(ctxt):
# Setup the build directory with links to the relevant files.
ctxt.symlink(Label("//bazel:repositories.sh"), "repositories.sh")
ctxt.symlink(Label("//bazel:repositories.bat"), "repositories.bat")
ctxt.symlink(
Label("//ci/build_container:build_and_install_deps.sh"),
"build_and_install_deps.sh",
Expand All @@ -81,11 +88,25 @@ def _build_recipe_repository_impl(ctxt):
ctxt.symlink(Label("//ci/prebuilt:BUILD"), "BUILD")

# Run the build script.
environment = {}
command = []
env = {}
if ctxt.os.name.upper().startswith("WINDOWS"):
vc_path = find_vc_path(ctxt)
current_path = get_env_var(ctxt, "PATH", None, False)
env = setup_vc_env_vars(ctxt, vc_path)
env["PATH"] += (";%s" % current_path)
env["CC"] = "cl"
env["CXX"] = "cl"
env["CXXFLAGS"] = "-DNDEBUG"
env["CFLAGS"] = "-DNDEBUG"
command = ["./repositories.bat"] + ctxt.attr.recipes
else:
command = ["./repositories.sh"] + ctxt.attr.recipes

print("Fetching external dependencies...")
result = ctxt.execute(
["./repositories.sh"] + ctxt.attr.recipes,
environment = environment,
command,
environment = env,
quiet = False,
)
print(result.stdout)
Expand Down
2 changes: 1 addition & 1 deletion bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ REPOSITORY_LOCATIONS = dict(
remote = "https://github.com/bombela/backward-cpp",
),
com_github_circonus_labs_libcircllhist = dict(
commit = "476687ac9cc636fc92ac3070246d757ae6854547", # 2018-05-08
commit = "050da53a44dede7bda136b93a9aeef47bd91fa12", # 2018-07-02
remote = "https://github.com/circonus-labs/libcircllhist",
),
com_github_cyan4973_xxhash = dict(
Expand Down
22 changes: 22 additions & 0 deletions ci/build_setup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$ErrorActionPreference = "Stop";
trap { $host.SetShouldExit(1) }

if ("$env:NUM_CPUS" -eq "") {
$env:NUM_CPUS = (Get-WmiObject -class Win32_computersystem).NumberOfLogicalProcessors
}

if ("$env:ENVOY_BAZEL_ROOT" -eq "") {
Write-Host "ENVOY_BAZEL_ROOT must be set!"
throw
}

mkdir -force "$env:ENVOY_BAZEL_ROOT" > $nul

$env:ENVOY_SRCDIR = [System.IO.Path]::GetFullPath("$PSScriptRoot\..")

echo "ENVOY_BAZEL_ROOT: $env:ENVOY_BAZEL_ROOT"
echo "ENVOY_SRCDIR: $env:ENVOY_SRCDIR"

$env:BAZEL_BASE_OPTIONS="--nomaster_bazelrc --output_base=$env:ENVOY_BAZEL_ROOT --bazelrc=$env:ENVOY_SRCDIR\windows\tools\bazel.rc --batch"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--batch is deprecated. You'll have to manually shut down the Bazel server afterwards.

$env:BAZEL_BUILD_OPTIONS="--strategy=Genrule=standalone --spawn_strategy=standalone --verbose_failures --action_env=HOME --action_env=PYTHONUSERBASE --jobs=$env:NUM_CPUS --show_task_finish $env:BAZEL_BUILD_EXTRA_OPTIONS"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does $HOME have any value on Windows? (Also in BAZEL_TEST_OPTIONS below.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not. It doesn't look like we need any of the other action_env or test_env environment variables on Windows, so we'll remove them as well

$env:BAZEL_TEST_OPTIONS="$env:BAZEL_BUILD_OPTIONS --test_env=HOME --test_env=PYTHONUSERBASE --test_env=UBSAN_OPTIONS=print_stacktrace=1 --cache_test_results=no --test_output=all $env:BAZEL_EXTRA_TEST_OPTIONS"
20 changes: 20 additions & 0 deletions ci/do_ci.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$ErrorActionPreference = "Stop";
trap { $host.SetShouldExit(1) }

. "$PSScriptRoot\build_setup.ps1"
Write-Host "building using $env:NUM_CPUS CPUs"

function bazel_debug_binary_build() {
echo "Building..."
pushd "$env:ENVOY_SRCDIR"
bazel $env:BAZEL_BASE_OPTIONS.Split(" ") build $env:BAZEL_BUILD_OPTIONS.Split(" ") -c dbg "//source/exe:envoy-static"
$exit = $LASTEXITCODE
if ($exit -ne 0) {
popd
exit $exit
}
popd
}

echo "bazel debug build..."
bazel_debug_binary_build
40 changes: 33 additions & 7 deletions ci/prebuilt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ licenses(["notice"]) # Apache 2

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

config_setting(
name = "windows_x86_64",
values = {"cpu": "x64_windows"},
)

cc_library(
name = "ares",
srcs = ["thirdparty_build/lib/libcares.a"],
srcs = select({
":windows_x86_64": ["thirdparty_build/lib/cares.lib"],
"//conditions:default": ["thirdparty_build/lib/libcares.a"],
}),
hdrs = glob(["thirdparty_build/include/ares*.h"]),
includes = ["thirdparty_build/include"],
)

cc_library(
name = "benchmark",
srcs = ["thirdparty_build/lib/libbenchmark.a"],
srcs = select({
":windows_x86_64": ["thirdparty_build/lib/benchmark.lib"],
"//conditions:default": ["thirdparty_build/lib/libbenchmark.a"],
}),
hdrs = ["thirdparty_build/include/testing/base/public/benchmark.h"],
includes = ["thirdparty_build/include"],
)

cc_library(
name = "event",
srcs = ["thirdparty_build/lib/libevent.a"],
srcs = select({
":windows_x86_64": ["thirdparty_build/lib/event.lib"],
"//conditions:default": ["thirdparty_build/lib/libevent.a"],
}),
hdrs = glob(["thirdparty_build/include/event2/**/*.h"]),
includes = ["thirdparty_build/include"],
)
Expand All @@ -31,7 +45,10 @@ cc_library(

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

cc_library(
name = "nghttp2",
srcs = ["thirdparty_build/lib/libnghttp2.a"],
srcs = select({
":windows_x86_64": ["thirdparty_build/lib/nghttp2.lib"],
"//conditions:default": ["thirdparty_build/lib/libnghttp2.a"],
}),
hdrs = glob(["thirdparty_build/include/nghttp2/**/*.h"]),
includes = ["thirdparty_build/include"],
)
Expand All @@ -54,14 +74,20 @@ cc_library(

cc_library(
name = "yaml_cpp",
srcs = ["thirdparty_build/lib/libyaml-cpp.a"],
srcs = select({
":windows_x86_64": glob(["thirdparty_build/lib/libyaml-cpp*.lib"]),
"//conditions:default": ["thirdparty_build/lib/libyaml-cpp.a"],
}),
hdrs = glob(["thirdparty_build/include/yaml-cpp/**/*.h"]),
includes = ["thirdparty_build/include"],
)

cc_library(
name = "zlib",
srcs = ["thirdparty_build/lib/libz.a"],
srcs = select({
":windows_x86_64": glob(["thirdparty_build/lib/zlibstaticd.lib"]),
"//conditions:default": ["thirdparty_build/lib/libz.a"],
}),
hdrs = [
"thirdparty_build/include/zconf.h",
"thirdparty_build/include/zlib.h",
Expand Down
10 changes: 2 additions & 8 deletions source/common/event/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ envoy_cc_library(
name = "libevent_lib",
srcs = ["libevent.cc"],
hdrs = ["libevent.h"],
external_deps = [
"event",
"event_pthreads",
],
libevent_dep = 1,
deps = [
"//source/common/common:assert_lib",
"//source/common/common:c_smart_ptr_lib",
Expand All @@ -71,10 +68,7 @@ envoy_cc_library(
name = "dispatched_thread_lib",
srcs = ["dispatched_thread.cc"],
hdrs = ["dispatched_thread.h"],
external_deps = [
"event",
"event_pthreads",
],
libevent_dep = 1,
deps = [
":dispatcher_lib",
"//include/envoy/event:dispatcher_interface",
Expand Down
2 changes: 1 addition & 1 deletion source/common/filesystem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ envoy_cc_library(
"inotify/watcher_impl.h",
],
}),
external_deps = ["event"],
libevent_dep = 1,
strip_include_prefix = select({
"@bazel_tools//tools/osx:darwin": "kqueue",
"//conditions:default": "inotify",
Expand Down
Loading