[docs] Fix build-docs.sh
[llvm-project.git] / llvm / cmake / modules / CrossCompile.cmake
blob52b893dd0214b6a6eb3c75628a6185797f86e160
1 include(AddLLVM)
2 include(LLVMExternalProjectUtils)
5 function(llvm_create_cross_target project_name target_name toolchain buildtype)
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)
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   elseif (NOT CMAKE_CROSSCOMPILING)
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}")
27   # project specific version of the flags up above
28   set(CROSS_TOOLCHAIN_FLAGS_${project_name}_${target_name} ""
29     CACHE STRING "Toolchain configuration for ${project_name}_${target_name}")
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()
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}...")
46   add_custom_target(CREATE_${project_name}_${target_name}
47     DEPENDS ${${project_name}_${target_name}_BUILD})
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}")
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   string(REPLACE ";" "$<SEMICOLON>" llvm_enable_runtimes_arg
61          "${LLVM_ENABLE_RUNTIMES}")
63   set(external_project_source_dirs)
64   foreach(project ${LLVM_EXTERNAL_PROJECTS})
65     canonicalize_tool_name(${project} name)
66     list(APPEND external_project_source_dirs
67          "-DLLVM_EXTERNAL_${name}_SOURCE_DIR=${LLVM_EXTERNAL_${name}_SOURCE_DIR}")
68   endforeach()
70   add_custom_command(OUTPUT ${${project_name}_${target_name}_BUILD}/CMakeCache.txt
71     COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
72         -DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}"
73         -DCMAKE_C_COMPILER_LAUNCHER="${CMAKE_C_COMPILER_LAUNCHER}"
74         -DCMAKE_CXX_COMPILER_LAUNCHER="${CMAKE_CXX_COMPILER_LAUNCHER}"
75         ${CROSS_TOOLCHAIN_FLAGS_${target_name}} ${CMAKE_CURRENT_SOURCE_DIR}
76         ${CROSS_TOOLCHAIN_FLAGS_${project_name}_${target_name}}
77         -DLLVM_TARGET_IS_CROSSCOMPILE_HOST=TRUE
78         -DLLVM_TARGETS_TO_BUILD="${targets_to_build_arg}"
79         -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="${experimental_targets_to_build_arg}"
80         -DLLVM_DEFAULT_TARGET_TRIPLE="${LLVM_TARGET_TRIPLE}"
81         -DLLVM_TARGET_ARCH="${LLVM_TARGET_ARCH}"
82         -DLLVM_ENABLE_PROJECTS="${llvm_enable_projects_arg}"
83         -DLLVM_EXTERNAL_PROJECTS="${llvm_external_projects_arg}"
84         -DLLVM_ENABLE_RUNTIMES="${llvm_enable_runtimes_arg}"
85         ${external_project_source_dirs}
86         -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN="${LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN}"
87         ${build_type_flags} ${linker_flag} ${external_clang_dir}
88         ${ARGN}
89     WORKING_DIRECTORY ${${project_name}_${target_name}_BUILD}
90     DEPENDS CREATE_${project_name}_${target_name}
91     COMMENT "Configuring ${target_name} ${project_name}...")
93   add_custom_target(CONFIGURE_${project_name}_${target_name}
94     DEPENDS ${${project_name}_${target_name}_BUILD}/CMakeCache.txt)
96 endfunction()
98 # Sets up a native build for a tool, used e.g. for cross-compilation and
99 # LLVM_OPTIMIZED_TABLEGEN. Always builds in Release.
100 # - target: The target to build natively
101 # - output_path_var: A variable name which receives the path to the built target
102 # - DEPENDS: Any additional dependencies for the target
103 function(build_native_tool target output_path_var)
104   cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
106   if(CMAKE_CONFIGURATION_TYPES)
107     set(output_path "${${PROJECT_NAME}_NATIVE_BUILD}/Release/bin/${target}")
108   else()
109     set(output_path "${${PROJECT_NAME}_NATIVE_BUILD}/bin/${target}")
110   endif()
111   set(output_path ${output_path}${LLVM_HOST_EXECUTABLE_SUFFIX})
113   llvm_ExternalProject_BuildCmd(build_cmd ${target} ${${PROJECT_NAME}_NATIVE_BUILD}
114                                 CONFIGURATION Release)
115   add_custom_command(OUTPUT "${output_path}"
116                      COMMAND ${build_cmd}
117                      DEPENDS CONFIGURE_${PROJECT_NAME}_NATIVE ${ARG_DEPENDS}
118                      WORKING_DIRECTORY "${${PROJECT_NAME}_NATIVE_BUILD}"
119                      COMMENT "Building native ${target}..."
120                      USES_TERMINAL)
121   set(${output_path_var} "${output_path}" PARENT_SCOPE)
122 endfunction()