@@ -186,7 +186,8 @@ void CodeGenModule::Release() {
186
186
EmitStaticExternCAliases ();
187
187
EmitLLVMUsed ();
188
188
189
- if (CodeGenOpts.Autolink && Context.getLangOpts ().Modules ) {
189
+ if (CodeGenOpts.Autolink &&
190
+ (Context.getLangOpts ().Modules || !LinkerOptionsMetadata.empty ())) {
190
191
EmitModuleLinkOptions ();
191
192
}
192
193
@@ -762,31 +763,41 @@ void CodeGenModule::EmitLLVMUsed() {
762
763
GV->setSection (" llvm.metadata" );
763
764
}
764
765
766
+ void CodeGenModule::AppendLinkerOptions (StringRef Opts) {
767
+ llvm::Value *MDOpts = llvm::MDString::get (getLLVMContext (), Opts);
768
+ LinkerOptionsMetadata.push_back (llvm::MDNode::get (getLLVMContext (), MDOpts));
769
+ }
770
+
771
+ void CodeGenModule::AddDependentLib (StringRef Lib) {
772
+ llvm::SmallString<24 > Opt;
773
+ getTargetCodeGenInfo ().getDependentLibraryOption (Lib, Opt);
774
+ llvm::Value *MDOpts = llvm::MDString::get (getLLVMContext (), Opt);
775
+ LinkerOptionsMetadata.push_back (llvm::MDNode::get (getLLVMContext (), MDOpts));
776
+ }
777
+
765
778
// / \brief Add link options implied by the given module, including modules
766
779
// / it depends on, using a postorder walk.
767
- static void addLinkOptionsPostorder (llvm::LLVMContext &Context ,
780
+ static void addLinkOptionsPostorder (CodeGenModule &CGM ,
768
781
Module *Mod,
769
782
SmallVectorImpl<llvm::Value *> &Metadata,
770
783
llvm::SmallPtrSet<Module *, 16 > &Visited) {
771
784
// Import this module's parent.
772
785
if (Mod->Parent && Visited.insert (Mod->Parent )) {
773
- addLinkOptionsPostorder (Context , Mod->Parent , Metadata, Visited);
786
+ addLinkOptionsPostorder (CGM , Mod->Parent , Metadata, Visited);
774
787
}
775
788
776
789
// Import this module's dependencies.
777
790
for (unsigned I = Mod->Imports .size (); I > 0 ; --I) {
778
791
if (Visited.insert (Mod->Imports [I-1 ]))
779
- addLinkOptionsPostorder (Context , Mod->Imports [I-1 ], Metadata, Visited);
792
+ addLinkOptionsPostorder (CGM , Mod->Imports [I-1 ], Metadata, Visited);
780
793
}
781
794
782
795
// Add linker options to link against the libraries/frameworks
783
796
// described by this module.
797
+ llvm::LLVMContext &Context = CGM.getLLVMContext ();
784
798
for (unsigned I = Mod->LinkLibraries .size (); I > 0 ; --I) {
785
- // FIXME: -lfoo is Unix-centric and -framework Foo is Darwin-centric.
786
- // We need to know more about the linker to know how to encode these
787
- // options propertly.
788
-
789
- // Link against a framework.
799
+ // Link against a framework. Frameworks are currently Darwin only, so we
800
+ // don't to ask TargetCodeGenInfo for the spelling of the linker option.
790
801
if (Mod->LinkLibraries [I-1 ].IsFramework ) {
791
802
llvm::Value *Args[2 ] = {
792
803
llvm::MDString::get (Context, " -framework" ),
@@ -798,9 +809,10 @@ static void addLinkOptionsPostorder(llvm::LLVMContext &Context,
798
809
}
799
810
800
811
// Link against a library.
801
- llvm::Value *OptString
802
- = llvm::MDString::get (Context,
803
- " -l" + Mod->LinkLibraries [I-1 ].Library );
812
+ llvm::SmallString<24 > Opt;
813
+ CGM.getTargetCodeGenInfo ().getDependentLibraryOption (
814
+ Mod->LinkLibraries [I-1 ].Library , Opt);
815
+ llvm::Value *OptString = llvm::MDString::get (Context, Opt);
804
816
Metadata.push_back (llvm::MDNode::get (Context, OptString));
805
817
}
806
818
}
@@ -852,20 +864,23 @@ void CodeGenModule::EmitModuleLinkOptions() {
852
864
}
853
865
854
866
// Add link options for all of the imported modules in reverse topological
855
- // order.
867
+ // order. We don't do anything to try to order import link flags with respect
868
+ // to linker options inserted by things like #pragma comment().
856
869
SmallVector<llvm::Value *, 16 > MetadataArgs;
857
870
Visited.clear ();
858
871
for (llvm::SetVector<clang::Module *>::iterator M = LinkModules.begin (),
859
872
MEnd = LinkModules.end ();
860
873
M != MEnd; ++M) {
861
874
if (Visited.insert (*M))
862
- addLinkOptionsPostorder (getLLVMContext () , *M, MetadataArgs, Visited);
875
+ addLinkOptionsPostorder (* this , *M, MetadataArgs, Visited);
863
876
}
864
877
std::reverse (MetadataArgs.begin (), MetadataArgs.end ());
878
+ LinkerOptionsMetadata.append (MetadataArgs.begin (), MetadataArgs.end ());
865
879
866
880
// Add the linker options metadata flag.
867
881
getModule ().addModuleFlag (llvm::Module::AppendUnique, " Linker Options" ,
868
- llvm::MDNode::get (getLLVMContext (), MetadataArgs));
882
+ llvm::MDNode::get (getLLVMContext (),
883
+ LinkerOptionsMetadata));
869
884
}
870
885
871
886
void CodeGenModule::EmitDeferred () {
0 commit comments