<commit_before># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
    "@io_bazel_rules_go//go/private:skylib/lib/sets.bzl",
    "sets",
)
load(
    "@io_bazel_rules_go//go/private:context.bzl",
    "go_context",
)
load(
    "@io_bazel_rules_go//go/private:common.bzl",
    "SHARED_LIB_EXTENSIONS",
    "as_iterable",
    "as_list",
    "as_set",
    "join_srcs",
    "pkg_dir",
    "split_srcs",
)
load(
    "@io_bazel_rules_go//go/private:providers.bzl",
    "GoLibrary",
)
load(
    "@io_bazel_rules_go//go/private:rules/rule.bzl",
    "go_rule",
)
load(
    "@io_bazel_rules_go//go/private:mode.bzl",
    "LINKMODE_C_ARCHIVE",
    "LINKMODE_C_SHARED",
    "extldflags_from_cc_toolchain",
    "mode_string",
    "new_mode",
)
load(
    "@io_bazel_rules_go//go/platform:list.bzl",
    "GOARCH",
    "GOOS",
    "GOOS_GOARCH",
    "MSAN_GOOS_GOARCH",
    "RACE_GOOS_GOARCH",
)
load(
    "@io_bazel_rules_go_compat//:compat.bzl",
    "CC_PROVIDER_NAME",
    "cc_compile_flags",
    "cc_defines",
    "cc_includes",
    "cc_libs",
    "cc_link_flags",
    "cc_quote_includes",
    "cc_system_includes",
    "cc_transitive_headers",
    "has_cc",
)

_CgoCodegen = provider()
_CgoInfo = provider()

# Maximum number of characters in stem of base name for mangled cgo files.
# Some file systems have fairly short limits (eCryptFS has a limit of 143),
# and this should be kept below those to accomodate number suffixes and
# extensions.
MAX_STEM_LENGTH = 130

def _mangle(src, stems):
    """_mangle returns a file stem and extension for a source file that will
    be passed to cgo. The stem will be unique among other sources in the same
    library. It will not contain any separators, so cgo's name mangling algorithm
    will be a no-op."""
    stem, _, ext = src.basename.rpartition(".")
    if len(stem) > MAX_STEM_LENGTH:
        stem = stem[:MAX_STEM_LENGTH]
    if stem in stems:
        for i in range(100):
            next_stem = "{}_{}".format(stem, i)
            if next_stem not in stems:
                break
        if next_stem in stems:
            fail("could not find unique mangled name for {}".format(src.path))
        stem = next_stem
    stems[stem] = True
    return stem, ext

_DEFAULT_PLATFORM_COPTS = select({
    "@io_bazel_rules_go//go/platform:darwin": [],
    "@io_bazel_rules_go//go/platform:windows_amd64": ["-mthreads"],
    "//conditions:default": ["-pthread"],
})

_DEFAULT_PLATFORM_LINKOPTS = select({
    "@io_bazel_rules_go//go/platform:android": ["-llog", "-ldl"],
    "@io_bazel_rules_go//go/platform:darwin": [],
    "@io_bazel_rules_go//go/platform:windows_amd64": ["-mthreads"],
    "//conditions:default": ["-pthread"],
})

def _c_filter_options(options, blacklist):
    return [
        opt
        for opt in options
        if not any([opt.startswith(prefix) for prefix in blacklist])
    ]

def _select_archive(files):
    """Selects a single archive from a list of files produced by a
    static cc_library.

    In some configurations, cc_library can produce multiple files, and the
    order isn't guaranteed, so we can't simply pick the first one.
    """

    # list of file extensions in descending order or preference.
    exts = [".pic.lo", ".lo", ".a"]
    for ext in exts:
        for f in as_iterable(files):
            if f.basename.endswith(ext):
                return f

def _select_archives(libs):
    """Selects a one per item in a cc_library, if needed.

    If no archive can be extracted from all the libraries, this will fail.
    """

    # list of file extensions in descending order or preference.
    outs = []
    for lib in libs:
        archive = _select_archive(lib.files)
        if archive:
            outs.append(archive)
    if not outs:
        fail("cc_library(s) did not produce any files")
    return outs

def _include_unique(opts, flag, include, seen):
    if include in seen:
        return
    seen[include] = True
    opts.extend([flag, include])

def _encode_cgo_mode(goos, goarch, race, msan):
    return "_".join((goos, goarch, race, msan))

def _cgo_codegen_impl(ctx):
    go = go_context(ctx)
    if not go.cgo_tools:
        fail("Go toolchain does not support cgo")
    linkopts = extldflags_from_cc_toolchain(go) + ctx.attr.linkopts
    cppopts = list(ctx.attr.cppopts)
    copts = go.cgo_tools.c_compile_options + ctx.attr.copts
    deps = depset([], order = "topological")
    cgo_export_h = go.declare_file(go, path = "_cgo_export.h")
    cgo_export_c = go.declare_file(go, path = "_cgo_export.c")
    cgo_main = go.declare_file(go, path = "_cgo_main.c")
    cgo_types = go.declare_file(go, path = "_cgo_gotypes.go")
    out_dir = cgo_main.dirname

    builder_args = go.builder_args(go, "cgo")  # interpreted by builder
    tool_args = go.tool_args(go)  # interpreted by cgo<commit_msg>cgo: don't require cc_library to produce files (#1956)<commit_after># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
    "@io_bazel_rules_go//go/private:skylib/lib/sets.bzl",
    "sets",
)
load(
    "@io_bazel_rules_go//go/private:context.bzl",
    "go_context",
)
load(
    "@io_bazel_rules_go//go/private:common.bzl",
    "SHARED_LIB_EXTENSIONS",
    "as_iterable",
    "as_list",
    "as_set",
    "join_srcs",
    "pkg_dir",
    "split_srcs",
)
load(
    "@io_bazel_rules_go//go/private:providers.bzl",
    "GoLibrary",
)
load(
    "@io_bazel_rules_go//go/private:rules/rule.bzl",
    "go_rule",
)
load(
    "@io_bazel_rules_go//go/private:mode.bzl",
    "LINKMODE_C_ARCHIVE",
    "LINKMODE_C_SHARED",
    "extldflags_from_cc_toolchain",
    "mode_string",
    "new_mode",
)
load(
    "@io_bazel_rules_go//go/platform:list.bzl",
    "GOARCH",
    "GOOS",
    "GOOS_GOARCH",
    "MSAN_GOOS_GOARCH",
    "RACE_GOOS_GOARCH",
)
load(
    "@io_bazel_rules_go_compat//:compat.bzl",
    "CC_PROVIDER_NAME",
    "cc_compile_flags",
    "cc_defines",
    "cc_includes",
    "cc_libs",
    "cc_link_flags",
    "cc_quote_includes",
    "cc_system_includes",
    "cc_transitive_headers",
    "has_cc",
)

_CgoCodegen = provider()
_CgoInfo = provider()

# Maximum number of characters in stem of base name for mangled cgo files.
# Some file systems have fairly short limits (eCryptFS has a limit of 143),
# and this should be kept below those to accomodate number suffixes and
# extensions.
MAX_STEM_LENGTH = 130

def _mangle(src, stems):
    """_mangle returns a file stem and extension for a source file that will
    be passed to cgo. The stem will be unique among other sources in the same
    library. It will not contain any separators, so cgo's name mangling algorithm
    will be a no-op."""
    stem, _, ext = src.basename.rpartition(".")
    if len(stem) > MAX_STEM_LENGTH:
        stem = stem[:MAX_STEM_LENGTH]
    if stem in stems:
        for i in range(100):
            next_stem = "{}_{}".format(stem, i)
            if next_stem not in stems:
                break
        if next_stem in stems:
            fail("could not find unique mangled name for {}".format(src.path))
        stem = next_stem
    stems[stem] = True
    return stem, ext

_DEFAULT_PLATFORM_COPTS = select({
    "@io_bazel_rules_go//go/platform:darwin": [],
    "@io_bazel_rules_go//go/platform:windows_amd64": ["-mthreads"],
    "//conditions:default": ["-pthread"],
})

_DEFAULT_PLATFORM_LINKOPTS = select({
    "@io_bazel_rules_go//go/platform:android": ["-llog", "-ldl"],
    "@io_bazel_rules_go//go/platform:darwin": [],
    "@io_bazel_rules_go//go/platform:windows_amd64": ["-mthreads"],
    "//conditions:default": ["-pthread"],
})

def _c_filter_options(options, blacklist):
    return [
        opt
        for opt in options
        if not any([opt.startswith(prefix) for prefix in blacklist])
    ]

def _select_archives(libs):
    """Selects static archives to pack from a list of cc_library targets.
    Returns at most one file per library.

    Each cc_library may produce several files which are logically the same
    static library. We prefer files with the extensions .pic.lo, .lo, or .a
    in that order. If a cc_library is empty, it may not produce any files,
    so _select_archives may return fewer archives than libs.
    """
    # list of file extensions in descending order or preference.
    exts = [".pic.lo", ".lo", ".a"]
    outs = []
    for lib in libs:
        out = None
        for ext in exts:
            for f in as_iterable(lib.files):
                if f.basename.endswith(ext):
                    out = f
                    break
            if out:
                break
        if out:
            outs.append(out)
    return outs

def _include_unique(opts, flag, include, seen):
    if include in seen:
        return
    seen[include] = True
    opts.extend([flag, include])

def _encode_cgo_mode(goos, goarch, race, msan):
    return "_".join((goos, goarch, race, msan))

def _cgo_codegen_impl(ctx):
    go = go_context(ctx)
    if not go.cgo_tools:
        fail("Go toolchain does not support cgo")
    linkopts = extldflags_from_cc_toolchain(go) + ctx.attr.linkopts
    cppopts = list(ctx.attr.cppopts)
    copts = go.cgo_tools.c_compile_options + ctx.attr.copts
    deps = depset([], order = "topological")
    cgo_export_h = go.declare_file(go, path = "_cgo_export.h")
    cgo_export_c = go.declare_file(go, path = "_cgo_export.c")
    cgo_main = go.declare_file(go, path = "_cgo_main.c")
    cgo_types = go.declare_file(go, path = "_cgo_gotypes.go")
    out_dir = cgo_main.dirname

    builder_args = go.builder_args(go, "cgo")  # interpreted by builder
    tool_args = go.tool_args(go)  # interpreted by cgo
{'fraction_non_alphanumeric': 0.11516801201426695, 'fraction_numerical': 0.004317627182278956, 'mean_word_length': 3.1330488750969745, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3273684', 'n_tokens_mistral': 3723, 'n_tokens_neox': 3602, 'n_words': 991}