Skip to content

Commit 305b25c

Browse files
mgornyvzakhari
andauthored
[flang] Support discovering LLVM/Clang/MLIR without explicit *_DIR (#122639)
Support discovering LLVM, Clang and MLIR via the standard CMake logic in addition to explicitly specified `LLVM_DIR`, etc. To prevent breaking anyone's workflow the way #120914 did, this change explicitly introduces two possible code paths based on variables provided: 1. If `LLVM_DIR`, etc. are defined, the current logic is used as-is. 2. If they are not defined, `find_package()` is called normally to discover the packages using the standard CMake logic, and the discovered paths are added --------- Co-authored-by: Slava Zakharin <[email protected]>
1 parent 8d306cc commit 305b25c

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

flang/CMakeLists.txt

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,37 @@ if (FLANG_STANDALONE_BUILD)
9191

9292
# If the user specifies a relative path to LLVM_DIR, the calls to include
9393
# LLVM modules fail. Append the absolute path to LLVM_DIR instead.
94-
get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR}
95-
REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
96-
list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
94+
if (LLVM_DIR)
95+
get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR}
96+
REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
97+
list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
98+
endif()
9799
# We need a pre-built/installed version of LLVM.
98100
find_package(LLVM REQUIRED HINTS "${LLVM_DIR_ABSOLUTE}")
101+
if (NOT LLVM_DIR_ABSOLUTE)
102+
# If the user did not specify a LLVM_DIR (and therefore LLVM_DIR_ABSOLUTE
103+
# was not set), append the discovered path to CMAKE_MODULE_PATH.
104+
list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
105+
endif()
99106

100107
# Users might specify a path to CLANG_DIR that's:
101108
# * a full path, or
102109
# * a path relative to the path of this script.
103110
# Append the absolute path to CLANG_DIR so that find_package works in both
104111
# cases.
105-
get_filename_component(
106-
CLANG_DIR_ABSOLUTE
107-
${CLANG_DIR}
108-
REALPATH
109-
BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
110-
list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
111-
112-
# TODO: Remove when libclangDriver is lifted out of Clang
113-
find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
114-
if (NOT Clang_FOUND)
115-
message(FATAL_ERROR "Failed to find Clang")
112+
if (CLANG_DIR)
113+
get_filename_component(
114+
CLANG_DIR_ABSOLUTE
115+
${CLANG_DIR}
116+
REALPATH
117+
BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
118+
list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
119+
120+
# TODO: Remove when libclangDriver is lifted out of Clang
121+
find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
122+
else()
123+
find_package(Clang REQUIRED)
124+
list(APPEND CMAKE_MODULE_PATH ${Clang_DIR})
116125
endif()
117126

118127
# If LLVM links to zlib we need the imported targets so we can too.
@@ -134,10 +143,15 @@ if (FLANG_STANDALONE_BUILD)
134143
include(TableGen)
135144
# If the user specifies a relative path to MLIR_DIR, the calls to include
136145
# MLIR modules fail. Append the absolute path to MLIR_DIR instead.
137-
get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR}
138-
REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
139-
list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
146+
if (MLIR_DIR)
147+
get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR}
148+
REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
149+
list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
150+
endif()
140151
find_package(MLIR REQUIRED CONFIG HINTS ${MLIR_DIR_ABSOLUTE})
152+
if (NOT MLIR_DIR_ABSOLUTE)
153+
list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR})
154+
endif()
141155
# Use SYSTEM for the same reasons as for LLVM includes
142156
include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
143157
include(AddMLIR)

0 commit comments

Comments
 (0)