[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / cmake / modules / CrossCompile.cmake
blob6af47b51d4c6066bc64283cdd74a899a78a6c6d1
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         -DLLVM_INCLUDE_BENCHMARKS=OFF
88         -DLLVM_INCLUDE_TESTS=OFF
89         ${build_type_flags} ${linker_flag} ${external_clang_dir}
90         ${ARGN}
91     WORKING_DIRECTORY ${${project_name}_${target_name}_BUILD}
92     DEPENDS CREATE_${project_name}_${target_name}
93     COMMENT "Configuring ${target_name} ${project_name}...")
95   add_custom_target(CONFIGURE_${project_name}_${target_name}
96     DEPENDS ${${project_name}_${target_name}_BUILD}/CMakeCache.txt)
98 endfunction()
100 function(get_native_tool_path target output_path_var)
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   set(${output_path_var} ${output_path}${LLVM_HOST_EXECUTABLE_SUFFIX} PARENT_SCOPE)
107 endfunction()
109 # Sets up a native build for a tool, used e.g. for cross-compilation and
110 # LLVM_OPTIMIZED_TABLEGEN. Always builds in Release.
111 # - target: The target to build natively
112 # - output_path_var: A variable name which receives the path to the built target
113 # - DEPENDS: Any additional dependencies for the target
114 function(build_native_tool target output_path_var)
115   cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
117   get_native_tool_path(${target} output_path)
119   # Make chain of preceding actions
120   if(CMAKE_GENERATOR MATCHES "Visual Studio")
121     get_property(host_targets GLOBAL PROPERTY ${PROJECT_NAME}_HOST_TARGETS)
122     set_property(GLOBAL APPEND PROPERTY ${PROJECT_NAME}_HOST_TARGETS ${output_path})
123   endif()
125   llvm_ExternalProject_BuildCmd(build_cmd ${target} ${${PROJECT_NAME}_NATIVE_BUILD}
126                                 CONFIGURATION Release)
127   add_custom_command(OUTPUT "${output_path}"
128                      COMMAND ${build_cmd}
129                      DEPENDS CONFIGURE_${PROJECT_NAME}_NATIVE ${ARG_DEPENDS} ${host_targets}
130                      WORKING_DIRECTORY "${${PROJECT_NAME}_NATIVE_BUILD}"
131                      COMMENT "Building native ${target}..."
132                      USES_TERMINAL)
133   set(${output_path_var} "${output_path}" PARENT_SCOPE)
134 endfunction()