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