From 4faf53ef0ef44a540eebdbe2fd923d531fb62bc1 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Wed, 27 Nov 2019 23:07:13 +0200 Subject: [PATCH] Fix clad development style builds. Add clang 10/git support. --- CMakeLists.txt | 29 +----- Compatibility.cmake | 30 ++++++ README.md | 3 +- include/clad/Differentiator/Compatibility.h | 17 ++++ .../clad/Differentiator/DerivativeBuilder.h | 2 +- lib/Differentiator/CMakeLists.txt | 92 +++++-------------- lib/Differentiator/ConstantFolder.cpp | 2 +- lib/Differentiator/DerivativeBuilder.cpp | 6 +- lib/Differentiator/DiffPlanner.cpp | 5 +- lib/Differentiator/Version.cpp | 4 +- tools/CMakeLists.txt | 2 +- tools/ClangPlugin.h | 2 +- 12 files changed, 89 insertions(+), 105 deletions(-) create mode 100644 Compatibility.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 401c81ca3..5b3ba8083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,9 @@ cmake_minimum_required(VERSION 3.7.0) +if(POLICY CMP0075) + cmake_policy(SET CMP0075 NEW) +endif() + # 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 ) @@ -109,30 +113,6 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) # For consistency we should set it to the correct value. set(LLVM_CONFIG_HAS_RTTI NO CACHE BOOL "" FORCE) - - ## Compatibility - - # Clang 8 remove add_llvm_loadable_module for cmake files. - # Recomended is use of add_llvm_library with MODULE argument. - if (CLANG_PACKAGE_VERSION VERSION_LESS 8.0) - macro(clad_compat_add_llvm_loadable_module module_name) - add_llvm_loadable_module(${module_name} ${ARGN}) - endmacro(clad_compat_add_llvm_loadable_module) - else() - macro(clad_compat_add_llvm_loadable_module module_name) - add_llvm_library(${module_name} MODULE ${ARGN}) - endmacro(clad_compat_add_llvm_loadable_module) - endif() - - # Clang 9 remove add_version_info_from_vcs. - # Recomended is use of get_source_info - if (NOT CLANG_PACKAGE_VERSION VERSION_LESS 9.0) - macro(add_version_info_from_vcs VERS) - get_source_info(${CMAKE_CURRENT_SOURCE_DIR} VERS DUMMY_REP) - endmacro(add_version_info_from_vcs) - endif() - - ## Init # In case this was a path to a build folder of llvm still try to find AddLLVM @@ -194,6 +174,7 @@ if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") endif() ## +include(Compatibility.cmake) set(C_INCLUDE_DIRS "" CACHE STRING "Colon separated list of directories clad will search for headers.") diff --git a/Compatibility.cmake b/Compatibility.cmake new file mode 100644 index 000000000..b64188f25 --- /dev/null +++ b/Compatibility.cmake @@ -0,0 +1,30 @@ +## Compatibility + +# Clang 8 remove add_llvm_loadable_module for cmake files. +# Recomended is use of add_llvm_library with MODULE argument. +if (NOT COMMAND add_llvm_loadable_module) + macro(add_llvm_loadable_module module_name) + add_llvm_library(${module_name} MODULE ${ARGN}) + endmacro(add_llvm_loadable_module) +endif() + +# Clang 9 remove add_version_info_from_vcs. +# Recomended is use of get_source_info +if (NOT COMMAND add_version_info_from_vcs) + set(clad_compat__isClang9 TRUE) + macro(add_version_info_from_vcs VERS) + get_source_info(${CMAKE_CURRENT_SOURCE_DIR} VERS DUMMY_REP) + endmacro(add_version_info_from_vcs) +endif() + +# Clang 9 change find_first_existing_vc_file interface. +# find_first_existing_vc_file(var path) --> find_first_existing_vc_file(path var) +if (clad_compat__isClang9) + macro(clad_compat__find_first_existing_vc_file path out_var) + find_first_existing_vc_file(${path} out_var) + endmacro(clad_compat__find_first_existing_vc_file) +else() + macro(clad_compat__find_first_existing_vc_file path out_var) + find_first_existing_vc_file(out_var ${path}) + endmacro(clad_compat__find_first_existing_vc_file) +endif() diff --git a/README.md b/README.md index d678a7cb8..d0f6ce2fe 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST ``` ### Building from source LLVM, Clang and clad (development environment) ``` + sudo -H pip install lit LAST_KNOWN_GOOD_LLVM=$(wget https://raw.githubusercontent.com/vgvassilev/clad/master/LastKnownGoodLLVMRevision.txt -O - -q --no-check-certificate) LAST_KNOWN_GOOD_CLANG=$(wget https://raw.githubusercontent.com/vgvassilev/clad/master/LastKnownGoodClangRevision.txt -O - -q --no-check-certificate) git clone https://github.com/llvm-mirror/llvm.git src @@ -175,7 +176,7 @@ Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST cd ../ mkdir obj inst cd obj - cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DCMAKE_INSTALL_PREFIX=../inst ../src/ + cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DCMAKE_INSTALL_PREFIX=../inst -DLLVM_EXTERNAL_LIT="`which lit`" ../src/ make && make install ``` diff --git a/include/clad/Differentiator/Compatibility.h b/include/clad/Differentiator/Compatibility.h index 097ee61a1..ad3c1e495 100644 --- a/include/clad/Differentiator/Compatibility.h +++ b/include/clad/Differentiator/Compatibility.h @@ -257,6 +257,23 @@ static inline ConstexprSpecKind Function_GetConstexprKind(const FunctionDecl* F) #endif +// Clang 10 change add new param in getConstantArrayType. + +static inline QualType getConstantArrayType(const ASTContext &Ctx, + QualType EltTy, + const llvm::APInt &ArySize, + const Expr* SizeExpr, + ArrayType::ArraySizeModifier ASM, + unsigned IndexTypeQuals) +{ +#if CLANG_VERSION_MAJOR < 10 + return Ctx.getConstantArrayType(EltTy, ArySize, ASM, IndexTypeQuals); +#elif CLANG_VERSION_MAJOR >= 10 + return Ctx.getConstantArrayType(EltTy, ArySize, SizeExpr, ASM, IndexTypeQuals); +#endif +} + + } // namespace clad_compat #endif //CLAD_COMPATIBILITY diff --git a/include/clad/Differentiator/DerivativeBuilder.h b/include/clad/Differentiator/DerivativeBuilder.h index 678187aa9..298273b15 100644 --- a/include/clad/Differentiator/DerivativeBuilder.h +++ b/include/clad/Differentiator/DerivativeBuilder.h @@ -31,7 +31,7 @@ namespace clad { namespace utils { class StmtClone; } - class DiffRequest; + struct DiffRequest; namespace plugin { class CladPlugin; clang::FunctionDecl* ProcessDiffRequest(CladPlugin& P, DiffRequest& request); diff --git a/lib/Differentiator/CMakeLists.txt b/lib/Differentiator/CMakeLists.txt index 4706cd9de..ea0a643bc 100644 --- a/lib/Differentiator/CMakeLists.txt +++ b/lib/Differentiator/CMakeLists.txt @@ -1,77 +1,29 @@ -# Figure out if we can track VC revisions. -function(find_first_existing_file out_var) - foreach(file ${ARGN}) - if(EXISTS "${file}") - set(${out_var} "${file}" PARENT_SCOPE) - return() - endif() - endforeach() -endfunction() - -macro(find_first_existing_vc_file out_var path) - set(git_path "${path}/.git") - - # Normally '.git' is a directory that contains a 'logs/HEAD' file that - # is updated as modifications are made to the repository. In case the - # repository is a Git submodule, '.git' is a file that contains text that - # indicates where the repository's Git directory exists. - if (EXISTS "${git_path}" AND NOT IS_DIRECTORY "${git_path}") - FILE(READ "${git_path}" file_contents) - if("${file_contents}" MATCHES "^gitdir: ([^\n]+)") - # '.git' is indeed a link to the submodule's Git directory. - # Use the path to that Git directory. - set(git_path "${path}/${CMAKE_MATCH_1}") - endif() - endif() - - find_first_existing_file(${out_var} - "${git_path}/logs/HEAD" # Git or Git submodule - "${path}/.svn/wc.db" # SVN 1.7 - "${path}/.svn/entries" # SVN 1.6 - ) -endmacro() - set(CLANG_SOURCE_DIR ${CLAD_SOURCE_DIR}/../clang/) -find_first_existing_vc_file(clang_vc "${CLANG_SOURCE_DIR}") -find_first_existing_vc_file(clad_vc "${CLAD_SOURCE_DIR}") +clad_compat__find_first_existing_vc_file("${CLANG_SOURCE_DIR}" clang_vc) +clad_compat__find_first_existing_vc_file("${CLAD_SOURCE_DIR}" clad_vc) set(last_known_good_rev "${CLAD_SOURCE_DIR}/LastKnownGoodLLVMRevision.txt") # The VC revision include that we want to generate. -set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/SVNVersion.inc") - -set(get_svn_script "${LLVM_CMAKE_PATH}/GetSVN.cmake") - -if(DEFINED clang_vc AND DEFINED clad_vc) - # Create custom target to generate the VC revision include. - add_custom_command(OUTPUT "${version_inc}" - DEPENDS "${clang_vc}" "${clad_vc}" "${get_svn_script}" "${last_known_good_rev}" - COMMAND - ${CMAKE_COMMAND} "-DFIRST_SOURCE_DIR=${CLANG_SOURCE_DIR}" - "-DFIRST_NAME=CLANG" - "-DSECOND_SOURCE_DIR=${CLAD_SOURCE_DIR}" - "-DSECOND_NAME=CLAD" - "-DHEADER_FILE=${version_inc}" - -P "${get_svn_script}") - - # Mark the generated header as being generated. - set_source_files_properties("${version_inc}" - PROPERTIES GENERATED TRUE - HEADER_FILE_ONLY TRUE) - - # Tell Version.cpp that it needs to build with -DHAVE_SVN_VERSION_INC. - set_source_files_properties(Version.cpp - PROPERTIES COMPILE_DEFINITIONS "HAVE_CLAD_VERSION_INC") -else() - # Not producing a VC revision include. - set(version_inc) - - # Being able to force-set the SVN revision in cases where it isn't available - # is useful for performance tracking, and matches compatibility from autoconf. - if(SVN_REVISION) - set_source_files_properties(Version.cpp - PROPERTIES COMPILE_DEFINITIONS "CLAD_REVISION=\"${SVN_REVISION}\"") - endif() -endif() +set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/VCSVersion.inc") + +set(generate_vcs_version_script "${LLVM_CMAKE_PATH}/GenerateVersionFromVCS.cmake") + +# Create custom target to generate the VC revision include. +add_custom_command(OUTPUT "${version_inc}" + DEPENDS "${clang_vc}" "${clad_vc}" "${generate_vcs_version_script}" + COMMAND ${CMAKE_COMMAND} "-DNAMES=\"LLVM;CLANG\"" + "-DLLVM_SOURCE_DIR=${llvm_source_dir}" + "-DCLANG_SOURCE_DIR=${clang_source_dir}" + "-DHEADER_FILE=${version_inc}" + -P "${generate_vcs_version_script}") + +# Mark the generated header as being generated. +set_source_files_properties("${version_inc}" + PROPERTIES GENERATED TRUE + HEADER_FILE_ONLY TRUE) + +set_property(SOURCE Version.cpp APPEND PROPERTY + COMPILE_DEFINITIONS "HAVE_VCS_VERSION_INC") file(READ ${last_known_good_rev} CLAD_CLANG_COMPAT_REVISION) # Trim spaces diff --git a/lib/Differentiator/ConstantFolder.cpp b/lib/Differentiator/ConstantFolder.cpp index 7173838a7..ac17c6182 100644 --- a/lib/Differentiator/ConstantFolder.cpp +++ b/lib/Differentiator/ConstantFolder.cpp @@ -126,7 +126,7 @@ namespace clad { Expr* ConstantFolder::synthesizeLiteral(QualType QT, ASTContext& C, uint64_t val) { - SourceLocation noLoc; + //SourceLocation noLoc; Expr* Result = 0; if (QT->isIntegralType(C)) { llvm::APInt APVal(C.getIntWidth(QT), val, diff --git a/lib/Differentiator/DerivativeBuilder.cpp b/lib/Differentiator/DerivativeBuilder.cpp index af1ef46ac..d0e97947b 100644 --- a/lib/Differentiator/DerivativeBuilder.cpp +++ b/lib/Differentiator/DerivativeBuilder.cpp @@ -435,9 +435,10 @@ namespace clad { // Converts an independent argument from VarDecl to a StringLiteral Expr QualType CharTyConst = m_Context.CharTy.withConst(); QualType StrTy = - m_Context.getConstantArrayType(CharTyConst, + clad_compat::getConstantArrayType(m_Context, CharTyConst, llvm::APInt(32, independentArg->getNameAsString().size() + 1), + nullptr, ArrayType::Normal, /*IndexTypeQuals*/0); StringLiteral* independentArgString = @@ -2509,8 +2510,9 @@ namespace clad { // We also need to create an array to store the result of gradient call. auto size_type_bits = m_Context.getIntWidth(m_Context.getSizeType()); auto ArrayType = - m_Context.getConstantArrayType(CE->getType(), + clad_compat::getConstantArrayType(m_Context, CE->getType(), llvm::APInt(size_type_bits, NArgs), + nullptr, ArrayType::ArraySizeModifier::Normal, 0); // No IndexTypeQualifiers diff --git a/lib/Differentiator/DiffPlanner.cpp b/lib/Differentiator/DiffPlanner.cpp index 9cb289168..753ca3a8c 100644 --- a/lib/Differentiator/DiffPlanner.cpp +++ b/lib/Differentiator/DiffPlanner.cpp @@ -82,8 +82,9 @@ namespace clad { // the nul terminator character as well as the string length for pascal // strings. QualType StrTy = - C.getConstantArrayType(CharTyConst, + clad_compat::getConstantArrayType(C, CharTyConst, llvm::APInt(32, Out.str().size() + 1), + nullptr, ArrayType::Normal, /*IndexTypeQuals*/0); @@ -140,7 +141,7 @@ namespace clad { call->setCallee(CladGradientExprNew); } - DiffCollector::DiffCollector(DeclGroupRef DGR, DiffSchedule& plans, Sema& S) + DiffCollector::DiffCollector(DeclGroupRef DGR, DiffSchedule& plans, clang::Sema& S) : m_DiffPlans(plans), m_TopMostFD(nullptr), m_Sema(S) { if (DGR.isSingleDecl()) TraverseDecl(DGR.getSingleDecl()); diff --git a/lib/Differentiator/Version.cpp b/lib/Differentiator/Version.cpp index ccc5f8497..0913294cc 100644 --- a/lib/Differentiator/Version.cpp +++ b/lib/Differentiator/Version.cpp @@ -6,8 +6,8 @@ #include "clad/Differentiator/Version.h" -#ifdef HAVE_CLAD_VERSION_INC -# include "SVNVersion.inc" +#ifdef HAVE_VCS_VERSION_INC +#include "VCSVersion.inc" #endif namespace clad { diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3b57dbff0..cd128ad36 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -7,7 +7,7 @@ add_llvm_library(cladPlugin RequiredSymbols.cpp ) if (NOT CLAD_BUILD_STATIC_ONLY) - clad_compat_add_llvm_loadable_module(clad + add_llvm_loadable_module(clad ClangPlugin.cpp RequiredSymbols.cpp PLUGIN_TOOL diff --git a/tools/ClangPlugin.h b/tools/ClangPlugin.h index 26faa31ca..5139f9426 100644 --- a/tools/ClangPlugin.h +++ b/tools/ClangPlugin.h @@ -29,7 +29,7 @@ namespace clang { } namespace clad { - class DiffRequest; + struct DiffRequest; namespace plugin { struct DifferentiationOptions { DifferentiationOptions()