Skip to content

Commit

Permalink
[cmake] Improve the cmake infrastructure.
Browse files Browse the repository at this point in the history
This patch continues to improve the builds of Clad in the context of the llvm
mono repo. It tries to reuse as much infrastructure as possible between the
packaged version of llvm and in-tree builds. This is why we have dropped the
CLANG_ variable as they are not available in the in-tree builds. We have a
good replacement as we always require LLVM and Clang to be in sync and this
is the way they are distributed anyway.
  • Loading branch information
vgvassilev committed Dec 1, 2024
1 parent cf07998 commit a73b705
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 48 deletions.
60 changes: 20 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ set(CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
)

# Define clad supported version of clang and llvm.
set(CLANG_MIN_SUPPORTED 8.0)
set(CLANG_MAX_SUPPORTED "18.1.x")
set(CLANG_VERSION_UPPER_BOUND 18.2.0)
set(LLVM_MIN_SUPPORTED 8.0)
set(LLVM_MAX_SUPPORTED "18.1.x")
set(LLVM_VERSION_UPPER_BOUND 18.2.0)

# If we are not building as a part of LLVM, build clad as an
# standalone project, using LLVM as an external library:
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
project(clad)
set(CLAD_BUILT_STANDALONE 1)

## Define clad supported version of clang and llvm

set(CLANG_MIN_SUPPORTED 8.0)
set(CLANG_MAX_SUPPORTED "18.1.x")
set(CLANG_VERSION_UPPER_BOUND 18.2.0)
set(LLVM_MIN_SUPPORTED 8.0)
set(LLVM_MAX_SUPPORTED "18.1.x")
set(LLVM_VERSION_UPPER_BOUND 18.2.0)
project(clad)

if (NOT DEFINED Clang_DIR)
set(Clang_DIR ${LLVM_DIR})
Expand Down Expand Up @@ -134,26 +135,21 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
message(FATAL_ERROR "Please set Clang_DIR pointing to the clang build or installation folder")
endif()

set(CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
set(CLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
set(CLANG_VERSION_PATCH ${LLVM_VERSION_PATCH})
set(CLANG_PACKAGE_VERSION ${LLVM_PACKAGE_VERSION})

if (CLANG_PACKAGE_VERSION VERSION_LESS CLANG_MIN_SUPPORTED OR CLANG_PACKAGE_VERSION VERSION_GREATER_EQUAL CLANG_VERSION_UPPER_BOUND)
message(FATAL_ERROR "Found unsupported version: Clang ${CLANG_PACKAGE_VERSION};\nPlease set Clang_DIR pointing to the clang version ${CLANG_MIN_SUPPORTED} to ${CLANG_MAX_SUPPORTED} build or installation folder")
if (LLVM_PACKAGE_VERSION VERSION_LESS CLANG_MIN_SUPPORTED OR LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL CLANG_VERSION_UPPER_BOUND)
message(FATAL_ERROR "Found unsupported version: Clang ${LLVM_PACKAGE_VERSION};\nPlease set Clang_DIR pointing to the clang version ${CLANG_MIN_SUPPORTED} to ${CLANG_MAX_SUPPORTED} build or installation folder")
endif()

message(STATUS "Found supported version: Clang ${CLANG_PACKAGE_VERSION}")
message(STATUS "Found supported version: Clang ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")

if (CLANG_VERSION_MAJOR GREATER_EQUAL 16)
if (LLVM_VERSION_MAJOR GREATER_EQUAL 16)
if (NOT CMAKE_CXX_STANDARD)
set (CMAKE_CXX_STANDARD 17)
endif()
if (CMAKE_CXX_STANDARD LESS 17)
message(fatal "LLVM/Clad requires C++17 or later")
endif()
elseif (CLANG_VERSION_MAJOR GREATER_EQUAL 10)
elseif (LLVM_VERSION_MAJOR GREATER_EQUAL 10)
if (NOT CMAKE_CXX_STANDARD)
set (CMAKE_CXX_STANDARD 14)
endif()
Expand All @@ -179,17 +175,11 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# In rare cases we might want to have clang installed in a different place
# than llvm and the header files should be found first (even though the
# LLVM_INCLUDE_DIRS) contain clang headers, too.
include_directories(SYSTEM ${CLANG_INCLUDE_DIRS})
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/)

set( CLAD_BUILT_STANDALONE 1 )
else()
# Not standalone; we are building within llvm
if (NOT "clang" IN_LIST LLVM_ENABLE_PROJECTS)
Expand All @@ -198,24 +188,14 @@ else()

message(STATUS "Building Clad as part of LLVM")

# Try finding the LLVMConfig.cmake if we build against prebuilt LLVM
set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
if(EXISTS ${LLVMCONFIG_FILE})
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
include(${LLVMCONFIG_FILE})
if (PACKAGE_VERSION VERSION_LESS CLANG_MIN_SUPPORTED OR PACKAGE_VERSION VERSION_GREATER_EQUAL CLANG_VERSION_UPPER_BOUND)
message(WARNING "Building against unsupported version: Clang ${PACKAGE_VERSION};\n Supported versions are from ${CLANG_MIN_SUPPORTED} to ${CLANG_MAX_SUPPORTED}.")
endif()

set(CLANG_SOURCE_DIR ${LLVM_SOURCE_DIR}/../clang)
set(CLANG_BINARY_DIR ${LLVM_TOOLS_BINARY_DIR}/clang)
if (NOT CLANG_INCLUDE_DIRS)
set (CLANG_INCLUDE_DIRS "${LLVM_MAIN_SRC_DIR}/tools/clang/include")
if (NOT EXISTS ${CLANG_INCLUDE_DIRS})
# Otherwise assume the monorepo layout.
set (CLANG_INCLUDE_DIRS ${LLVM_MAIN_SRC_DIR}/../clang/include )
endif()
set (CLANG_INCLUDE_DIRS "${CLANG_INCLUDE_DIRS}" "${LLVM_BINARY_DIR}/tools/clang/include")
endif()
if (NOT LLVM_INCLUDE_DIRS)
set (LLVM_INCLUDE_DIRS "${LLVM_MAIN_SRC_DIR}/include" "${LLVM_BINARY_DIR}/include")
set(CLANG_INCLUDE_DIRS "${CLANG_SOURCE_DIR}/include" "${CLANG_BINARY_DIR}/include")
endif()
endif()

Expand Down
5 changes: 0 additions & 5 deletions lib/Differentiator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
set(CLANG_SOURCE_DIR ${CLAD_SOURCE_DIR}/../clang/)
if (NOT EXISTS ${CLANG_SOURCE_DIR})
set(CLANG_SOURCE_DIR "${Clang_DIR}/")
endif()

clad_compat__find_first_existing_vc_file("${CLANG_SOURCE_DIR}" clang_vc)
clad_compat__find_first_existing_vc_file("${CLAD_SOURCE_DIR}" clad_vc)

Expand Down
2 changes: 1 addition & 1 deletion test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sys
## Autogenerated by LLVM/clad configuration.
# Do not edit!
llvm_version_major = @LLVM_VERSION_MAJOR@
config.clang_version_major = @CLANG_VERSION_MAJOR@
config.clang_version_major = @LLVM_VERSION_MAJOR@
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
Expand Down
4 changes: 2 additions & 2 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ if (NOT CLAD_BUILD_STATIC_ONLY)
)

# Set build byproducts only needed by Ninja.
set(_enzyme_static_target EnzymeStatic-${CLANG_VERSION_MAJOR})
set(_enzyme_static_target EnzymeStatic-${LLVM_VERSION_MAJOR})
set(_enzyme_static_archive_name ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}${CMAKE_STATIC_LIBRARY_PREFIX}${_enzyme_static_target}${CMAKE_STATIC_LIBRARY_SUFFIX})
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
set(ENZYME_BYPRODUCTS ${_enzyme_static_archive_name})
Expand Down Expand Up @@ -134,4 +134,4 @@ if (NOT CLAD_BUILD_STATIC_ONLY)
add_dependencies(clad LLVMEnzyme)
endif(CLAD_ENABLE_ENZYME_BACKEND)
endif()
set_source_files_properties(ClangPlugin.cpp PROPERTIES COMPILE_DEFINITIONS CLANG_MAJOR_VERSION="${CLANG_VERSION_MAJOR}")
set_source_files_properties(ClangPlugin.cpp PROPERTIES COMPILE_DEFINITIONS CLANG_MAJOR_VERSION="${LLVM_VERSION_MAJOR}")

0 comments on commit a73b705

Please sign in to comment.