Buck: cgo_library()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

cgo_library()

This is liable to change in the future.

A cgo_library() rule builds an object from the supplied set of Go/C source files and dependencies. The outputs are linked into go executable in the last step (compile). The 'go build' command would collect the cgo directives from the source files, however with buck the flags needs to be passed in the cgo_library manually This rule borrows from cxx_binary since C/C++ sources are being compiled.

Arguments

  • name (required) #

    The short name for this build target.

  • package_name (defaults to go.prefix + path relative to the buck root) #

    Sets the full name of the package being compiled. This defaults to the path from the buck root. (e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b")

  • srcs (defaults to []) #

    The set of source files to be compiled by this rule. .go files will be compiled with the CGO compiler. Each file needs to have import "C" declared.

  • go_srcs (required) #

    The set of source files to be compiled by this rule. Go (.go) files are compiled with the Go compiler. In contrast to the srcs argument, these files cannot have import "C" declared.

  • headers (defaults to []) #

    The set of header files that are made available for inclusion to the source files in this target. These should be specified as either a list of header files or a dictionary of header names to header files. The header name can contain forward slashes (/). The headers can be included with #include "$HEADER_NAMESPACE/$HEADER_NAME" or #include <$HEADER_NAMESPACE/$HEADER_NAME>, where $HEADER_NAMESPACE is the value of the target's header_namespace attribute, and $HEADER_NAME is the header name if specified, and the filename of the header file otherwise. See header_namespace for more information.

  • preprocessor_flags (defaults to []) #

    Flags to use when preprocessing any of the above sources (which require preprocessing).

  • platform_preprocessor_flags (defaults to []) #

    Platform specific preprocessor flags. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of flags to use when preprocessing the target's sources. See preprocessor_flags for more information.

  • cgo_compiler_flags_arg (defaults to []) #

    The set of additional compiler flags to pass to go tool cgo.

  • compiler_flags (defaults to []) #

    Flags to use when compiling any of the above sources (which require compilation).

  • platform_compiler_flags (defaults to []) #

    Platform specific compiler flags. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of flags to use when compiling the target's sources. See compiler_flags for more information.

  • linker_extra_outputs (defaults to []) #

    Declares extra outputs that the linker emits. These identifiers can be used in $(output ...) macros in linker_flags to interpolate the output path into the linker command line. Useful for custom linkers that emit extra output files.

  • linker_flags (defaults to []) #

    Flags to add to the linker command line whenever the output from this rule is used in a link operation, such as linked into an executable or a shared library.

  • platform_linker_flags (defaults to []) #

    Platform-specific linker flags. This argument is specified as a list of pairs where the first element in each pair is an un-anchored regex against which the platform name is matched. The regex should use java.util.regex.Pattern syntax. The second element in each pair is a list of linker flags. If the regex matches the platform, these flags are added to the linker command line when the output from this rule is used in a link operation.

  • raw_headers (defaults to []) #

    The set of header files that can be used for inclusion to the source files in the target and all targets that transitively depend on it. Buck doesn't add raw headers to the search path of a compiler/preprocessor automatically.include_directories and public_include_directories are the recommended way to add raw headers to the search path (they will be added via -I).compiler_flags, preprocessor_flags and exported_preprocessor_flagscan also be used to add such raw headers to the search path if inclusion via -isystem or-iquote is needed.raw_headers cannot be used together with headers or exported_headers in the same target.

  • go_compiler_flags (defaults to []) #

    The set of additional compiler flags to pass to go tool compile.

  • go_assembler_flags (defaults to []) #

    The set of additional assembler flags to pass to go tool asm.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

  • licenses (defaults to []) #

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter().

Examples

# A rule that builds a Go native executable with linked cgo library based on
# C/C++ util library.
go_binary(
    name = "bin",
    srcs = ["main.go"],
    deps = [":lib"]
)

cgo_library(
    name = "lib",
    srcs = ["cgo_source.go"],
    deps = [":util"],
)

cxx_library(
    name = "util",
    srcs = ["util.c"],
    headers = ["util.h"],
)