Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 pmbaty 1
include(ExternalProject)
2
 
3
# llvm_ExternalProject_BuildCmd(out_var target)
4
#   Utility function for constructing command lines for external project targets
5
function(llvm_ExternalProject_BuildCmd out_var target bin_dir)
6
  cmake_parse_arguments(ARG "" "CONFIGURATION" "" ${ARGN})
7
  if(NOT ARG_CONFIGURATION)
8
    set(ARG_CONFIGURATION "$<CONFIG>")
9
  endif()
10
  if (CMAKE_GENERATOR MATCHES "Make")
11
    # Use special command for Makefiles to support parallelism.
12
    set(${out_var} "$(MAKE)" "-C" "${bin_dir}" "${target}" PARENT_SCOPE)
13
  else()
14
    set(tool_args "${LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS}")
15
    if(NOT tool_args STREQUAL "")
16
      string(CONFIGURE "${tool_args}" tool_args @ONLY)
17
      string(PREPEND tool_args "-- ")
18
      separate_arguments(tool_args UNIX_COMMAND "${tool_args}")
19
    endif()
20
    set(${out_var} ${CMAKE_COMMAND} --build ${bin_dir} --target ${target}
21
                                    --config ${ARG_CONFIGURATION} ${tool_args} PARENT_SCOPE)
22
  endif()
23
endfunction()
24
 
25
# is_msvc_triple(out_var triple)
26
#   Checks whether the passed triple refers to an MSVC environment
27
function(is_msvc_triple out_var triple)
28
  if (triple MATCHES ".*-windows-msvc.*")
29
    set(${out_var} TRUE PARENT_SCOPE)
30
  else()
31
    set(${out_var} FALSE PARENT_SCOPE)
32
  endif()
33
endfunction()
34
 
35
 
36
# llvm_ExternalProject_Add(name source_dir ...
37
#   USE_TOOLCHAIN
38
#     Use just-built tools (see TOOLCHAIN_TOOLS)
39
#   EXCLUDE_FROM_ALL
40
#     Exclude this project from the all target
41
#   NO_INSTALL
42
#     Don't generate install targets for this project
43
#   ALWAYS_CLEAN
44
#     Always clean the sub-project before building
45
#   CMAKE_ARGS arguments...
46
#     Optional cmake arguments to pass when configuring the project
47
#   TOOLCHAIN_TOOLS targets...
48
#     Targets for toolchain tools (defaults to clang;lld)
49
#   DEPENDS targets...
50
#     Targets that this project depends on
51
#   EXTRA_TARGETS targets...
52
#     Extra targets in the subproject to generate targets for
53
#   PASSTHROUGH_PREFIXES prefix...
54
#     Extra variable prefixes (name is always included) to pass down
55
#   STRIP_TOOL path
56
#     Use provided strip tool instead of the default one.
57
#   TARGET_TRIPLE triple
58
#     Optional target triple to pass to the compiler
59
#   )
60
function(llvm_ExternalProject_Add name source_dir)
61
  cmake_parse_arguments(ARG
62
    "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
63
    "SOURCE_DIR"
64
    "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL;TARGET_TRIPLE"
65
    ${ARGN})
66
  canonicalize_tool_name(${name} nameCanon)
67
 
68
  foreach(arg ${ARG_CMAKE_ARGS})
69
    if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
70
      string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
71
    endif()
72
  endforeach()
73
 
74
  # If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
75
  # reflect CMake's own default.
76
  if (NOT _cmake_system_name)
77
    set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
78
  endif()
79
 
80
  if(NOT ARG_TARGET_TRIPLE)
81
    set(target_triple ${LLVM_DEFAULT_TARGET_TRIPLE})
82
  else()
83
    set(target_triple ${ARG_TARGET_TRIPLE})
84
  endif()
85
 
86
  is_msvc_triple(is_msvc_target ${target_triple})
87
 
88
  if(NOT ARG_TOOLCHAIN_TOOLS)
89
    set(ARG_TOOLCHAIN_TOOLS clang)
90
    # AIX 64-bit XCOFF and big AR format is not yet supported in some of these tools.
91
    if(NOT _cmake_system_name STREQUAL AIX)
92
      list(APPEND ARG_TOOLCHAIN_TOOLS lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
93
      if(_cmake_system_name STREQUAL Darwin)
94
        list(APPEND ARG_TOOLCHAIN_TOOLS llvm-libtool-darwin llvm-lipo)
95
      elseif(is_msvc_target)
96
        list(APPEND ARG_TOOLCHAIN_TOOLS llvm-lib)
97
      else()
98
        # TODO: These tools don't fully support Mach-O format yet.
99
        list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip llvm-readelf)
100
      endif()
101
    endif()
102
  endif()
103
  foreach(tool ${ARG_TOOLCHAIN_TOOLS})
104
    if(TARGET ${tool})
105
      list(APPEND TOOLCHAIN_TOOLS ${tool})
106
 
107
      # $<TARGET_FILE:tgt> only works on add_executable or add_library targets
108
      # The below logic mirrors cmake's own implementation
109
      get_target_property(target_type "${tool}" TYPE)
110
      if(NOT target_type STREQUAL "OBJECT_LIBRARY" AND
111
         NOT target_type STREQUAL "UTILITY" AND
112
         NOT target_type STREQUAL "GLOBAL_TARGET" AND
113
         NOT target_type STREQUAL "INTERFACE_LIBRARY")
114
        list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
115
      endif()
116
 
117
    endif()
118
  endforeach()
119
 
120
  if(NOT ARG_RUNTIME_LIBRARIES)
121
    set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
122
  endif()
123
  foreach(lib ${ARG_RUNTIME_LIBRARIES})
124
    if(TARGET ${lib})
125
      list(APPEND RUNTIME_LIBRARIES ${lib})
126
    endif()
127
  endforeach()
128
 
129
  if(ARG_ALWAYS_CLEAN)
130
    set(always_clean clean)
131
  endif()
132
 
133
  if(clang IN_LIST TOOLCHAIN_TOOLS)
134
    set(CLANG_IN_TOOLCHAIN On)
135
  endif()
136
 
137
  if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
138
    list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
139
  endif()
140
 
141
  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
142
  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
143
 
144
  add_custom_target(${name}-clear
145
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
146
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
147
    COMMENT "Clobbering ${name} build and stamp directories"
148
    USES_TERMINAL
149
    )
150
 
151
  # Find all variables that start with a prefix and propagate them through
152
  get_cmake_property(variableNames VARIABLES)
153
 
154
  list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
155
  foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
156
    foreach(variableName ${variableNames})
157
      if(variableName MATCHES "^${prefix}")
158
        string(REPLACE ";" "|" value "${${variableName}}")
159
        list(APPEND PASSTHROUGH_VARIABLES
160
          -D${variableName}=${value})
161
      endif()
162
    endforeach()
163
  endforeach()
164
 
165
  if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
166
    if(CLANG_IN_TOOLCHAIN)
167
      if(is_msvc_target)
168
        set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
169
                          -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
170
                          -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
171
      else()
172
        set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
173
                          -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
174
                          -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
175
      endif()
176
    endif()
177
    if(lld IN_LIST TOOLCHAIN_TOOLS)
178
      if(is_msvc_target)
179
        list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
180
      elseif(NOT _cmake_system_name STREQUAL Darwin)
181
        list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
182
      endif()
183
    endif()
184
    if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
185
      if(is_msvc_target)
186
        list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib${CMAKE_EXECUTABLE_SUFFIX})
187
      else()
188
        list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
189
      endif()
190
    endif()
191
    if(llvm-libtool-darwin IN_LIST TOOLCHAIN_TOOLS)
192
      list(APPEND compiler_args -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin${CMAKE_EXECUTABLE_SUFFIX})
193
    endif()
194
    if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
195
      list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
196
    endif()
197
    if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
198
      list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
199
    endif()
200
    if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
201
      list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
202
    endif()
203
    if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
204
      list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
205
    endif()
206
    if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
207
      list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
208
    endif()
209
    if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
210
      list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
211
    endif()
212
    if(llvm-readelf IN_LIST TOOLCHAIN_TOOLS)
213
      list(APPEND compiler_args -DCMAKE_READELF=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-readelf${CMAKE_EXECUTABLE_SUFFIX})
214
    endif()
215
    list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
216
  endif()
217
 
218
  if(ARG_STRIP_TOOL)
219
    list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
220
  endif()
221
 
222
  add_custom_command(
223
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
224
    DEPENDS ${ARG_DEPENDS}
225
    COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
226
    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
227
    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
228
    COMMENT "Clobbering bootstrap build and stamp directories"
229
    )
230
 
231
  add_custom_target(${name}-clobber
232
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
233
 
234
  if(ARG_EXCLUDE_FROM_ALL)
235
    set(exclude EXCLUDE_FROM_ALL 1)
236
  endif()
237
 
238
  if(CMAKE_SYSROOT)
239
    set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
240
  endif()
241
 
242
  if(CMAKE_CROSSCOMPILING)
243
    set(compiler_args -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER}
244
                      -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
245
                      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
246
                      -DCMAKE_LINKER=${CMAKE_LINKER}
247
                      -DCMAKE_AR=${CMAKE_AR}
248
                      -DCMAKE_RANLIB=${CMAKE_RANLIB}
249
                      -DCMAKE_NM=${CMAKE_NM}
250
                      -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
251
                      -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
252
                      -DCMAKE_STRIP=${CMAKE_STRIP}
253
                      -DCMAKE_READELF=${CMAKE_READELF})
254
    set(llvm_config_path ${LLVM_CONFIG_PATH})
255
 
256
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
257
      string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR
258
             ${PACKAGE_VERSION})
259
      set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION_MAJOR}")
260
      set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
261
      foreach(type ${flag_types})
262
        set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
263
      endforeach()
264
      string(REPLACE ";" "|" flag_string "${flag_types}")
265
      foreach(arg ${ARG_CMAKE_ARGS})
266
        if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
267
          foreach(type ${flag_types})
268
            if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
269
              string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
270
              set(${type}_flag "${${type}_flag} ${flag_value}")
271
            endif()
272
          endforeach()
273
        else()
274
          list(APPEND cmake_args ${arg})
275
        endif()
276
      endforeach()
277
      foreach(type ${flag_types})
278
        list(APPEND cmake_args ${${type}_flag})
279
      endforeach()
280
    else()
281
      set(cmake_args ${ARG_CMAKE_ARGS})
282
    endif()
283
  else()
284
    set(llvm_config_path "$<TARGET_FILE:llvm-config>")
285
    set(cmake_args ${ARG_CMAKE_ARGS})
286
  endif()
287
 
288
  if(ARG_TARGET_TRIPLE)
289
    list(APPEND compiler_args -DCMAKE_C_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
290
    list(APPEND compiler_args -DCMAKE_CXX_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
291
    list(APPEND compiler_args -DCMAKE_ASM_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
292
  endif()
293
 
294
  ExternalProject_Add(${name}
295
    DEPENDS ${ARG_DEPENDS} llvm-config
296
    ${name}-clobber
297
    PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
298
    SOURCE_DIR ${source_dir}
299
    STAMP_DIR ${STAMP_DIR}
300
    BINARY_DIR ${BINARY_DIR}
301
    ${exclude}
302
    CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
303
               ${compiler_args}
304
               -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
305
               ${sysroot_arg}
306
               -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
307
               -DLLVM_CONFIG_PATH=${llvm_config_path}
308
               -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
309
               -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
310
               -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
311
               -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
312
               -DLLVM_USE_RELATIVE_PATHS_IN_FILES=${LLVM_USE_RELATIVE_PATHS_IN_FILES}
313
               -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
314
               -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
315
               -DPACKAGE_VERSION=${PACKAGE_VERSION}
316
               -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
317
               -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
318
               -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
319
               -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
320
               -DCMAKE_EXPORT_COMPILE_COMMANDS=1
321
               ${cmake_args}
322
               ${PASSTHROUGH_VARIABLES}
323
    INSTALL_COMMAND ""
324
    STEP_TARGETS configure build
325
    BUILD_ALWAYS 1
326
    USES_TERMINAL_CONFIGURE 1
327
    USES_TERMINAL_BUILD 1
328
    USES_TERMINAL_INSTALL 1
329
    LIST_SEPARATOR |
330
    )
331
 
332
  if(ARG_USE_TOOLCHAIN)
333
    set(force_deps DEPENDS ${TOOLCHAIN_BINS})
334
  endif()
335
 
336
  llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
337
  ExternalProject_Add_Step(${name} clean
338
    COMMAND ${run_clean}
339
    COMMENT "Cleaning ${name}..."
340
    DEPENDEES configure
341
    ${force_deps}
342
    WORKING_DIRECTORY ${BINARY_DIR}
343
    EXCLUDE_FROM_MAIN 1
344
    USES_TERMINAL 1
345
    )
346
  ExternalProject_Add_StepTargets(${name} clean)
347
 
348
  if(ARG_USE_TOOLCHAIN)
349
    add_dependencies(${name}-clean ${name}-clobber)
350
    set_target_properties(${name}-clean PROPERTIES
351
      SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
352
  endif()
353
 
354
  if(NOT ARG_NO_INSTALL)
355
    install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -DCMAKE_INSTALL_DO_STRIP=\${CMAKE_INSTALL_DO_STRIP} -P ${BINARY_DIR}/cmake_install.cmake\)"
356
      COMPONENT ${name})
357
 
358
    add_llvm_install_targets(install-${name}
359
                             DEPENDS ${name}
360
                             COMPONENT ${name})
361
  endif()
362
 
363
  # Add top-level targets
364
  foreach(target ${ARG_EXTRA_TARGETS})
365
    if(DEFINED ${target})
366
      set(external_target "${${target}}")
367
    else()
368
      set(external_target "${target}")
369
    endif()
370
    llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
371
    add_custom_target(${target}
372
      COMMAND ${build_runtime_cmd}
373
      DEPENDS ${name}-configure
374
      WORKING_DIRECTORY ${BINARY_DIR}
375
      VERBATIM
376
      USES_TERMINAL)
377
  endforeach()
378
endfunction()