diff options
Diffstat (limited to 'tools/cpp/cc_configure.bzl')
-rw-r--r-- | tools/cpp/cc_configure.bzl | 193 |
1 files changed, 118 insertions, 75 deletions
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 4e77db5e6b..7754c8376d 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -13,6 +13,7 @@ # limitations under the License. """Rules for configuring the C++ toolchain (experimental).""" +load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") def _get_value(it): """Convert `it` in serialized protobuf format.""" @@ -110,7 +111,7 @@ def _execute(repository_ctx, command, environment = None, return stripped_stdout -def _get_tool_paths(repository_ctx, darwin, cc): +def _get_tool_paths(repository_ctx, cc): """Compute the path to the various tools.""" return {k: _which(repository_ctx, k, "/usr/bin/" + k) for k in [ @@ -124,8 +125,7 @@ def _get_tool_paths(repository_ctx, darwin, cc): "strip", ]} + { "gcc": cc, - "ar": "/usr/bin/libtool" - if darwin else _which(repository_ctx, "ar", "/usr/bin/ar") + "ar": _which(repository_ctx, "ar", "/usr/bin/ar") } @@ -220,7 +220,7 @@ def _is_gold_supported(repository_ctx, cc): ]) return result.return_code == 0 -def _crosstool_content(repository_ctx, cc, cpu_value, darwin): +def _crosstool_content(repository_ctx, cc, cpu_value): """Return the content for the CROSSTOOL file, in a dictionary.""" supports_gold_linker = _is_gold_supported(repository_ctx, cc) return { @@ -236,7 +236,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin): "supports_interface_shared_objects": False, "supports_normalizing_ar": False, "supports_start_end_lib": supports_gold_linker, - "target_libc": "macosx" if darwin else _get_env_var(repository_ctx, "BAZEL_TARGET_LIBC", "local", False), + "target_libc": _get_env_var(repository_ctx, "BAZEL_TARGET_LIBC", "local", False), "target_cpu": _get_env_var(repository_ctx, "BAZEL_TARGET_CPU", cpu_value, False), "target_system_name": _get_env_var(repository_ctx, "BAZEL_TARGET_SYSTEM", "local", False), "cxx_flag": [ @@ -252,24 +252,17 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin): repository_ctx, cc, "-Wl,-no-as-needed" ) + _add_option_if_supported( repository_ctx, cc, "-Wl,-z,relro,-z,now" - ) + ( - [ - "-undefined", - "dynamic_lookup", - "-headerpad_max_install_names", - ] if darwin else [ - "-B" + str(repository_ctx.path(cc).dirname), - # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. - "-B/usr/bin", - # Gold linker only? Can we enable this by default? - # "-Wl,--warn-execstack", - # "-Wl,--detect-odr-violations" - ] + _add_option_if_supported( - # Have gcc return the exit code from ld. - repository_ctx, cc, "-pass-exit-codes" - ) - ), - "ar_flag": ["-static", "-s", "-o"] if darwin else [], + ) + (["-B" + str(repository_ctx.path(cc).dirname), + # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. + "-B/usr/bin", + # Gold linker only? Can we enable this by default? + # "-Wl,--warn-execstack", + # "-Wl,--detect-odr-violations" + ] + _add_option_if_supported( + # Have gcc return the exit code from ld. + repository_ctx, cc, "-pass-exit-codes") + ), + "ar_flag": [], "cxx_builtin_include_directory": _get_cxx_inc_directories(repository_ctx, cc), "objcopy_embed_flag": ["-I", "binary"], "unfiltered_cxx_flag": @@ -292,7 +285,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin): # All warnings are enabled. Maybe enable -Werror as well? "-Wall", # Enable a few more warnings that aren't part of -Wall. - ] + (["-Wthread-safety", "-Wself-assign"] if darwin else [ + ] + ([ "-B" + str(repository_ctx.path(cc).dirname), # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. "-B/usr/bin", @@ -350,7 +343,7 @@ def _get_windows_msys_crosstool_content(repository_ctx): ' tool_path { name: "objdump" path: "%susr/bin/objdump" }\n' % msys_root + ' tool_path { name: "strip" path: "%susr/bin/strip" }'% msys_root ) -def _opt_content(darwin): +def _opt_content(): """Return the content of the opt specific section of the CROSSTOOL file.""" return { "compiler_flag": [ @@ -376,7 +369,7 @@ def _opt_content(darwin): "-ffunction-sections", "-fdata-sections" ], - "linker_flag": [] if darwin else ["-Wl,--gc-sections"] + "linker_flag": ["-Wl,--gc-sections"] } @@ -628,23 +621,7 @@ def _get_env(repository_ctx): else: return "" -def _coverage_feature(darwin): - if darwin: - compile_flags = """flag_group { - flag: '-fprofile-instr-generate' - flag: '-fcoverage-mapping' - }""" - link_flags = """flag_group { - flag: '-fprofile-instr-generate' - }""" - else: - compile_flags = """flag_group { - flag: '-fprofile-arcs' - flag: '-ftest-coverage' - }""" - link_flags = """flag_group { - flag: '-lgcov' - }""" +def _coverage_feature(): return """ feature { name: 'coverage' @@ -656,17 +633,51 @@ def _coverage_feature(darwin): action: 'c++-header-parsing' action: 'c++-header-preprocessing' action: 'c++-module-compile' - """ + compile_flags + """ + flag_group { + flag: '-fprofile-arcs' + flag: '-ftest-coverage' + } } flag_set { action: 'c++-link-interface-dynamic-library' action: 'c++-link-dynamic-library' action: 'c++-link-executable' - """ + link_flags + """ + flag_group { + flag: '-lgcov' + } } } """ +def _get_darwin_cxx_inc_directories(repository_ctx, cc): + """Compute the list of default C++ include directories on darwin. + + Uses the xcode-locator tool to add all xcode developer directories to the + list of builtin include paths. + + This should only be invoked on a darwin OS, as xcode-locator cannot be built + otherwise. + + Args: + repository_ctx: The repository context. + cc: The default C++ compiler on the local system. + Returns: + A 2-tuple containing: + include_paths: A list of builtin include paths. + err: An error string describing the error that occurred when attempting + to build and run xcode-locator, or None if the run was successful. + """ + + (toolchains, xcodeloc_err) = run_xcode_locator(repository_ctx, + Label("@bazel_tools//tools/osx:xcode_locator.m")) + + # TODO(cparsons): Falling back to the default C++ compiler builtin include + # paths shouldn't be unnecessary once all actions are using xcrun. + include_dirs = _get_cxx_inc_directories(repository_ctx, cc) + for toolchain in toolchains: + include_dirs.append(toolchain.developer_dir) + return (include_dirs, xcodeloc_err) + def _impl(repository_ctx): repository_ctx.file("tools/cpp/empty.cc", "int main() {}") cpu_value = _get_cpu_value(repository_ctx) @@ -735,37 +746,69 @@ def _impl(repository_ctx): else: darwin = cpu_value == "darwin" cc = _find_cc(repository_ctx) - tool_paths = _get_tool_paths(repository_ctx, darwin, - "cc_wrapper.sh" if darwin else str(cc)) - crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value, darwin) - opt_content = _opt_content(darwin) - dbg_content = _dbg_content() - _tpl(repository_ctx, "BUILD", { - "%{name}": cpu_value, - "%{supports_param_files}": "0" if darwin else "1", - "%{cc_compiler_deps}": ":cc_wrapper" if darwin else ":empty", - "%{compiler}": _get_env_var(repository_ctx, "BAZEL_COMPILER", "compiler", False), - }) _tpl(repository_ctx, - "osx_cc_wrapper.sh" if darwin else "linux_cc_wrapper.sh", - {"%{cc}": str(cc), "%{env}": _get_env(repository_ctx)}, - "cc_wrapper.sh") - _tpl(repository_ctx, "CROSSTOOL", { - "%{cpu}": cpu_value, - "%{default_toolchain_name}": _get_env_var(repository_ctx, "CC_TOOLCHAIN_NAME", "local", False), - "%{toolchain_name}": _get_env_var(repository_ctx, "CC_TOOLCHAIN_NAME", "local", False), - "%{content}": _build_crosstool(crosstool_content) + "\n" + - _build_tool_path(tool_paths), - "%{opt_content}": _build_crosstool(opt_content, " "), - "%{dbg_content}": _build_crosstool(dbg_content, " "), - "%{cxx_builtin_include_directory}": "", - "%{coverage}": _coverage_feature(darwin), - "%{msvc_env_tmp}": "", - "%{msvc_env_path}": "", - "%{msvc_env_include}": "", - "%{msvc_env_lib}": "", - }) - + "osx_cc_wrapper.sh" if darwin else "linux_cc_wrapper.sh", + {"%{cc}": str(cc), "%{env}": _get_env(repository_ctx)}, + "cc_wrapper.sh") + if darwin: + repository_ctx.symlink( + Label("@bazel_tools//tools/objc:xcrunwrapper.sh"), "xcrunwrapper.sh") + repository_ctx.symlink( + Label("@bazel_tools//tools/objc:libtool.sh"), "libtool") + repository_ctx.symlink( + Label("@bazel_tools//tools/objc:make_hashed_objlist.py"), + "make_hashed_objlist.py") + repository_ctx.symlink( + Label("@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"), + "wrapped_clang") + repository_ctx.symlink( + Label("@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"), + "wrapped_clang_pp") + repository_ctx.symlink( + Label("@bazel_tools//tools/osx/crosstool:BUILD.tpl"), + "BUILD") + repository_ctx.symlink( + Label("@bazel_tools//tools/osx/crosstool:osx_archs.bzl"), + "osx_archs.bzl") + (include_paths, xcodeloc_err) = _get_darwin_cxx_inc_directories(repository_ctx, cc) + cxx_include_directories = [] + for path in include_paths: + cxx_include_directories.append(("cxx_builtin_include_directory: \"%s\"" % path)) + if xcodeloc_err: + cxx_include_directories.append("# Error: " + xcodeloc_err + "\n") + repository_ctx.template( + "CROSSTOOL", + Label("@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl"), + {"%{cxx_builtin_include_directory}": "\n".join(cxx_include_directories)}) + else: + tool_paths = _get_tool_paths(repository_ctx, str(cc)) + crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value) + opt_content = _opt_content() + dbg_content = _dbg_content() + _tpl(repository_ctx, "BUILD", { + "%{name}": cpu_value, + "%{supports_param_files}": "1", + "%{cc_compiler_deps}": ":empty", + "%{compiler}": _get_env_var(repository_ctx, "BAZEL_COMPILER", "compiler", False), + }) + _tpl(repository_ctx, "CROSSTOOL", { + "%{cpu}": cpu_value, + "%{default_toolchain_name}": _get_env_var(repository_ctx, + "CC_TOOLCHAIN_NAME", + "local", + False), + "%{toolchain_name}": _get_env_var(repository_ctx, "CC_TOOLCHAIN_NAME", "local", False), + "%{content}": _build_crosstool(crosstool_content) + "\n" + + _build_tool_path(tool_paths), + "%{opt_content}": _build_crosstool(opt_content, " "), + "%{dbg_content}": _build_crosstool(dbg_content, " "), + "%{cxx_builtin_include_directory}": "", + "%{coverage}": _coverage_feature(), + "%{msvc_env_tmp}": "", + "%{msvc_env_path}": "", + "%{msvc_env_include}": "", + "%{msvc_env_lib}": "", + }) cc_autoconf = repository_rule( implementation=_impl, |