Skip to content

Commit e7a9820

Browse files
author
Chris Bieneman
committedFeb 8, 2017
[CMake] Fix is_llvm_target_library and support out-of-order components
Summary: This patch is required by D28855, and enables us to rely on CMake's ability to handle out of order target dependencies. Reviewers: mgorny, chapuni, bryant Subscribers: llvm-commits, jgosnell Differential Revision: https://reviews.llvm.org/D28869 llvm-svn: 294514
1 parent cb1aab6 commit e7a9820

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed
 

‎llvm/cmake/modules/LLVM-Config.cmake

+73-11
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,61 @@ function(link_system_libs target)
88
message(AUTHOR_WARNING "link_system_libs no longer needed")
99
endfunction()
1010

11-
11+
# is_llvm_target_library(
12+
# library
13+
# Name of the LLVM library to check
14+
# return_var
15+
# Output variable name
16+
# ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS
17+
# ALL_TARGETS - default looks at the full list of known targets
18+
# INCLUDED_TARGETS - looks only at targets being configured
19+
# OMITTED_TARGETS - looks only at targets that are not being configured
20+
# )
1221
function(is_llvm_target_library library return_var)
22+
cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
1323
# Sets variable `return_var' to ON if `library' corresponds to a
1424
# LLVM supported target. To OFF if it doesn't.
1525
set(${return_var} OFF PARENT_SCOPE)
1626
string(TOUPPER "${library}" capitalized_lib)
17-
string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
27+
if(ARG_INCLUDED_TARGETS)
28+
string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
29+
elseif(ARG_OMITTED_TARGETS)
30+
set(omitted_targets ${LLVM_ALL_TARGETS})
31+
list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
32+
string(TOUPPER "${omitted_targets}" targets)
33+
else()
34+
string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
35+
endif()
1836
foreach(t ${targets})
1937
if( capitalized_lib STREQUAL t OR
20-
capitalized_lib STREQUAL "LLVM${t}" OR
21-
capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
22-
capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
23-
capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
24-
capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
25-
capitalized_lib STREQUAL "LLVM${t}INFO" )
38+
capitalized_lib STREQUAL "${t}" OR
39+
capitalized_lib STREQUAL "${t}DESC" OR
40+
capitalized_lib STREQUAL "${t}CODEGEN" OR
41+
capitalized_lib STREQUAL "${t}ASMPARSER" OR
42+
capitalized_lib STREQUAL "${t}ASMPRINTER" OR
43+
capitalized_lib STREQUAL "${t}DISASSEMBLER" OR
44+
capitalized_lib STREQUAL "${t}INFO" OR
45+
capitalized_lib STREQUAL "${t}UTILS" )
2646
set(${return_var} ON PARENT_SCOPE)
2747
break()
2848
endif()
2949
endforeach()
3050
endfunction(is_llvm_target_library)
3151

52+
function(is_llvm_target_specifier library return_var)
53+
is_llvm_target_library(${library} ${return_var} ${ARGN})
54+
string(TOUPPER "${library}" capitalized_lib)
55+
if(NOT ${return_var})
56+
if( capitalized_lib STREQUAL "ALLTARGETSASMPARSERS" OR
57+
capitalized_lib STREQUAL "ALLTARGETSDESCS" OR
58+
capitalized_lib STREQUAL "ALLTARGETSDISASSEMBLERS" OR
59+
capitalized_lib STREQUAL "ALLTARGETSINFOS" OR
60+
capitalized_lib STREQUAL "NATIVE" OR
61+
capitalized_lib STREQUAL "NATIVECODEGEN" )
62+
set(${return_var} ON PARENT_SCOPE)
63+
endif()
64+
endif()
65+
endfunction()
3266

3367
macro(llvm_config executable)
3468
cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
@@ -93,6 +127,21 @@ function(llvm_map_components_to_libnames out_libs)
93127
endif()
94128
string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
95129

130+
get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
131+
132+
# Generally in our build system we avoid order-dependence. Unfortunately since
133+
# not all targets create the same set of libraries we actually need to ensure
134+
# that all build targets associated with a target are added before we can
135+
# process target dependencies.
136+
if(NOT LLVM_TARGETS_CONFIGURED)
137+
foreach(c ${link_components})
138+
is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
139+
if(iltl_result)
140+
message(FATAL_ERROR "Specified target library before target registration is complete.")
141+
endif()
142+
endforeach()
143+
endif()
144+
96145
# Expand some keywords:
97146
list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
98147
list(FIND link_components "engine" engine_required)
@@ -141,6 +190,12 @@ function(llvm_map_components_to_libnames out_libs)
141190
if( TARGET LLVM${c}Disassembler )
142191
list(APPEND expanded_components "LLVM${c}Disassembler")
143192
endif()
193+
if( TARGET LLVM${c}Info )
194+
list(APPEND expanded_components "LLVM${c}Info")
195+
endif()
196+
if( TARGET LLVM${c}Utils )
197+
list(APPEND expanded_components "LLVM${c}Utils")
198+
endif()
144199
elseif( c STREQUAL "native" )
145200
# already processed
146201
elseif( c STREQUAL "nativecodegen" )
@@ -198,9 +253,16 @@ function(llvm_map_components_to_libnames out_libs)
198253
list(FIND capitalized_libs LLVM${capitalized} lib_idx)
199254
if( lib_idx LESS 0 )
200255
# The component is unknown. Maybe is an omitted target?
201-
is_llvm_target_library(${c} iltl_result)
202-
if( NOT iltl_result )
203-
message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
256+
is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
257+
if(iltl_result)
258+
# A missing library to a directly referenced omitted target would be bad.
259+
message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
260+
else()
261+
# If it is not an omitted target we should assume it is a component
262+
# that hasn't yet been processed by CMake. Missing components will
263+
# cause errors later in the configuration, so we can safely assume
264+
# that this is valid here.
265+
list(APPEND expanded_components LLVM${c})
204266
endif()
205267
else( lib_idx LESS 0 )
206268
list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)

‎llvm/lib/Target/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ foreach(t ${LLVM_TARGETS_TO_BUILD})
1717
message(STATUS "Targeting ${t}")
1818
add_subdirectory(${t})
1919
endforeach()
20+
21+
# Currently we do not allow libraries from lib to reference targets directly.
22+
# This property is used to enforce that convention. It is important because the
23+
# logic in llvm_map_components_to_libnames is order dependent on the target
24+
# libraries being created.
25+
set_property(GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED On)

0 commit comments

Comments
 (0)