From 2dc7679cc1b5caa9bb943d0994faa6e0267414f2 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 23 May 2022 20:59:31 +0000 Subject: [PATCH] Fixed the Windows build of protoc by statically linking deps. --- BUILD.bazel | 4 ++-- build_defs/BUILD.bazel | 14 ++++++++++++++ build_defs/cpp_opts.bzl | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 476371733..6017730f0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -6,7 +6,7 @@ load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") load("@rules_python//python:defs.bzl", "py_library") load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") -load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS") +load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS", "PROTOC_LINK_OPTS") load( ":protobuf.bzl", "adapt_proto_library", @@ -442,7 +442,7 @@ cc_library( cc_binary( name = "protoc", srcs = ["src/google/protobuf/compiler/main.cc"], - linkopts = LINK_OPTS, + linkopts = LINK_OPTS + PROTOC_LINK_OPTS, visibility = ["//visibility:public"], deps = [":protoc_lib"], ) diff --git a/build_defs/BUILD.bazel b/build_defs/BUILD.bazel index 29a1f1256..a5d831c9a 100644 --- a/build_defs/BUILD.bazel +++ b/build_defs/BUILD.bazel @@ -59,6 +59,20 @@ config_setting( }, ) +config_setting( + name = "config_win32", + values = { + "cpu": "win32", + }, +) + +config_setting( + name = "config_win64", + values = { + "cpu": "win64", + }, +) + # Internal testing: starlark_cc_proto_library( diff --git a/build_defs/cpp_opts.bzl b/build_defs/cpp_opts.bzl index f868c4ae5..1d5594d17 100644 --- a/build_defs/cpp_opts.bzl +++ b/build_defs/cpp_opts.bzl @@ -38,3 +38,10 @@ LINK_OPTS = select({ "-lm", ], }) + +# When cross-compiling for Windows we need to statically link pthread and the C++ library. +PROTOC_LINK_OPTS = select({ + "//build_defs:config_win32": ["-static"], + "//build_defs:config_win64": ["-static"], + "//conditions:default": [], +})