From e4195e2e120f657353732dd0ad2d16b2b650b75b Mon Sep 17 00:00:00 2001 From: Jonathan Anderson Date: Tue, 1 Nov 2016 21:27:42 +0000 Subject: [PATCH] Add rules to build LLVM IR binaries and libraries. Running `make libfoo.ll` or `make libfoo.bc` within a library directory will now give us an LLVM IR version of the library, and `make foo.full.ll` or `make foo.full.bc` will give us an IR version of a binary. As part of this change, we add an LLVM_LINK variable to sys.mk that can be specified/overridden using an external toolchain. Reviewed by: bdrewery, brooks Approved by: rwatson (mentor) Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D8388 --- Makefile.inc1 | 4 ++-- share/mk/bsd.lib.mk | 14 +++++++++++++- share/mk/bsd.prog.mk | 21 +++++++++++++++++---- share/mk/sys.mk | 2 ++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Makefile.inc1 b/Makefile.inc1 index 931af3134669..2b00ff1422b8 100644 --- a/Makefile.inc1 +++ b/Makefile.inc1 @@ -543,8 +543,8 @@ HMAKE+= PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT CROSSENV+= CC="${XCC} ${XCFLAGS}" CXX="${XCXX} ${XCXXFLAGS} ${XCFLAGS}" \ CPP="${XCPP} ${XCFLAGS}" \ - AS="${XAS}" AR="${XAR}" LD="${XLD}" NM=${XNM} \ - OBJCOPY="${XOBJCOPY}" \ + AS="${XAS}" AR="${XAR}" LD="${XLD}" LLVM_LINK="${XLLVM_LINK}" \ + NM=${XNM} OBJCOPY="${XOBJCOPY}" \ RANLIB=${XRANLIB} STRINGS=${XSTRINGS} \ SIZE="${XSIZE}" diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk index 0126a020e999..1fab97cf9e8d 100644 --- a/share/mk/bsd.lib.mk +++ b/share/mk/bsd.lib.mk @@ -78,7 +78,7 @@ CTFFLAGS+= -g # prefer .s to a .c, add .po, remove stuff not used in the BSD libraries # .pico used for PIC object files -.SUFFIXES: .out .o .po .pico .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln +.SUFFIXES: .out .o .bc .ll .po .pico .S .asm .s .c .cc .cpp .cxx .C .f .y .l .ln .if !defined(PICFLAG) .if ${MACHINE_CPUARCH} == "sparc64" @@ -199,6 +199,18 @@ lib${LIB_PRIVATE}${LIB}_p.a: ${POBJS} ${RANLIB} ${RANLIBFLAGS} ${.TARGET} .endif +.if defined(LLVM_LINK) +BCOBJS= ${OBJS:.o=.bco} ${STATICOBJS:.o=.bco} +LLOBJS= ${OBJS:.o=.llo} ${STATICOBJS:.o=.llo} +CLEANFILES+= ${BCOBJS} ${LLOBJS} + +lib${LIB_PRIVATE}${LIB}.bc: ${BCOBJS} + ${LLVM_LINK} -o ${.TARGET} ${BCOBJS} + +lib${LIB_PRIVATE}${LIB}.ll: ${LLOBJS} + ${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS} +.endif + .if defined(SHLIB_NAME) || \ defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB) SOBJS+= ${OBJS:.o=.pico} diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk index deb78a3ded89..43cfb1271a5a 100644 --- a/share/mk/bsd.prog.mk +++ b/share/mk/bsd.prog.mk @@ -4,7 +4,7 @@ .include .include -.SUFFIXES: .out .o .c .cc .cpp .cxx .C .m .y .l .ln .s .S .asm +.SUFFIXES: .out .o .bc .c .cc .cpp .cxx .C .m .y .l .ll .ln .s .S .asm # XXX The use of COPTS in modern makefiles is discouraged. .if defined(COPTS) @@ -147,6 +147,19 @@ ${PROGNAME}.debug: ${PROG_FULL} ${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET} .endif +.if defined(LLVM_LINK) +# LLVM bitcode / textual IR representations of the program +BCOBJS= ${OBJS:.o=.bco} +LLOBJS= ${OBJS:.o=.llo} + +${PROG_FULL}.bc: ${BCOBJS} + ${LLVM_LINK} -o ${.TARGET} ${BCOBJS} + +${PROG_FULL}.ll: ${LLOBJS} + ${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS} + +.endif # defined(LLVM_LINK) + .if ${MK_MAN} != "no" && !defined(MAN) && \ !defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \ !defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \ @@ -166,14 +179,14 @@ all: all-man .endif .if defined(PROG) -CLEANFILES+= ${PROG} +CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll .if ${MK_DEBUG_FILES} != "no" -CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug +CLEANFILES+= ${PROG_FULL} ${PROG_FULL}.bc ${PROGNAME}.debug ${PROG_FULL}.ll .endif .endif .if defined(OBJS) -CLEANFILES+= ${OBJS} +CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS} .endif .include diff --git a/share/mk/sys.mk b/share/mk/sys.mk index cc4a1feb61bb..2c3743342018 100644 --- a/share/mk/sys.mk +++ b/share/mk/sys.mk @@ -231,6 +231,8 @@ LINTLIBFLAGS ?= -cghapbxu -C ${LIB} MAKE ?= make .if !defined(%POSIX) +LLVM_LINK ?= llvm-link + LORDER ?= lorder NM ?= nm