llvm.org GIT mirror llvm / f6ace19
Revert commit 145449 (ddunbar) since it is breaking the dragonegg buildbots. Original commit message: llvm-config: Replace with C++ version (was llvm-config-2). - Reapply of r144300, with lots of fixes/migration easement in between. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@145582 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 7 years ago
17 changed file(s) with 1304 addition(s) and 427 deletion(s). Raw diff Collapse all Expand all
229229
230230 set(LLVMBUILDTOOL "${LLVM_MAIN_SRC_DIR}/utils/llvm-build/llvm-build")
231231 set(LLVMCONFIGLIBRARYDEPENDENCIESINC
232 "${LLVM_BINARY_DIR}/tools/llvm-config/LibraryDependencies.inc")
232 "${LLVM_BINARY_DIR}/tools/llvm-config-2/LibraryDependencies.inc")
233233 set(LLVMBUILDCMAKEFRAG
234234 "${LLVM_BINARY_DIR}/LLVMBuild.cmake")
235235 message(STATUS "Constructing LLVMBuild project information")
2626 ifneq ($(findstring llvmCore, $(RC_ProjectName)),llvmCore) # Normal build (not "Apple-style").
2727
2828 ifeq ($(BUILD_DIRS_ONLY),1)
29 DIRS := lib/Support lib/TableGen utils tools/llvm-config
29 DIRS := lib/Support lib/TableGen utils tools/llvm-config-2
3030 OPTIONAL_DIRS := tools/clang/utils/TableGen
3131 else
3232 DIRS := lib/Support lib/TableGen utils lib/VMCore lib tools/llvm-shlib \
33 tools/llvm-config tools runtime docs unittests
33 tools/llvm-config tools/llvm-config-2 tools runtime docs unittests
3434 OPTIONAL_DIRS := projects bindings
3535 endif
3636
7878 # The files we are going to generate using llvm-build.
7979 LLVMBuildMakeFrag := $(PROJ_OBJ_ROOT)/Makefile.llvmbuild
8080 LLVMConfigLibraryDependenciesInc := \
81 $(PROJ_OBJ_ROOT)/tools/llvm-config/LibraryDependencies.inc
81 $(PROJ_OBJ_ROOT)/tools/llvm-config-2/LibraryDependencies.inc
8282
8383 # This is for temporary backwards compatibility.
8484 ifndef TARGET_NATIVE_ARCH
527527 endif
528528 endif
529529 ifeq ($(LLVM_CROSS_COMPILING),1)
530 LLVM_CONFIG := $(BuildLLVMToolDir)/llvm-config$(BUILD_EXEEXT)
531 else
532 LLVM_CONFIG := $(LLVMToolDir)/llvm-config$(EXEEXT)
530 LLVM_CONFIG := $(BuildLLVMToolDir)/llvm-config-2$(BUILD_EXEEXT)
531 else
532 LLVM_CONFIG := $(LLVMToolDir)/llvm-config-2$(EXEEXT)
533533 endif
534534 ifndef LLVMLD
535535 LLVMLD := $(LLVMToolDir)/llvm-ld$(EXEEXT)
15771577 AC_CONFIG_FILES([tools/clang/docs/doxygen.cfg])
15781578 fi
15791579
1580 dnl Do the first stage of configuration for llvm-config.in.
1581 AC_CONFIG_FILES([tools/llvm-config/llvm-config.in])
1582
15801583 dnl OCaml findlib META file
15811584 AC_CONFIG_FILES([bindings/ocaml/llvm/META.llvm])
15821585
2109921099
2110021100 fi
2110121101
21102
21102 ac_config_files="$ac_config_files tools/llvm-config/llvm-config.in"
2110321103
2110421104
2110521105 ac_config_files="$ac_config_files bindings/ocaml/llvm/META.llvm"
1010 endif( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/polly/CMakeLists.txt )
1111
1212 if( NOT WIN32 OR MSYS OR CYGWIN )
13 # We currently require 'sed' to build llvm-config, so don't try to build it
13 # It is useful to build llvm-config before the other tools, so we
14 # have a fresh LibDeps.txt for regenerating the hard-coded library
15 # dependencies. llvm-config/CMakeLists.txt takes care of this but we
16 # must keep llvm-config as the first entry on the list of tools to
17 # be built.
18 add_subdirectory(llvm-config)
19
20 # We currently require 'sed' to build llvm-config-2, so don't try to build it
1421 # on pure Win32.
15 add_subdirectory(llvm-config)
22 add_subdirectory(llvm-config-2)
1623 endif()
1724
1825 add_subdirectory(opt)
3333 bugpoint llvm-bcanalyzer llvm-stub \
3434 llvm-diff macho-dump llvm-objdump \
3535 llvm-rtdyld llvm-dwarfdump llvm-cov \
36 llvm-size
36 llvm-size llvm-config-2
3737
3838 # Let users override the set of tools to build from the command line.
3939 ifdef ONLY_TOOLS
None //===-- BuildVariables.inc.in - llvm-config build variables -*- C++ -*-----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is configured by the build system to define the variables
10 // llvm-config wants to report to the user, but which can only be determined at
11 // build time.
12 //
13 // The non .in variant of this file has been autogenerated by the LLVM build. Do
14 // not edit!
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define LLVM_SRC_ROOT "@LLVM_SRC_ROOT@"
19 #define LLVM_OBJ_ROOT "@LLVM_OBJ_ROOT@"
20 #define LLVM_CPPFLAGS "@LLVM_CPPFLAGS@"
21 #define LLVM_CFLAGS "@LLVM_CFLAGS@"
22 #define LLVM_LDFLAGS "@LLVM_LDFLAGS@"
23 #define LLVM_CXXFLAGS "@LLVM_CXXFLAGS@"
24 #define LLVM_BUILDMODE "@LLVM_BUILDMODE@"
25 #define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
None set(LLVM_LINK_COMPONENTS support)
0 include(TestBigEndian)
11
2 # We need to generate the BuildVariables.inc file containing values which are
3 # only defined when under certain build modes. Unfortunately, that precludes
4 # doing this inside CMake so we have to shell out to sed. For now, that means we
5 # can't expect to build llvm-config on Window.s
6 set(BUILDVARIABLES_SRCPATH ${CMAKE_CURRENT_SOURCE_DIR}/BuildVariables.inc.in)
7 set(BUILDVARIABLES_OBJPATH ${CMAKE_CURRENT_BINARY_DIR}/BuildVariables.inc)
8 set(SEDSCRIPT_OBJPATH ${CMAKE_CURRENT_BINARY_DIR}/BuildVariables.configure.sed)
2 include(FindPerl)
3 if( NOT PERL_FOUND )
4 message(FATAL_ERROR "Perl required but not found!")
5 endif( NOT PERL_FOUND )
96
10 # Compute the substitution values for various items.
7 set(PERL ${PERL_EXECUTABLE})
8 set(VERSION PACKAGE_VERSION)
9 set(PREFIX ${CMAKE_INSTALL_PREFIX})
10 set(abs_top_srcdir ${LLVM_MAIN_SRC_DIR})
11 set(abs_top_builddir ${LLVM_BINARY_DIR})
12 execute_process(COMMAND date
13 OUTPUT_VARIABLE LLVM_CONFIGTIME
14 OUTPUT_STRIP_TRAILING_WHITESPACE)
15 # LLVM_ON_UNIX and LLVM_ON_WIN32 already set.
16 # those are set to blank by `autoconf' on MinGW, so it seems they are not required:
17 #set(LLVMGCCDIR "")
18 #set(LLVMGCC "")
19 #set(LLVMGXX "")
20 test_big_endian(IS_BIG_ENDIAN)
21 if( IS_BIG_ENDIAN )
22 set(ENDIAN "big")
23 else( IS_BIG_ENDIAN )
24 set(ENDIAN "little")
25 endif( IS_BIG_ENDIAN )
26 set(SHLIBEXT ${LTDL_SHLIB_EXT})
27 #EXEEXT already set.
28 set(OS "${CMAKE_SYSTEM}")
29 set(target "${TARGET_TRIPLE}")
30 set(ARCH "${LLVM_NATIVE_ARCH}")
31
1132 get_system_libs(LLVM_SYSTEM_LIBS_LIST)
1233 foreach(l ${LLVM_SYSTEM_LIBS_LIST})
13 set(SYSTEM_LIBS ${SYSTEM_LIBS} "-l${l}")
34 set(LLVM_SYSTEM_LIBS ${LLVM_SYSTEM_LIBS} "-l${l}")
1435 endforeach()
36
37 foreach(c ${LLVM_TARGETS_TO_BUILD})
38 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
39 endforeach(c)
40 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
41 set(TARGET_HAS_JIT "1") # TODO
42
43 # Avoids replacement at config-time:
44 set(LLVM_CPPFLAGS "@LLVM_CPPFLAGS@")
45 set(LLVM_CFLAGS "@LLVM_CFLAGS@")
46 set(LLVM_CXXFLAGS "@LLVM_CXXFLAGS@")
47 set(LLVM_LDFLAGS "@LLVM_LDFLAGS@")
48 set(LIBS "@LIBS@")
49 set(LLVM_BUILDMODE "@LLVM_BUILDMODE@")
50 set(LLVM_OBJ_SUFFIX "@LLVM_OBJ_SUFFIX@")
51
52 configure_file(
53 ${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.in.in
54 ${CMAKE_CURRENT_BINARY_DIR}/llvm-config.in
55 @ONLY
56 )
57
58 set(LIBDEPS LibDeps.txt)
59 set(LIBDEPS_TMP LibDeps.txt.tmp)
60 set(FINAL_LIBDEPS FinalLibDeps.txt)
61 set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config)
62 set(LLVM_CONFIG_IN ${CMAKE_CURRENT_BINARY_DIR}/llvm-config.in)
63
64 if( CMAKE_CROSSCOMPILING )
65 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
66 endif()
67
68 find_program(NM_PATH nm PATH_SUFFIXES /bin)
69
70 if( NOT NM_PATH )
71 message(FATAL_ERROR "`nm' not found")
72 endif()
73
74 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
75
76 add_custom_command(OUTPUT ${LIBDEPS_TMP}
77 COMMAND ${PERL_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/GenLibDeps.pl -flat ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR} ${NM_PATH} > ${LIBDEPS_TMP}
78 DEPENDS ${llvm_libs}
79 COMMENT "Regenerating ${LIBDEPS_TMP}")
80
81 add_custom_command(OUTPUT ${LIBDEPS}
82 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBDEPS_TMP} ${LIBDEPS}
83 DEPENDS ${LIBDEPS_TMP}
84 COMMENT "Updating ${LIBDEPS} if necessary...")
85
86 # This must stop the build if find-cycles.pl returns error:
87 add_custom_command(OUTPUT ${FINAL_LIBDEPS}
88 COMMAND ${CMAKE_COMMAND} -E remove -f ${FINAL_LIBDEPS} ${FINAL_LIBDEPS}.tmp
89 COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/find-cycles.pl < ${LIBDEPS} > ${FINAL_LIBDEPS}.tmp
90 COMMAND ${CMAKE_COMMAND} -E copy ${FINAL_LIBDEPS}.tmp ${FINAL_LIBDEPS}
91 DEPENDS ${LIBDEPS}
92 COMMENT "Checking for cyclic dependencies between LLVM libraries.")
93
1594 set(C_FLGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
1695 set(CXX_FLGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
1796 set(CPP_FLGS "${CMAKE_CPP_FLAGS} ${CMAKE_CPP_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
1897
19 add_custom_command(OUTPUT ${BUILDVARIABLES_OBJPATH}
20 COMMAND echo s!@LLVM_SRC_ROOT@!${LLVM_MAIN_SRC_DIR}! > ${SEDSCRIPT_OBJPATH}
21 COMMAND echo s!@LLVM_OBJ_ROOT@!${LLVM_BINARY_DIR}! >> ${SEDSCRIPT_OBJPATH}
22 COMMAND echo s!@LLVM_CPPFLAGS@!${CPP_FLGS}! >> ${SEDSCRIPT_OBJPATH}
23 COMMAND echo s!@LLVM_CFLAGS@!${C_FLGS}! >> ${SEDSCRIPT_OBJPATH}
24 COMMAND echo s!@LLVM_CXXFLAGS@!${CXX_FLGS}! >> ${SEDSCRIPT_OBJPATH}
98 # We don't want certain flags on the output of
99 # llvm-config --cflags --cxxflags
100 macro(remove_option_from_llvm_config option)
101 llvm_replace_compiler_option(C_FLGS "${option}" "")
102 llvm_replace_compiler_option(CXX_FLGS "${option}" "")
103 llvm_replace_compiler_option(CPP_FLGS "${option}" "")
104 endmacro(remove_option_from_llvm_config)
105 remove_option_from_llvm_config("-pedantic")
106 remove_option_from_llvm_config("-Wall")
107 remove_option_from_llvm_config("-W")
108
109 add_custom_command(OUTPUT ${LLVM_CONFIG}
110 COMMAND echo s!@LLVM_CPPFLAGS@!${CPP_FLGS}! > temp.sed
111 COMMAND echo s!@LLVM_CFLAGS@!${C_FLGS}! >> temp.sed
112 COMMAND echo s!@LLVM_CXXFLAGS@!${CXX_FLGS}! >> temp.sed
25113 # TODO: Use general flags for linking! not just for shared libs:
26 COMMAND echo s!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}! >> ${SEDSCRIPT_OBJPATH}
27 COMMAND echo s!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}! >> ${SEDSCRIPT_OBJPATH}
28 COMMAND echo s!@LLVM_SYSTEM_LIBS@!${SYSTEM_LIBS}! >> ${SEDSCRIPT_OBJPATH}
29 COMMAND sed -f ${SEDSCRIPT_OBJPATH} < ${BUILDVARIABLES_SRCPATH} > ${BUILDVARIABLES_OBJPATH}
114 COMMAND echo s!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}! >> temp.sed
115 COMMAND echo s!@LIBS@!${LLVM_SYSTEM_LIBS}! >> temp.sed
116 COMMAND echo s!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}! >> temp.sed
117 COMMAND echo s!@LLVM_OBJ_SUFFIX@!! >> temp.sed
118 COMMAND sed -f temp.sed < ${LLVM_CONFIG_IN} > ${LLVM_CONFIG}
119 COMMAND ${CMAKE_COMMAND} -E remove -f temp.sed
120 COMMAND cat ${FINAL_LIBDEPS} >> ${LLVM_CONFIG}
121 COMMAND chmod +x ${LLVM_CONFIG}
30122 VERBATIM
31 COMMENT "Building BuildVariables.inc include."
123 DEPENDS ${FINAL_LIBDEPS} ${LLVM_CONFIG_IN}
124 COMMENT "Building llvm-config script."
32125 )
33126
34 # Add the llvm-config tool.
35 add_llvm_tool(llvm-config
36 llvm-config.cpp
37 )
127 add_custom_target(llvm-config.target ALL
128 DEPENDS ${LLVM_CONFIG})
38129
39 # Add the dependency on the generation step.
40 add_file_dependencies(${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.cpp ${BUILDVARIABLES_OBJPATH})
130 # Ensure we build llvm-config after we build all of the libraries so that we
131 # have their full dependencies.
132 add_dependencies(llvm-config.target ${llvm_libs})
133
134 install(FILES ${LLVM_CONFIG}
135 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
136 WORLD_READ WORLD_EXECUTE
137 DESTINATION bin)
None ##===- tools/llvm-config/Makefile---------------------------*- Makefile -*-===##
1 #
0 ##===- tools/llvm-config/Makefile --------------------------*- Makefile -*-===##
1 #
22 # The LLVM Compiler Infrastructure
33 #
44 # This file is distributed under the University of Illinois Open Source
66 #
77 ##===----------------------------------------------------------------------===##
88
9 LEVEL := ../..
10 TOOLNAME := llvm-config
11 USEDLIBS := LLVMSupport.a
9 LEVEL = ../..
1210
13 # We generate sources in the build directory, make sure it is in the include
14 # paths.
15 INCLUDE_BUILD_DIR := 1
16
17 # This tool has no plugins, optimize startup time.
18 TOOL_NO_EXPORTS := 1
19
20 # Note that we have to use lazy expansion here.
21 BUILDVARIABLES_SRCPATH = $(PROJ_SRC_ROOT)/tools/$(TOOLNAME)/BuildVariables.inc.in
22 BUILDVARIABLES_OBJPATH = $(ObjDir)/BuildVariables.inc
23 BUILT_SOURCES = $(BUILDVARIABLES_OBJPATH)
11 EXTRA_DIST = LibDeps.txt FinalLibDeps.txt llvm-config.in.in find-cycles.pl
2412
2513 include $(LEVEL)/Makefile.common
2614
15 # If we don't have Perl, we can't generate the library dependencies upon which
16 # llvm-config depends. Therefore, only if we detect perl will we do anything
17 # useful.
18 ifeq ($(HAVE_PERL),1)
19
2720 # Combine preprocessor flags (except for -I) and CXX flags.
28 SUB_CPPFLAGS := ${CPP.BaseFlags}
29 SUB_CFLAGS := ${CPP.BaseFlags} ${C.Flags}
30 SUB_CXXFLAGS := ${CPP.BaseFlags} ${CXX.Flags}
21 SUB_CPPFLAGS = ${CPP.BaseFlags}
22 SUB_CFLAGS = ${CPP.BaseFlags} ${C.Flags}
23 SUB_CXXFLAGS = ${CPP.BaseFlags} ${CXX.Flags}
3124
3225 # This is blank for now. We need to be careful about adding stuff here:
3326 # LDFLAGS tend not to be portable, and we don't currently require the
3427 # user to use libtool when linking against LLVM.
35 SUB_LDFLAGS :=
28 SUB_LDFLAGS =
3629
37 $(ObjDir)/BuildVariables.inc: $(BUILDVARIABLES_SRCPATH) Makefile $(ObjDir)/.dir
38 $(Echo) "Building llvm-config BuildVariables.inc file."
39 $(Verb) $(ECHO) 's/@LLVM_SRC_ROOT@/$(subst /,\/,$(LLVM_SRC_ROOT))/' \
30 FinalLibDeps = $(PROJ_OBJ_DIR)/FinalLibDeps.txt
31 LibDeps = $(PROJ_OBJ_DIR)/LibDeps.txt
32 LibDepsTemp = $(PROJ_OBJ_DIR)/LibDeps.txt.tmp
33 GenLibDeps = $(PROJ_SRC_ROOT)/utils/GenLibDeps.pl
34
35 $(LibDepsTemp): $(GenLibDeps) $(LibDir) $(wildcard $(LibDir)/*.a $(LibDir)/*.o)
36 $(Echo) "Regenerating LibDeps.txt.tmp"
37 $(Verb) $(PERL) $(GenLibDeps) -flat $(LibDir) "$(NM_PATH)" > $(LibDepsTemp)
38
39 $(LibDeps): $(LibDepsTemp)
40 $(Verb) $(CMP) -s $@ $< || ( $(CP) $< $@ && \
41 $(EchoCmd) Updated LibDeps.txt because dependencies changed )
42
43 # Find all the cyclic dependencies between various LLVM libraries, so we
44 # don't have to process them at runtime.
45 $(FinalLibDeps): find-cycles.pl $(LibDeps)
46 $(Echo) "Checking for cyclic dependencies between LLVM libraries."
47 $(Verb) $(PERL) $< < $(LibDeps) > $@ || rm -f $@
48
49 # Rerun our configure substitutions as needed.
50 ConfigInIn = $(PROJ_SRC_DIR)/llvm-config.in.in
51 llvm-config.in: $(ConfigInIn) $(ConfigStatusScript)
52 $(Verb) cd $(PROJ_OBJ_ROOT) ; \
53 $(ConfigStatusScript) tools/llvm-config/llvm-config.in
54
55 # Build our final script.
56 $(ToolDir)/llvm-config: llvm-config.in $(FinalLibDeps)
57 $(Echo) "Building llvm-config script."
58 $(Verb) $(ECHO) 's/@LLVM_CPPFLAGS@/$(subst /,\/,$(SUB_CPPFLAGS))/' \
4059 > temp.sed
41 $(Verb) $(ECHO) 's/@LLVM_OBJ_ROOT@/$(subst /,\/,$(LLVM_OBJ_ROOT))/' \
42 >> temp.sed
43 $(Verb) $(ECHO) 's/@LLVM_CPPFLAGS@/$(subst /,\/,$(SUB_CPPFLAGS))/' \
44 >> temp.sed
4560 $(Verb) $(ECHO) 's/@LLVM_CFLAGS@/$(subst /,\/,$(SUB_CFLAGS))/' \
4661 >> temp.sed
4762 $(Verb) $(ECHO) 's/@LLVM_CXXFLAGS@/$(subst /,\/,$(SUB_CXXFLAGS))/' \
5065 >> temp.sed
5166 $(Verb) $(ECHO) 's/@LLVM_BUILDMODE@/$(subst /,\/,$(BuildMode))/' \
5267 >> temp.sed
53 $(Verb) $(ECHO) 's/@LLVM_SYSTEM_LIBS@/$(subst /,\/,$(LIBS))/' \
68 $(Verb) $(ECHO) 's/@LLVM_OBJ_SUFFIX@/$(subst /,\/,/$(BuildMode))/' \
5469 >> temp.sed
5570 $(Verb) $(SED) -f temp.sed < $< > $@
5671 $(Verb) $(RM) temp.sed
72 $(Verb) cat $(FinalLibDeps) >> $@
73 $(Verb) chmod +x $@
74
75 else
76 # We don't have perl, just generate a dummy llvm-config
77 $(ToolDir)/llvm-config:
78 $(Echo) "Building place holder llvm-config script."
79 $(Verb) $(ECHO) 'echo llvm-config: Perl not found so llvm-config could not be generated' >> $@
80 $(Verb) chmod +x $@
81
82 endif
83 # Hook into the standard Makefile rules.
84 all-local:: $(ToolDir)/llvm-config
85 clean-local::
86 $(Verb) $(RM) -f $(ToolDir)/llvm-config llvm-config.in $(FinalLibDeps) \
87 $(LibDeps) GenLibDeps.out
88 install-local:: all-local
89 $(Echo) Installing llvm-config
90 $(Verb) $(MKDIR) $(DESTDIR)$(PROJ_bindir)
91 $(Verb) $(ScriptInstall) $(ToolDir)/llvm-config $(DESTDIR)$(PROJ_bindir)
92
0 #!/usr/bin/perl
1 #
2 # Program: find-cycles.pl
3 #
4 # Synopsis: Given a list of possibly cyclic dependencies, merge all the
5 # cycles. This makes it possible to topologically sort the
6 # dependencies between different parts of LLVM.
7 #
8 # Syntax: find-cycles.pl < LibDeps.txt > FinalLibDeps.txt
9 #
10 # Input: cycmem1: cycmem2 dep1 dep2
11 # cycmem2: cycmem1 dep3 dep4
12 # boring: dep4
13 #
14 # Output: cycmem1 cycmem2: dep1 dep2 dep3 dep4
15 # boring: dep4
16 #
17 # This file was written by Eric Kidd, and is placed into the public domain.
18 #
19
20 use 5.006;
21 use strict;
22 use warnings;
23
24 my %DEPS;
25 my @CYCLES;
26 sub find_all_cycles;
27
28 # Read our dependency information.
29 while (<>) {
30 chomp;
31 my ($module, $dependency_str) = /^\s*([^:]+):\s*(.*)\s*$/;
32 die "Malformed data: $_" unless defined $dependency_str;
33 my @dependencies = split(/ /, $dependency_str);
34 $DEPS{$module} = \@dependencies;
35 }
36
37 # Partition our raw dependencies into sets of cyclically-connected nodes.
38 find_all_cycles();
39
40 # Print out the finished cycles, with their dependencies.
41 my @output;
42 my $cycles_found = 0;
43 foreach my $cycle (@CYCLES) {
44 my @modules = sort keys %{$cycle};
45
46 # Merge the dependencies of all modules in this cycle.
47 my %dependencies;
48 foreach my $module (@modules) {
49 @dependencies{@{$DEPS{$module}}} = 1;
50 }
51
52 # Prune the known cyclic dependencies.
53 foreach my $module (@modules) {
54 delete $dependencies{$module};
55 }
56
57 # Warn about possible linker problems.
58 my @archives = grep(/\.a$/, @modules);
59 if (@archives > 1) {
60 $cycles_found = $cycles_found + 1;
61 print STDERR "find-cycles.pl: Circular dependency between *.a files:\n";
62 print STDERR "find-cycles.pl: ", join(' ', @archives), "\n";
63 push @modules, @archives; # WORKAROUND: Duplicate *.a files. Ick.
64 } elsif (@modules > 1) {
65 $cycles_found = $cycles_found + 1;
66 print STDERR "find-cycles.pl: Circular dependency between *.o files:\n";
67 print STDERR "find-cycles.pl: ", join(' ', @modules), "\n";
68 push @modules, @modules; # WORKAROUND: Duplicate *.o files. Ick.
69 }
70
71 # Add to our output. (@modules is already as sorted as we need it to be.)
72 push @output, (join(' ', @modules) . ': ' .
73 join(' ', sort keys %dependencies) . "\n");
74 }
75 print sort @output;
76
77 exit $cycles_found;
78
79 #==========================================================================
80 # Depedency Cycle Support
81 #==========================================================================
82 # For now, we have cycles in our dependency graph. Ideally, each cycle
83 # would be collapsed down to a single *.a file, saving us all this work.
84 #
85 # To understand this code, you'll need a working knowledge of Perl 5,
86 # and possibly some quality time with 'man perlref'.
87
88 my %SEEN;
89 my %CYCLES;
90 sub find_cycles ($@);
91 sub found_cycles ($@);
92
93 sub find_all_cycles {
94 # Find all multi-item cycles.
95 my @modules = sort keys %DEPS;
96 foreach my $module (@modules) { find_cycles($module); }
97
98 # Build fake one-item "cycles" for the remaining modules, so we can
99 # treat them uniformly.
100 foreach my $module (@modules) {
101 unless (defined $CYCLES{$module}) {
102 my %cycle = ($module, 1);
103 $CYCLES{$module} = \%cycle;
104 }
105 }
106
107 # Find all our unique cycles. We have to do this the hard way because
108 # we apparently can't store hash references as hash keys without making
109 # 'strict refs' sad.
110 my %seen;
111 foreach my $cycle (values %CYCLES) {
112 unless ($seen{$cycle}) {
113 $seen{$cycle} = 1;
114 push @CYCLES, $cycle;
115 }
116 }
117 }
118
119 # Walk through our graph depth-first (keeping a trail in @path), and report
120 # any cycles we find.
121 sub find_cycles ($@) {
122 my ($module, @path) = @_;
123 if (str_in_list($module, @path)) {
124 found_cycle($module, @path);
125 } else {
126 return if defined $SEEN{$module};
127 $SEEN{$module} = 1;
128 foreach my $dep (@{$DEPS{$module}}) {
129 find_cycles($dep, @path, $module);
130 }
131 }
132 }
133
134 # Give a cycle, attempt to merge it with pre-existing cycle data.
135 sub found_cycle ($@) {
136 my ($module, @path) = @_;
137
138 # Pop any modules which aren't part of our cycle.
139 while ($path[0] ne $module) { shift @path; }
140 #print join("->", @path, $module) . "\n";
141
142 # Collect the modules in our cycle into a hash.
143 my %cycle;
144 foreach my $item (@path) {
145 $cycle{$item} = 1;
146 if (defined $CYCLES{$item}) {
147 # Looks like we intersect with an existing cycle, so merge
148 # all those in, too.
149 foreach my $old_item (keys %{$CYCLES{$item}}) {
150 $cycle{$old_item} = 1;
151 }
152 }
153 }
154
155 # Update our global cycle table.
156 my $cycle_ref = \%cycle;
157 foreach my $item (keys %cycle) {
158 $CYCLES{$item} = $cycle_ref;
159 }
160 #print join(":", sort keys %cycle) . "\n";
161 }
162
163 sub str_in_list ($@) {
164 my ($str, @list) = @_;
165 foreach my $item (@list) {
166 return 1 if ($item eq $str);
167 }
168 return 0;
169 }
None //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tool encapsulates information about an LLVM project configuration for
10 // use by other project's build environments (to determine installed path,
11 // available features, required libraries, etc.).
12 //
13 // Note that although this tool *may* be used by some parts of LLVM's build
14 // itself (i.e., the Makefiles use it to compute required libraries when linking
15 // tools), this tool is primarily designed to support external projects.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Config/config.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include
30 #include
31 #include
32
33 using namespace llvm;
34
35 // Include the build time variables we can report to the user. This is generated
36 // at build time from the BuildVariables.inc.in file by the build system.
37 #include "BuildVariables.inc"
38
39 // Include the component table. This creates an array of struct
40 // AvailableComponent entries, which record the component name, library name,
41 // and required components for all of the available libraries.
42 //
43 // Not all components define a library, we also use "library groups" as a way to
44 // create entries for pseudo groups like x86 or all-targets.
45 #include "LibraryDependencies.inc"
46
47 /// \brief Traverse a single component adding to the topological ordering in
48 /// \arg RequiredLibs.
49 ///
50 /// \param Name - The component to traverse.
51 /// \param ComponentMap - A prebuilt map of component names to descriptors.
52 /// \param VisitedComponents [in] [out] - The set of already visited components.
53 /// \param RequiredLibs [out] - The ordered list of required libraries.
54 static void VisitComponent(StringRef Name,
55 const StringMap &ComponentMap,
56 std::set &VisitedComponents,
57 std::vector &RequiredLibs) {
58 // Lookup the component.
59 AvailableComponent *AC = ComponentMap.lookup(Name);
60 assert(AC && "Invalid component name!");
61
62 // Add to the visited table.
63 if (!VisitedComponents.insert(AC).second) {
64 // We are done if the component has already been visited.
65 return;
66 }
67
68 // Otherwise, visit all the dependencies.
69 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
70 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
71 RequiredLibs);
72 }
73
74 // Add to the required library list.
75 if (AC->Library)
76 RequiredLibs.push_back(AC->Library);
77 }
78
79 /// \brief Compute the list of required libraries for a given list of
80 /// components, in an order suitable for passing to a linker (that is, libraries
81 /// appear prior to their dependencies).
82 ///
83 /// \param Components - The names of the components to find libraries for.
84 /// \param RequiredLibs [out] - On return, the ordered list of libraries that
85 /// are required to link the given components.
86 void ComputeLibsForComponents(const std::vector &Components,
87 std::vector &RequiredLibs) {
88 std::set VisitedComponents;
89
90 // Build a map of component names to information.
91 StringMap ComponentMap;
92 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
93 AvailableComponent *AC = &AvailableComponents[i];
94 ComponentMap[AC->Name] = AC;
95 }
96
97 // Visit the components.
98 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
99 // Users are allowed to provide mixed case component names.
100 std::string ComponentLower = Components[i].lower();
101
102 // Validate that the user supplied a valid component name.
103 if (!ComponentMap.count(ComponentLower)) {
104 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
105 << "\n";
106 exit(1);
107 }
108
109 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
110 RequiredLibs);
111 }
112
113 // The list is now ordered with leafs first, we want the libraries to printed
114 // in the reverse order of dependency.
115 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
116 }
117
118 /* *** */
119
120 void usage() {
121 errs() << "\
122 usage: llvm-config
123 \n\
124 Get various configuration information needed to compile programs which use\n\
125 LLVM. Typically called from 'configure' scripts. Examples:\n\
126 llvm-config --cxxflags\n\
127 llvm-config --ldflags\n\
128 llvm-config --libs engine bcreader scalaropts\n\
129 \n\
130 Options:\n\
131 --version Print LLVM version.\n\
132 --prefix Print the installation prefix.\n\
133 --src-root Print the source root LLVM was built from.\n\
134 --obj-root Print the object root used to build LLVM.\n\
135 --bindir Directory containing LLVM executables.\n\
136 --includedir Directory containing LLVM headers.\n\
137 --libdir Directory containing LLVM libraries.\n\
138 --cppflags C preprocessor flags for files that include LLVM headers.\n\
139 --cflags C compiler flags for files that include LLVM headers.\n\
140 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
141 --ldflags Print Linker flags.\n\
142 --libs Libraries needed to link against LLVM components.\n\
143 --libnames Bare library names for in-tree builds.\n\
144 --libfiles Fully qualified library filenames for makefile depends.\n\
145 --components List of all possible components.\n\
146 --targets-built List of all targets currently built.\n\
147 --host-target Target triple used to configure LLVM.\n\
148 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
149 Typical components:\n\
150 all All LLVM libraries (default).\n\
151 backend Either a native backend or the C backend.\n\
152 engine Either a native JIT or a bitcode interpreter.\n";
153 exit(1);
154 }
155
156 /// \brief Compute the path to the main executable.
157 llvm::sys::Path GetExecutablePath(const char *Argv0) {
158 // This just needs to be some symbol in the binary; C++ doesn't
159 // allow taking the address of ::main however.
160 void *P = (void*) (intptr_t) GetExecutablePath;
161 return llvm::sys::Path::GetMainExecutable(Argv0, P);
162 }
163
164 int main(int argc, char **argv) {
165 std::vector Components;
166 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
167 bool HasAnyOption = false;
168
169 // llvm-config is designed to support being run both from a development tree
170 // and from an installed path. We try and auto-detect which case we are in so
171 // that we can report the correct information when run from a development
172 // tree.
173 bool IsInDevelopmentTree, DevelopmentTreeLayoutIsCMakeStyle;
174 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]).str());
175 std::string CurrentExecPrefix;
176 std::string ActiveObjRoot;
177
178 // Create an absolute path, and pop up one directory (we expect to be inside a
179 // bin dir).
180 sys::fs::make_absolute(CurrentPath);
181 CurrentExecPrefix = sys::path::parent_path(
182 sys::path::parent_path(CurrentPath)).str();
183
184 // Check to see if we are inside a development tree by comparing to possible
185 // locations (prefix style or CMake style). This could be wrong in the face of
186 // symbolic links, but is good enough.
187 if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE) {
188 IsInDevelopmentTree = true;
189 DevelopmentTreeLayoutIsCMakeStyle = false;
190
191 // If we are in a development tree, then check if we are in a BuildTools
192 // directory. This indicates we are built for the build triple, but we
193 // always want to provide information for the host triple.
194 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
195 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
196 } else {
197 ActiveObjRoot = LLVM_OBJ_ROOT;
198 }
199 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/bin") {
200 IsInDevelopmentTree = true;
201 DevelopmentTreeLayoutIsCMakeStyle = true;
202 ActiveObjRoot = LLVM_OBJ_ROOT;
203 } else {
204 IsInDevelopmentTree = false;
205 }
206
207 // Compute various directory locations based on the derived location
208 // information.
209 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
210 std::string ActiveIncludeOption;
211 if (IsInDevelopmentTree) {
212 ActivePrefix = CurrentExecPrefix;
213
214 // CMake organizes the products differently than a normal prefix style
215 // layout.
216 if (DevelopmentTreeLayoutIsCMakeStyle) {
217 ActiveIncludeDir = ActiveObjRoot + "/include";
218 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE;
219 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE;
220 } else {
221 ActiveIncludeDir = ActiveObjRoot + "/include";
222 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin";
223 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib";
224 }
225
226 // We need to include files from both the source and object trees.
227 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
228 "-I" + ActiveObjRoot + "/include");
229 } else {
230 ActivePrefix = CurrentExecPrefix;
231 ActiveIncludeDir = ActivePrefix + "/include";
232 ActiveBinDir = ActivePrefix + "/bin";
233 ActiveLibDir = ActivePrefix + "/lib";
234 ActiveIncludeOption = "-I" + ActiveIncludeDir;
235 }
236
237 raw_ostream &OS = outs();
238 for (int i = 1; i != argc; ++i) {
239 StringRef Arg = argv[i];
240
241 if (Arg.startswith("-")) {
242 HasAnyOption = true;
243 if (Arg == "--version") {
244 OS << PACKAGE_VERSION << '\n';
245 } else if (Arg == "--prefix") {
246 OS << ActivePrefix << '\n';
247 } else if (Arg == "--bindir") {
248 OS << ActiveBinDir << '\n';
249 } else if (Arg == "--includedir") {
250 OS << ActiveIncludeDir << '\n';
251 } else if (Arg == "--libdir") {
252 OS << ActiveLibDir << '\n';
253 } else if (Arg == "--cppflags") {
254 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
255 } else if (Arg == "--cflags") {
256 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
257 } else if (Arg == "--cxxflags") {
258 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
259 } else if (Arg == "--ldflags") {
260 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS
261 << ' ' << LLVM_SYSTEM_LIBS << '\n';
262 } else if (Arg == "--libs") {
263 PrintLibs = true;
264 } else if (Arg == "--libnames") {
265 PrintLibNames = true;
266 } else if (Arg == "--libfiles") {
267 PrintLibFiles = true;
268 } else if (Arg == "--components") {
269 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
270 OS << ' ';
271 OS << AvailableComponents[j].Name;
272 }
273 OS << '\n';
274 } else if (Arg == "--targets-built") {
275 bool First = true;
276 for (TargetRegistry::iterator I = TargetRegistry::begin(),
277 E = TargetRegistry::end(); I != E; First = false, ++I) {
278 if (!First)
279 OS << ' ';
280 OS << I->getName();
281 }
282 OS << '\n';
283 } else if (Arg == "--host-target") {
284 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
285 } else if (Arg == "--build-mode") {
286 OS << LLVM_BUILDMODE << '\n';
287 } else if (Arg == "--obj-root") {
288 OS << LLVM_OBJ_ROOT << '\n';
289 } else if (Arg == "--src-root") {
290 OS << LLVM_SRC_ROOT << '\n';
291 } else {
292 usage();
293 }
294 } else {
295 Components.push_back(Arg);
296 }
297 }
298
299 if (!HasAnyOption)
300 usage();
301
302 if (PrintLibs || PrintLibNames || PrintLibFiles) {
303 // Construct the list of all the required libraries.
304 std::vector RequiredLibs;
305 ComputeLibsForComponents(Components, RequiredLibs);
306
307 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
308 StringRef Lib = RequiredLibs[i];
309 if (i)
310 OS << ' ';
311
312 if (PrintLibNames) {
313 OS << Lib;
314 } else if (PrintLibFiles) {
315 OS << ActiveLibDir << '/' << Lib;
316 } else if (PrintLibs) {
317 // If this is a typical library name, include it using -l.
318 if (Lib.startswith("lib") && Lib.endswith(".a")) {
319 OS << "-l" << Lib.slice(3, Lib.size()-2);
320 continue;
321 }
322
323 // Otherwise, print the full path.
324 OS << ActiveLibDir << '/' << Lib;
325 }
326 }
327 OS << '\n';
328 } else if (!Components.empty()) {
329 errs() << "llvm-config: error: components given, but unused\n\n";
330 usage();
331 }
332
333 return 0;
334 }
0 #!@PERL@
1 ##===- tools/llvm-config ---------------------------------------*- perl -*-===##
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 # Synopsis: Prints out compiler options needed to build against an installed
11 # copy of LLVM.
12 #
13 # Syntax: llvm-config OPTIONS... [COMPONENTS...]
14 #
15 ##===----------------------------------------------------------------------===##
16
17 use 5.006;
18 use strict;
19 use warnings;
20 use Cwd 'abs_path';
21
22 #---- begin autoconf values ----
23 my $PACKAGE_NAME = q{@PACKAGE_NAME@};
24 my $VERSION = q{@PACKAGE_VERSION@};
25 my $PREFIX = q{@LLVM_PREFIX@};
26 my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
27 my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
28 my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
29 my $ARCH = lc(q{@ARCH@});
30 my $TARGET_TRIPLE = q{@target@};
31 my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
32 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
33 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
34 #---- end autoconf values ----
35
36 # Must pretend x86_64 architecture is really x86, otherwise the native backend
37 # won't get linked in.
38 $ARCH = "x86" if $ARCH eq "x86_64";
39
40 #---- begin Makefile values ----
41 my $CPPFLAGS = q{@LLVM_CPPFLAGS@};
42 my $CFLAGS = q{@LLVM_CFLAGS@};
43 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
44 my $LDFLAGS = q{@LLVM_LDFLAGS@};
45 my $SYSTEM_LIBS = q{@LIBS@};
46 my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
47 my $LLVM_OBJ_SUFFIX = q{@LLVM_OBJ_SUFFIX@};
48 #---- end Makefile values ----
49
50 # Figure out where llvm-config is being run from. Primarily, we care if it has
51 # been installed, or is running from the build directory, which changes the
52 # locations of some files.
53
54 # Convert the current executable name into its directory (e.g. ".").
55 my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
56
57 # Turn the directory into an absolute directory on the file system, also pop up
58 # from "bin" into the build or prefix dir.
59 my $ABS_RUN_DIR = abs_path("$RUN_DIR/..");
60 chomp($ABS_RUN_DIR);
61
62 # Compute the absolute object directory build, e.g. "foo/llvm/Debug".
63 my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT$LLVM_OBJ_SUFFIX";
64 $ABS_OBJ_ROOT = abs_path("$ABS_OBJ_ROOT") if (-d $ABS_OBJ_ROOT);
65 chomp($ABS_OBJ_ROOT);
66
67 my $INCLUDEDIR = "$ABS_RUN_DIR/include";
68 my $INCLUDEOPTION = "-I$INCLUDEDIR";
69 my $LIBDIR = "$ABS_RUN_DIR/lib";
70 my $BINDIR = "$ABS_RUN_DIR/bin";
71 if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
72 # If we are running out of the build directory, the include dir is in the
73 # srcdir.
74 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
75 # We need include files from both the srcdir and objdir.
76 $INCLUDEOPTION = "-I$INCLUDEDIR -I$LLVM_OBJ_ROOT/include"
77 } else {
78 # If installed, ignore the prefix the tree was configured with, use the
79 # current prefix.
80 $PREFIX = $ABS_RUN_DIR;
81 }
82
83 sub usage;
84 sub fix_library_names (@);
85 sub fix_library_files (@);
86 sub expand_dependencies (@);
87 sub name_map_entries;
88
89 # Parse our command-line arguments.
90 usage if @ARGV == 0;
91 my @components;
92 my $has_opt = 0;
93 my $want_libs = 0;
94 my $want_libnames = 0;
95 my $want_libfiles = 0;
96 my $want_components = 0;
97 foreach my $arg (@ARGV) {
98 if ($arg =~ /^-/) {
99 if ($arg eq "--version") {
100 $has_opt = 1; print "$VERSION\n";
101 } elsif ($arg eq "--prefix") {
102 $has_opt = 1; print "$PREFIX\n";
103 } elsif ($arg eq "--bindir") {
104 $has_opt = 1; print "$BINDIR\n";
105 } elsif ($arg eq "--includedir") {
106 $has_opt = 1; print "$INCLUDEDIR\n";
107 } elsif ($arg eq "--libdir") {
108 $has_opt = 1; print "$LIBDIR\n";
109 } elsif ($arg eq "--cppflags") {
110 $has_opt = 1; print "$INCLUDEOPTION $CPPFLAGS\n";
111 } elsif ($arg eq "--cflags") {
112 $has_opt = 1; print "$INCLUDEOPTION $CFLAGS\n";
113 } elsif ($arg eq "--cxxflags") {
114 $has_opt = 1; print "$INCLUDEOPTION $CXXFLAGS\n";
115 } elsif ($arg eq "--ldflags") {
116 $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n";
117 } elsif ($arg eq "--libs") {
118 $has_opt = 1; $want_libs = 1;
119 } elsif ($arg eq "--libnames") {
120 $has_opt = 1; $want_libnames = 1;
121 } elsif ($arg eq "--libfiles") {
122 $has_opt = 1; $want_libfiles = 1;
123 } elsif ($arg eq "--components") {
124 $has_opt = 1; print join(' ', name_map_entries), "\n";
125 } elsif ($arg eq "--targets-built") {
126 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
127 } elsif ($arg eq "--host-target") {
128 $has_opt = 1; print "$TARGET_TRIPLE\n";
129 } elsif ($arg eq "--build-mode") {
130 $has_opt = 1; print "$LLVM_BUILDMODE\n";
131 } elsif ($arg eq "--obj-root") {
132 $has_opt = 1; print abs_path("$LLVM_OBJ_ROOT/");
133 } elsif ($arg eq "--src-root") {
134 $has_opt = 1; print abs_path("$LLVM_SRC_ROOT/");
135 } else {
136 usage();
137 }
138 } else {
139 push @components, $arg;
140 }
141 }
142
143 # If no options were specified, fail.
144 usage unless $has_opt;
145
146 # If no components were specified, default to 'all'.
147 if (@components == 0) {
148 push @components, 'all';
149 }
150
151 # Force component names to lower case.
152 @components = map lc, @components;
153
154 # Handle any arguments which require building our dependency graph.
155 if ($want_libs || $want_libnames || $want_libfiles) {
156 my @libs = expand_dependencies(@components);
157 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
158 print join(' ', @libs), "\n" if ($want_libnames);
159 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
160 }
161
162 exit 0;
163
164 #==========================================================================
165 # Support Routines
166 #==========================================================================
167
168 sub usage {
169 print STDERR <<__EOD__;
170 Usage: llvm-config
171
172 Get various configuration information needed to compile programs which use
173 LLVM. Typically called from 'configure' scripts. Examples:
174 llvm-config --cxxflags
175 llvm-config --ldflags
176 llvm-config --libs engine bcreader scalaropts
177
178 Options:
179 --version Print LLVM version.
180 --prefix Print the installation prefix.
181 --src-root Print the source root LLVM was built from.
182 --obj-root Print the object root used to build LLVM.
183 --bindir Directory containing LLVM executables.
184 --includedir Directory containing LLVM headers.
185 --libdir Directory containing LLVM libraries.
186 --cppflags C preprocessor flags for files that include LLVM headers.
187 --cflags C compiler flags for files that include LLVM headers.
188 --cxxflags C++ compiler flags for files that include LLVM headers.
189 --ldflags Print Linker flags.
190 --libs Libraries needed to link against LLVM components.
191 --libnames Bare library names for in-tree builds.
192 --libfiles Fully qualified library filenames for makefile depends.
193 --components List of all possible components.
194 --targets-built List of all targets currently built.
195 --host-target Target triple used to configure LLVM.
196 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
197 Typical components:
198 all All LLVM libraries (default).
199 engine Either a native JIT or a bitcode interpreter.
200 __EOD__
201 exit(1);
202 }
203
204 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
205 # files which can't be found using -L.
206 sub fix_library_names (@) {
207 my @libs = @_;
208 my @result;
209 foreach my $lib (@libs) {
210 # Transform the bare library name appropriately.
211 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
212 if (defined $basename) {
213 push @result, "-l$basename";
214 } else {
215 push @result, "$LIBDIR/$lib";
216 }
217 }
218 return @result;
219 }
220
221 # Turn the list of libraries into a list of files.
222 sub fix_library_files(@) {
223 my @libs = @_;
224 my @result;
225 foreach my $lib (@libs) {
226 # Transform the bare library name into a filename.
227 push @result, "$LIBDIR/$lib";
228 }
229 return @result;
230 }
231
232 #==========================================================================
233 # Library Dependency Analysis
234 #==========================================================================
235 # Given a few human-readable library names, find all their dependencies
236 # and sort them into an order which the linker will like. If we packed
237 # our libraries into fewer archives, we could make the linker do much
238 # of this work for us.
239 #
240 # Libraries have two different types of names in this code: Human-friendly
241 # "component" names entered on the command-line, and the raw file names
242 # we use internally (and ultimately pass to the linker).
243 #
244 # To understand this code, you'll need a working knowledge of Perl 5,
245 # and possibly some quality time with 'man perlref'.
246
247 sub load_dependencies;
248 sub build_name_map;
249 sub have_native_backend;
250 sub find_best_engine;
251 sub expand_names (@);
252 sub find_all_required_sets (@);
253 sub find_all_required_sets_helper ($$@);
254
255 # Each "set" contains one or more libraries which must be included as a
256 # group (due to cyclic dependencies). Sets are represented as a Perl array
257 # reference pointing to a list of internal library names.
258 my @SETS;
259
260 # Various mapping tables.
261 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
262 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
263 my %NAME_MAP; # Maps human-entered names to internal names.
264
265 # Have our dependencies been loaded yet?
266 my $DEPENDENCIES_LOADED = 0;
267
268 # Given a list of human-friendly component names, translate them into a
269 # complete set of linker arguments.
270 sub expand_dependencies (@) {
271 my @libs = @_;
272 load_dependencies;
273 my @required_sets = find_all_required_sets(expand_names(@libs));
274 my @sorted_sets = topologically_sort_sets(@required_sets);
275
276 # Expand the library sets into libraries.
277 my @result;
278 foreach my $set (@sorted_sets) { push @result, @{$set}; }
279 return @result;
280 }
281
282 # Load in the raw dependency data stored at the end of this file.
283 sub load_dependencies {
284 return if $DEPENDENCIES_LOADED;
285 $DEPENDENCIES_LOADED = 1;
286 while () {
287 # Parse our line.
288 my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/;
289 die "Malformed dependency data" unless defined $deps;
290 my @libs = split(' ', $libs);
291 my @deps = split(' ', $deps);
292
293 # Record our dependency data.
294 my $set = \@libs;
295 push @SETS, $set;
296 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
297 $SET_DEPS{$set} = \@deps;
298 }
299 build_name_map;
300 }
301
302 # Build a map converting human-friendly component names into internal
303 # library names.
304 sub build_name_map {
305 # Add entries for all the actual libraries.
306 foreach my $set (@SETS) {
307 foreach my $lib (sort @$set) {
308 my $short_name = $lib;
309 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
310 $short_name =~ tr/A-Z/a-z/;
311 $NAME_MAP{$short_name} = [$lib];
312 }
313 }
314
315 # Add target-specific entries
316 my @all_targets;
317 foreach my $target (@TARGETS_BUILT) {
318 # FIXME: Temporary, until we don't switch all targets
319 if (defined $NAME_MAP{$target.'asmprinter'}) {
320 $NAME_MAP{$target} = [$target.'info',
321 $target.'asmprinter',
322 $target.'codegen']
323 } elsif (defined $NAME_MAP{$target.'codegen'}) {
324 $NAME_MAP{$target} = [$target.'info',
325 $target.'codegen']
326 } else {
327 $NAME_MAP{$target} = [$target.'info',
328 $NAME_MAP{$target}[0]]
329 }
330
331 if (defined $NAME_MAP{$target.'asmparser'}) {
332 push @{$NAME_MAP{$target}},$target.'asmparser'
333 }
334
335 if (defined $NAME_MAP{$target.'disassembler'}) {
336 push @{$NAME_MAP{$target}},$target.'disassembler'
337 }
338
339 push @all_targets, $target;
340 }
341
342 # Add virtual entries.
343 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
344 $NAME_MAP{'nativecodegen'} = have_native_backend() ? [$ARCH.'codegen'] : [];
345 $NAME_MAP{'engine'} = find_best_engine;
346 $NAME_MAP{'all-targets'} = \@all_targets;
347 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
348 }
349
350 # Return true if we have a native backend to use.
351 sub have_native_backend {
352 my %BUILT;
353 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
354 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
355 }
356
357 # Find a working subclass of ExecutionEngine for this platform.
358 sub find_best_engine {
359 if (have_native_backend && $TARGET_HAS_JIT) {
360 return ['jit', 'native'];
361 } else {
362 return ['interpreter'];
363 }
364 }
365
366 # Get all the human-friendly component names.
367 sub name_map_entries {
368 load_dependencies;
369 return sort keys %NAME_MAP;
370 }
371
372 # Map human-readable names to internal library names.
373 sub expand_names (@) {
374 my @names = @_;
375 my @result;
376 foreach my $name (@names) {
377 if (defined $LIB_TO_SET_MAP{$name}) {
378 # We've hit bottom: An actual library name.
379 push @result, $name;
380 } elsif (defined $NAME_MAP{$name}) {
381 # We've found a short name to expand.
382 push @result, expand_names(@{$NAME_MAP{$name}});
383 } else {
384 print STDERR "llvm-config: unknown component name: $name\n";
385 exit(1);
386 }
387 }
388 return @result;
389 }
390
391 # Given a list of internal library names, return all sets of libraries which
392 # will need to be included by the linker (in no particular order).
393 sub find_all_required_sets (@) {
394 my @libs = @_;
395 my %sets_added;
396 my @result;
397 find_all_required_sets_helper(\%sets_added, \@result, @libs);
398 return @result;
399 }
400
401 # Recursive closures are pretty broken in Perl, so we're going to separate
402 # this function from find_all_required_sets and pass in the state we need
403 # manually, as references. Yes, this is fairly unpleasant.
404 sub find_all_required_sets_helper ($$@) {
405 my ($sets_added, $result, @libs) = @_;
406 foreach my $lib (@libs) {
407 my $set = $LIB_TO_SET_MAP{$lib};
408 next if defined $$sets_added{$set};
409 $$sets_added{$set} = 1;
410 push @$result, $set;
411 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
412 }
413 }
414
415 # Print a list of sets, with a label. Used for debugging.
416 sub print_sets ($@) {
417 my ($label, @sets) = @_;
418 my @output;
419 foreach my $set (@sets) { push @output, join(',', @$set); }
420 print "$label: ", join(';', @output), "\n";
421 }
422
423 # Returns true if $lib is a key in $added.
424 sub has_lib_been_added ($$) {
425 my ($added, $lib) = @_;
426 return defined $$added{$LIB_TO_SET_MAP{$lib}};
427 }
428
429 # Returns true if all the dependencies of $set appear in $added.
430 sub have_all_deps_been_added ($$) {
431 my ($added, $set) = @_;
432 #print_sets(" Checking", $set);
433 #print_sets(" Wants", $SET_DEPS{$set});
434 foreach my $lib (@{$SET_DEPS{$set}}) {
435 return 0 unless has_lib_been_added($added, $lib);
436 }
437 return 1;
438 }
439
440 # Given a list of sets, topologically sort them using dependencies.
441 sub topologically_sort_sets (@) {
442 my @sets = @_;
443 my %added;
444 my @result;
445 SCAN: while (@sets) { # We'll delete items from @sets as we go.
446 #print_sets("So far", reverse(@result));
447 #print_sets("Remaining", @sets);
448 for (my $i = 0; $i < @sets; ++$i) {
449 my $set = $sets[$i];
450 if (have_all_deps_been_added(\%added, $set)) {
451 push @result, $set;
452 $added{$set} = 1;
453 #print "Removing $i.\n";
454 splice(@sets, $i, 1);
455 next SCAN; # Restart our scan.
456 }
457 }
458 die "Can't find a library with no dependencies";
459 }
460 return reverse(@result);
461 }
462
463 # Our library dependency data will be added after the '__END__' token, and will
464 # be read through the magic filehandle.
465 __END__
0 //===-- BuildVariables.inc.in - llvm-config build variables -*- C++ -*-----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is configured by the build system to define the variables
10 // llvm-config wants to report to the user, but which can only be determined at
11 // build time.
12 //
13 // The non .in variant of this file has been autogenerated by the LLVM build. Do
14 // not edit!
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define LLVM_SRC_ROOT "@LLVM_SRC_ROOT@"
19 #define LLVM_OBJ_ROOT "@LLVM_OBJ_ROOT@"
20 #define LLVM_CPPFLAGS "@LLVM_CPPFLAGS@"
21 #define LLVM_CFLAGS "@LLVM_CFLAGS@"
22 #define LLVM_LDFLAGS "@LLVM_LDFLAGS@"
23 #define LLVM_CXXFLAGS "@LLVM_CXXFLAGS@"
24 #define LLVM_BUILDMODE "@LLVM_BUILDMODE@"
25 #define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
0 set(LLVM_LINK_COMPONENTS support)
1
2 # We need to generate the BuildVariables.inc file containing values which are
3 # only defined when under certain build modes. Unfortunately, that precludes
4 # doing this inside CMake so we have to shell out to sed. For now, that means we
5 # can't expect to build llvm-config on Window.s
6 set(BUILDVARIABLES_SRCPATH ${CMAKE_CURRENT_SOURCE_DIR}/BuildVariables.inc.in)
7 set(BUILDVARIABLES_OBJPATH ${CMAKE_CURRENT_BINARY_DIR}/BuildVariables.inc)
8 set(SEDSCRIPT_OBJPATH ${CMAKE_CURRENT_BINARY_DIR}/BuildVariables.configure.sed)
9
10 # Compute the substitution values for various items.
11 get_system_libs(LLVM_SYSTEM_LIBS_LIST)
12 foreach(l ${LLVM_SYSTEM_LIBS_LIST})
13 set(SYSTEM_LIBS ${SYSTEM_LIBS} "-l${l}")
14 endforeach()
15 set(C_FLGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
16 set(CXX_FLGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
17 set(CPP_FLGS "${CMAKE_CPP_FLAGS} ${CMAKE_CPP_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
18
19 add_custom_command(OUTPUT ${BUILDVARIABLES_OBJPATH}
20 COMMAND echo s!@LLVM_SRC_ROOT@!${LLVM_MAIN_SRC_DIR}! > ${SEDSCRIPT_OBJPATH}
21 COMMAND echo s!@LLVM_OBJ_ROOT@!${LLVM_BINARY_DIR}! >> ${SEDSCRIPT_OBJPATH}
22 COMMAND echo s!@LLVM_CPPFLAGS@!${CPP_FLGS}! >> ${SEDSCRIPT_OBJPATH}
23 COMMAND echo s!@LLVM_CFLAGS@!${C_FLGS}! >> ${SEDSCRIPT_OBJPATH}
24 COMMAND echo s!@LLVM_CXXFLAGS@!${CXX_FLGS}! >> ${SEDSCRIPT_OBJPATH}
25 # TODO: Use general flags for linking! not just for shared libs:
26 COMMAND echo s!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}! >> ${SEDSCRIPT_OBJPATH}
27 COMMAND echo s!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}! >> ${SEDSCRIPT_OBJPATH}
28 COMMAND echo s!@LLVM_SYSTEM_LIBS@!${SYSTEM_LIBS}! >> ${SEDSCRIPT_OBJPATH}
29 COMMAND sed -f ${SEDSCRIPT_OBJPATH} < ${BUILDVARIABLES_SRCPATH} > ${BUILDVARIABLES_OBJPATH}
30 VERBATIM
31 COMMENT "Building BuildVariables.inc include."
32 )
33
34 # Add the llvm-config tool.
35 add_llvm_tool(llvm-config-2
36 llvm-config.cpp
37 )
38
39 # Add the dependency on the generation step.
40 add_file_dependencies(${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.cpp ${BUILDVARIABLES_OBJPATH})
0 ##===- tools/llvm-config/Makefile---------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL := ../..
10 TOOLNAME := llvm-config-2
11 USEDLIBS := LLVMSupport.a
12
13 # We generate sources in the build directory, make sure it is in the include
14 # paths.
15 INCLUDE_BUILD_DIR := 1
16
17 # This tool has no plugins, optimize startup time.
18 TOOL_NO_EXPORTS := 1
19
20 # Note that we have to use lazy expansion here.
21 BUILDVARIABLES_SRCPATH = $(PROJ_SRC_ROOT)/tools/$(TOOLNAME)/BuildVariables.inc.in
22 BUILDVARIABLES_OBJPATH = $(ObjDir)/BuildVariables.inc
23 BUILT_SOURCES = $(BUILDVARIABLES_OBJPATH)
24
25 include $(LEVEL)/Makefile.common
26
27 # Combine preprocessor flags (except for -I) and CXX flags.
28 SUB_CPPFLAGS := ${CPP.BaseFlags}
29 SUB_CFLAGS := ${CPP.BaseFlags} ${C.Flags}
30 SUB_CXXFLAGS := ${CPP.BaseFlags} ${CXX.Flags}
31
32 # This is blank for now. We need to be careful about adding stuff here:
33 # LDFLAGS tend not to be portable, and we don't currently require the
34 # user to use libtool when linking against LLVM.
35 SUB_LDFLAGS :=
36
37 $(ObjDir)/BuildVariables.inc: $(BUILDVARIABLES_SRCPATH) Makefile $(ObjDir)/.dir
38 $(Echo) "Building llvm-config BuildVariables.inc file."
39 $(Verb) $(ECHO) 's/@LLVM_SRC_ROOT@/$(subst /,\/,$(LLVM_SRC_ROOT))/' \
40 > temp.sed
41 $(Verb) $(ECHO) 's/@LLVM_OBJ_ROOT@/$(subst /,\/,$(LLVM_OBJ_ROOT))/' \
42 >> temp.sed
43 $(Verb) $(ECHO) 's/@LLVM_CPPFLAGS@/$(subst /,\/,$(SUB_CPPFLAGS))/' \
44 >> temp.sed
45 $(Verb) $(ECHO) 's/@LLVM_CFLAGS@/$(subst /,\/,$(SUB_CFLAGS))/' \
46 >> temp.sed
47 $(Verb) $(ECHO) 's/@LLVM_CXXFLAGS@/$(subst /,\/,$(SUB_CXXFLAGS))/' \
48 >> temp.sed
49 $(Verb) $(ECHO) 's/@LLVM_LDFLAGS@/$(subst /,\/,$(SUB_LDFLAGS))/' \
50 >> temp.sed
51 $(Verb) $(ECHO) 's/@LLVM_BUILDMODE@/$(subst /,\/,$(BuildMode))/' \
52 >> temp.sed
53 $(Verb) $(ECHO) 's/@LLVM_SYSTEM_LIBS@/$(subst /,\/,$(LIBS))/' \
54 >> temp.sed
55 $(Verb) $(SED) -f temp.sed < $< > $@
56 $(Verb) $(RM) temp.sed
0 //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tool encapsulates information about an LLVM project configuration for
10 // use by other project's build environments (to determine installed path,
11 // available features, required libraries, etc.).
12 //
13 // Note that although this tool *may* be used by some parts of LLVM's build
14 // itself (i.e., the Makefiles use it to compute required libraries when linking
15 // tools), this tool is primarily designed to support external projects.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Config/config.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include
30 #include
31 #include
32
33 using namespace llvm;
34
35 // Include the build time variables we can report to the user. This is generated
36 // at build time from the BuildVariables.inc.in file by the build system.
37 #include "BuildVariables.inc"
38
39 // Include the component table. This creates an array of struct
40 // AvailableComponent entries, which record the component name, library name,
41 // and required components for all of the available libraries.
42 //
43 // Not all components define a library, we also use "library groups" as a way to
44 // create entries for pseudo groups like x86 or all-targets.
45 #include "LibraryDependencies.inc"
46
47 /// \brief Traverse a single component adding to the topological ordering in
48 /// \arg RequiredLibs.
49 ///
50 /// \param Name - The component to traverse.
51 /// \param ComponentMap - A prebuilt map of component names to descriptors.
52 /// \param VisitedComponents [in] [out] - The set of already visited components.
53 /// \param RequiredLibs [out] - The ordered list of required libraries.
54 static void VisitComponent(StringRef Name,
55 const StringMap &ComponentMap,
56 std::set &VisitedComponents,
57 std::vector &RequiredLibs) {
58 // Lookup the component.
59 AvailableComponent *AC = ComponentMap.lookup(Name);
60 assert(AC && "Invalid component name!");
61
62 // Add to the visited table.
63 if (!VisitedComponents.insert(AC).second) {
64 // We are done if the component has already been visited.
65 return;
66 }
67
68 // Otherwise, visit all the dependencies.
69 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
70 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
71 RequiredLibs);
72 }
73
74 // Add to the required library list.
75 if (AC->Library)
76 RequiredLibs.push_back(AC->Library);
77 }
78
79 /// \brief Compute the list of required libraries for a given list of
80 /// components, in an order suitable for passing to a linker (that is, libraries
81 /// appear prior to their dependencies).
82 ///
83 /// \param Components - The names of the components to find libraries for.
84 /// \param RequiredLibs [out] - On return, the ordered list of libraries that
85 /// are required to link the given components.
86 void ComputeLibsForComponents(const std::vector &Components,
87 std::vector &RequiredLibs) {
88 std::set VisitedComponents;
89
90 // Build a map of component names to information.
91 StringMap ComponentMap;
92 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
93 AvailableComponent *AC = &AvailableComponents[i];
94 ComponentMap[AC->Name] = AC;
95 }
96
97 // Visit the components.
98 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
99 // Users are allowed to provide mixed case component names.
100 std::string ComponentLower = Components[i].lower();
101
102 // Validate that the user supplied a valid component name.
103 if (!ComponentMap.count(ComponentLower)) {
104 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
105 << "\n";
106 exit(1);
107 }
108
109 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
110 RequiredLibs);
111 }
112
113 // The list is now ordered with leafs first, we want the libraries to printed
114 // in the reverse order of dependency.
115 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
116 }
117
118 /* *** */
119
120 void usage() {
121 errs() << "\
122 usage: llvm-config
123 \n\
124 Get various configuration information needed to compile programs which use\n\
125 LLVM. Typically called from 'configure' scripts. Examples:\n\
126 llvm-config --cxxflags\n\
127 llvm-config --ldflags\n\
128 llvm-config --libs engine bcreader scalaropts\n\
129 \n\
130 Options:\n\
131 --version Print LLVM version.\n\
132 --prefix Print the installation prefix.\n\
133 --src-root Print the source root LLVM was built from.\n\
134 --obj-root Print the object root used to build LLVM.\n\
135 --bindir Directory containing LLVM executables.\n\
136 --includedir Directory containing LLVM headers.\n\
137 --libdir Directory containing LLVM libraries.\n\
138 --cppflags C preprocessor flags for files that include LLVM headers.\n\
139 --cflags C compiler flags for files that include LLVM headers.\n\
140 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
141 --ldflags Print Linker flags.\n\
142 --libs Libraries needed to link against LLVM components.\n\
143 --libnames Bare library names for in-tree builds.\n\
144 --libfiles Fully qualified library filenames for makefile depends.\n\
145 --components List of all possible components.\n\
146 --targets-built List of all targets currently built.\n\
147 --host-target Target triple used to configure LLVM.\n\
148 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
149 Typical components:\n\
150 all All LLVM libraries (default).\n\
151 backend Either a native backend or the C backend.\n\
152 engine Either a native JIT or a bitcode interpreter.\n";
153 exit(1);
154 }
155
156 /// \brief Compute the path to the main executable.
157 llvm::sys::Path GetExecutablePath(const char *Argv0) {
158 // This just needs to be some symbol in the binary; C++ doesn't
159 // allow taking the address of ::main however.
160 void *P = (void*) (intptr_t) GetExecutablePath;
161 return llvm::sys::Path::GetMainExecutable(Argv0, P);
162 }
163
164 int main(int argc, char **argv) {
165 std::vector Components;
166 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
167 bool HasAnyOption = false;
168
169 // llvm-config is designed to support being run both from a development tree
170 // and from an installed path. We try and auto-detect which case we are in so
171 // that we can report the correct information when run from a development
172 // tree.
173 bool IsInDevelopmentTree, DevelopmentTreeLayoutIsCMakeStyle;
174 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]).str());
175 std::string CurrentExecPrefix;
176 std::string ActiveObjRoot;
177
178 // Create an absolute path, and pop up one directory (we expect to be inside a
179 // bin dir).
180 sys::fs::make_absolute(CurrentPath);
181 CurrentExecPrefix = sys::path::parent_path(
182 sys::path::parent_path(CurrentPath)).str();
183
184 // Check to see if we are inside a development tree by comparing to possible
185 // locations (prefix style or CMake style). This could be wrong in the face of
186 // symbolic links, but is good enough.
187 if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE) {
188 IsInDevelopmentTree = true;
189 DevelopmentTreeLayoutIsCMakeStyle = false;
190
191 // If we are in a development tree, then check if we are in a BuildTools
192 // directory. This indicates we are built for the build triple, but we
193 // always want to provide information for the host triple.
194 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
195 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
196 } else {
197 ActiveObjRoot = LLVM_OBJ_ROOT;
198 }
199 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/bin") {
200 IsInDevelopmentTree = true;
201 DevelopmentTreeLayoutIsCMakeStyle = true;
202 ActiveObjRoot = LLVM_OBJ_ROOT;
203 } else {
204 IsInDevelopmentTree = false;
205 }
206
207 // Compute various directory locations based on the derived location
208 // information.
209 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
210 std::string ActiveIncludeOption;
211 if (IsInDevelopmentTree) {
212 ActivePrefix = CurrentExecPrefix;
213
214 // CMake organizes the products differently than a normal prefix style
215 // layout.
216 if (DevelopmentTreeLayoutIsCMakeStyle) {
217 ActiveIncludeDir = ActiveObjRoot + "/include";
218 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE;
219 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE;
220 } else {
221 ActiveIncludeDir = ActiveObjRoot + "/include";
222 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin";
223 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib";
224 }
225
226 // We need to include files from both the source and object trees.
227 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
228 "-I" + ActiveObjRoot + "/include");
229 } else {
230 ActivePrefix = CurrentExecPrefix;
231 ActiveIncludeDir = ActivePrefix + "/include";
232 ActiveBinDir = ActivePrefix + "/bin";
233 ActiveLibDir = ActivePrefix + "/lib";
234 ActiveIncludeOption = "-I" + ActiveIncludeDir;
235 }
236
237 raw_ostream &OS = outs();
238 for (int i = 1; i != argc; ++i) {
239 StringRef Arg = argv[i];
240
241 if (Arg.startswith("-")) {
242 HasAnyOption = true;
243 if (Arg == "--version") {
244 OS << PACKAGE_VERSION << '\n';
245 } else if (Arg == "--prefix") {
246 OS << ActivePrefix << '\n';
247 } else if (Arg == "--bindir") {
248 OS << ActiveBinDir << '\n';
249 } else if (Arg == "--includedir") {
250 OS << ActiveIncludeDir << '\n';
251 } else if (Arg == "--libdir") {
252 OS << ActiveLibDir << '\n';
253 } else if (Arg == "--cppflags") {
254 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
255 } else if (Arg == "--cflags") {
256 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
257 } else if (Arg == "--cxxflags") {
258 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
259 } else if (Arg == "--ldflags") {
260 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS
261 << ' ' << LLVM_SYSTEM_LIBS << '\n';
262 } else if (Arg == "--libs") {
263 PrintLibs = true;
264 } else if (Arg == "--libnames") {
265 PrintLibNames = true;
266 } else if (Arg == "--libfiles") {
267 PrintLibFiles = true;
268 } else if (Arg == "--components") {
269 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
270 OS << ' ';
271 OS << AvailableComponents[j].Name;
272 }
273 OS << '\n';
274 } else if (Arg == "--targets-built") {
275 bool First = true;
276 for (TargetRegistry::iterator I = TargetRegistry::begin(),
277 E = TargetRegistry::end(); I != E; First = false, ++I) {
278 if (!First)
279 OS << ' ';
280 OS << I->getName();
281 }
282 OS << '\n';
283 } else if (Arg == "--host-target") {
284 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
285 } else if (Arg == "--build-mode") {
286 OS << LLVM_BUILDMODE << '\n';
287 } else if (Arg == "--obj-root") {
288 OS << LLVM_OBJ_ROOT << '\n';
289 } else if (Arg == "--src-root") {
290 OS << LLVM_SRC_ROOT << '\n';
291 } else {
292 usage();
293 }
294 } else {
295 Components.push_back(Arg);
296 }
297 }
298
299 if (!HasAnyOption)
300 usage();
301
302 if (PrintLibs || PrintLibNames || PrintLibFiles) {
303 // Construct the list of all the required libraries.
304 std::vector RequiredLibs;
305 ComputeLibsForComponents(Components, RequiredLibs);
306
307 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
308 StringRef Lib = RequiredLibs[i];
309 if (i)
310 OS << ' ';
311
312 if (PrintLibNames) {
313 OS << Lib;
314 } else if (PrintLibFiles) {
315 OS << ActiveLibDir << '/' << Lib;
316 } else if (PrintLibs) {
317 // If this is a typical library name, include it using -l.
318 if (Lib.startswith("lib") && Lib.endswith(".a")) {
319 OS << "-l" << Lib.slice(3, Lib.size()-2);
320 continue;
321 }
322
323 // Otherwise, print the full path.
324 OS << ActiveLibDir << '/' << Lib;
325 }
326 }
327 OS << '\n';
328 } else if (!Components.empty()) {
329 errs() << "llvm-config: error: components given, but unused\n\n";
330 usage();
331 }
332
333 return 0;
334 }