Skip to content
bootstrap 3.73 KiB
Newer Older
#!/usr/bin/env bash

cd $(dirname $(readlink -f $0))
autoreconf -fiv

# Patch ltmain.sh
if [[ -f ltmain.sh ]] ; then
   echo -n "Patching ltmain.sh... "

   # (A) Apply some of the patches that Debian introduces to ltmain.sh in order to not link against
   # all the dependencies of our libraries. Thus, reducing the over-linking produced by libtool.

   # Concatenate dependency_libs only if link_all_deplibs != no
   case="link) libs=\"\$deplibs %DEPLIBS% \$dependency_libs\" ;;"
   newcase="link) libs=\"\$deplibs %DEPLIBS%\"\n"
   newcase+="\t\ttest \"X\$link_all_deplibs\" != Xno \&\& libs=\"\$libs \$dependency_libs\" ;;"
   script_1="s/$case/$newcase/"

   # In the step of checking convenience libraries, iterate dependency_libs only if libdir is empty
   # The objective here is to move 3 lines down (elif -> fi) until the end of the debplib loop.
   script_2="
      # Match only from '# It is a libtool convenience library' until the end of the next loop
      /# It is a libtool convenience library/,/done/ {

         # If the elif block is found, move it to the hold buffer
         /elif/ {
            # Append the next two lines to the pattern buffer
            N
            N
            # Exchange pattern and hold buffers to keep the 3 lines
            x
            # Delele the current pattern buffer which should be blank
            d
         }

         # If the end of the loop is found, append the contents of the hold buffer
         /done/ {
            # First, we switch buffers to check if the hold buffer contains our code
            x
            # If the hold buffer contained elif, it means that the elif block was prior to the loop
            /elif/ {
               # Restore the initial pattern
               x
               # Append the hold buffer to the pattern space
               G
            }
            # If the hold buffer was empty, the elif block was not prior and the patch is not needed
            /^$/ {
               # Switch buffers again to restore the initial pattern
               x
            }
         }
      }"

      # (B) Libtool has a weird 'feature' in which places all the linker options '-Wl,option' at the
      # end of the command line. Some options, like '--as-needed', need to be placed before the
      # library list to have any effect.

      parse_flags="       -Wl,*--as-needed*)\n"
      parse_flags+="        deplibs=\"\$deplibs \$wl--as-needed\"\n"
      parse_flags+="        ;;\n"
      parse_flags+="      -Wl,*--no-as-needed*)\n"
      parse_flags+="        deplibs=\"\$deplibs \$wl--no-as-needed\"\n"
      parse_flags+="        ;;\n"

      iter_flags="          -Wl,--as-needed)\n"
      iter_flags+="           if test \"\$linkmode,\$pass\" = \"prog,link\"; then\n"
      iter_flags+="             compile_deplibs=\"\$deplib \$compile_deplibs\"\n"
      iter_flags+="             finalize_deplibs=\"\$deplib \$finalize_deplibs\"\n"
      iter_flags+="          else\n"
      iter_flags+="            deplibs=\"\$deplib \$deplibs\"\n"
      iter_flags+="          fi\n"
      iter_flags+="          continue\n"
      iter_flags+="          ;;\n"
      script_3="
         # Match only from within the func_mode_link() function
         /func_mode_link\s*\(\)/,/^}/ {

            # Prepend parsing of -Wl,--as-nedded / -Wl,--no-as-needed
            /-Wl,\*)/ {
               s/.*/$parse_flags&/
            }

            # Place flags before the library list
            # We are looking for the loop where -pthread is parsed from \$deplib
            /case \$deplib in/ {
               n
               /-pthread/ {
                  s/.*/$iter_flags&/
               }
            }
         }"

      # Apply scripts
      sed -i -e "$script_1" -e "$script_2" -e "$script_3" ltmain.sh

   echo "DONE"
fi