From aab8463fc98d68eb4c407e7a99e1eb2937eefaa0 Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Sun, 1 Dec 2024 09:19:24 +0000 Subject: [PATCH 1/3] [cmake] Remove dead code --- CMakeLists.txt | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e0dccaf3..a7f2fceed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,24 +186,6 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) include_directories(SYSTEM ${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) - # If the llvm sources are present add them with higher priority. - if (LLVM_BUILD_MAIN_SRC_DIR) - # LLVM_INCLUDE_DIRS contains the include paths to both LLVM's source and - # build directories. Since we cannot just include ClangConfig.cmake (see - # fixme above) we have to do a little more work to get the right include - # paths for clang. - # - # FIXME: We only support in-tree builds of clang, that is clang being built - # in llvm_src/tools/clang. - include_directories(SYSTEM ${LLVM_BUILD_MAIN_SRC_DIR}/tools/clang/include/) - - if (NOT LLVM_BUILD_BINARY_DIR) - message(FATAL "LLVM_BUILD_* values should be available for the build tree") - endif() - - include_directories(SYSTEM ${LLVM_BUILD_BINARY_DIR}/tools/clang/include/) - endif() - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/) From cf07998097c8a8a371e43ed076ea9fa30d3ce842 Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Sun, 1 Dec 2024 09:19:47 +0000 Subject: [PATCH 2/3] [cmake] GTest depends on clad-aware clang. Make sure for to add the right dependency in cases clad is built against the monorepo. --- unittests/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index c714bee97..db528990c 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -4,6 +4,9 @@ set_target_properties(CladUnitTests PROPERTIES FOLDER "Clad tests") # LLVM builds (not installed llvm) provides gtest. if (NOT TARGET gtest) include(CladGoogleTest) + if(NOT CLAD_BUILT_STANDALONE) + add_dependencies(googletest clang) + endif() endif() # add_clad_unittest(test_dirname file1.cpp file2.cpp) From 622765731c3c4f3df155700368e303a55f5dc51c Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Sun, 1 Dec 2024 10:32:28 +0000 Subject: [PATCH 3/3] [cmake] Improve the cmake infrastructure. 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. --- CMakeLists.txt | 60 +++++++++++-------------------- Compatibility.cmake | 4 +-- README.md | 20 ++++------- lib/Differentiator/CMakeLists.txt | 5 --- test/lit.site.cfg.in | 2 +- tools/CMakeLists.txt | 4 +-- 6 files changed, 31 insertions(+), 64 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7f2fceed..b1cace8f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) @@ -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() @@ -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) @@ -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() diff --git a/Compatibility.cmake b/Compatibility.cmake index de69699ea..b5dae29f5 100644 --- a/Compatibility.cmake +++ b/Compatibility.cmake @@ -19,7 +19,7 @@ if (NOT COMMAND add_version_info_from_vcs) # find_first_existing_vc_file(var path) --> find_first_existing_vc_file(path var) macro(clad_compat__find_first_existing_vc_file path out_var) if (COMMAND find_first_existing_vc_file) - find_first_existing_vc_file(${path} out_var) + find_first_existing_vc_file("${path}" out_var) endif() endmacro(clad_compat__find_first_existing_vc_file) else() @@ -27,7 +27,7 @@ else() # find_first_existing_vc_file(var path) --> find_first_existing_vc_file(path var) macro(clad_compat__find_first_existing_vc_file path out_var) if (COMMAND find_first_existing_vc_file) - find_first_existing_vc_file(out_var ${path}) + find_first_existing_vc_file(out_var "${path}") endif() endmacro(clad_compat__find_first_existing_vc_file) endif() diff --git a/README.md b/README.md index bfae57d9b..52926cde7 100644 --- a/README.md +++ b/README.md @@ -280,25 +280,17 @@ Clone the LLVM project and checkout the required LLVM version (Currently support ``` git clone https://github.com/llvm/llvm-project.git +git clone https://github.com/vgvassilev/clad.git cd llvm-project -git checkout llvmorg-16.0.0 -``` -Build Clang: -``` -mkdir build && cd build -cmake -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE="DEBUG" -DLLVM_TARGETS_TO_BUILD=host -DLLVM_INSTALL_UTILS=ON ../llvm -cmake --build . --target clang --parallel $(nproc --all) -make -j8 check-clang # this installs llvm-config required by lit -cd ../.. +git checkout llvmorg-18.0.0 ``` -Clone and build Clad: +Build Clad with Clang and LLVM: ``` -git clone https://github.com/vgvassilev/clad.git -cd clad mkdir build && cd build -cmake -DLLVM_DIR=PATH/TO/llvm-project/build -DCMAKE_BUILD_TYPE=DEBUG -DLLVM_EXTERNAL_LIT="$(which lit)" ../ -make -j8 clad +cmake -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_EXTERNAL_PROJECTS=clad -DLLVM_EXTERNAL_CLAD_SOURCE_DIR=../../clad -DCMAKE_BUILD_TYPE="Debug" -DLLVM_TARGETS_TO_BUILD=host -DLLVM_INSTALL_UTILS=ON ../llvm +cmake --build . --target clad --parallel $(nproc --all) +cd ../.. ``` Run the Clad tests: diff --git a/lib/Differentiator/CMakeLists.txt b/lib/Differentiator/CMakeLists.txt index 177608449..22e293ca5 100644 --- a/lib/Differentiator/CMakeLists.txt +++ b/lib/Differentiator/CMakeLists.txt @@ -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) diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in index b1eccea24..d40f514ab 100644 --- a/test/lit.site.cfg.in +++ b/test/lit.site.cfg.in @@ -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@" diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index b5884863d..ecf61f4ae 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -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}) @@ -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}")