From 34f7364332c95b63a742709b96141142f6cfe3d8 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 21 Jan 2016 15:39:22 -0800 Subject: [PATCH] rustc_llvm: Tweak how initialization is performed Refactor a bit to have less repetition and #[cfg] and try to bury it all inside of a macro. --- mk/llvm.mk | 2 +- src/librustc_llvm/lib.rs | 117 +++++++++++++-------------------------- 2 files changed, 40 insertions(+), 79 deletions(-) diff --git a/mk/llvm.mk b/mk/llvm.mk index a4174efa5ef..2bdbef35bad 100644 --- a/mk/llvm.mk +++ b/mk/llvm.mk @@ -102,7 +102,7 @@ $(foreach host,$(CFG_HOST), \ define LLVM_LINKAGE_DEPS $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc_llvm: $$(LLVM_LINKAGE_PATH_$(2)) RUSTFLAGS$(1)_rustc_llvm_T_$(2) += $$(shell echo $$(LLVM_ALL_COMPONENTS_$(2)) | tr '-' '_' |\ - sed -e 's/^ //;s/\([^ ]*\)/\-\-cfg have_component_\1/g') + sed -e 's/^ //;s/\([^ ]*\)/\-\-cfg "llvm_component=\\"\1\\""/g') endef $(foreach source,$(CFG_HOST), \ diff --git a/src/librustc_llvm/lib.rs b/src/librustc_llvm/lib.rs index 8877479104e..1933c926e30 100644 --- a/src/librustc_llvm/lib.rs +++ b/src/librustc_llvm/lib.rs @@ -2166,53 +2166,6 @@ extern { pub fn LLVMRustFreeOperandBundleDef(Bundle: OperandBundleDefRef); } -#[cfg(have_component_x86)] -extern { - pub fn LLVMInitializeX86TargetInfo(); - pub fn LLVMInitializeX86Target(); - pub fn LLVMInitializeX86TargetMC(); - pub fn LLVMInitializeX86AsmPrinter(); - pub fn LLVMInitializeX86AsmParser(); -} -#[cfg(have_component_arm)] -extern { - pub fn LLVMInitializeARMTargetInfo(); - pub fn LLVMInitializeARMTarget(); - pub fn LLVMInitializeARMTargetMC(); - pub fn LLVMInitializeARMAsmPrinter(); - pub fn LLVMInitializeARMAsmParser(); -} -#[cfg(have_component_aarch64)] -extern { - pub fn LLVMInitializeAArch64TargetInfo(); - pub fn LLVMInitializeAArch64Target(); - pub fn LLVMInitializeAArch64TargetMC(); - pub fn LLVMInitializeAArch64AsmPrinter(); - pub fn LLVMInitializeAArch64AsmParser(); -} -#[cfg(have_component_mips)] -extern { - pub fn LLVMInitializeMipsTargetInfo(); - pub fn LLVMInitializeMipsTarget(); - pub fn LLVMInitializeMipsTargetMC(); - pub fn LLVMInitializeMipsAsmPrinter(); - pub fn LLVMInitializeMipsAsmParser(); -} -#[cfg(have_component_powerpc)] -extern { - pub fn LLVMInitializePowerPCTargetInfo(); - pub fn LLVMInitializePowerPCTarget(); - pub fn LLVMInitializePowerPCTargetMC(); - pub fn LLVMInitializePowerPCAsmPrinter(); - pub fn LLVMInitializePowerPCAsmParser(); -} -#[cfg(have_component_pnacl)] -extern { - pub fn LLVMInitializePNaClTargetInfo(); - pub fn LLVMInitializePNaClTarget(); - pub fn LLVMInitializePNaClTargetMC(); -} - // LLVM requires symbols from this library, but apparently they're not printed // during llvm-config? #[cfg(windows)] @@ -2399,20 +2352,14 @@ pub unsafe fn debug_loc_to_string(c: ContextRef, tr: DebugLocRef) -> String { pub fn initialize_available_targets() { macro_rules! init_target( - ($cfg:ident $arch:ident) => { { + ($cfg:meta, $($method:ident),*) => { { #[cfg($cfg)] fn init() { + extern { + $(fn $method();)* + } unsafe { - let f = concat_idents!(LLVMInitialize, $arch, TargetInfo); - f(); - let f = concat_idents!(LLVMInitialize, $arch, Target); - f(); - let f = concat_idents!(LLVMInitialize, $arch, TargetMC); - f(); - let f = concat_idents!(LLVMInitialize, $arch, AsmPrinter); - f(); - let f = concat_idents!(LLVMInitialize, $arch, AsmParser); - f(); + $($method();)* } } #[cfg(not($cfg))] @@ -2420,26 +2367,40 @@ pub fn initialize_available_targets() { init(); } } ); - - init_target!(have_component_powerpc PowerPC); - init_target!(have_component_mips Mips); - init_target!(have_component_aarch64 AArch64); - init_target!(have_component_arm ARM); - init_target!(have_component_x86 X86); - - // PNaCl doesn't provide some of the optional target components, so we - // manually initialize it here. - #[cfg(have_component_pnacl)] - fn init_pnacl() { - unsafe { - LLVMInitializePNaClTargetInfo(); - LLVMInitializePNaClTarget(); - LLVMInitializePNaClTargetMC(); - } - } - #[cfg(not(have_component_pnacl))] - fn init_pnacl() { } - init_pnacl(); + init_target!(llvm_component = "x86", + LLVMInitializeX86TargetInfo, + LLVMInitializeX86Target, + LLVMInitializeX86TargetMC, + LLVMInitializeX86AsmPrinter, + LLVMInitializeX86AsmParser); + init_target!(llvm_component = "arm", + LLVMInitializeARMTargetInfo, + LLVMInitializeARMTarget, + LLVMInitializeARMTargetMC, + LLVMInitializeARMAsmPrinter, + LLVMInitializeARMAsmParser); + init_target!(llvm_component = "aarch64", + LLVMInitializeAArch64TargetInfo, + LLVMInitializeAArch64Target, + LLVMInitializeAArch64TargetMC, + LLVMInitializeAArch64AsmPrinter, + LLVMInitializeAArch64AsmParser); + init_target!(llvm_component = "mips", + LLVMInitializeMipsTargetInfo, + LLVMInitializeMipsTarget, + LLVMInitializeMipsTargetMC, + LLVMInitializeMipsAsmPrinter, + LLVMInitializeMipsAsmParser); + init_target!(llvm_component = "powerpc", + LLVMInitializePowerPCTargetInfo, + LLVMInitializePowerPCTarget, + LLVMInitializePowerPCTargetMC, + LLVMInitializePowerPCAsmPrinter, + LLVMInitializePowerPCAsmParser); + init_target!(llvm_component = "pnacl", + LLVMInitializePNaClTargetInfo, + LLVMInitializePNaClTarget, + LLVMInitializePNaClTargetMC); } pub fn last_error() -> Option {