+# Sets ld_sysroot to the current sysroot (empty if not supported) and
+# returns true if the target ld supports sysroot.
+proc check_sysroot_available { } {
+ global ld_sysroot_available_saved ld ld_sysroot
+ if {![info exists ld_sysroot_available_saved]} {
+ # Check if ld supports --sysroot *other* than empty.
+ set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
+ if { $ld_sysroot == "" } {
+ set ld_sysroot_available_saved 0
+ } else {
+ set ld_sysroot_available_saved 1
+ }
+ }
+ return $ld_sysroot_available_saved
+}
+
+# Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
+proc check_gcc_plugin_enabled { } {
+ global CC
+
+ if {![info exists CC]} {
+ set CC [find_gcc]
+ }
+ if { $CC == ""} {
+ return 0
+ }
+ set state [remote_exec host $CC -v]
+ if { [lindex $state 0] != 0 } {
+ return 0;
+ }
+ for { set i 1 } { $i < [llength $state] } { incr i } {
+ set v [lindex $state $i]
+ if { [ string match "*--disable-plugin*" $v ] } {
+ verbose "plugin is disabled by $v"
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+# Returns true if the target compiler supports LTO
+proc check_lto_available { } {
+ global lto_available_saved
+ global CC
+
+ if {![info exists lto_available_saved]} {
+ if { ![check_gcc_plugin_enabled] } {
+ set lto_available_saved 0
+ return 0
+ }
+ # This test will hide LTO bugs in ld. Since GCC 4.9 adds
+ # -ffat-lto-objects, we always run LTO tests on Linux with
+ # GCC 4.9 or newer.
+ if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
+ set lto_available_saved 1
+ return 1
+ }
+ # Check if gcc supports -flto -fuse-linker-plugin
+ set flags ""
+ if [board_info [target_info name] exists cflags] {
+ append flags " [board_info [target_info name] cflags]"
+ }
+ if [board_info [target_info name] exists ldflags] {
+ append flags " [board_info [target_info name] ldflags]"
+ }
+
+ set basename "tmpdir/lto[pid]"
+ set src ${basename}.c
+ set output ${basename}.out
+ set f [open $src "w"]
+ puts $f "int main() { return 0; }"
+ close $f
+ if [is_remote host] {
+ set src [remote_download host $src]
+ }
+ set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
+ remote_file host delete $src
+ remote_file host delete $output
+ file delete $src
+ }
+ return $lto_available_saved
+}
+
+# Returns true if the target compiler supports LTO -ffat-lto-objects
+proc check_lto_fat_available { } {
+ global lto_fat_available_saved
+ global CC
+
+ if {![info exists lto_fat_available_saved]} {
+ if { ![check_gcc_plugin_enabled] } {
+ set lto_fat_available_saved 0
+ return 0
+ }
+ # This test will hide LTO bugs in ld. Since GCC 4.9 adds
+ # -ffat-lto-objects, we always run LTO tests on Linux with
+ # GCC 4.9 or newer.
+ if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
+ set lto_fat_available_saved 1
+ return 1
+ }
+ # Check if gcc supports -flto -fuse-linker-plugin
+ set flags ""
+ if [board_info [target_info name] exists cflags] {
+ append flags " [board_info [target_info name] cflags]"
+ }
+ if [board_info [target_info name] exists ldflags] {
+ append flags " [board_info [target_info name] ldflags]"
+ }
+
+ set basename "tmpdir/lto[pid]"
+ set src ${basename}.c
+ set output ${basename}.out
+ set f [open $src "w"]
+ puts $f "int main() { return 0; }"
+ close $f
+ if [is_remote host] {
+ set src [remote_download host $src]
+ }
+ set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
+ remote_file host delete $src
+ remote_file host delete $output
+ file delete $src
+ }
+ return $lto_fat_available_saved
+}
+
+# Returns true if the target compiler supports LTO and -shared
+proc check_lto_shared_available { } {
+ global lto_shared_available_saved
+ global CC
+
+ if {![info exists lto_shared_available_saved]} {
+ if { ![check_gcc_plugin_enabled] } {
+ set lto_shared_available_saved 0
+ return 0
+ }
+ # This test will hide LTO bugs in ld. Since GCC 4.9 adds
+ # -ffat-lto-objects, we always run LTO tests on Linux with
+ # GCC 4.9 or newer.
+ if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
+ set lto_shared_available_saved 1
+ return 1
+ }
+ # Check if gcc supports -flto -fuse-linker-plugin -shared
+ set flags ""
+ if [board_info [target_info name] exists cflags] {
+ append flags " [board_info [target_info name] cflags]"
+ }
+ if [board_info [target_info name] exists ldflags] {
+ append flags " [board_info [target_info name] ldflags]"
+ }
+
+ set basename "tmpdir/lto_shared[pid]"
+ set src ${basename}.c
+ set output ${basename}.so
+ set f [open $src "w"]
+ puts $f ""
+ close $f
+ if [is_remote host] {
+ set src [remote_download host $src]
+ }
+ set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
+ remote_file host delete $src
+ remote_file host delete $output
+ file delete $src
+ }
+ return $lto_shared_available_saved
+}
+