Centralize the handling of install_name and rpath.
[oota-llvm.git] / tools / llvm-shlib / Makefile
1 ##===- tools/shlib/Makefile --------------------------------*- Makefile -*-===##
2
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
7
8 ##===----------------------------------------------------------------------===##
9
10 LEVEL := ../..
11
12 LIBRARYNAME = LLVM-$(LLVMVersion)
13
14 NO_BUILD_ARCHIVE := 1
15 LINK_LIBS_IN_SHARED := 1
16 SHARED_LIBRARY := 1
17
18 include $(LEVEL)/Makefile.config
19
20 ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
21   EXPORTED_SYMBOL_FILE = $(ObjDir)/$(LIBRARYNAME).exports
22
23   ifeq (1,$(ENABLE_EMBED_STDCXX))
24     # It is needed to force static-stdc++.a linked.
25     SHLIB_FRAG_NAMES += stdc++.a.o
26   endif
27
28 endif
29
30 include $(LEVEL)/Makefile.common
31
32 # Include all archives in libLLVM.(so|dylib) except the ones that have
33 # their own dynamic libraries and TableGen.
34 Archives := $(wildcard $(LibDir)/libLLVM*.a)
35 SharedLibraries := $(wildcard $(LibDir)/libLLVM*$(SHLIBEXT))
36 ExcludeFromLibLlvm := $(basename $(SharedLibraries)).a %/libLLVMTableGen.a
37 IncludeInLibLlvm := $(filter-out $(ExcludeFromLibLlvm), $(Archives))
38 LLVMLibsOptions := $(IncludeInLibLlvm:$(LibDir)/lib%.a=-l%)
39 LLVMLibsPaths   := $(IncludeInLibLlvm)
40
41 $(LibName.SO): $(LLVMLibsPaths)
42
43 ifeq ($(HOST_OS),Darwin)
44     # set dylib internal version number to llvmCore submission number
45     ifdef LLVM_SUBMIT_VERSION
46         LLVMLibsOptions := $(LLVMLibsOptions) -Wl,-current_version \
47                         -Wl,$(LLVM_SUBMIT_VERSION).$(LLVM_SUBMIT_SUBVERSION) \
48                         -Wl,-compatibility_version -Wl,1
49     endif
50     # Include everything from the .a's into the shared library.
51     LLVMLibsOptions    := $(LLVMLibsOptions) -all_load
52 endif
53
54 ifeq ($(HOST_OS), $(filter $(HOST_OS), DragonFly Linux FreeBSD GNU/kFreeBSD OpenBSD GNU Bitrig))
55     # Include everything from the .a's into the shared library.
56     LLVMLibsOptions := -Wl,--whole-archive $(LLVMLibsOptions) \
57                        -Wl,--no-whole-archive
58 endif
59
60 ifeq ($(HOST_OS), $(filter $(HOST_OS), DragonFly Linux FreeBSD GNU/kFreeBSD GNU))
61     # Add soname to the library.
62     LLVMLibsOptions += -Wl,--soname,lib$(LIBRARYNAME)$(SHLIBEXT)
63 endif
64
65 ifeq ($(HOST_OS), $(filter $(HOST_OS), Linux GNU GNU/kFreeBSD))
66     # Don't allow unresolved symbols.
67     LLVMLibsOptions += -Wl,--no-undefined
68 endif
69
70 ifeq ($(HOST_OS),SunOS)
71     # add -z allextract ahead of other libraries on Solaris
72     LLVMLibsOptions := -Wl,-z -Wl,allextract $(LLVMLibsOptions)
73 endif
74
75 ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
76
77 SHLIB_STUBS := $(addprefix $(ObjDir)/, $(SHLIB_FRAG_NAMES))
78 SHLIB_FRAGS := $(patsubst %.a.o, $(ObjDir)/%.syms.txt, $(LIBRARYNAME).a.o $(SHLIB_FRAG_NAMES))
79 LLVMLibsOptions := $(SHLIB_STUBS) $(LLVMLibsOptions)
80
81 $(LibName.SO): $(SHLIB_STUBS)
82
83 %.syms.txt: %.a.o
84         $(Echo) Collecting global symbols of $(notdir $*)
85         $(Verb) $(NM_PATH) -g $< > $@
86
87 $(ObjDir)/$(LIBRARYNAME).exports: $(SHLIB_FRAGS) $(ObjDir)/.dir
88         $(Echo) Generating exports for $(LIBRARYNAME)
89         $(Verb) ($(SED) -n \
90                         -e "s/^.* T _\([^.][^.]*\)$$/\1/p" \
91                         -e "s/^.* [BDR] _\([^.][^.]*\)$$/\1 DATA/p" \
92                         $(SHLIB_FRAGS) \
93                  | sort -u) > $@
94
95 $(ObjDir)/$(LIBRARYNAME).a.o: $(LLVMLibsPaths) $(ObjDir)/.dir
96         $(Echo) Linking all LLVMLibs together for $(LIBRARYNAME)
97         $(Verb) $(Link) -nostartfiles -Wl,-r -nodefaultlibs -o $@ \
98                         -Wl,--whole-archive $(LLVMLibsPaths) \
99                         -Wl,--no-whole-archive
100
101 $(ObjDir)/stdc++.a.o: $(ObjDir)/.dir
102         $(Echo) Linking all libs together for static libstdc++.a
103         $(Verb) $(Link) -nostartfiles -Wl,-r -nodefaultlibs -o $@ \
104                         -Wl,--whole-archive -lstdc++ \
105                         -Wl,--no-whole-archive
106 # FIXME: workaround to invalidate -lstdc++
107         $(Echo) Making dummy -lstdc++ to lib
108         $(Verb) $(AR) rc $(ToolDir)/libstdc++.dll.a
109 # FIXME: Is install-local needed?
110
111 clean-local::
112         $(Verb) $(RM) -f $(ToolDir)/libstdc++.dll.a
113
114 endif