@@ -120,7 +120,7 @@ def _execute(repository_ctx, command, environment = None,
120120 return stripped_stdout
121121
122122
123- def _get_tool_paths (repository_ctx , cc ):
123+ def _get_tool_paths (repository_ctx , darwin , cc ):
124124 """Compute the path to the various tools. Doesn't %-escape the result!"""
125125 return {k : _which (repository_ctx , k , "/usr/bin/" + k )
126126 for k in [
@@ -134,7 +134,8 @@ def _get_tool_paths(repository_ctx, cc):
134134 "strip" ,
135135 ]} + {
136136 "gcc" : cc ,
137- "ar" : _which (repository_ctx , "ar" , "/usr/bin/ar" )
137+ "ar" : "/usr/bin/libtool"
138+ if darwin else _which (repository_ctx , "ar" , "/usr/bin/ar" )
138139 }
139140
140141
@@ -234,7 +235,7 @@ def _is_gold_supported(repository_ctx, cc):
234235 return result .return_code == 0
235236
236237
237- def _crosstool_content (repository_ctx , cc , cpu_value ):
238+ def _crosstool_content (repository_ctx , cc , cpu_value , darwin ):
238239 """Return the content for the CROSSTOOL file, in a dictionary."""
239240 supports_gold_linker = _is_gold_supported (repository_ctx , cc )
240241 return {
@@ -250,7 +251,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value):
250251 "supports_interface_shared_objects" : False ,
251252 "supports_normalizing_ar" : False ,
252253 "supports_start_end_lib" : supports_gold_linker ,
253- "target_libc" : _escape_string (_get_env_var (repository_ctx , "BAZEL_TARGET_LIBC" , "local" , False )),
254+ "target_libc" : "macosx" if darwin else _escape_string (_get_env_var (repository_ctx , "BAZEL_TARGET_LIBC" , "local" , False )),
254255 "target_cpu" : _escape_string (_get_env_var (repository_ctx , "BAZEL_TARGET_CPU" , cpu_value , False )),
255256 "target_system_name" : _escape_string (_get_env_var (repository_ctx , "BAZEL_TARGET_SYSTEM" , "local" , False )),
256257 "cxx_flag" : [
@@ -266,17 +267,22 @@ def _crosstool_content(repository_ctx, cc, cpu_value):
266267 repository_ctx , cc , "-Wl,-no-as-needed"
267268 ) + _add_option_if_supported (
268269 repository_ctx , cc , "-Wl,-z,relro,-z,now"
269- ) + (["-B" + str (repository_ctx .path (cc ).dirname ),
270- # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760.
271- "-B/usr/bin" ,
272- # Gold linker only? Can we enable this by default?
273- # "-Wl,--warn-execstack",
274- # "-Wl,--detect-odr-violations"
275- ] + _add_option_if_supported (
276- # Have gcc return the exit code from ld.
277- repository_ctx , cc , "-pass-exit-codes" )
270+ ) + ([
271+ "-undefined" ,
272+ "dynamic_lookup" ,
273+ "-headerpad_max_install_names" ,
274+ ] if darwin else [
275+ "-B" + str (repository_ctx .path (cc ).dirname ),
276+ # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760.
277+ "-B/usr/bin" ,
278+ # Gold linker only? Can we enable this by default?
279+ # "-Wl,--warn-execstack",
280+ # "-Wl,--detect-odr-violations"
281+ ] + _add_option_if_supported (
282+ # Have gcc return the exit code from ld.
283+ repository_ctx , cc , "-pass-exit-codes" )
278284 ),
279- "ar_flag" : [],
285+ "ar_flag" : ["-static" , "-s" , "-o" ] if darwin else [ ],
280286 "cxx_builtin_include_directory" : _get_escaped_cxx_inc_directories (repository_ctx , cc ),
281287 "objcopy_embed_flag" : ["-I" , "binary" ],
282288 "unfiltered_cxx_flag" :
@@ -299,7 +305,7 @@ def _crosstool_content(repository_ctx, cc, cpu_value):
299305 # All warnings are enabled. Maybe enable -Werror as well?
300306 "-Wall" ,
301307 # Enable a few more warnings that aren't part of -Wall.
302- ] + ([
308+ ] + (["-Wthread-safety" , "-Wself-assign" ] if darwin else [
303309 "-B" + _escape_string (str (repository_ctx .path (cc ).dirname )),
304310 # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760.
305311 "-B/usr/bin" ,
@@ -360,7 +366,7 @@ def _get_escaped_windows_msys_crosstool_content(repository_ctx):
360366 ' tool_path { name: "strip" path: "%susr/bin/strip" }' % escaped_msys_root )
361367
362368
363- def _opt_content ():
369+ def _opt_content (darwin ):
364370 """Return the content of the opt specific section of the CROSSTOOL file."""
365371 return {
366372 "compiler_flag" : [
@@ -386,7 +392,7 @@ def _opt_content():
386392 "-ffunction-sections" ,
387393 "-fdata-sections"
388394 ],
389- "linker_flag" : ["-Wl,--gc-sections" ]
395+ "linker_flag" : [] if darwin else [ "-Wl,--gc-sections" ]
390396 }
391397
392398
@@ -669,7 +675,23 @@ def _get_env(repository_ctx):
669675 return ""
670676
671677
672- def _coverage_feature ():
678+ def _coverage_feature (darwin ):
679+ if darwin :
680+ compile_flags = """flag_group {
681+ flag: '-fprofile-instr-generate'
682+ flag: '-fcoverage-mapping'
683+ }"""
684+ link_flags = """flag_group {
685+ flag: '-fprofile-instr-generate'
686+ }"""
687+ else :
688+ compile_flags = """flag_group {
689+ flag: '-fprofile-arcs'
690+ flag: '-ftest-coverage'
691+ }"""
692+ link_flags = """flag_group {
693+ flag: '-lgcov'
694+ }"""
673695 return """
674696 feature {
675697 name: 'coverage'
@@ -681,51 +703,37 @@ def _coverage_feature():
681703 action: 'c++-header-parsing'
682704 action: 'c++-header-preprocessing'
683705 action: 'c++-module-compile'
684- flag_group {
685- flag: '-fprofile-arcs'
686- flag: '-ftest-coverage'
687- }
706+ """ + compile_flags + """
707+
708+
709+
688710 }
689711 flag_set {
690712 action: 'c++-link-interface-dynamic-library'
691713 action: 'c++-link-dynamic-library'
692714 action: 'c++-link-executable'
693- flag_group {
694- flag: '-lgcov'
695- }
715+ """ + link_flags + """
696716 }
697717 }
698718 """
699719
700-
701- def _get_escaped_darwin_cxx_inc_directories (repository_ctx , cc ):
702- """Compute the list of default C++ include directories on darwin.
703-
704- Uses the xcode-locator tool to add all xcode developer directories to the
705- list of builtin include paths.
706-
707- This should only be invoked on a darwin OS, as xcode-locator cannot be built
708- otherwise.
720+ def _get_escaped_xcode_cxx_inc_directories (repository_ctx , cc , xcode_toolchains ):
721+ """Compute the list of default C++ include paths on Xcode-enabled darwin.
709722
710723 Args:
711724 repository_ctx: The repository context.
712725 cc: The default C++ compiler on the local system.
726+ xcode_toolchains: A list containing the xcode toolchains available
713727 Returns:
714- A 2-tuple containing:
715728 include_paths: A list of builtin include paths.
716- err: An error string describing the error that occurred when attempting
717- to build and run xcode-locator, or None if the run was successful.
718729 """
719730
720- (toolchains , xcodeloc_err ) = run_xcode_locator (repository_ctx ,
721- Label ("@bazel_tools//tools/osx:xcode_locator.m" ))
722-
723731 # TODO(cparsons): Falling back to the default C++ compiler builtin include
724732 # paths shouldn't be unnecessary once all actions are using xcrun.
725733 include_dirs = _get_escaped_cxx_inc_directories (repository_ctx , cc )
726- for toolchain in toolchains :
734+ for toolchain in xcode_toolchains :
727735 include_dirs .append (_escape_string (toolchain .developer_dir ))
728- return ( include_dirs , xcodeloc_err )
736+ return include_dirs
729737
730738
731739def _impl (repository_ctx ):
@@ -801,12 +809,17 @@ def _impl(repository_ctx):
801809 })
802810 else :
803811 darwin = cpu_value == "darwin"
812+ xcode_toolchains = []
813+ if darwin :
814+ (xcode_toolchains , xcodeloc_err ) = run_xcode_locator (
815+ repository_ctx ,
816+ Label ("@bazel_tools//tools/osx:xcode_locator.m" ))
804817 cc = _find_cc (repository_ctx )
805818 _tpl (repository_ctx , "osx_cc_wrapper.sh" if darwin else "linux_cc_wrapper.sh" , {
806819 "%{cc}" : _escape_string (str (cc )),
807820 "%{env}" : _escape_string (_get_env (repository_ctx ))
808821 }, "cc_wrapper.sh" )
809- if darwin :
822+ if xcode_toolchains :
810823 repository_ctx .symlink (
811824 Label ("@bazel_tools//tools/objc:xcrunwrapper.sh" ), "xcrunwrapper.sh" )
812825 repository_ctx .symlink (
@@ -829,7 +842,7 @@ def _impl(repository_ctx):
829842 repository_ctx .symlink (
830843 Label ("@bazel_tools//tools/osx/crosstool:osx_archs.bzl" ),
831844 "osx_archs.bzl" )
832- ( escaped_include_paths , xcodeloc_err ) = _get_escaped_darwin_cxx_inc_directories (repository_ctx , cc )
845+ escaped_include_paths = _get_escaped_xcode_cxx_inc_directories (repository_ctx , cc , xcode_toolchains )
833846 escaped_cxx_include_directories = []
834847 for path in escaped_include_paths :
835848 escaped_cxx_include_directories .append (("cxx_builtin_include_directory: \" %s\" " % path ))
@@ -840,17 +853,21 @@ def _impl(repository_ctx):
840853 Label ("@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl" ),
841854 {"%{cxx_builtin_include_directory}" : "\n " .join (escaped_cxx_include_directories )})
842855 else :
843- tool_paths = _get_tool_paths (repository_ctx , str (cc ))
844- crosstool_content = _crosstool_content (repository_ctx , cc , cpu_value )
845- opt_content = _opt_content ()
856+ tool_paths = _get_tool_paths (repository_ctx , darwin ,
857+ "cc_wrapper.sh" if darwin else str (cc ))
858+ crosstool_content = _crosstool_content (repository_ctx , cc , cpu_value , darwin )
859+ opt_content = _opt_content (darwin )
846860 dbg_content = _dbg_content ()
847861 _tpl (repository_ctx , "BUILD" , {
848- "%{name}" : _escape_string (cpu_value ),
849- "%{supports_param_files}" : "1" ,
850- "%{cc_compiler_deps}" : ":empty" ,
851- "%{compiler}" : _escape_string (
852- _get_env_var (repository_ctx , "BAZEL_COMPILER" , "compiler" , False )),
862+ "%{name}" : cpu_value ,
863+ "%{supports_param_files}" : "0" if darwin else "1" ,
864+ "%{cc_compiler_deps}" : ":cc_wrapper" if darwin else ":empty" ,
865+ "%{compiler}" : _get_env_var (repository_ctx , "BAZEL_COMPILER" , "compiler" , False ),
853866 })
867+ _tpl (repository_ctx ,
868+ "osx_cc_wrapper.sh" if darwin else "linux_cc_wrapper.sh" ,
869+ {"%{cc}" : str (cc ), "%{env}" : _get_env (repository_ctx )},
870+ "cc_wrapper.sh" )
854871 _tpl (repository_ctx , "CROSSTOOL" , {
855872 "%{cpu}" : _escape_string (cpu_value ),
856873 "%{default_toolchain_name}" : _escape_string (
@@ -865,7 +882,7 @@ def _impl(repository_ctx):
865882 "%{opt_content}" : _build_crosstool (opt_content , " " ),
866883 "%{dbg_content}" : _build_crosstool (dbg_content , " " ),
867884 "%{cxx_builtin_include_directory}" : "" ,
868- "%{coverage}" : _coverage_feature (),
885+ "%{coverage}" : _coverage_feature (darwin ),
869886 "%{msvc_env_tmp}" : "" ,
870887 "%{msvc_env_path}" : "" ,
871888 "%{msvc_env_include}" : "" ,
0 commit comments