llvm.org GIT mirror llvm / a04a818
Revert r257003 This revision breaks llvm-config if you set BUILD_SHARED_LIBS=on in a CMake build. Backing out until the fix is ready to land. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257457 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Wilkins 3 years ago
6 changed file(s) with 51 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
910910 set(cppflags "${cppflags} -I${d}")
911911 endforeach(d)
912912 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
913 if (LLVM_LINK_LLVM_DYLIB)
914 set(linkmode "dylib")
915 else()
916 set(linkmode "component-libs")
917 endif()
913918 add_custom_command(OUTPUT ${binpath}
914 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
919 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "linkmode=${linkmode}"
915920 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
916921 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
917922 ${llvmlibs} ${ARG_DEPENDS}
2828 #define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@"
2929 #define LLVM_HAS_RTTI "@LLVM_HAS_RTTI@"
3030 #define LLVM_ENABLE_DYLIB "@LLVM_BUILD_LLVM_DYLIB@"
31 #define LLVM_LINK_DYLIB "@LLVM_LINK_LLVM_DYLIB@"
3231 #define LLVM_ENABLE_SHARED "@LLVM_ENABLE_SHARED@"
3332 #define LLVM_DYLIB_COMPONENTS "@LLVM_DYLIB_COMPONENTS@"
34 #define LLVM_DYLIB_VERSION "@LLVM_DYLIB_VERSION@"
2525 set(LLVM_CXXFLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${COMPILE_FLAGS} ${LLVM_DEFINITIONS}")
2626 set(LLVM_BUILD_SYSTEM cmake)
2727 set(LLVM_HAS_RTTI ${LLVM_CONFIG_HAS_RTTI})
28 set(LLVM_DYLIB_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX}")
2928
3029 # Use the C++ link flags, since they should be a superset of C link flags.
3130 set(LLVM_LDFLAGS "${CMAKE_CXX_LINK_FLAGS}")
4343 LLVM_HAS_RTTI := YES
4444 endif
4545
46 LLVM_DYLIB_VERSION := $(LLVM_VERSION_MAJOR).$(LLVM_VERSION_MINOR)$(LLVM_VERSION_SUFFIX)
47
4846 # This is blank for now. We need to be careful about adding stuff here:
4947 # LDFLAGS tend not to be portable, and we don't currently require the
5048 # user to use libtool when linking against LLVM.
8482 >> temp.sed
8583 $(Verb) $(ECHO) 's/@LLVM_HAS_RTTI@/$(LLVM_HAS_RTTI)/' \
8684 >> temp.sed
87 $(Verb) $(ECHO) 's/@LLVM_DYLIB_VERSION@/$(LLVM_DYLIB_VERSION)/' \
88 >> temp.sed
8985 $(Verb) $(SED) -f temp.sed < $< > $@
9086 $(Verb) $(RM) temp.sed
9187
2828 #include "llvm/Support/raw_ostream.h"
2929 #include
3030 #include
31 #include
3132 #include
32 #include
3333
3434 using namespace llvm;
3535
4444 // Not all components define a library, we also use "library groups" as a way to
4545 // create entries for pseudo groups like x86 or all-targets.
4646 #include "LibraryDependencies.inc"
47
48 // LinkMode determines what libraries and flags are returned by llvm-config.
49 enum LinkMode {
50 // LinkModeAuto will link with the default link mode for the installation,
51 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back
52 // to the alternative if the required libraries are not available.
53 LinkModeAuto = 0,
54
55 // LinkModeDyLib will link with the single dynamic library if it exists,
56 // and return an error if it does not exist.
57 LinkModeDyLib = 1,
58
59 // LinkModeStatic will link with the individual component static libraries
60 // if they exist, and fail if any one does not exist.
61 LinkModeStatic = 2,
62 };
6347
6448 /// \brief Traverse a single component adding to the topological ordering in
6549 /// \arg RequiredLibs.
7155 /// libraries.
7256 /// \param GetComponentNames - Get the component names instead of the
7357 /// library name.
74 static void VisitComponent(const std::string &Name,
75 const StringMap &ComponentMap,
76 std::set &VisitedComponents,
58 static void VisitComponent(const std::string& Name,
59 const StringMap &ComponentMap,
60 std::set &VisitedComponents,
7761 std::vector &RequiredLibs,
7862 bool IncludeNonInstalled, bool GetComponentNames,
79 const std::string *ActiveLibDir,
80 std::vector *Missing) {
63 const std::string *ActiveLibDir, bool *HasMissing) {
8164 // Lookup the component.
8265 AvailableComponent *AC = ComponentMap.lookup(Name);
8366 assert(AC && "Invalid component name!");
9679 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
9780 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
9881 RequiredLibs, IncludeNonInstalled, GetComponentNames,
99 ActiveLibDir, Missing);
82 ActiveLibDir, HasMissing);
10083 }
10184
10285 if (GetComponentNames) {
10689
10790 // Add to the required library list.
10891 if (AC->Library) {
109 if (Missing && ActiveLibDir) {
110 std::string path = *ActiveLibDir + "/" + AC->Library;
111 if (!sys::fs::exists(path))
112 Missing->push_back(path);
92 if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
93 *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
11394 }
11495 RequiredLibs.push_back(AC->Library);
11596 }
126107 static std::vector
127108 ComputeLibsForComponents(const std::vector &Components,
128109 bool IncludeNonInstalled, bool GetComponentNames,
129 const std::string *ActiveLibDir,
130 std::vector *Missing) {
110 const std::string *ActiveLibDir, bool *HasMissing) {
131111 std::vector RequiredLibs;
132112 std::set VisitedComponents;
133113
134114 // Build a map of component names to information.
135 StringMap *> ComponentMap;
115 StringMap*> ComponentMap;
136116 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
137117 AvailableComponent *AC = &AvailableComponents[i];
138118 ComponentMap[AC->Name] = AC;
152132
153133 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
154134 RequiredLibs, IncludeNonInstalled, GetComponentNames,
155 ActiveLibDir, Missing);
135 ActiveLibDir, HasMissing);
156136 }
157137
158138 // The list is now ordered with leafs first, we want the libraries to printed
198178 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
199179 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
200180 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
201 --link-shared Link the components as a shared library.\n\
202 --link-static Link the components as a static libraries.\n\
203181 Typical components:\n\
204182 all All LLVM libraries (default).\n\
205183 engine Either a native JIT or a bitcode interpreter.\n";
210188 std::string GetExecutablePath(const char *Argv0) {
211189 // This just needs to be some symbol in the binary; C++ doesn't
212190 // allow taking the address of ::main however.
213 void *P = (void *)(intptr_t)GetExecutablePath;
191 void *P = (void*) (intptr_t) GetExecutablePath;
214192 return llvm::sys::fs::getMainExecutable(Argv0, P);
215193 }
216194
264242 // Create an absolute path, and pop up one directory (we expect to be inside a
265243 // bin dir).
266244 sys::fs::make_absolute(CurrentPath);
267 CurrentExecPrefix =
268 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
245 CurrentExecPrefix = sys::path::parent_path(
246 sys::path::parent_path(CurrentPath)).str();
269247
270248 // Check to see if we are inside a development tree by comparing to possible
271249 // locations (prefix style or CMake style).
326304 }
327305
328306 // We need to include files from both the source and object trees.
329 ActiveIncludeOption =
330 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
307 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
308 "-I" + ActiveObjRoot + "/include");
331309 } else {
332310 ActivePrefix = CurrentExecPrefix;
333311 ActiveIncludeDir = ActivePrefix + "/include";
344322 /// in the first place. This can't be done at configure/build time.
345323
346324 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
347 StaticPrefix, StaticDir = "lib";
325 StaticPrefix, StaticDir = "lib";
348326 const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
349327 if (HostTriple.isOSWindows()) {
350328 SharedExt = "dll";
351 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
329 SharedVersionedExt = PACKAGE_VERSION ".dll";
352330 StaticExt = "a";
353331 SharedDir = ActiveBinDir;
354332 StaticDir = ActiveLibDir;
355333 StaticPrefix = SharedPrefix = "lib";
356334 } else if (HostTriple.isOSDarwin()) {
357335 SharedExt = "dylib";
358 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
336 SharedVersionedExt = PACKAGE_VERSION ".dylib";
359337 StaticExt = "a";
360338 StaticDir = SharedDir = ActiveLibDir;
361339 StaticPrefix = SharedPrefix = "lib";
362340 } else {
363341 // default to the unix values:
364342 SharedExt = "so";
365 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
343 SharedVersionedExt = PACKAGE_VERSION ".so";
366344 StaticExt = "a";
367345 StaticDir = SharedDir = ActiveLibDir;
368346 StaticPrefix = SharedPrefix = "lib";
383361
384362 bool DyLibExists = false;
385363 const std::string DyLibName =
386 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
387
388 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
389 // for "--libs", etc, if they exist. This behaviour can be overridden with
390 // --link-static or --link-shared.
391 bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0);
364 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
392365
393366 if (BuiltDyLib) {
394367 DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
395 if (!DyLibExists) {
396 // The shared library does not exist: don't error unless the user
397 // explicitly passes --link-shared.
398 LinkDyLib = false;
399 }
400 }
401 LinkMode LinkMode = LinkDyLib ? LinkModeDyLib : LinkModeAuto;
368 }
402369
403370 /// Get the component's library name without the lib prefix and the
404371 /// extension. Returns true if Lib is in a recognized format.
533500 OS << ActivePrefix << '\n';
534501 } else if (Arg == "--src-root") {
535502 OS << LLVM_SRC_ROOT << '\n';
536 } else if (Arg == "--link-shared") {
537 LinkMode = LinkModeDyLib;
538 } else if (Arg == "--link-static") {
539 LinkMode = LinkModeStatic;
540503 } else {
541504 usage();
542505 }
547510
548511 if (!HasAnyOption)
549512 usage();
550
551 if (LinkMode == LinkModeDyLib && !DyLibExists) {
552 errs() << "llvm-config: error: " << DyLibName << " is missing\n\n";
553 usage();
554 }
555513
556514 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
557515 PrintSharedMode) {
566524 Components.push_back("all");
567525
568526 // Construct the list of all the required libraries.
569 std::vector MissingLibs;
527 bool HasMissing = false;
570528 std::vector RequiredLibs =
571529 ComputeLibsForComponents(Components,
572530 /*IncludeNonInstalled=*/IsInDevelopmentTree,
573 false, &ActiveLibDir, &MissingLibs);
574 if (!MissingLibs.empty()) {
575 switch (LinkMode) {
576 case LinkModeDyLib:
577 break;
578 case LinkModeAuto:
579 if (DyLibExists) {
580 LinkMode = LinkModeDyLib;
581 break;
582 }
583 errs()
584 << "llvm-config: error: component libraries and shared library\n\n";
585 // fall through
586 case LinkModeStatic:
587 for (auto &Lib : MissingLibs)
588 errs() << "llvm-config: error: missing: " << Lib << "\n";
589 return 1;
590 }
591 } else if (LinkMode == LinkModeAuto) {
592 LinkMode = LinkModeStatic;
593 }
531 false, &ActiveLibDir, &HasMissing);
594532
595533 if (PrintSharedMode) {
596534 std::unordered_set FullDyLibComponents;
610548 }
611549 FullDyLibComponents.clear();
612550
613 if (LinkMode == LinkModeDyLib) {
551 if (HasMissing && DyLibExists) {
614552 OS << "shared\n";
615553 return 0;
616554 } else {
642580 }
643581 };
644582
645 if (LinkMode == LinkModeDyLib) {
583 if (HasMissing && DyLibExists) {
646584 PrintForLib(DyLibName, true);
647585 } else {
648586 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
8787 return outstr
8888 }
8989
90 func llvmFlags() compilerFlags {
91 ldflags := llvmConfig("--ldflags", "--libs", "--system-libs")
90 func llvmFlags(linkmode string) compilerFlags {
91 ldflags := llvmConfig("--ldflags")
92 switch linkmode {
93 case linkmodeComponentLibs:
94 ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...)
95 case linkmodeDylib:
96 ldflags += " -lLLVM"
97 default:
98 panic("invalid linkmode: " + linkmode)
99 }
100 ldflags += " " + llvmConfig("--system-libs")
92101 if runtime.GOOS != "darwin" {
93102 // OS X doesn't like -rpath with cgo. See:
94103 // https://code.google.com/p/go/issues/detail?id=7293
123132 fmt.Println(strings.Join(components, " "))
124133 }
125134
126 func printConfig() {
127 flags := llvmFlags()
135 func printConfig(linkmode string) {
136 flags := llvmFlags(linkmode)
128137
129138 fmt.Printf(`// +build !byollvm
130139
143152 `, flags.cpp, flags.cxx, flags.ld)
144153 }
145154
146 func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags string) {
155 func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode string) {
147156 args = addTag(args, "byollvm")
148157
149158 srcdir := llvmConfig("--src-root")
172181 newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
173182 newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
174183
175 flags := llvmFlags()
184 flags := llvmFlags(linkmode)
176185
177186 newenv := []string{
178187 "CC=" + cc,
240249 ldflags := os.Getenv("CGO_LDFLAGS")
241250 gocmd := "go"
242251 llgo := ""
252 linkmode := linkmodeComponentLibs
243253
244254 flags := []struct {
245255 name string
251261 {"llgo", &llgo},
252262 {"cppflags", &cppflags},
253263 {"ldflags", &ldflags},
264 {"linkmode", &linkmode},
254265 }
255266
256267 args := os.Args[1:]
271282
272283 switch args[0] {
273284 case "build", "get", "install", "run", "test":
274 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags)
285 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode)
275286 case "print-components":
276287 printComponents()
277288 case "print-config":
278 printConfig()
289 printConfig(linkmode)
279290 default:
280291 usage()
281292 }