llvm.org GIT mirror llvm / 4c56efb
Provide CMake package modules in install tree Teach the Makefile build system to generate and install CMake modules LLVMConfig.cmake and LLVMConfigVersion.cmake so that applications that build with CMake can use 'find_package(LLVM)' even when LLVM is not built with CMake. These modules tell such applications about available LLVM libraries and their dependencies. Run llvm-config to generate the list of libraries and use the results of llvm-build to generate the library dependencies. Use sed to perform substitutions in the LLVMConfig.cmake.in and LLVMConfigVersion.cmake.in sources that our CMake build system uses. Teach the Makefile build system to generate the LLVMExports.cmake file with content similar to that produced by the CMake install(EXPORT) command. Extend llvm-build with an option to generate the library dependencies fragment for this file. Contributed by Brad King. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201053 91177308-0d34-0410-b5e6-96231b3b80d8 NAKAMURA Takumi 5 years ago
5 changed file(s) with 173 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
1414 # 3. Build IR, which builds the Intrinsics.inc file used by libs.
1515 # 4. Build libs, which are needed by llvm-config.
1616 # 5. Build llvm-config, which determines inter-lib dependencies for tools.
17 # 6. Build tools and docs.
17 # 6. Build tools, docs, and cmake modules.
1818 #
1919 # When cross-compiling, there are some things (tablegen) that need to
2020 # be build for the build system first.
3030 OPTIONAL_DIRS := tools/clang/utils/TableGen
3131 else
3232 DIRS := lib/Support lib/TableGen utils lib/IR lib tools/llvm-shlib \
33 tools/llvm-config tools docs unittests
33 tools/llvm-config tools docs cmake unittests
3434 OPTIONAL_DIRS := projects bindings
3535 endif
3636
7777
7878 # The files we are going to generate using llvm-build.
7979 LLVMBuildMakeFrag := $(PROJ_OBJ_ROOT)/Makefile.llvmbuild
80 LLVMBuildCMakeFrag := $(PROJ_OBJ_ROOT)/LLVMBuild.cmake
81 LLVMBuildCMakeExportsFrag := $(PROJ_OBJ_ROOT)/cmake/modules/LLVMBuildExports.cmake
82 LLVMBuildMakeFrags := \
83 $(LLVMBuildMakeFrag) \
84 $(LLVMBuildCMakeFrag) \
85 $(LLVMBuildCMakeExportsFrag)
8086 LLVMConfigLibraryDependenciesInc := \
8187 $(PROJ_OBJ_ROOT)/tools/llvm-config/LibraryDependencies.inc
8288
9399 #
94100 # We include a dependency on this Makefile to ensure that changes to the
95101 # generation command get picked up.
96 $(LLVMBuildMakeFrag): $(PROJ_SRC_ROOT)/Makefile.rules \
97 $(PROJ_OBJ_ROOT)/Makefile.config
102 $(LLVMBuildMakeFrags): $(PROJ_SRC_ROOT)/Makefile.rules \
103 $(PROJ_OBJ_ROOT)/Makefile.config
98104 $(Echo) Constructing LLVMBuild project information.
99105 $(Verb)$(PYTHON) $(LLVMBuildTool) \
100106 --native-target "$(TARGET_NATIVE_ARCH)" \
101107 --enable-targets "$(TARGETS_TO_BUILD)" \
102108 --enable-optional-components "$(OPTIONAL_COMPONENTS)" \
103109 --write-library-table $(LLVMConfigLibraryDependenciesInc) \
104 --write-make-fragment $(LLVMBuildMakeFrag)
110 --write-make-fragment $(LLVMBuildMakeFrag) \
111 --write-cmake-fragment $(LLVMBuildCMakeFrag) \
112 --write-cmake-exports-fragment $(LLVMBuildCMakeExportsFrag)
105113
106114 # For completeness, let Make know how the extra files are generated.
107 $(LLVMConfigLibraryDependenciesInc): $(LLVMBuildMakeFrag)
115 $(LLVMConfigLibraryDependenciesInc): $(LLVMBuildMakeFrags)
108116
109117 # Include the generated Makefile fragment.
110118 #
119127
120128 # Clean the generated makefile fragment at the top-level.
121129 clean-local::
122 -$(Verb) $(RM) -f $(LLVMBuildMakeFrag)
130 -$(Verb) $(RM) -f $(LLVMBuildMakeFrags)
123131 endif
124132 -include $(LLVMBuildMakeFrag)
125133
0 ##===- cmake/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 LEVEL = ..
9 DIRS := modules
10
11 include $(LEVEL)/Makefile.common
0 ##===- cmake/modules/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
11 LINK_COMPONENTS := all
12
13 include $(LEVEL)/Makefile.common
14
15 PROJ_cmake := $(DESTDIR)$(PROJ_prefix)/share/llvm/cmake
16
17 OBJMODS := LLVMConfig.cmake LLVMConfigVersion.cmake LLVMExports.cmake
18
19 # TODO: Teach LLVM-Config.cmake to work without explicit terminfo libs.
20 TERMINFO_LIBS := tinfo terminfo curses ncurses ncursesw
21 TERMINFO_LIBS := $(filter $(TERMINFO_LIBS),$(subst -l,,$(LIBS)))
22
23 $(PROJ_OBJ_DIR)/LLVMConfig.cmake: LLVMConfig.cmake.in $(LLVMBuildCMakeFrag)
24 $(Echo) 'Generating LLVM CMake package config file'
25 $(Verb) ( \
26 cat $< | sed \
27 -e 's/@LLVM_CONFIG_CODE@/set(LLVM_INSTALL_PREFIX "'"$(subst /,\/,$(PROJ_prefix))"'")/' \
28 -e 's/@LLVM_VERSION_MAJOR@/'"$(LLVM_VERSION_MAJOR)"'/' \
29 -e 's/@LLVM_VERSION_MINOR@/'"$(LLVM_VERSION_MINOR)"'/' \
30 -e 's/@PACKAGE_VERSION@/'"$(LLVMVersion)"'/' \
31 -e 's/@LLVM_COMMON_DEPENDS@//' \
32 -e 's/"@llvm_libs@"/'"$(subst -l,,$(LLVMConfigLibs))"'/' \
33 -e 's/@LLVM_ALL_TARGETS@/'"$(ALL_TARGETS)"'/' \
34 -e 's/@LLVM_TARGETS_TO_BUILD@/'"$(TARGETS_TO_BUILD)"'/' \
35 -e 's/@LLVM_TARGETS_WITH_JIT@/'"$(TARGETS_WITH_JIT)"'/' \
36 -e 's/@TARGET_TRIPLE@/'"$(TARGET_TRIPLE)"'/' \
37 -e 's/@LLVM_ENABLE_TERMINFO@/'"$(ENABLE_TERMINFO)"'/' \
38 -e 's/@LLVM_ENABLE_THREADS@/'"$(ENABLE_THREADS)"'/' \
39 -e 's/@LLVM_ENABLE_ZLIB@/'"$(ENABLE_ZLIB)"'/' \
40 -e 's/@LLVM_NATIVE_ARCH@/'"$(LLVM_NATIVE_ARCH)"'/' \
41 -e 's/@LLVM_ENABLE_PIC@/'"$(ENABLE_PIC)"'/' \
42 -e 's/@HAVE_TERMINFO@/'"$(HAVE_TERMINFO)"'/' \
43 -e 's/@TERMINFO_LIBS@/'"$(TERMINFO_LIBS)"'/' \
44 -e 's/@HAVE_LIBDL@/'"$(HAVE_DLOPEN)"'/' \
45 -e 's/@HAVE_LIBPTHREAD@/'"$(HAVE_PTHREAD)"'/' \
46 -e 's/@HAVE_LIBZ@/'"$(HAVE_LIBZ)"'/' \
47 -e 's/@LLVM_ON_UNIX@/'"$(LLVM_ON_UNIX)"'/' \
48 -e 's/@LLVM_ON_WIN32@/'"$(LLVM_ON_WIN32)"'/' \
49 -e 's/@LLVM_CONFIG_INCLUDE_DIRS@/'"$(subst /,\/,$(PROJ_includedir))"'/' \
50 -e 's/@LLVM_CONFIG_LIBRARY_DIRS@/'"$(subst /,\/,$(PROJ_libdir))"'/' \
51 -e 's/@LLVM_CONFIG_CMAKE_DIR@/'"$(subst /,\/,$(PROJ_cmake))"'/' \
52 -e 's/@LLVM_CONFIG_EXPORTS_FILE@/$${LLVM_CMAKE_DIR}\/LLVMExports.cmake/' \
53 -e 's/@all_llvm_lib_deps@//' \
54 && \
55 # TODO: Teach LLVM-Config.cmake to use builtin CMake features \
56 # for library dependencies. For now add the generated fragments. \
57 grep '^set_property.*LLVMBUILD_LIB_DEPS_' "$(LLVMBuildCMakeFrag)" \
58 ) > $@
59
60 $(PROJ_OBJ_DIR)/LLVMConfigVersion.cmake: LLVMConfigVersion.cmake.in
61 $(Echo) 'Generating LLVM CMake package version file'
62 $(Verb) cat $< | sed \
63 -e 's/@PACKAGE_VERSION@/'"$(LLVMVersion)"'/' \
64 > $@
65
66 $(PROJ_OBJ_DIR)/LLVMExports.cmake: $(LLVMBuildCMakeExportsFrag)
67 $(Echo) 'Generating LLVM CMake target exports file'
68 $(Verb) ( \
69 echo '# LLVM CMake target exports. Do not include directly.' && \
70 for lib in $(subst -l,,$(LLVMConfigLibs)); do \
71 echo 'add_library('"$$lib"' STATIC IMPORTED)' && \
72 echo 'set_property(TARGET '"$$lib"' PROPERTY IMPORTED_LOCATION "'"$(PROJ_libdir)/lib$$lib.a"'")' ; \
73 done && \
74 cat "$(LLVMBuildCMakeExportsFrag)" \
75 ) > $@
76
77 all-local:: $(addprefix $(PROJ_OBJ_DIR)/, $(OBJMODS))
78
79 SKIPSRCMODS := \
80 CheckAtomic.cmake \
81 GetHostTriple.cmake \
82 LLVMBuildExports.cmake \
83 LLVMConfig.cmake \
84 LLVMConfigVersion.cmake \
85 LLVMExports.cmake \
86 VersionFromVCS.cmake
87
88 SRCMODS := $(notdir $(wildcard $(PROJ_SRC_DIR)/*.cmake))
89 SRCMODS := $(filter-out $(SKIPSRCMODS),$(SRCMODS))
90 INSTSRCMODS := $(addprefix $(PROJ_cmake)/, $(SRCMODS))
91 INSTOBJMODS := $(addprefix $(PROJ_cmake)/, $(OBJMODS))
92
93 $(PROJ_cmake):
94 $(Echo) Making install directory: $@
95 $(Verb) $(MKDIR) $@
96
97 $(INSTSRCMODS): $(PROJ_cmake)/%.cmake: $(PROJ_SRC_DIR)/%.cmake | $(PROJ_cmake)
98 $(Echo) Installing cmake modules: $(notdir $<)
99 $(Verb) $(DataInstall) $< $(PROJ_cmake)
100
101 $(INSTOBJMODS): $(PROJ_cmake)/%.cmake: $(PROJ_OBJ_DIR)/%.cmake | $(PROJ_cmake)
102 $(Echo) Installing cmake modules: $(notdir $<)
103 $(Verb) $(DataInstall) $< $(PROJ_cmake)
104
105 install-local:: $(INSTSRCMODS) $(INSTOBJMODS)
572572
573573 f.close()
574574
575 def write_cmake_exports_fragment(self, output_path):
576 """
577 write_cmake_exports_fragment(output_path) -> None
578
579 Generate a CMake fragment which includes LLVMBuild library
580 dependencies expressed similarly to how CMake would write
581 them via install(EXPORT).
582 """
583
584 dependencies = list(self.get_fragment_dependencies())
585
586 # Write out the CMake exports fragment.
587 make_install_dir(os.path.dirname(output_path))
588 f = open(output_path, 'w')
589
590 f.write("""\
591 # Explicit library dependency information.
592 #
593 # The following property assignments tell CMake about link
594 # dependencies of libraries imported from LLVM.
595 """)
596 for ci in self.ordered_component_infos:
597 # We only write the information for libraries currently.
598 if ci.type_name != 'Library':
599 continue
600
601 f.write("""\
602 set_property(TARGET %s PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES %s)\n""" % (
603 ci.get_prefixed_library_name(), " ".join(sorted(
604 dep.get_prefixed_library_name()
605 for dep in self.get_required_libraries_for_component(ci)))))
606
607 f.close()
608
575609 def write_make_fragment(self, output_path):
576610 """
577611 write_make_fragment(output_path) -> None
778812 group.add_option("", "--write-cmake-fragment",
779813 dest="write_cmake_fragment", metavar="PATH",
780814 help="Write the CMake project information to PATH",
815 action="store", default=None)
816 group.add_option("", "--write-cmake-exports-fragment",
817 dest="write_cmake_exports_fragment", metavar="PATH",
818 help="Write the CMake exports information to PATH",
781819 action="store", default=None)
782820 group.add_option("", "--write-make-fragment",
783821 dest="write_make_fragment", metavar="PATH",
860898 # Write out the cmake fragment, if requested.
861899 if opts.write_cmake_fragment:
862900 project_info.write_cmake_fragment(opts.write_cmake_fragment)
901 if opts.write_cmake_exports_fragment:
902 project_info.write_cmake_exports_fragment(opts.write_cmake_exports_fragment)
863903
864904 # Configure target definition files, if requested.
865905 if opts.configure_target_def_files: