#!/bin/sh

git init -b master
cat > meson.build << EOF
use_elf_tls = false
if not with_platform_windows or not with_shared_glapi
  pre_args += '-DUSE_ELF_TLS'
  use_elf_tls = true

  if with_platform_android and get_option('platform-sdk-version') >= 29
    # By default the NDK compiler, at least, emits emutls references instead of
    # ELF TLS, even when building targeting newer API levels.  Make it actually do
    # ELF TLS instead.
    c_args += '-fno-emulated-tls'
    cpp_args += '-fno-emulated-tls'
  endif

  # -mtls-dialect=gnu2 speeds up non-initial-exec TLS significantly but requires
  # full toolchain (including libc) support.
  have_mtls_dialect = false
  foreach c_arg : get_option('c_args')
    if c_arg.startswith('-mtls-dialect=')
      have_mtls_dialect = true
      break
    endif
  endforeach
  if not have_mtls_dialect
    # need .run to check libc support. meson aborts when calling .run when
    # cross-compiling, but because this is just an optimization we can skip it
    if meson.is_cross_build()
      warning('cannot auto-detect -mtls-dialect when cross-compiling, using compiler default')
    else
      # -fpic to force dynamic tls, otherwise TLS relaxation defeats check
      gnu2_test = cc.run('int __thread x; int main() { return x; }', args: ['-mtls-dialect=gnu2', '-fpic'], name: '-mtls-dialect=gnu2')
      if gnu2_test.returncode() == 0
        c_args += '-mtls-dialect=gnu2'
        cpp_args += '-mtls-dialect=gnu2'
      endif
    endif
  endif
endif
EOF
git commit -m base meson.build
cat > meson.build << EOF
use_elf_tls = true
pre_args += '-DUSE_ELF_TLS'

if with_platform_android and get_option('platform-sdk-version') >= 29
  # By default the NDK compiler, at least, emits emutls references instead of
  # ELF TLS, even when building targeting newer API levels.  Make it actually do
  # ELF TLS instead.
  c_args += '-fno-emulated-tls'
  cpp_args += '-fno-emulated-tls'
endif

# -mtls-dialect=gnu2 speeds up non-initial-exec TLS significantly but requires
# full toolchain (including libc) support.
have_mtls_dialect = false
foreach c_arg : get_option('c_args')
  if c_arg.startswith('-mtls-dialect=')
    have_mtls_dialect = true
    break
  endif
endforeach
if not have_mtls_dialect
  # need .run to check libc support. meson aborts when calling .run when
  # cross-compiling, but because this is just an optimization we can skip it
  if meson.is_cross_build()
    warning('cannot auto-detect -mtls-dialect when cross-compiling, using compiler default')
  else
    # -fpic to force dynamic tls, otherwise TLS relaxation defeats check
    gnu2_test = cc.run('int __thread x; int main() { return x; }', args: ['-mtls-dialect=gnu2', '-fpic'], name: '-mtls-dialect=gnu2')
    if gnu2_test.returncode() == 0
      c_args += '-mtls-dialect=gnu2'
      cpp_args += '-mtls-dialect=gnu2'
    endif
  endif
endif
EOF
git commit -m update meson.build
git checkout -b branch HEAD^
git apply << EOF
diff --git a/meson.build b/meson.build
index 2f44c9d..58388dd 100644
--- a/meson.build
+++ b/meson.build
@@ -28,7 +28,16 @@ if not with_platform_windows or not with_shared_glapi
     else
       # -fpic to force dynamic tls, otherwise TLS relaxation defeats check
       gnu2_test = cc.run('int __thread x; int main() { return x; }', args: ['-mtls-dialect=gnu2', '-fpic'], name: '-mtls-dialect=gnu2')
-      if gnu2_test.returncode() == 0
+      # https://gitlab.freedesktop.org/mesa/mesa/-/issues/5665
+      if gnu2_test.returncode() == 0 and (
+           host_machine.cpu_family() != 'x86_64' or
+           # https://github.com/mesonbuild/meson/issues/6377
+           #cc.get_linker_id() != 'ld.lld' or
+           cc.links('''int __thread x; int y; int main() { __asm__(
+                  "leaq x@TLSDESC(%rip), %rax\n"
+                  "movq y@GOTPCREL(%rip), %rdx\n"
+                  "call *x@TLSCALL(%rax)\n"); }''', name: 'split TLSDESC')
+           )
         c_args += '-mtls-dialect=gnu2'
         cpp_args += '-mtls-dialect=gnu2'
       endif
EOF
git commit -m mine meson.build
