ProjectLLVM.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Configures LLVM dependency
  2. #
  3. # This function handles everything needed to setup LLVM project.
  4. # By default it downloads and builds LLVM from source.
  5. # In case LLVM_DIR variable is set it tries to use the pointed pre-built
  6. # LLVM package. LLVM_DIR should point LLVM's shared cmake files to be used
  7. # by find_package(... CONFIG) function.
  8. #
  9. # Creates a target representing all required LLVM libraries and include path.
  10. function(configure_llvm_project)
  11. if (LLVM_DIR)
  12. find_package(LLVM REQUIRED CONFIG)
  13. llvm_map_components_to_libnames(LIBS mcjit ipo x86codegen)
  14. # To create a fake imported library later on we need to know the
  15. # location of some library
  16. list(GET LIBS 0 MAIN_LIB)
  17. get_property(CONFIGS TARGET ${MAIN_LIB} PROPERTY IMPORTED_CONFIGURATIONS)
  18. list(GET CONFIGS 0 CONFIG) # Just get the first one. Usually there is only one.
  19. if (CONFIG)
  20. get_property(MAIN_LIB TARGET ${MAIN_LIB} PROPERTY IMPORTED_LOCATION_${CONFIG})
  21. else()
  22. set(CONFIG Unknown)
  23. get_property(MAIN_LIB TARGET ${MAIN_LIB} PROPERTY IMPORTED_LOCATION)
  24. endif()
  25. message(STATUS "LLVM ${LLVM_VERSION} (${CONFIG}; ${LLVM_ENABLE_ASSERTIONS}; ${LLVM_DIR})")
  26. if (NOT EXISTS ${MAIN_LIB})
  27. # Add some diagnostics to detect issues before building.
  28. message(FATAL_ERROR "LLVM library not found: ${MAIN_LIB}")
  29. endif()
  30. else()
  31. # List of required LLVM libs.
  32. # Generated with `llvm-config --libs mcjit ipo x86codegen`
  33. # Only used here locally to setup the "llvm" imported target
  34. set(LIBS
  35. LLVMMCJIT
  36. LLVMX86CodeGen LLVMGlobalISel LLVMX86Desc LLVMX86Info LLVMMCDisassembler
  37. LLVMX86AsmPrinter LLVMX86Utils LLVMSelectionDAG LLVMAsmPrinter LLVMDebugInfoCodeView
  38. LLVMDebugInfoMSF LLVMCodeGen LLVMipo LLVMInstrumentation LLVMVectorize LLVMScalarOpts
  39. LLVMLinker LLVMIRReader LLVMAsmParser LLVMInstCombine LLVMTransformUtils LLVMBitWriter
  40. LLVMExecutionEngine LLVMTarget LLVMAnalysis LLVMProfileData LLVMRuntimeDyld LLVMObject
  41. LLVMMCParser LLVMBitReader LLVMMC LLVMCore LLVMBinaryFormat LLVMSupport LLVMDemangle
  42. )
  43. # System libs that LLVM depend on.
  44. # See `llvm-config --system-libs`
  45. if (APPLE)
  46. set(SYSTEM_LIBS pthread)
  47. elseif (UNIX)
  48. set(SYSTEM_LIBS pthread dl)
  49. endif()
  50. if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
  51. set(BUILD_COMMAND $(MAKE))
  52. else()
  53. set(BUILD_COMMAND cmake --build <BINARY_DIR> --config Release)
  54. endif()
  55. include(ExternalProject)
  56. ExternalProject_Add(llvm
  57. PREFIX ${CMAKE_SOURCE_DIR}/deps
  58. URL http://llvm.org/releases/5.0.0/llvm-5.0.0.src.tar.xz
  59. URL_HASH SHA256=e35dcbae6084adcf4abb32514127c5eabd7d63b733852ccdb31e06f1373136da
  60. DOWNLOAD_NO_PROGRESS TRUE
  61. BINARY_DIR ${CMAKE_SOURCE_DIR}/deps # Build directly to install dir to avoid copy.
  62. CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
  63. -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
  64. -DLLVM_ENABLE_TERMINFO=OFF # Disable terminal color support
  65. -DLLVM_ENABLE_ZLIB=OFF # Disable compression support -- not needed at all
  66. -DLLVM_TARGETS_TO_BUILD=X86
  67. -DLLVM_INCLUDE_TOOLS=OFF
  68. -DLLVM_INCLUDE_EXAMPLES=OFF
  69. -DLLVM_INCLUDE_TESTS=OFF
  70. LOG_CONFIGURE TRUE
  71. BUILD_COMMAND ${BUILD_COMMAND}
  72. INSTALL_COMMAND cmake --build <BINARY_DIR> --config Release --target install
  73. LOG_INSTALL TRUE
  74. EXCLUDE_FROM_ALL TRUE
  75. )
  76. ExternalProject_Get_Property(llvm INSTALL_DIR)
  77. set(LLVM_LIBRARY_DIRS ${INSTALL_DIR}/lib)
  78. set(LLVM_INCLUDE_DIRS ${INSTALL_DIR}/include)
  79. file(MAKE_DIRECTORY ${LLVM_INCLUDE_DIRS}) # Must exists.
  80. foreach(LIB ${LIBS})
  81. list(APPEND LIBFILES "${LLVM_LIBRARY_DIRS}/${CMAKE_STATIC_LIBRARY_PREFIX}${LIB}${CMAKE_STATIC_LIBRARY_SUFFIX}")
  82. endforeach()
  83. # Pick one of the libraries to be the main one. It does not matter which one
  84. # but the imported target requires the IMPORTED_LOCATION property.
  85. list(GET LIBFILES 0 MAIN_LIB)
  86. list(REMOVE_AT LIBFILES 0)
  87. set(LIBS ${LIBFILES} ${SYSTEM_LIBS})
  88. endif()
  89. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  90. # Clang needs this to build LLVM. Weird that the GCC does not.
  91. set(DEFINES __STDC_LIMIT_MACROS __STDC_CONSTANT_MACROS)
  92. endif()
  93. # Create the target representing
  94. add_library(LLVM::JIT STATIC IMPORTED)
  95. set_property(TARGET LLVM::JIT PROPERTY IMPORTED_CONFIGURATIONS Release)
  96. set_property(TARGET LLVM::JIT PROPERTY IMPORTED_LOCATION_RELEASE ${MAIN_LIB})
  97. set_property(TARGET LLVM::JIT PROPERTY INTERFACE_COMPILE_DEFINITIONS ${DEFINES})
  98. set_property(TARGET LLVM::JIT PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${LLVM_INCLUDE_DIRS})
  99. set_property(TARGET LLVM::JIT PROPERTY INTERFACE_LINK_LIBRARIES ${LIBS})
  100. if (TARGET llvm)
  101. add_dependencies(LLVM::JIT llvm)
  102. endif()
  103. endfunction()