mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Shiz <hi@shiz.me>
To: musl@lists.openwall.com
Subject: [PATCH 1/2] add musl-clang, a wrapper for system clang installs
Date: Fri, 29 May 2015 18:48:45 +0200	[thread overview]
Message-ID: <1432918126-27741-1-git-send-email-hi@shiz.me> (raw)

musl-clang allows the user to compile musl-powered programs using their
already existent clang install, without needing a special cross compiler.
it does this by wrapping around both the system clang install and the linker
and passing them special flags to re-target musl at runtime. it does only
affect invocations done through the special musl-clang wrapper script,
so that the user setup remains fully intact otherwise.

the clang wrapper consists of the compiler frontend wrapper script,
musl-clang, and the linker wrapper script, ld.musl-clang.
musl-clang makes sure clang invokes ld.musl-clang to link objects;
neither script needs to be in PATH for the wrapper to work.
---
 .gitignore             |  1 +
 Makefile               |  6 +++++-
 tools/ld.musl-clang.in | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tools/musl-clang.in    | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 tools/ld.musl-clang.in
 create mode 100644 tools/musl-clang.in

diff --git a/.gitignore b/.gitignore
index 070f549..00e4d0a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,6 @@ arch/*/bits/alltypes.h
 config.mak
 include/bits
 tools/musl-gcc
+tools/*musl-clang
 lib/musl-gcc.specs
 src/internal/version.h
diff --git a/Makefile b/Makefile
index 3bd7b4d..0537c6a 100644
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,7 @@ STATIC_LIBS = lib/libc.a
 SHARED_LIBS = lib/libc.so
 TOOL_LIBS = lib/musl-gcc.specs
 ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
-ALL_TOOLS = tools/musl-gcc
+ALL_TOOLS = tools/musl-gcc tools/musl-clang tools/ld.musl-clang
 
 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
 
@@ -156,6 +156,10 @@ tools/musl-gcc: config.mak
 	printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
 	chmod +x $@
 
+tools/%-clang: tools/%-clang.in config.mak
+	sed -e 's!@INCDIR@!$(includedir)!g' -e 's!@LIBDIR@!$(libdir)!g' -e 's!@LDSO@!$(LDSO_PATHNAME)!g' $< > $@
+	chmod +x $@
+
 $(DESTDIR)$(bindir)/%: tools/%
 	$(INSTALL) -D $< $@
 
diff --git a/tools/ld.musl-clang.in b/tools/ld.musl-clang.in
new file mode 100644
index 0000000..bb77bb1
--- /dev/null
+++ b/tools/ld.musl-clang.in
@@ -0,0 +1,45 @@
+#!/bin/sh
+cc="@CC@"
+libc_lib="@LIBDIR@"
+ldso="@LDSO@"
+cleared=
+shared=
+crt=
+userlinkdir=
+userlink=
+
+for x ; do
+    test "$cleared" || set -- ; cleared=1
+
+    case "$x" in
+        -L-user-start)
+            userlinkdir=1
+            ;;
+        -L-user-end)
+            userlinkdir=
+            ;;
+        -L*)
+            test "$userlinkdir" && set -- "$@" "$x"
+            ;;
+        -l-user-start)
+            test "$shared" || set -- "$@" "$libc_lib/Scrt1.o" "$libc_lib/crti.o"
+            userlink=1
+            ;;
+        -l-user-end)
+            userlink=
+            ;;
+        -l*)
+            test "$crt" -o "$shared" || set -- "$@" "$libc_lib/crtn.o" ; crt=1
+            test "$userlink" && set -- "$@" "$x"
+            ;;
+        -shared)
+            shared=1
+            set -- "$@" -shared
+            ;;
+        *)
+            set -- "$@" "$x"
+            ;;
+    esac
+done
+
+exec $($cc -print-prog-name=ld) -nostdlib "$@" -dynamic-linker "$ldso"
diff --git a/tools/musl-clang.in b/tools/musl-clang.in
new file mode 100644
index 0000000..51e0196
--- /dev/null
+++ b/tools/musl-clang.in
@@ -0,0 +1,35 @@
+#!/bin/sh
+cc="@CC@"
+libc_inc="@INCDIR@"
+libc_lib="@LIBDIR@"
+thisdir="`cd "$(dirname "$0")"; pwd`"
+
+# prevent clang from running the linker (and erroring) on no input.
+sflags=
+eflags=
+for x ; do
+    case "$x" in
+        -l*) input=1 ;;
+        -*) input= ;;
+        *) input=1 ;;
+    esac
+    if test "$input" ; then
+        sflags="-l-user-start"
+        eflags="-lc -l-user-end"
+        break
+    fi
+done
+
+exec $cc \
+    -B"$thisdir" \
+    -fuse-ld=musl-clang \
+    -rtlib=compiler-rt \
+    -nostdinc \
+    -nostartfiles \
+    -isystem "$libc_inc" \
+    -L-user-start \
+    $sflags \
+    "$@" \
+    $eflags \
+    -L"$libc_lib" \
+    -L-user-end
-- 
2.3.6



             reply	other threads:[~2015-05-29 16:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-29 16:48 Shiz [this message]
2015-05-29 16:55 ` [PATCH 2/2] build: overhaul wrapper script system for multiple wrapper support Shiz
2015-06-01  3:18   ` Rich Felker
2015-06-01 14:15     ` Shiz
2015-06-01 14:47       ` Rich Felker
2015-06-01 15:39         ` Shiz
2015-06-01 16:03           ` Rich Felker
2015-05-29 17:03 ` [PATCH 1/2] add musl-clang, a wrapper for system clang installs Rich Felker
2015-05-29 17:11   ` Shiz
2015-05-29 17:13     ` Rich Felker
2015-05-29 17:35       ` Shiz
2015-05-29 18:43 ` [PATCH v2] " Shiz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1432918126-27741-1-git-send-email-hi@shiz.me \
    --to=hi@shiz.me \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).