diff mbox

[1,of,1] refine static linking check to better guide user

Message ID CAKduhSsi3RO49fPLMsBxE+iy5REJJGB7UxOxv5dJuf2KLAzc_w@mail.gmail.com
State Superseded
Headers show

Commit Message

Daniel Price Nov. 19, 2012, 10:40 p.m. UTC
An IRC discussion with Yann last week led to a request that I submit a
patch which covers a series of blunders I made as a novice user of
crosstool.  One was that I didn't have static libc installed on my
host system, and was stymied by the wording of the message: "Static
linking impossible on the host system" (solution: install the static
glibc package).  Two was that I had a misconfigured directory
structure which had a ':' in it-- this causes $PATH to be misparsed,
and, ironically, trips the "Static linking impossible" message all
over again.

Via google, I have noticed that the "static linking" error message has
come up a number of times, and this patch hopefully will help to
clarify for the user what they might need to do to make crosstool work
for them by first proving that gcc is present, can compile a trivial
program, and can compile trivial static binaries.  I also adjusted the
error messages somewhat in order to provide some hints if things go
wrong.

Comments appreciated; I made a number of editorial decisions which
perhaps not everyone would agree with.  Also, my ability to test this
on a variety of hosts is very limited.  Thanks,

       -dp

# HG changeset patch
# User Daniel Price <daniel.price@gmail.com>
# Date 1353364265 28800
# Node ID fe774e44305a643f553b509a147e7fc78ed7d3dd
# Parent  1c68438f44f74e7fcf761838271f1e4d156ceeda
scripts: refine static linking check to better guide the user

Signed-off-by: Daniel Price <daniel.price@gmail.com>

                 CT_DoLog DEBUG "  '${!v}-${tool}' -> '${where}'"
                 printf "#${BANG}${CT_CONFIG_SHELL}\nexec '${where}'
\"\${@}\"\n" >"${CT_BUILDTOOLS_PREFIX_DIR}/bin/${!v}-${tool}"
@@ -475,17 +474,60 @@
         *)  ;;
     esac

+    # Now that we've set up $PATH, sanity test that GCC is runnable so that
+    # the user can troubleshoot problems if not.
+    CT_DoLog DEBUG "Sanity testing gcc"
+    gccout="${CT_BUILD_DIR}/.gcc-output"
+    GCC=${CT_HOST}-gcc
+    ret=0
+    ${GCC} -v > $gccout 2>&1 || ret=$?
+    if [ $ret != 0 ]; then
+        CT_DoLog DEBUG "Failed to invoke '${GCC} -v' (exited ${ret}):
Output Follows:"
+        CT_DoLog DEBUG "$(cat ${gccout})"
+    fi
+    case $ret in
+    0)
+        ;;
+    126)
+            CT_Abort "${GCC}: cannot execute; check permissions."
+                ;;
+    127)
+            CT_Abort "${GCC}: not found in PATH; check for
metacharacters or other problems in PATH (PATH=${PATH})"
+                ;;
+    *)
+            CT_Abort "Ran '${GCC} -v', but command failed with exit ${ret}"
+                ;;
+    esac
+    rm -f "${gccout}"
+
+    CT_DoLog DEBUG "Testing that gcc can compile a trivial program"
+    tmp="${CT_BUILD_DIR}/.gcc-test"
+    # Try a trivial program to ensure the compiler works.
+    if ! "${CT_HOST}-gcc" -xc - -o "${tmp}"  > ${gccout} 2>&1 <<-_EOF_
+                int main() {return 0; }
+            _EOF_
+    then
+        CT_DoLog DEBUG "'${GCC}' failed (exited ${ret}): Output Follows:"
+        CT_DoLog DEBUG "$(cat ${gccout})"
+        CT_Abort "Couldn't compile a trivial program using ${CT_HOST}-gcc"
+    fi
+    rm -f "${tmp}" "${gccout}"
+
     # Now we know our host and where to find the host tools, we can check
     # if static link was requested, but only if it was requested
     if [ "${CT_WANTS_STATIC_LINK}" = "y" ]; then
         tmp="${CT_BUILD_DIR}/.static-test"
-        if ! "${CT_HOST}-gcc" -xc - -static -o "${tmp}" >/dev/null
2>&1 <<-_EOF_
+
+        CT_DoLog DEBUG "Testing that gcc can compile a trivial
statically linked program"
+        if ! "${CT_HOST}-gcc" -xc - -static -o "${tmp}" > ${gccout}
2>&1 <<-_EOF_
                 int main() { return 0; }
             _EOF_
         then
-            CT_Abort "Static linking impossible on the host system
'${CT_HOST}'"
+            CT_DoLog DEBUG "'${GCC}' failed (exited ${ret}): Output Follows:"
+            CT_DoLog DEBUG "$(cat ${gccout})"
+            CT_Abort "Static linking impossible on the host system
'${CT_HOST}'; is libc.a installed?"
         fi
-        rm -f "${tmp}"
+        rm -f "${tmp}" "${gccout}"
     fi

     # Help gcc



--
Daniel.Price@gmail.com; Twitter: @danielbprice

--
For unsubscribe information see http://sourceware.org/lists.html#faq
diff mbox

Patch

diff -r 1c68438f44f7 -r fe774e44305a scripts/crosstool-NG.sh.in
--- a/scripts/crosstool-NG.sh.in    Fri Nov 16 14:59:27 2012 +0100
+++ b/scripts/crosstool-NG.sh.in    Mon Nov 19 14:31:05 2012 -0800
@@ -422,8 +422,7 @@ 
                 where=$(CT_Which "${tool}")
             fi

-            # Not all tools are available for all platforms, but some
are really,
-            # bally needed
+            # Not all tools are available for all platforms, but some
are required.
             if [ -n "${where}" ]; then