diff options
Diffstat (limited to 'pw_build/bazel_internal/pigweed_internal.bzl')
-rw-r--r-- | pw_build/bazel_internal/pigweed_internal.bzl | 180 |
1 files changed, 71 insertions, 109 deletions
diff --git a/pw_build/bazel_internal/pigweed_internal.bzl b/pw_build/bazel_internal/pigweed_internal.bzl index ed452ffe8..b8b4afb67 100644 --- a/pw_build/bazel_internal/pigweed_internal.bzl +++ b/pw_build/bazel_internal/pigweed_internal.bzl @@ -15,130 +15,92 @@ # the License. """ An internal set of tools for creating embedded CC targets. """ -load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") -load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME") - -DEBUGGING = [ - "-g", -] - -# Standard compiler flags to reduce output binary size. -REDUCED_SIZE_COPTS = [ - "-fno-common", - "-fno-exceptions", - "-ffunction-sections", - "-fdata-sections", -] - -STRICT_WARNINGS_COPTS = [ - "-Wall", - "-Wextra", - # Make all warnings errors, except for the exemptions below. - "-Werror", - "-Wno-error=cpp", # preprocessor #warning statement - "-Wno-error=deprecated-declarations", # [[deprecated]] attribute -] - -DISABLE_PENDING_WORKAROUND_COPTS = [ - "-Wno-private-header", -] - -PW_DEFAULT_COPTS = ( - DEBUGGING + - REDUCED_SIZE_COPTS + - STRICT_WARNINGS_COPTS + - DISABLE_PENDING_WORKAROUND_COPTS +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") + +def _print_platform_impl(_, ctx): + if hasattr(ctx.rule.attr, "constraint_values"): + for cv in ctx.rule.attr.constraint_values: + # buildifier: disable=print + print(str(ctx.rule.attr.name) + " specifies " + str(cv)) + return [] + +print_platform = aspect( + implementation = _print_platform_impl, + attr_aspects = ["parents"], + doc = """ + This is a little debug utility that traverses the platform inheritance + hierarchy and prints all the constraint values. + + Example usage: + + bazel build \ + //pw_build/platforms:lm3s6965evb \ + --aspects \ + pw_build/bazel_internal/pigweed_internal.bzl%print_platform + """, ) -KYTHE_COPTS = [ - "-Wno-unknown-warning-option", -] - -def add_defaults(kwargs): - """Adds default arguments suitable for both C and C++ code to kwargs. +def compile_cc( + ctx, + srcs, + hdrs, + deps, + includes = [], + defines = [], + user_compile_flags = []): + """Compiles a list C++ source files. Args: - kwargs: cc_* arguments to be modified. + ctx: Rule context + srcs: List of C/C++ source files to compile + hdrs: List of C/C++ header files to compile with + deps: Dependencies to link with + includes: List of include paths + defines: List of preprocessor defines to use + user_compile_flags: Extra compiler flags to pass when compiling. + + Returns: + A CcInfo provider. """ - - copts = PW_DEFAULT_COPTS + kwargs.get("copts", []) - kwargs["copts"] = select({ - "@pigweed//pw_build:kythe": copts + KYTHE_COPTS, - "//conditions:default": copts, - }) - - # Set linkstatic to avoid building .so files. - kwargs["linkstatic"] = True - - kwargs.setdefault("features", []) - - # Crosstool--adding this line to features disables header modules, which - # don't work with -fno-rtti. Note: this is not a command-line argument, - # it's "minus use_header_modules". - kwargs["features"].append("-use_header_modules") - -def _preprocess_linker_script_impl(ctx): cc_toolchain = find_cpp_toolchain(ctx) - output_script = ctx.actions.declare_file(ctx.label.name + ".ld") feature_configuration = cc_common.configure_features( ctx = ctx, cc_toolchain = cc_toolchain, requested_features = ctx.features, unsupported_features = ctx.disabled_features, ) - cxx_compiler_path = cc_common.get_tool_for_action( - feature_configuration = feature_configuration, - action_name = C_COMPILE_ACTION_NAME, - ) - c_compile_variables = cc_common.create_compile_variables( + + compilation_contexts = [dep[CcInfo].compilation_context for dep in deps] + compilation_context, compilation_outputs = cc_common.compile( + name = ctx.label.name, + actions = ctx.actions, feature_configuration = feature_configuration, cc_toolchain = cc_toolchain, - user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts, - ) - action_flags = cc_common.get_memory_inefficient_command_line( - feature_configuration = feature_configuration, - action_name = C_COMPILE_ACTION_NAME, - variables = c_compile_variables, + srcs = srcs, + includes = includes, + defines = defines, + public_hdrs = hdrs, + user_compile_flags = user_compile_flags, + compilation_contexts = compilation_contexts, ) - env = cc_common.get_environment_variables( + + linking_contexts = [dep[CcInfo].linking_context for dep in deps] + linking_context, _ = cc_common.create_linking_context_from_compilation_outputs( + actions = ctx.actions, feature_configuration = feature_configuration, - action_name = C_COMPILE_ACTION_NAME, - variables = c_compile_variables, - ) - ctx.actions.run( - outputs = [output_script], - inputs = depset( - [ctx.file.linker_script], - transitive = [cc_toolchain.all_files], - ), - executable = cxx_compiler_path, - arguments = [ - "-E", - "-P", - "-xc", - ctx.file.linker_script.short_path, - "-o", - output_script.path, - ] + [ - "-D" + d - for d in ctx.attr.defines - ] + action_flags + ctx.attr.copts, - env = env, + cc_toolchain = cc_toolchain, + compilation_outputs = compilation_outputs, + linking_contexts = linking_contexts, + disallow_dynamic_library = True, + name = ctx.label.name, ) - return [DefaultInfo(files = depset([output_script]))] -pw_linker_script = rule( - _preprocess_linker_script_impl, - attrs = { - "copts": attr.string_list(doc = "C compile options."), - "defines": attr.string_list(doc = "C preprocessor defines."), - "linker_script": attr.label( - mandatory = True, - allow_single_file = True, - doc = "Linker script to preprocess.", - ), - "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), - }, - toolchains = use_cpp_toolchain(), - fragments = ["cpp"], -) + transitive_output_files = [dep[DefaultInfo].files for dep in deps] + output_files = depset( + compilation_outputs.pic_objects + compilation_outputs.objects, + transitive = transitive_output_files, + ) + return [DefaultInfo(files = output_files), CcInfo( + compilation_context = compilation_context, + linking_context = linking_context, + )] |