<commit_before>def envoy_package():
    native.package(default_visibility = ["//visibility:public"])

# 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++0x",
    ] + 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"],
    }) + select({
        repository + "//bazel:disable_tcmalloc": [],
        "//conditions:default": ["-DTCMALLOC"],
    }) + select({
        repository + "//bazel:disable_signal_trace": [],
        "//conditions:default": ["-DENVOY_HANDLE_SIGNALS"],
    }) + select({
        # TCLAP command line parser needs this to support int64_t/uint64_t
        "@bazel_tools//tools/osx:darwin": ["-DHAVE_LONG_LONG"],
        "//conditions:default": [],
    }) + envoy_select_hot_restart(["-DENVOY_HOT_RESTART"], repository)

# Compute the final linkopts based on various options.
def envoy_linkopts():
    return select({
        # OSX provides system and stdc++ libraries dynamically, so they can't be linked statically.
        # Further, the system library transitively links common libraries (e.g., pthread).
        # TODO(zuercher): build id could be supported via "-sectcreate __TEXT __build_id <file>"
        # The file could should contain the current git SHA (or enough placeholder data to allow
        # it to be rewritten by tools/git_sha_rewriter.py).
        "@bazel_tools//tools/osx:darwin": [
            # TODO(zuercher): should be able to remove this after the next gperftools release after
            # 2.6.1 (see discussion at https://github.com/gperftools/gperftools/issues/901)
            "-Wl,-U,___lsan_ignore_object",
            # See note here: http://luajit.org/install.html
            "-pagezero_size 10000", "-image_base 100000000",
        ],
        "//conditions:default": [
            "-pthread",
            "-lrt",
            "-ldl",
            # Force MD5 hash in build. This is part of the workaround for
            # https://github.com/bazelbuild/bazel/issues/2805. Bazel actually
            # does this by itself prior to
            # https://github.com/bazelbuild/bazel/commit/724706ba4836c3366fc85b40ed50ccf92f4c3882.
            # Ironically, forcing it here so that in future releases we will
            # have the same behavior. When everyone is using an updated version
            # of Bazel, we can use linkopts to set the git SHA1 directly in the
            # --build-id and avoid doing the following.
            '-Wl,--build-id=md5',
            '-Wl,--hash-style=gnu',
            "-static-libstdc++",
            "-static-libgcc",
        ],
    })

# Compute the test linkopts based on various options.
def envoy_test_linkopts():
    return select({
        "@bazel_tools//tools/osx:darwin": [
            # TODO(zuercher): should be able to remove this after the next gperftools release after
            # 2.6.1 (see discussion at https://github.com/gperftools/gperftools/issues/901)
            "-Wl,-U,___lsan_ignore_object",
            # See note here: http://luajit.org/install.html
            "-pagezero_size 10000", "-image_base 100000000",
        ],

        # TODO(mattklein123): It's not great that we universally link against the following libs.
        # In particular, -latomic and -lrt are not needed on all platforms. Make this more granular.
        "//conditions:default": ["-pthread", "-latomic", "-lrt", "-ldl"],
    })

# References to Envoy external dependencies should be wrapped with this function.
def envoy_external_dep_path(dep):
    return "//external:%s" % dep

# Dependencies on tcmalloc_and_profiler should be wrapped with this function.
def tcmalloc_external_dep(repository):
    return select({
        repository + "//bazel:disable_tcmalloc": None,
        "//conditions:default": envoy_external_dep_path("tcmalloc_and_profiler"),
    })

# As above, but wrapped in list form for adding to dep lists. This smell seems needed as
# SelectorValue values have to match the attribute type. See
# https://github.com/bazelbuild/bazel/issues/2273.
def tcmalloc_external_deps(repository):
    return select({
        repository + "//bazel:disable_tcmalloc": [],
        "//conditions:default": [envoy_external_dep_path("tcmalloc_and_profiler")],
    })

# 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").
def envoy_include_prefix(path):
    if path.startswith('source/') or path.startswith('include/'):
        return '/'.join(path.split('/')[1:])
    return None

# Envoy C++ library targets that need no transformations or additional dependencies before being
# passed to cc_library should be specified with this function. Note: this exists to ensure that
# all envoy targets pass through an envoy-declared skylark function where they can be modified
# before being passed to a native bazel function.
def envoy_basic_cc_library(name, **kargs):
    native.cc_library(name = name, **kargs)

# Envoy C++ library targets should be specified with this function.
def envoy_cc_library(name,
                     srcs = [],
                     hdrs = [],
                     copts = [],
                     visibility = None,
                     external_deps = [],
                     tcmalloc_dep = None,
                     repository = "",
                     linkstamp = None,
                     tags = [],
                     deps = [],
                     strip_include_prefix = None):
    if tcmalloc_dep:
        deps += tcmalloc_external_deps(repository)
    native.cc_library(
        name = name,
        srcs = srcs,
        hdrs = hdrs,
        copts = envoy_copts(repository) + copts,
        visibility = visibility,
        tags = tags,
        deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [
            repository + "//include/envoy/common:base_includes",
            envoy_external_dep_path('spdlog'),
            envoy_external_dep_path('fmtlib'),
        ],
        include_prefix = envoy_include_prefix(PACKAGE_NAME),
        alwayslink = 1,
        linkstatic = 1,
        linkstamp = linkstamp,
        strip_include_prefix = strip_include_prefix,
   )

def _git_stamped_genrule(repository, name):
    # To workaround https://github.com/bazelbuild/bazel/issues/2805, we
    # do binary rewriting to replace the linker produced MD5 hash with the
    # version_generated.cc git SHA1 hash (truncated).
    rewriter = repository + "//tools:git_sha_rewriter.py"
    native.genrule(
        name = name + "_stamped",
        srcs = [name],
        outs = [name + ".stamped"],
        cmd = "cp $(location " + name + ") $@ && " +
              "chmod u+w $@ && " +
              "$(location " + rewriter + ") $@",
        tools = [rewriter],
    )

# Envoy C++ binary targets should be specified with this function.
def envoy_cc_binary(name,
                    srcs = [],
                    data = [],
                    testonly = 0,
                    visibility = None,
                    repository = "",
                    stamped = False,
                    deps = []):
    # Implicit .stamped targets to obtain builds with the (truncated) git SHA1.
    if stamped:
        _git_stamped_genrule(repository, name)
        _git_stamped_genrule(repository, name + ".stripped")
    native.cc_binary(
        name = name,
        srcs = srcs,
        data = data,
        copts = envoy_copts(repository),
        linkopts = envoy_linkopts(),
        testonly = testonly,
        linkstatic = 1,
        visibility = visibility,
        malloc = tcmalloc_external_dep(repository),
        # See above comment on MD5 hash, this is another "force MD5 stamps" to make sure our
        # rewriting is robust.
        stamp = 1,<commit_msg>Allow custom link options for envoy_cc_binary (#1996)<commit_after>def envoy_package():
    native.package(default_visibility = ["//visibility:public"])

# 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++0x",
    ] + 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"],
    }) + select({
        repository + "//bazel:disable_tcmalloc": [],
        "//conditions:default": ["-DTCMALLOC"],
    }) + select({
        repository + "//bazel:disable_signal_trace": [],
        "//conditions:default": ["-DENVOY_HANDLE_SIGNALS"],
    }) + select({
        # TCLAP command line parser needs this to support int64_t/uint64_t
        "@bazel_tools//tools/osx:darwin": ["-DHAVE_LONG_LONG"],
        "//conditions:default": [],
    }) + envoy_select_hot_restart(["-DENVOY_HOT_RESTART"], repository)

# Compute the final linkopts based on various options.
def envoy_linkopts():
    return select({
        # OSX provides system and stdc++ libraries dynamically, so they can't be linked statically.
        # Further, the system library transitively links common libraries (e.g., pthread).
        # TODO(zuercher): build id could be supported via "-sectcreate __TEXT __build_id <file>"
        # The file could should contain the current git SHA (or enough placeholder data to allow
        # it to be rewritten by tools/git_sha_rewriter.py).
        "@bazel_tools//tools/osx:darwin": [
            # TODO(zuercher): should be able to remove this after the next gperftools release after
            # 2.6.1 (see discussion at https://github.com/gperftools/gperftools/issues/901)
            "-Wl,-U,___lsan_ignore_object",
            # See note here: http://luajit.org/install.html
            "-pagezero_size 10000", "-image_base 100000000",
        ],
        "//conditions:default": [
            "-pthread",
            "-lrt",
            "-ldl",
            # Force MD5 hash in build. This is part of the workaround for
            # https://github.com/bazelbuild/bazel/issues/2805. Bazel actually
            # does this by itself prior to
            # https://github.com/bazelbuild/bazel/commit/724706ba4836c3366fc85b40ed50ccf92f4c3882.
            # Ironically, forcing it here so that in future releases we will
            # have the same behavior. When everyone is using an updated version
            # of Bazel, we can use linkopts to set the git SHA1 directly in the
            # --build-id and avoid doing the following.
            '-Wl,--build-id=md5',
            '-Wl,--hash-style=gnu',
            "-static-libstdc++",
            "-static-libgcc",
        ],
    })

# Compute the test linkopts based on various options.
def envoy_test_linkopts():
    return select({
        "@bazel_tools//tools/osx:darwin": [
            # TODO(zuercher): should be able to remove this after the next gperftools release after
            # 2.6.1 (see discussion at https://github.com/gperftools/gperftools/issues/901)
            "-Wl,-U,___lsan_ignore_object",
            # See note here: http://luajit.org/install.html
            "-pagezero_size 10000", "-image_base 100000000",
        ],

        # TODO(mattklein123): It's not great that we universally link against the following libs.
        # In particular, -latomic and -lrt are not needed on all platforms. Make this more granular.
        "//conditions:default": ["-pthread", "-latomic", "-lrt", "-ldl"],
    })

# References to Envoy external dependencies should be wrapped with this function.
def envoy_external_dep_path(dep):
    return "//external:%s" % dep

# Dependencies on tcmalloc_and_profiler should be wrapped with this function.
def tcmalloc_external_dep(repository):
    return select({
        repository + "//bazel:disable_tcmalloc": None,
        "//conditions:default": envoy_external_dep_path("tcmalloc_and_profiler"),
    })

# As above, but wrapped in list form for adding to dep lists. This smell seems needed as
# SelectorValue values have to match the attribute type. See
# https://github.com/bazelbuild/bazel/issues/2273.
def tcmalloc_external_deps(repository):
    return select({
        repository + "//bazel:disable_tcmalloc": [],
        "//conditions:default": [envoy_external_dep_path("tcmalloc_and_profiler")],
    })

# 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").
def envoy_include_prefix(path):
    if path.startswith('source/') or path.startswith('include/'):
        return '/'.join(path.split('/')[1:])
    return None

# Envoy C++ library targets that need no transformations or additional dependencies before being
# passed to cc_library should be specified with this function. Note: this exists to ensure that
# all envoy targets pass through an envoy-declared skylark function where they can be modified
# before being passed to a native bazel function.
def envoy_basic_cc_library(name, **kargs):
    native.cc_library(name = name, **kargs)

# Envoy C++ library targets should be specified with this function.
def envoy_cc_library(name,
                     srcs = [],
                     hdrs = [],
                     copts = [],
                     visibility = None,
                     external_deps = [],
                     tcmalloc_dep = None,
                     repository = "",
                     linkstamp = None,
                     tags = [],
                     deps = [],
                     strip_include_prefix = None):
    if tcmalloc_dep:
        deps += tcmalloc_external_deps(repository)
    native.cc_library(
        name = name,
        srcs = srcs,
        hdrs = hdrs,
        copts = envoy_copts(repository) + copts,
        visibility = visibility,
        tags = tags,
        deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [
            repository + "//include/envoy/common:base_includes",
            envoy_external_dep_path('spdlog'),
            envoy_external_dep_path('fmtlib'),
        ],
        include_prefix = envoy_include_prefix(PACKAGE_NAME),
        alwayslink = 1,
        linkstatic = 1,
        linkstamp = linkstamp,
        strip_include_prefix = strip_include_prefix,
   )

def _git_stamped_genrule(repository, name):
    # To workaround https://github.com/bazelbuild/bazel/issues/2805, we
    # do binary rewriting to replace the linker produced MD5 hash with the
    # version_generated.cc git SHA1 hash (truncated).
    rewriter = repository + "//tools:git_sha_rewriter.py"
    native.genrule(
        name = name + "_stamped",
        srcs = [name],
        outs = [name + ".stamped"],
        cmd = "cp $(location " + name + ") $@ && " +
              "chmod u+w $@ && " +
              "$(location " + rewriter + ") $@",
        tools = [rewriter],
    )

# Envoy C++ binary targets should be specified with this function.
def envoy_cc_binary(name,
                    srcs = [],
                    data = [],
                    testonly = 0,
                    visibility = None,
                    repository = "",
                    stamped = False,
                    deps = [],
                    linkopts = envoy_linkopts()):
    # Implicit .stamped targets to obtain builds with the (truncated) git SHA1.
    if stamped:
        _git_stamped_genrule(repository, name)
        _git_stamped_genrule(repository, name + ".stripped")
    native.cc_binary(
        name = name,
        srcs = srcs,
        data = data,
        copts = envoy_copts(repository),
        linkopts = linkopts,
        testonly = testonly,
        linkstatic = 1,
        visibility = visibility,
        malloc = tcmalloc_external_dep(repository),
        # See above comment on MD5 hash, this is another "force MD5 stamps" to make sure our
        # rewriting is robust.
        stamp = 1,
{'fraction_non_alphanumeric': 0.1110975832470932, 'fraction_numerical': 0.012783831496925793, 'mean_word_length': 2.288230584467574, 'pattern_counts': {'":': 34, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6059882', 'n_tokens_mistral': 4941, 'n_tokens_neox': 4696, 'n_words': 1333}