Skip to content

Commit f150c17

Browse files
committed
Auto merge of rust-lang#27937 - DiamondLovesYou:llvm-root-and-shared, r=alexcrichton
This handles cases when the LLVM used isn't configured will the 'usual' targets. Also, cases where LLVM is shared are also handled (ie with `LD_LIBRARY_PATH` etc).
2 parents 9ea4b4f + 7bd69f2 commit f150c17

File tree

12 files changed

+115
-49
lines changed

12 files changed

+115
-49
lines changed

configure

+9-1
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,13 @@ then
939939
putvar CFG_ENABLE_CLANG
940940
fi
941941

942+
if [ -z "$CFG_DISABLE_LIBCPP" -a -n "$CFG_ENABLE_CLANG" ]
943+
then
944+
CFG_USING_LIBCPP="1"
945+
else
946+
CFG_USING_LIBCPP="0"
947+
fi
948+
942949
# Same with jemalloc. save the setting here.
943950
if [ -n "$CFG_DISABLE_JEMALLOC" ]
944951
then
@@ -1682,7 +1689,7 @@ do
16821689
CXXFLAGS="$CXXFLAGS $LLVM_CXXFLAGS"
16831690
LDFLAGS="$LDFLAGS $LLVM_LDFLAGS"
16841691

1685-
if [ -z "$CFG_DISABLE_LIBCPP" ] && [ -n "$CFG_USING_CLANG" ]; then
1692+
if [ "$CFG_USING_LIBCPP" != "0" ]; then
16861693
LLVM_OPTS="$LLVM_OPTS --enable-libcpp"
16871694
fi
16881695

@@ -1743,6 +1750,7 @@ putvar CFG_AARCH64_LINUX_ANDROID_NDK
17431750
putvar CFG_ARM_LINUX_ANDROIDEABI_NDK
17441751
putvar CFG_I686_LINUX_ANDROID_NDK
17451752
putvar CFG_MANDIR
1753+
putvar CFG_USING_LIBCPP
17461754

17471755
# Avoid spurious warnings from clang by feeding it original source on
17481756
# ccache-miss rather than preprocessed input.

mk/llvm.mk

+11-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ endif
8080

8181

8282
# LLVM linkage:
83+
# Note: Filter with llvm-config so that optional targets which aren't present
84+
# don't cause errors (ie PNaCl's target is only present within PNaCl's LLVM
85+
# fork).
8386
LLVM_LINKAGE_PATH_$(1):=$$(abspath $$(RT_OUTPUT_DIR_$(1))/llvmdeps.rs)
8487
$$(LLVM_LINKAGE_PATH_$(1)): $(S)src/etc/mklldeps.py $$(LLVM_CONFIG_$(1))
85-
$(Q)$(CFG_PYTHON) "$$<" "$$@" "$$(LLVM_COMPONENTS)" "$$(CFG_ENABLE_LLVM_STATIC_STDCPP)" \
86-
$$(LLVM_CONFIG_$(1)) "$(CFG_STDCPP_NAME)"
88+
$(Q)$(CFG_PYTHON) "$$<" "$$@" "$$(filter $$(shell \
89+
$$(LLVM_CONFIG_$(1)) --components), \
90+
$(LLVM_OPTIONAL_COMPONENTS)) $(LLVM_REQUIRED_COMPONENTS)" \
91+
"$$(CFG_ENABLE_LLVM_STATIC_STDCPP)" $$(LLVM_CONFIG_$(1)) \
92+
"$(CFG_STDCPP_NAME)" "$$(CFG_USING_LIBCPP)"
8793
endef
8894

8995
$(foreach host,$(CFG_HOST), \
@@ -94,7 +100,9 @@ $(foreach host,$(CFG_HOST), \
94100

95101
# This can't be done in target.mk because it's included before this file.
96102
define LLVM_LINKAGE_DEPS
97-
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc_llvm: $$(LLVM_LINKAGE_PATH_$(2))
103+
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc_llvm: $$(LLVM_LINKAGE_PATH_$(3))
104+
RUSTFLAGS$(1)_rustc_llvm_T_$(3) += $$(shell echo $$(LLVM_ALL_COMPONENTS_$(3)) | tr '-' '_' |\
105+
sed -e 's/^ //;s/\([^ ]*\)/\-\-cfg have_component_\1/g')
98106
endef
99107

100108
$(foreach source,$(CFG_HOST), \

mk/main.mk

+13-2
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,18 @@ endif
276276
# LLVM macros
277277
######################################################################
278278

279-
LLVM_COMPONENTS=x86 arm aarch64 mips powerpc ipo bitreader bitwriter linker asmparser mcjit \
279+
LLVM_OPTIONAL_COMPONENTS=x86 arm aarch64 mips powerpc
280+
LLVM_REQUIRED_COMPONENTS=ipo bitreader bitwriter linker asmparser mcjit \
280281
interpreter instrumentation
281282

283+
ifneq ($(CFG_LLVM_ROOT),)
284+
# Ensure we only try to link targets that the installed LLVM actually has:
285+
LLVM_COMPONENTS := $(filter $(shell $(CFG_LLVM_ROOT)/bin/llvm-config$(X_$(CFG_BUILD)) --components),\
286+
$(LLVM_OPTIONAL_COMPONENTS)) $(LLVM_REQUIRED_COMPONENTS)
287+
else
288+
LLVM_COMPONENTS := $(LLVM_OPTIONAL_COMPONENTS) $(LLVM_REQUIRED_COMPONENTS)
289+
endif
290+
282291
# Only build these LLVM tools
283292
LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
284293

@@ -314,6 +323,8 @@ LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
314323
LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X_$(1))
315324
LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X_$(1))
316325

326+
LLVM_ALL_COMPONENTS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --components)
327+
317328
endef
318329

319330
$(foreach host,$(CFG_HOST), \
@@ -476,7 +487,7 @@ endif
476487
endif
477488

478489
LD_LIBRARY_PATH_ENV_HOSTDIR$(1)_T_$(2)_H_$(3) := \
479-
$$(CURDIR)/$$(HLIB$(1)_H_$(3))
490+
$$(CURDIR)/$$(HLIB$(1)_H_$(3)):$$(CFG_LLVM_INST_DIR_$(3))/lib
480491
LD_LIBRARY_PATH_ENV_TARGETDIR$(1)_T_$(2)_H_$(3) := \
481492
$$(CURDIR)/$$(TLIB1_T_$(2)_H_$(CFG_BUILD))
482493

mk/target.mk

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
131131
@$$(call E, rustc: $$@)
132132
$$(STAGE$(1)_T_$(2)_H_$(3)) \
133133
$$(STDCPP_LIBDIR_RUSTFLAGS_$(2)) \
134+
$$(LLVM_LIBDIR_RUSTFLAGS_$(2)) \
134135
-o $$@ $$< --cfg $(4)
135136

136137
endef

mk/tests.mk

+3-1
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,9 @@ $(3)/test/run-make/%-$(1)-T-$(2)-H-$(3).ok: \
10721072
"$$(LD_LIBRARY_PATH_ENV_TARGETDIR$(1)_T_$(2)_H_$(3))" \
10731073
$(1) \
10741074
$$(S) \
1075-
$(3)
1075+
$(3) \
1076+
"$$(LLVM_LIBDIR_RUSTFLAGS_$(3))" \
1077+
"$$(LLVM_ALL_COMPONENTS_$(3))"
10761078
@touch -r [email protected]_time $$@ && rm [email protected]_time
10771079
else
10781080
# FIXME #11094 - The above rule doesn't work right for multiple targets

src/etc/maketest.py

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def convert_path_spec(name, value):
5353
putenv('TARGET_RPATH_DIR', os.path.abspath(sys.argv[11]))
5454
putenv('RUST_BUILD_STAGE', sys.argv[12])
5555
putenv('S', os.path.abspath(sys.argv[13]))
56+
putenv('RUSTFLAGS', sys.argv[15])
57+
putenv('LLVM_COMPONENTS', sys.argv[16])
5658
putenv('PYTHON', sys.executable)
5759
os.putenv('TARGET', target_triple)
5860

src/etc/mklldeps.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
enable_static = sys.argv[3]
1919
llvm_config = sys.argv[4]
2020
stdcpp_name = sys.argv[5]
21+
use_libcpp = sys.argv[6]
2122

2223
f.write("""// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2324
// file at the top-level directory of this distribution and at
@@ -44,11 +45,25 @@ def run(args):
4445
sys.exit(1)
4546
return out
4647

48+
def runErr(args):
49+
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50+
out, err = proc.communicate()
51+
52+
if err:
53+
return False, out
54+
else:
55+
return True, out
56+
4757
f.write("\n")
4858

59+
args = [llvm_config, '--shared-mode']
60+
args.extend(components)
61+
llvm_shared, out = runErr(args)
62+
if llvm_shared:
63+
llvm_shared = 'shared' in out
64+
4965
# LLVM libs
5066
args = [llvm_config, '--libs', '--system-libs']
51-
5267
args.extend(components)
5368
out = run(args)
5469
for lib in out.strip().replace("\n", ' ').split(' '):
@@ -63,8 +78,7 @@ def run(args):
6378
elif lib[0] == '-':
6479
lib = lib.strip()[1:]
6580
f.write("#[link(name = \"" + lib + "\"")
66-
# LLVM libraries are all static libraries
67-
if 'LLVM' in lib:
81+
if not llvm_shared and 'LLVM' in lib:
6882
f.write(", kind = \"static\"")
6983
f.write(")]\n")
7084

@@ -83,7 +97,7 @@ def run(args):
8397
# Note that we use `cfg_attr` here because on MSVC the C++ standard library
8498
# is not c++ or stdc++, but rather the linker takes care of linking the
8599
# right standard library.
86-
if 'stdlib=libc++' in out:
100+
if use_libcpp != "0" or 'stdlib=libc++' in out:
87101
f.write("#[cfg_attr(not(target_env = \"msvc\"), link(name = \"c++\"))]\n")
88102
else:
89103
f.write("#[cfg_attr(not(target_env = \"msvc\"), link(name = \"" + stdcpp_name + "\"))]\n")

src/librustc_llvm/lib.rs

+32
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(link_args)]
3434
#![feature(staged_api)]
3535
#![feature(linked_from)]
36+
#![feature(concat_idents)]
3637

3738
extern crate libc;
3839
#[macro_use] #[no_link] extern crate rustc_bitflags;
@@ -2327,6 +2328,37 @@ pub unsafe fn debug_loc_to_string(c: ContextRef, tr: DebugLocRef) -> String {
23272328
.expect("got a non-UTF8 DebugLoc from LLVM")
23282329
}
23292330

2331+
pub fn initialize_available_targets() {
2332+
macro_rules! init_target(
2333+
($cfg:ident $arch:ident) => { {
2334+
#[cfg($cfg)]
2335+
fn init() {
2336+
unsafe {
2337+
let f = concat_idents!(LLVMInitialize, $arch, TargetInfo);
2338+
f();
2339+
let f = concat_idents!(LLVMInitialize, $arch, Target);
2340+
f();
2341+
let f = concat_idents!(LLVMInitialize, $arch, TargetMC);
2342+
f();
2343+
let f = concat_idents!(LLVMInitialize, $arch, AsmPrinter);
2344+
f();
2345+
let f = concat_idents!(LLVMInitialize, $arch, AsmParser);
2346+
f();
2347+
}
2348+
}
2349+
#[cfg(not($cfg))]
2350+
fn init() { }
2351+
init();
2352+
} }
2353+
);
2354+
2355+
init_target!(have_component_powerpc PowerPC);
2356+
init_target!(have_component_mips Mips);
2357+
init_target!(have_component_aarch64 AArch64);
2358+
init_target!(have_component_arm ARM);
2359+
init_target!(have_component_x86 X86);
2360+
}
2361+
23302362
// The module containing the native LLVM dependencies, generated by the build system
23312363
// Note that this must come after the rustllvm extern declaration so that
23322364
// parts of LLVM that rustllvm depends on aren't thrown away by the linker.

src/librustc_trans/back/write.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -961,39 +961,7 @@ pub unsafe fn configure_llvm(sess: &Session) {
961961

962962
llvm::LLVMInitializePasses();
963963

964-
// Only initialize the platforms supported by Rust here, because
965-
// using --llvm-root will have multiple platforms that rustllvm
966-
// doesn't actually link to and it's pointless to put target info
967-
// into the registry that Rust cannot generate machine code for.
968-
llvm::LLVMInitializeX86TargetInfo();
969-
llvm::LLVMInitializeX86Target();
970-
llvm::LLVMInitializeX86TargetMC();
971-
llvm::LLVMInitializeX86AsmPrinter();
972-
llvm::LLVMInitializeX86AsmParser();
973-
974-
llvm::LLVMInitializeARMTargetInfo();
975-
llvm::LLVMInitializeARMTarget();
976-
llvm::LLVMInitializeARMTargetMC();
977-
llvm::LLVMInitializeARMAsmPrinter();
978-
llvm::LLVMInitializeARMAsmParser();
979-
980-
llvm::LLVMInitializeAArch64TargetInfo();
981-
llvm::LLVMInitializeAArch64Target();
982-
llvm::LLVMInitializeAArch64TargetMC();
983-
llvm::LLVMInitializeAArch64AsmPrinter();
984-
llvm::LLVMInitializeAArch64AsmParser();
985-
986-
llvm::LLVMInitializeMipsTargetInfo();
987-
llvm::LLVMInitializeMipsTarget();
988-
llvm::LLVMInitializeMipsTargetMC();
989-
llvm::LLVMInitializeMipsAsmPrinter();
990-
llvm::LLVMInitializeMipsAsmParser();
991-
992-
llvm::LLVMInitializePowerPCTargetInfo();
993-
llvm::LLVMInitializePowerPCTarget();
994-
llvm::LLVMInitializePowerPCTargetMC();
995-
llvm::LLVMInitializePowerPCAsmPrinter();
996-
llvm::LLVMInitializePowerPCAsmParser();
964+
llvm::initialize_available_targets();
997965

998966
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
999967
llvm_args.as_ptr());

src/test/run-make/execution-engine/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This is a basic test of LLVM ExecutionEngine functionality using compiled
55
# Rust code built using the `rustc` crate.
66

7+
ifeq ($(filter executionengine,$(LLVM_COMPONENTS)),executionengine)
8+
79
ifneq ($(shell uname),FreeBSD)
810
all:
911
$(RUSTC) test.rs
@@ -12,3 +14,8 @@ else
1214
all:
1315

1416
endif
17+
18+
else
19+
all:
20+
21+
endif

src/test/run-make/simd-ffi/Makefile

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
-include ../tools.mk
22

3+
TARGETS =
4+
ifeq ($(filter arm,$(LLVM_COMPONENTS)),arm)
35
# construct a fairly exhaustive list of platforms that we
46
# support. These ones don't follow a pattern
5-
TARGETS=arm-linux-androideabi arm-unknown-linux-gnueabihf arm-unknown-linux-gnueabi
7+
TARGETS += arm-linux-androideabi arm-unknown-linux-gnueabihf arm-unknown-linux-gnueabi
8+
endif
9+
10+
ifeq ($(filter x86,$(LLVM_COMPONENTS)),x86)
11+
X86_ARCHS = i686 x86_64
12+
else
13+
X86_ARCHS =
14+
endif
615

716
# these ones do, each OS lists the architectures it supports
8-
LINUX=aarch64 i686 x86_64 mips mipsel
9-
WINDOWS=i686 x86_64
17+
LINUX=$(filter aarch64 mips,$(LLVM_COMPONENTS)) $(X86_ARCHS)
18+
ifeq ($(filter mips,$(LLVM_COMPONENTS)),mips)
19+
LINUX += mipsel
20+
endif
21+
22+
WINDOWS=$(X86_ARCHS)
1023
# fails with: failed to get iphonesimulator SDK path: no such file or directory
1124
#IOS=i386 aarch64 armv7
12-
DARWIN=i686 x86_64
25+
DARWIN=$(X86_ARCHS)
1326

1427
$(foreach arch,$(LINUX),$(eval TARGETS += $(arch)-unknown-linux-gnu))
1528
$(foreach arch,$(WINDOWS),$(eval TARGETS += $(arch)-pc-windows-gnu))

src/test/run-make/tools.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TARGET_RPATH_ENV = \
66
$(LD_LIB_PATH_ENVVAR)="$(TMPDIR):$(TARGET_RPATH_DIR):$($(LD_LIB_PATH_ENVVAR))"
77

88
BARE_RUSTC := $(HOST_RPATH_ENV) $(RUSTC)
9-
RUSTC := $(BARE_RUSTC) --out-dir $(TMPDIR) -L $(TMPDIR)
9+
RUSTC := $(BARE_RUSTC) --out-dir $(TMPDIR) -L $(TMPDIR) $(RUSTFLAGS)
1010
#CC := $(CC) -L $(TMPDIR)
1111
HTMLDOCCK := $(PYTHON) $(S)/src/etc/htmldocck.py
1212

0 commit comments

Comments
 (0)