1include(AddLLVM) 2include(LLVMExternalProjectUtils) 3 4 5function(llvm_create_cross_target project_name target_name toolchain buildtype) 6 7 if(NOT DEFINED ${project_name}_${target_name}_BUILD) 8 set(${project_name}_${target_name}_BUILD 9 "${CMAKE_CURRENT_BINARY_DIR}/${target_name}") 10 set(${project_name}_${target_name}_BUILD 11 ${${project_name}_${target_name}_BUILD} PARENT_SCOPE) 12 message(STATUS "Setting native build dir to " ${${project_name}_${target_name}_BUILD}) 13 endif(NOT DEFINED ${project_name}_${target_name}_BUILD) 14 15 if (EXISTS ${LLVM_MAIN_SRC_DIR}/cmake/platforms/${toolchain}.cmake) 16 set(CROSS_TOOLCHAIN_FLAGS_INIT 17 -DCMAKE_TOOLCHAIN_FILE=\"${LLVM_MAIN_SRC_DIR}/cmake/platforms/${toolchain}.cmake\") 18 else() 19 set(CROSS_TOOLCHAIN_FLAGS_INIT 20 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 21 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} 22 ) 23 endif() 24 set(CROSS_TOOLCHAIN_FLAGS_${target_name} ${CROSS_TOOLCHAIN_FLAGS_INIT} 25 CACHE STRING "Toolchain configuration for ${target_name}") 26 27 # project specific version of the flags up above 28 set(CROSS_TOOLCHAIN_FLAGS_${project_name}_${target_name} "" 29 CACHE STRING "Toolchain configuration for ${Pproject_name}_${target_name}") 30 31 if (buildtype) 32 set(build_type_flags "-DCMAKE_BUILD_TYPE=${buildtype}") 33 endif() 34 if (LLVM_USE_LINKER AND NOT CMAKE_CROSSCOMPILING) 35 set(linker_flag "-DLLVM_USE_LINKER=${LLVM_USE_LINKER}") 36 endif() 37 if (LLVM_EXTERNAL_CLANG_SOURCE_DIR) 38 # Propagate LLVM_EXTERNAL_CLANG_SOURCE_DIR so that clang-tblgen can be built 39 set(external_clang_dir "-DLLVM_EXTERNAL_CLANG_SOURCE_DIR=${LLVM_EXTERNAL_CLANG_SOURCE_DIR}") 40 endif() 41 42 add_custom_command(OUTPUT ${${project_name}_${target_name}_BUILD} 43 COMMAND ${CMAKE_COMMAND} -E make_directory ${${project_name}_${target_name}_BUILD} 44 COMMENT "Creating ${${project_name}_${target_name}_BUILD}...") 45 46 add_custom_target(CREATE_${project_name}_${target_name} 47 DEPENDS ${${project_name}_${target_name}_BUILD}) 48 49 # Escape semicolons in the targets list so that cmake doesn't expand 50 # them to spaces. 51 string(REPLACE ";" "$<SEMICOLON>" targets_to_build_arg 52 "${LLVM_TARGETS_TO_BUILD}") 53 string(REPLACE ";" "$<SEMICOLON>" experimental_targets_to_build_arg 54 "${LLVM_EXPERIMENTAL_TARGETS_TO_BUILD}") 55 56 string(REPLACE ";" "$<SEMICOLON>" llvm_enable_projects_arg 57 "${LLVM_ENABLE_PROJECTS}") 58 string(REPLACE ";" "$<SEMICOLON>" llvm_external_projects_arg 59 "${LLVM_EXTERNAL_PROJECTS}") 60 61 set(external_project_source_dirs) 62 foreach(project ${LLVM_EXTERNAL_PROJECTS}) 63 canonicalize_tool_name(${project} name) 64 list(APPEND external_project_source_dirs 65 "-DLLVM_EXTERNAL_${name}_SOURCE_DIR=${LLVM_EXTERNAL_${name}_SOURCE_DIR}") 66 endforeach() 67 68 add_custom_command(OUTPUT ${${project_name}_${target_name}_BUILD}/CMakeCache.txt 69 COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" 70 -DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}" 71 ${CROSS_TOOLCHAIN_FLAGS_${target_name}} ${CMAKE_CURRENT_SOURCE_DIR} 72 ${CROSS_TOOLCHAIN_FLAGS_${project_name}_${target_name}} 73 -DLLVM_TARGET_IS_CROSSCOMPILE_HOST=TRUE 74 -DLLVM_TARGETS_TO_BUILD="${targets_to_build_arg}" 75 -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="${experimental_targets_to_build_arg}" 76 -DLLVM_DEFAULT_TARGET_TRIPLE="${TARGET_TRIPLE}" 77 -DLLVM_TARGET_ARCH="${LLVM_TARGET_ARCH}" 78 -DLLVM_ENABLE_PROJECTS="${llvm_enable_projects_arg}" 79 -DLLVM_EXTERNAL_PROJECTS="${llvm_external_projects_arg}" 80 ${external_project_source_dirs} 81 -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN="${LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN}" 82 ${build_type_flags} ${linker_flag} ${external_clang_dir} 83 ${ARGN} 84 WORKING_DIRECTORY ${${project_name}_${target_name}_BUILD} 85 DEPENDS CREATE_${project_name}_${target_name} 86 COMMENT "Configuring ${target_name} ${project_name}...") 87 88 add_custom_target(CONFIGURE_${project_name}_${target_name} 89 DEPENDS ${${project_name}_${target_name}_BUILD}/CMakeCache.txt) 90 91endfunction() 92 93# Sets up a native build for a tool, used e.g. for cross-compilation and 94# LLVM_OPTIMIZED_TABLEGEN. Always builds in Release. 95# - target: The target to build natively 96# - output_path_var: A variable name which receives the path to the built target 97# - DEPENDS: Any additional dependencies for the target 98function(build_native_tool target output_path_var) 99 cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN}) 100 101 if(CMAKE_CONFIGURATION_TYPES) 102 set(output_path "${${PROJECT_NAME}_NATIVE_BUILD}/Release/bin/${target}") 103 else() 104 set(output_path "${${PROJECT_NAME}_NATIVE_BUILD}/bin/${target}") 105 endif() 106 107 llvm_ExternalProject_BuildCmd(build_cmd ${target} ${${PROJECT_NAME}_NATIVE_BUILD} 108 CONFIGURATION Release) 109 add_custom_command(OUTPUT "${output_path}" 110 COMMAND ${build_cmd} 111 DEPENDS CONFIGURE_${PROJECT_NAME}_NATIVE ${ARG_DEPENDS} 112 WORKING_DIRECTORY "${${PROJECT_NAME}_NATIVE_BUILD}" 113 COMMENT "Building native ${target}..." 114 USES_TERMINAL) 115 set(${output_path_var} "${output_path}" PARENT_SCOPE) 116endfunction() 117