[flang-commits] [flang] [flang] Support discovering LLVM/Clang/MLIR without explicit *_DIR (PR #122639)
Michał Górny via flang-commits
flang-commits at lists.llvm.org
Sun Jan 12 06:08:00 PST 2025
https://github.com/mgorny created https://github.com/llvm/llvm-project/pull/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
>From 769ccb8d06281c213636155a642858a3fcb734c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sun, 12 Jan 2025 15:03:22 +0100
Subject: [PATCH] [flang] Support discovering LLVM/Clang/MLIR without explicit
*_DIR
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
---
flang/CMakeLists.txt | 48 ++++++++++++++++++++++++++++----------------
1 file changed, 31 insertions(+), 17 deletions(-)
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 68947eaa9c9bd7..ee64ee462b2fcc 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -91,28 +91,37 @@ if (FLANG_STANDALONE_BUILD)
# If the user specifies a relative path to LLVM_DIR, the calls to include
# LLVM modules fail. Append the absolute path to LLVM_DIR instead.
- get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR}
- REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
- list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
+ IF (LLVM_DIR)
+ get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR}
+ REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
+ list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
+ endif()
# We need a pre-built/installed version of LLVM.
find_package(LLVM REQUIRED HINTS "${LLVM_DIR_ABSOLUTE}")
+ if (NOT LLVM_DIR_ABSOLUTE)
+ # If the user did not specify a LLVM_DIR (and therefore LLVM_DIR_ABSOLUTE
+ # was not set), append the discovered path to CMAKE_MODULE_PATH.
+ list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
+ endif()
# Users might specify a path to CLANG_DIR that's:
# * a full path, or
# * a path relative to the path of this script.
# Append the absolute path to CLANG_DIR so that find_package works in both
# cases.
- get_filename_component(
- CLANG_DIR_ABSOLUTE
- ${CLANG_DIR}
- REALPATH
- BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
- list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
-
- # TODO: Remove when libclangDriver is lifted out of Clang
- find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
- if (NOT Clang_FOUND)
- message(FATAL_ERROR "Failed to find Clang")
+ IF (CLANG_DIR)
+ get_filename_component(
+ CLANG_DIR_ABSOLUTE
+ ${CLANG_DIR}
+ REALPATH
+ BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
+ list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
+
+ # TODO: Remove when libclangDriver is lifted out of Clang
+ find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
+ else()
+ find_package(Clang REQUIRED)
+ list(APPEND CMAKE_MODULE_PATH ${Clang_DIR})
endif()
# If LLVM links to zlib we need the imported targets so we can too.
@@ -134,10 +143,15 @@ if (FLANG_STANDALONE_BUILD)
include(TableGen)
# If the user specifies a relative path to MLIR_DIR, the calls to include
# MLIR modules fail. Append the absolute path to MLIR_DIR instead.
- get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR}
- REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
- list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
+ if (MLIR_DIR)
+ get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR}
+ REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
+ list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
+ endif()
find_package(MLIR REQUIRED CONFIG HINTS ${MLIR_DIR_ABSOLUTE})
+ if (NOT MLIR_DIR_ABSOLUTE)
+ list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR})
+ endif()
# Use SYSTEM for the same reasons as for LLVM includes
include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
include(AddMLIR)
More information about the flang-commits
mailing list