...

Text file src/github.com/bazelbuild/rules_go/go/private/actions/compilepkg.bzl

Documentation: github.com/bazelbuild/rules_go/go/private/actions

     1# Copyright 2019 The Bazel Authors. All rights reserved.
     2#
     3# Licensed under the Apache License, Version 2.0 (the "License");
     4# you may not use this file except in compliance with the License.
     5# You may obtain a copy of the License at
     6#
     7#    http://www.apache.org/licenses/LICENSE-2.0
     8#
     9# Unless required by applicable law or agreed to in writing, software
    10# distributed under the License is distributed on an "AS IS" BASIS,
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12# See the License for the specific language governing permissions and
    13# limitations under the License.
    14
    15load("//go/private:common.bzl", "GO_TOOLCHAIN_LABEL")
    16load(
    17    "//go/private:mode.bzl",
    18    "link_mode_args",
    19)
    20load("//go/private/actions:utils.bzl", "quote_opts")
    21
    22def _archive(v):
    23    importpaths = [v.data.importpath]
    24    importpaths.extend(v.data.importpath_aliases)
    25    return "{}={}={}".format(
    26        ":".join(importpaths),
    27        v.data.importmap,
    28        v.data.export_file.path if v.data.export_file else v.data.file.path,
    29    )
    30
    31def _facts(v):
    32    facts_file = v.data.facts_file
    33    if not facts_file:
    34        return None
    35    importpaths = [v.data.importpath]
    36    importpaths.extend(v.data.importpath_aliases)
    37    return "{}={}={}".format(
    38        ":".join(importpaths),
    39        v.data.importmap,
    40        facts_file.path,
    41    )
    42
    43def _embedroot_arg(src):
    44    return src.root.path
    45
    46def _embedlookupdir_arg(src):
    47    root_relative = src.dirname[len(src.root.path):]
    48    if root_relative.startswith("/"):
    49        root_relative = root_relative[len("/"):]
    50    return root_relative
    51
    52def emit_compilepkg(
    53        go,
    54        sources = None,
    55        cover = None,
    56        embedsrcs = [],
    57        importpath = "",
    58        importmap = "",
    59        archives = [],
    60        cgo = False,
    61        cgo_inputs = depset(),
    62        cppopts = [],
    63        copts = [],
    64        cxxopts = [],
    65        objcopts = [],
    66        objcxxopts = [],
    67        clinkopts = [],
    68        out_lib = None,
    69        out_export = None,
    70        out_facts = None,
    71        nogo = None,
    72        out_cgo_export_h = None,
    73        gc_goopts = [],
    74        testfilter = None,  # TODO: remove when test action compiles packages
    75        recompile_internal_deps = []):
    76    """Compiles a complete Go package."""
    77    if sources == None:
    78        fail("sources is a required parameter")
    79    if out_lib == None:
    80        fail("out_lib is a required parameter")
    81    if bool(nogo) != bool(out_facts):
    82        fail("nogo must be specified if and only if out_facts is specified")
    83
    84    inputs = (sources + embedsrcs + [go.package_list] +
    85              [archive.data.export_file for archive in archives] +
    86              go.sdk.tools + go.sdk.headers + go.stdlib.libs)
    87    outputs = [out_lib, out_export]
    88    env = go.env
    89
    90    args = go.builder_args(go, "compilepkg")
    91    args.add_all(sources, before_each = "-src")
    92    args.add_all(embedsrcs, before_each = "-embedsrc", expand_directories = False)
    93    args.add_all(
    94        sources + [out_lib] + embedsrcs,
    95        map_each = _embedroot_arg,
    96        before_each = "-embedroot",
    97        uniquify = True,
    98        expand_directories = False,
    99    )
   100    args.add_all(
   101        sources + [out_lib],
   102        map_each = _embedlookupdir_arg,
   103        before_each = "-embedlookupdir",
   104        uniquify = True,
   105        expand_directories = False,
   106    )
   107    if cover and go.coverdata:
   108        inputs.append(go.coverdata.data.export_file)
   109        args.add("-arc", _archive(go.coverdata))
   110        if go.mode.race:
   111            args.add("-cover_mode", "atomic")
   112        else:
   113            args.add("-cover_mode", "set")
   114        args.add("-cover_format", go.cover_format)
   115        args.add_all(cover, before_each = "-cover")
   116    args.add_all(archives, before_each = "-arc", map_each = _archive)
   117    if recompile_internal_deps:
   118        args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps")
   119    if importpath:
   120        args.add("-importpath", importpath)
   121    else:
   122        args.add("-importpath", go.label.name)
   123    if importmap:
   124        args.add("-p", importmap)
   125    args.add("-package_list", go.package_list)
   126
   127    args.add("-lo", out_lib)
   128    args.add("-o", out_export)
   129    if nogo:
   130        args.add_all(archives, before_each = "-facts", map_each = _facts)
   131        inputs.extend([archive.data.facts_file for archive in archives if archive.data.facts_file])
   132        args.add("-out_facts", out_facts)
   133        outputs.append(out_facts)
   134        args.add("-nogo", nogo)
   135        inputs.append(nogo)
   136    if out_cgo_export_h:
   137        args.add("-cgoexport", out_cgo_export_h)
   138        outputs.append(out_cgo_export_h)
   139    if testfilter:
   140        args.add("-testfilter", testfilter)
   141
   142    gc_flags = list(gc_goopts)
   143    gc_flags.extend(go.mode.gc_goopts)
   144    asm_flags = []
   145    if go.mode.race:
   146        gc_flags.append("-race")
   147    if go.mode.msan:
   148        gc_flags.append("-msan")
   149    if go.mode.debug:
   150        gc_flags.extend(["-N", "-l"])
   151    gc_flags.extend(go.toolchain.flags.compile)
   152    gc_flags.extend(link_mode_args(go.mode))
   153    asm_flags.extend(link_mode_args(go.mode))
   154    args.add("-gcflags", quote_opts(gc_flags))
   155    args.add("-asmflags", quote_opts(asm_flags))
   156
   157    env = go.env
   158    if cgo:
   159        inputs.extend(cgo_inputs.to_list())  # OPT: don't expand depset
   160        inputs.extend(go.crosstool)
   161        env["CC"] = go.cgo_tools.c_compiler_path
   162        if cppopts:
   163            args.add("-cppflags", quote_opts(cppopts))
   164        if copts:
   165            args.add("-cflags", quote_opts(copts))
   166        if cxxopts:
   167            args.add("-cxxflags", quote_opts(cxxopts))
   168        if objcopts:
   169            args.add("-objcflags", quote_opts(objcopts))
   170        if objcxxopts:
   171            args.add("-objcxxflags", quote_opts(objcxxopts))
   172        if clinkopts:
   173            args.add("-ldflags", quote_opts(clinkopts))
   174
   175    if go.mode.pgoprofile:
   176        args.add("-pgoprofile", go.mode.pgoprofile)
   177        inputs.append(go.mode.pgoprofile)
   178
   179    go.actions.run(
   180        inputs = inputs,
   181        outputs = outputs,
   182        mnemonic = "GoCompilePkg",
   183        executable = go.toolchain._builder,
   184        arguments = [args],
   185        env = go.env,
   186        toolchain = GO_TOOLCHAIN_LABEL,
   187    )

View as plain text