summaryrefslogtreecommitdiff
blob: 214a3bbbc0e2248e87b9f4da166d4c371d95b8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
From 55433193c56e6b2e0792c3ca1b0ef3881ab01138 Mon Sep 17 00:00:00 2001
From: Richard Diamond <wichard@vitalitystudios.com>
Date: Mon, 9 Nov 2015 23:15:38 +0000
Subject: [PATCH 2/4] Fix `llvm-config` to adapt to the install environment.

Summary:
This patch does a couple of things:

  - Adds a new argument `--shared-mode` which accepts a list of components and prints whether or not the provided components need to be linked statically or shared.
  - Fixes `--libnames` when CMake BUILD_SHARED_LIBS is used.
  - Fixes `--libnames`, `--libs`, and `--libfiles` for dylib when static components aren't installed.
  - Fixes `--libnames`, `--libs`, `--libfiles`, and `--components` to use LLVM_DYLIB_COMPONENTS as the component manifest for dylib linking.
  - Uses the host platform's usual convention for filename extensions and such, instead of always defaulting to Unix-izms.

Because I don't own a Mac, I am not able to test the Mac platform dependent stuff locally. If someone would be willing to run a build for me on their machine (unless there's a better option), I'd appreciate it.

Reviewers: jfb, brad.king, whitequark, beanz

Subscribers: beanz, jauhien, llvm-commits

Differential Revision: http://reviews.llvm.org/D13198

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252532 91177308-0d34-0410-b5e6-96231b3b80d8
---
 bindings/ocaml/Makefile.ocaml           |   2 +
 tools/llvm-config/BuildVariables.inc.in |   3 +
 tools/llvm-config/CMakeLists.txt        |   5 +
 tools/llvm-config/llvm-config.cpp       | 265 +++++++++++++++++++++++++++++---
 4 files changed, 253 insertions(+), 22 deletions(-)

diff --git a/bindings/ocaml/Makefile.ocaml b/bindings/ocaml/Makefile.ocaml
index 1f65a7b..22b96a2 100644
--- a/bindings/ocaml/Makefile.ocaml
+++ b/bindings/ocaml/Makefile.ocaml
@@ -277,6 +277,8 @@ uninstall-local:: uninstall-deplibs
 
 build-deplibs: $(OutputLibs)
 
+$(OcamlDir)/%.so: $(LibDir)/%.so
+	$(Verb) ln -sf $< $@
 $(OcamlDir)/%.a: $(LibDir)/%.a
 	$(Verb) ln -sf $< $@
 
diff --git a/tools/llvm-config/BuildVariables.inc.in b/tools/llvm-config/BuildVariables.inc.in
index 0f28fad..e384838 100644
--- a/tools/llvm-config/BuildVariables.inc.in
+++ b/tools/llvm-config/BuildVariables.inc.in
@@ -27,3 +27,6 @@
 #define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@"
 #define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
 #define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@"
+#define LLVM_ENABLE_DYLIB "@LLVM_BUILD_LLVM_DYLIB@"
+#define LLVM_ENABLE_SHARED "@LLVM_ENABLE_SHARED@"
+#define LLVM_DYLIB_COMPONENTS "@LLVM_DYLIB_COMPONENTS@"
diff --git a/tools/llvm-config/CMakeLists.txt b/tools/llvm-config/CMakeLists.txt
index dd53460..173f6dc 100644
--- a/tools/llvm-config/CMakeLists.txt
+++ b/tools/llvm-config/CMakeLists.txt
@@ -30,6 +30,11 @@ set(LLVM_BUILD_SYSTEM cmake)
 set(LLVM_LDFLAGS "${CMAKE_CXX_LINK_FLAGS}")
 set(LLVM_BUILDMODE ${CMAKE_BUILD_TYPE})
 set(LLVM_SYSTEM_LIBS ${SYSTEM_LIBS})
+if(BUILD_SHARED_LIBS)
+  set(LLVM_ENABLE_SHARED ON)
+else()
+  set(LLVM_ENABLE_SHARED OFF)
+endif()
 string(REPLACE ";" " " LLVM_TARGETS_BUILT "${LLVM_TARGETS_TO_BUILD}")
 configure_file(${BUILDVARIABLES_SRCPATH} ${BUILDVARIABLES_OBJPATH} @ONLY)
 
diff --git a/tools/llvm-config/llvm-config.cpp b/tools/llvm-config/llvm-config.cpp
index 4ce8f10..51efbd8 100644
--- a/tools/llvm-config/llvm-config.cpp
+++ b/tools/llvm-config/llvm-config.cpp
@@ -30,6 +30,7 @@
 #include <cstdlib>
 #include <set>
 #include <vector>
+#include <unordered_set>
 
 using namespace llvm;
 
@@ -51,12 +52,16 @@ using namespace llvm;
 /// \param Name - The component to traverse.
 /// \param ComponentMap - A prebuilt map of component names to descriptors.
 /// \param VisitedComponents [in] [out] - The set of already visited components.
-/// \param RequiredLibs [out] - The ordered list of required libraries.
+/// \param RequiredLibs [out] - The ordered list of required
+/// libraries.
+/// \param GetComponentNames - Get the component names instead of the
+/// library name.
 static void VisitComponent(StringRef Name,
                            const StringMap<AvailableComponent*> &ComponentMap,
                            std::set<AvailableComponent*> &VisitedComponents,
                            std::vector<StringRef> &RequiredLibs,
-                           bool IncludeNonInstalled) {
+                           bool IncludeNonInstalled, bool GetComponentNames,
+                           const std::string *ActiveLibDir, bool *HasMissing) {
   // Lookup the component.
   AvailableComponent *AC = ComponentMap.lookup(Name);
   assert(AC && "Invalid component name!");
@@ -74,12 +79,22 @@ static void VisitComponent(StringRef Name,
   // Otherwise, visit all the dependencies.
   for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
     VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
-                   RequiredLibs, IncludeNonInstalled);
+                   RequiredLibs, IncludeNonInstalled, GetComponentNames,
+                   ActiveLibDir, HasMissing);
+  }
+
+  if (GetComponentNames) {
+    RequiredLibs.push_back(Name);
+    return;
   }
 
   // Add to the required library list.
-  if (AC->Library)
+  if (AC->Library) {
+    if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
+      *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
+    }
     RequiredLibs.push_back(AC->Library);
+  }
 }
 
 /// \brief Compute the list of required libraries for a given list of
@@ -91,9 +106,12 @@ static void VisitComponent(StringRef Name,
 /// are required to link the given components.
 /// \param IncludeNonInstalled - Whether non-installed components should be
 /// reported.
+/// \param GetComponentNames - True if one would prefer the component names.
 static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
                                      std::vector<StringRef> &RequiredLibs,
-                                     bool IncludeNonInstalled) {
+                                     bool IncludeNonInstalled, bool GetComponentNames,
+                                     const std::string *ActiveLibDir,
+                                     bool *HasMissing) {
   std::set<AvailableComponent*> VisitedComponents;
 
   // Build a map of component names to information.
@@ -116,7 +134,8 @@ static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
     }
 
     VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
-                   RequiredLibs, IncludeNonInstalled);
+                   RequiredLibs, IncludeNonInstalled, GetComponentNames,
+                   ActiveLibDir, HasMissing);
   }
 
   // The list is now ordered with leafs first, we want the libraries to printed
@@ -157,6 +176,7 @@ Options:\n\
   --host-target     Target triple used to configure LLVM.\n\
   --build-mode      Print build mode of LLVM tree (e.g. Debug or Release).\n\
   --assertion-mode  Print assertion mode of LLVM tree (ON or OFF).\n\
+  --shared-mode     Print how the provided components can be collectively linked (`shared` or `static`).\n\
   --build-system    Print the build system used to build LLVM (autoconf or cmake).\n\
 Typical components:\n\
   all               All LLVM libraries (default).\n\
@@ -172,10 +192,38 @@ std::string GetExecutablePath(const char *Argv0) {
   return llvm::sys::fs::getMainExecutable(Argv0, P);
 }
 
+/// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
+/// the full list of components.
+std::vector<StringRef> GetAllDyLibComponents(const bool IsInDevelopmentTree,
+                                             const bool GetComponentNames) {
+  std::vector<StringRef> DyLibComponents;
+  {
+    StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
+    size_t Offset = 0;
+    while (true) {
+      const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
+      DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset));
+      if (NextOffset == std::string::npos) {
+        break;
+      }
+      Offset = NextOffset + 1;
+    }
+
+    assert(DyLibComponents.size() > 0);
+  }
+
+  std::vector<StringRef> Components;
+  ComputeLibsForComponents(DyLibComponents, Components,
+                           /*IncludeNonInstalled=*/IsInDevelopmentTree,
+                           GetComponentNames, nullptr, nullptr);
+
+  return std::move(Components);
+}
+
 int main(int argc, char **argv) {
   std::vector<StringRef> Components;
   bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
-  bool PrintSystemLibs = false;
+  bool PrintSystemLibs = false, PrintSharedMode = false;
   bool HasAnyOption = false;
 
   // llvm-config is designed to support being run both from a development tree
@@ -270,6 +318,108 @@ int main(int argc, char **argv) {
     ActiveIncludeOption = "-I" + ActiveIncludeDir;
   }
 
+  /// We only use `shared library` mode in cases where the static library form
+  /// of the components provided are not available; note however that this is
+  /// skipped if we're run from within the build dir. However, once installed,
+  /// we still need to provide correct output when the static archives are
+  /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
+  /// in the first place. This can't be done at configure/build time.
+
+  StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
+    StaticPrefix, StaticDir = "lib";
+  const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
+  if (HostTriple.isOSWindows()) {
+    SharedExt = "dll";
+    SharedVersionedExt = PACKAGE_VERSION ".dll";
+    StaticExt = "a";
+    SharedDir = ActiveBinDir;
+    StaticDir = ActiveLibDir;
+    StaticPrefix = SharedPrefix = "";
+  } else if (HostTriple.isOSDarwin()) {
+    SharedExt = "dylib";
+    SharedVersionedExt = PACKAGE_VERSION ".dylib";
+    StaticExt = "a";
+    StaticDir = SharedDir = ActiveLibDir;
+    StaticPrefix = SharedPrefix = "lib";
+  } else {
+    // default to the unix values:
+    SharedExt = "so";
+    SharedVersionedExt = PACKAGE_VERSION ".so";
+    StaticExt = "a";
+    StaticDir = SharedDir = ActiveLibDir;
+    StaticPrefix = SharedPrefix = "lib";
+  }
+
+  const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
+
+  enum { CMake, AutoConf } ConfigTool;
+  if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
+    ConfigTool = CMake;
+  } else {
+    ConfigTool = AutoConf;
+  }
+
+  /// CMake style shared libs, ie each component is in a shared library.
+  const bool BuiltSharedLibs =
+      (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
+
+  bool DyLibExists = false;
+  const std::string DyLibName =
+    (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
+
+  if (BuiltDyLib) {
+    DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
+  }
+
+  /// Get the component's library name without the lib prefix and the
+  /// extension. Returns true if Lib is in a recognized format.
+  auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
+                                          StringRef &Out) {
+    if (Lib.startswith("lib")) {
+      unsigned FromEnd;
+      if (Lib.endswith(StaticExt)) {
+        FromEnd = StaticExt.size() + 1;
+      } else if (Lib.endswith(SharedExt)) {
+        FromEnd = SharedExt.size() + 1;
+      } else {
+        FromEnd = 0;
+      }
+
+      if (FromEnd != 0) {
+        Out = Lib.slice(3, Lib.size() - FromEnd);
+        return true;
+      }
+    }
+
+    return false;
+  };
+  /// Maps Unixizms to the host platform.
+  auto GetComponentLibraryFileName = [&](const StringRef &Lib,
+                                         const bool ForceShared) {
+    std::string LibFileName = Lib;
+    StringRef LibName;
+    if (GetComponentLibraryNameSlice(Lib, LibName)) {
+      if (BuiltSharedLibs || ForceShared) {
+        LibFileName = (SharedPrefix + LibName + "." + SharedExt).str();
+      } else {
+        // default to static
+        LibFileName = (StaticPrefix + LibName + "." + StaticExt).str();
+      }
+    }
+
+    return LibFileName;
+  };
+  /// Get the full path for a possibly shared component library.
+  auto GetComponentLibraryPath = [&](const StringRef &Name,
+                                     const bool ForceShared) {
+    auto LibFileName = GetComponentLibraryFileName(Name, ForceShared);
+    if (BuiltSharedLibs || ForceShared) {
+      return (SharedDir + "/" + LibFileName).str();
+    } else {
+      return (StaticDir + "/" + LibFileName).str();
+    }
+  };
+
   raw_ostream &OS = outs();
   for (int i = 1; i != argc; ++i) {
     StringRef Arg = argv[i];
@@ -303,13 +453,33 @@ int main(int argc, char **argv) {
       } else if (Arg == "--libfiles") {
         PrintLibFiles = true;
       } else if (Arg == "--components") {
+        /// If there are missing static archives and a dylib was
+        /// built, print LLVM_DYLIB_COMPONENTS instead of everything
+        /// in the manifest.
+        std::vector<StringRef> Components;
         for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
           // Only include non-installed components when in a development tree.
           if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
             continue;
 
-          OS << ' ';
-          OS << AvailableComponents[j].Name;
+          Components.push_back(AvailableComponents[j].Name);
+          if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
+            if (DyLibExists &&
+                !sys::fs::exists(GetComponentLibraryPath(
+                    AvailableComponents[j].Library, false))) {
+              Components = GetAllDyLibComponents(IsInDevelopmentTree, true);
+              std::sort(Components.begin(), Components.end());
+              break;
+            }
+          }
+        }
+
+        for (unsigned I = 0; I < Components.size(); ++I) {
+          if (I) {
+            OS << ' ';
+          }
+
+          OS << Components[I];
         }
         OS << '\n';
       } else if (Arg == "--targets-built") {
@@ -324,6 +494,8 @@ int main(int argc, char **argv) {
 #else
         OS << "ON\n";
 #endif
+      } else if (Arg == "--shared-mode") {
+        PrintSharedMode = true;
       } else if (Arg == "--build-system") {
         OS << LLVM_BUILD_SYSTEM << '\n';
       } else if (Arg == "--obj-root") {
@@ -341,35 +513,84 @@ int main(int argc, char **argv) {
   if (!HasAnyOption)
     usage();
 
-  if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
+  if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
+      PrintSharedMode) {
+
+    if (PrintSharedMode && BuiltSharedLibs) {
+      OS << "shared\n";
+      return 0;
+    }
+
     // If no components were specified, default to "all".
     if (Components.empty())
       Components.push_back("all");
 
     // Construct the list of all the required libraries.
     std::vector<StringRef> RequiredLibs;
+    bool HasMissing = false;
     ComputeLibsForComponents(Components, RequiredLibs,
-                             /*IncludeNonInstalled=*/IsInDevelopmentTree);
+                             /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
+                             &ActiveLibDir, &HasMissing);
+
+    if (PrintSharedMode) {
+      std::unordered_set<std::string> FullDyLibComponents;
+      std::vector<StringRef> DyLibComponents =
+          GetAllDyLibComponents(IsInDevelopmentTree, false);
+
+      for (auto &Component : DyLibComponents) {
+        FullDyLibComponents.insert(Component);
+      }
+      DyLibComponents.clear();
+
+      for (auto &Lib : RequiredLibs) {
+        if (!FullDyLibComponents.count(Lib)) {
+          OS << "static\n";
+          return 0;
+        }
+      }
+      FullDyLibComponents.clear();
+
+      if (HasMissing && DyLibExists) {
+        OS << "shared\n";
+        return 0;
+      } else {
+        OS << "static\n";
+        return 0;
+      }
+    }
 
     if (PrintLibs || PrintLibNames || PrintLibFiles) {
-      for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
-        StringRef Lib = RequiredLibs[i];
-        if (i)
-          OS << ' ';
 
+      auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) {
         if (PrintLibNames) {
-          OS << Lib;
+          OS << GetComponentLibraryFileName(Lib, ForceShared);
         } else if (PrintLibFiles) {
-          OS << ActiveLibDir << '/' << Lib;
+          OS << GetComponentLibraryPath(Lib, ForceShared);
         } else if (PrintLibs) {
           // If this is a typical library name, include it using -l.
-          if (Lib.startswith("lib") && Lib.endswith(".a")) {
-            OS << "-l" << Lib.slice(3, Lib.size()-2);
-            continue;
+          StringRef LibName;
+          if (Lib.startswith("lib")) {
+            if (GetComponentLibraryNameSlice(Lib, LibName)) {
+              OS << "-l" << LibName;
+            } else {
+              OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared);
+            }
+          } else {
+            // Otherwise, print the full path.
+            OS << GetComponentLibraryPath(Lib, ForceShared);
           }
+        }
+      };
+
+      if (HasMissing && DyLibExists) {
+        PrintForLib(DyLibName, true);
+      } else {
+        for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
+          StringRef Lib = RequiredLibs[i];
+          if (i)
+            OS << ' ';
 
-          // Otherwise, print the full path.
-          OS << ActiveLibDir << '/' << Lib;
+          PrintForLib(Lib, false);
         }
       }
       OS << '\n';
-- 
2.7.4