| 1 |
import("//llvm/utils/gn/build/toolchain/compiler.gni") |
| 2 |
|
| 3 |
unix_copy_command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})" |
| 4 |
|
| 5 |
template("unix_toolchain") { |
| 6 |
toolchain(target_name) { |
| 7 |
# https://groups.google.com/a/chromium.org/g/gn-dev/c/F_lv5T-tNDM |
| 8 |
forward_variables_from(invoker.toolchain_args, "*") |
| 9 |
not_needed("*") |
| 10 |
|
| 11 |
forward_variables_from(invoker, "*") |
| 12 |
|
| 13 |
cc = "cc" |
| 14 |
cxx = "c++" |
| 15 |
|
| 16 |
if (clang_base_path != "") { |
| 17 |
cc = rebase_path(clang_base_path, root_build_dir) + "/bin/clang" |
| 18 |
cxx = rebase_path(clang_base_path, root_build_dir) + "/bin/clang++" |
| 19 |
} |
| 20 |
|
| 21 |
ld = cxx # Don't use compiler wrapper for linking. |
| 22 |
if (compiler_wrapper != "") { |
| 23 |
cc = "$compiler_wrapper $cc" |
| 24 |
cxx = "$compiler_wrapper $cxx" |
| 25 |
} |
| 26 |
|
| 27 |
tool("cc") { |
| 28 |
depfile = "{{output}}.d" |
| 29 |
command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}" |
| 30 |
depsformat = "gcc" |
| 31 |
description = "CC {{output}}" |
| 32 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ] |
| 33 |
} |
| 34 |
|
| 35 |
tool("cxx") { |
| 36 |
depfile = "{{output}}.d" |
| 37 |
command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}" |
| 38 |
depsformat = "gcc" |
| 39 |
description = "CXX {{output}}" |
| 40 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ] |
| 41 |
} |
| 42 |
|
| 43 |
tool("objcxx") { |
| 44 |
depfile = "{{output}}.d" |
| 45 |
command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_objcc}}" |
| 46 |
depsformat = "gcc" |
| 47 |
description = "OBJCXX {{output}}" |
| 48 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ] |
| 49 |
} |
| 50 |
|
| 51 |
tool("asm") { |
| 52 |
depfile = "{{output}}.d" |
| 53 |
command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{asmflags}}" |
| 54 |
depsformat = "gcc" |
| 55 |
description = "ASM {{output}}" |
| 56 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ] |
| 57 |
} |
| 58 |
|
| 59 |
tool("alink") { |
| 60 |
if (current_os == "ios" || current_os == "mac") { |
| 61 |
command = "libtool -D -static -no_warning_for_no_symbols {{arflags}} -o {{output}} {{inputs}}" |
| 62 |
} else { |
| 63 |
# Remove the output file first so that ar doesn't try to modify the |
| 64 |
# existing file. |
| 65 |
command = |
| 66 |
"rm -f {{output}} && $ar rcsD {{arflags}} {{output}} {{inputs}}" |
| 67 |
} |
| 68 |
description = "AR {{output}}" |
| 69 |
outputs = [ "{{output_dir}}/{{target_output_name}}.a" ] |
| 70 |
output_prefix = "lib" |
| 71 |
default_output_dir = "{{root_out_dir}}/lib" |
| 72 |
} |
| 73 |
|
| 74 |
# Make these apply to all tools below. |
| 75 |
lib_switch = "-l" |
| 76 |
lib_dir_switch = "-L" |
| 77 |
|
| 78 |
tool("solink") { |
| 79 |
outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" |
| 80 |
if (current_os == "ios" || current_os == "mac") { |
| 81 |
command = "$ld -shared {{ldflags}} -o $outfile {{inputs}} {{libs}} {{frameworks}}" |
| 82 |
default_output_extension = ".dylib" |
| 83 |
} else { |
| 84 |
command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{inputs}} {{libs}}" |
| 85 |
default_output_extension = ".so" |
| 86 |
} |
| 87 |
description = "SOLINK $outfile" |
| 88 |
outputs = [ outfile ] |
| 89 |
output_prefix = "lib" |
| 90 |
default_output_dir = "{{root_out_dir}}/lib" |
| 91 |
} |
| 92 |
|
| 93 |
tool("solink_module") { |
| 94 |
outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" |
| 95 |
if (current_os == "ios" || current_os == "mac") { |
| 96 |
command = "$ld -shared {{ldflags}} -Wl,-flat_namespace -Wl,-undefined,suppress -o $outfile {{inputs}} {{libs}} {{frameworks}}" |
| 97 |
default_output_extension = ".dylib" |
| 98 |
} else { |
| 99 |
command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{inputs}} {{libs}}" |
| 100 |
default_output_extension = ".so" |
| 101 |
} |
| 102 |
description = "SOLINK $outfile" |
| 103 |
outputs = [ outfile ] |
| 104 |
default_output_dir = "{{root_out_dir}}/lib" |
| 105 |
} |
| 106 |
|
| 107 |
tool("link") { |
| 108 |
outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" |
| 109 |
if (current_os == "ios" || current_os == "mac") { |
| 110 |
command = |
| 111 |
"$ld {{ldflags}} -o $outfile {{inputs}} {{libs}} {{frameworks}}" |
| 112 |
} else { |
| 113 |
command = "$ld {{ldflags}} -o $outfile -Wl,--start-group {{inputs}} -Wl,--end-group {{libs}}" |
| 114 |
} |
| 115 |
description = "LINK $outfile" |
| 116 |
outputs = [ outfile ] |
| 117 |
|
| 118 |
# Setting this allows targets to override the default executable output by |
| 119 |
# setting output_dir. |
| 120 |
default_output_dir = "{{root_out_dir}}/bin" |
| 121 |
} |
| 122 |
|
| 123 |
tool("copy") { |
| 124 |
command = unix_copy_command |
| 125 |
description = "COPY {{source}} {{output}}" |
| 126 |
} |
| 127 |
|
| 128 |
if (current_os == "ios" || current_os == "mac") { |
| 129 |
tool("copy_bundle_data") { |
| 130 |
# https://github.com/nico/hack/blob/master/notes/copydir.md |
| 131 |
_copydir = "cd {{source}} && " + |
| 132 |
"find . | cpio -pdl \"\$OLDPWD\"/{{output}} 2>/dev/null" |
| 133 |
command = "rm -rf {{output}} && if [[ -d {{source}} ]]; then " + |
| 134 |
_copydir + "; else " + unix_copy_command + "; fi" |
| 135 |
description = "COPY_BUNDLE_DATA {{source}} {{output}}" |
| 136 |
} |
| 137 |
tool("compile_xcassets") { |
| 138 |
command = "false" |
| 139 |
description = "The LLVM build doesn't use any xcasset files" |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
tool("stamp") { |
| 144 |
command = "touch {{output}}" |
| 145 |
description = "STAMP {{output}}" |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
unix_toolchain("unix") { |
| 151 |
if (current_os != "ios" && current_os != "mac") { |
| 152 |
if (clang_base_path != "") { |
| 153 |
ar = rebase_path(clang_base_path, root_build_dir) + "/bin/llvm-ar" |
| 154 |
} else { |
| 155 |
ar = "ar" |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
toolchain_args = { |
| 160 |
current_os = host_os |
| 161 |
current_cpu = host_cpu |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
# This template defines a toolchain that uses just-built clang and lld |
| 166 |
# as compiler and linker. |
| 167 |
template("stage2_unix_toolchain") { |
| 168 |
unix_toolchain(target_name) { |
| 169 |
toolchain_args = { |
| 170 |
forward_variables_from(invoker.toolchain_args, "*") |
| 171 |
|
| 172 |
clang_base_path = root_build_dir |
| 173 |
llvm_enable_zstd = false |
| 174 |
} |
| 175 |
|
| 176 |
deps = [ |
| 177 |
"//:clang($host_toolchain)", |
| 178 |
"//:lld($host_toolchain)", |
| 179 |
] |
| 180 |
if (toolchain_args.current_os != "ios" && |
| 181 |
toolchain_args.current_os != "mac") { |
| 182 |
ar = "bin/llvm-ar" |
| 183 |
deps += [ "//:llvm-ar($host_toolchain)" ] |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
stage2_unix_toolchain("stage2_unix") { |
| 189 |
toolchain_args = { |
| 190 |
current_os = host_os |
| 191 |
current_cpu = host_cpu |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
stage2_unix_toolchain("stage2_unix_x86") { |
| 196 |
toolchain_args = { |
| 197 |
current_os = host_os |
| 198 |
current_cpu = "x86" |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (android_ndk_path != "") { |
| 203 |
# Android compiler-rt libraries don't really work with per-target runtime |
| 204 |
# directories yet so force it off. |
| 205 |
# https://discourse.llvm.org/t/handling-version-numbers-in-per-target-runtime-directories/62717. |
| 206 |
stage2_unix_toolchain("stage2_android_aarch64") { |
| 207 |
toolchain_args = { |
| 208 |
current_os = "android" |
| 209 |
current_cpu = "arm64" |
| 210 |
clang_enable_per_target_runtime_dir = false |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
stage2_unix_toolchain("stage2_android_arm") { |
| 215 |
toolchain_args = { |
| 216 |
current_os = "android" |
| 217 |
current_cpu = "arm" |
| 218 |
clang_enable_per_target_runtime_dir = false |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
stage2_unix_toolchain("stage2_android_x64") { |
| 223 |
toolchain_args = { |
| 224 |
current_os = "android" |
| 225 |
current_cpu = "x64" |
| 226 |
clang_enable_per_target_runtime_dir = false |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
stage2_unix_toolchain("stage2_android_x86") { |
| 231 |
toolchain_args = { |
| 232 |
current_os = "android" |
| 233 |
current_cpu = "x86" |
| 234 |
clang_enable_per_target_runtime_dir = false |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (host_os == "mac") { |
| 240 |
stage2_unix_toolchain("stage2_ios_aarch64") { |
| 241 |
toolchain_args = { |
| 242 |
current_os = "ios" |
| 243 |
current_cpu = "arm64" |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
stage2_unix_toolchain("stage2_iossim_x64") { |
| 248 |
toolchain_args = { |
| 249 |
current_os = "ios" |
| 250 |
current_cpu = "x64" |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
stage2_unix_toolchain("stage2_baremetal_aarch64") { |
| 256 |
toolchain_args = { |
| 257 |
current_os = "baremetal" |
| 258 |
current_cpu = "arm64" |
| 259 |
|
| 260 |
# FIXME: These should be set in all toolchains building sanitizers, |
| 261 |
# see discussion at https://reviews.llvm.org/D127906#3587329 |
| 262 |
use_asan = false |
| 263 |
use_tsan = false |
| 264 |
use_ubsan = false |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
template("win_toolchain") { |
| 269 |
toolchain(target_name) { |
| 270 |
# https://groups.google.com/a/chromium.org/d/msg/gn-dev/F_lv5T-tNDM |
| 271 |
forward_variables_from(invoker.toolchain_args, "*") |
| 272 |
not_needed("*") |
| 273 |
|
| 274 |
forward_variables_from(invoker, "*") |
| 275 |
|
| 276 |
cl = "cl" |
| 277 |
link = "link" |
| 278 |
|
| 279 |
if (clang_base_path != "") { |
| 280 |
cl = rebase_path(clang_base_path, root_build_dir) + "/bin/clang-cl" |
| 281 |
if (use_lld) { |
| 282 |
link = rebase_path(clang_base_path, root_build_dir) + "/bin/lld-link" |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (compiler_wrapper != "") { |
| 287 |
cl = "$compiler_wrapper $cl" |
| 288 |
} |
| 289 |
|
| 290 |
tool("cc") { |
| 291 |
command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}" |
| 292 |
depsformat = "msvc" |
| 293 |
description = "CC {{output}}" |
| 294 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] |
| 295 |
} |
| 296 |
|
| 297 |
tool("cxx") { |
| 298 |
command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}" |
| 299 |
depsformat = "msvc" |
| 300 |
description = "CXX {{output}}" |
| 301 |
outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] |
| 302 |
} |
| 303 |
|
| 304 |
tool("alink") { |
| 305 |
command = "$link /lib /nologo {{arflags}} /out:{{output}} {{inputs}}" |
| 306 |
description = "LIB {{output}}" |
| 307 |
outputs = [ "{{output_dir}}/{{target_output_name}}.lib" ] |
| 308 |
default_output_dir = "{{root_out_dir}}/lib" |
| 309 |
} |
| 310 |
|
| 311 |
# Make these apply to all tools below. |
| 312 |
lib_switch = "" |
| 313 |
lib_dir_switch = "/LIBPATH:" |
| 314 |
|
| 315 |
tool("solink") { |
| 316 |
outprefix = "{{output_dir}}/{{target_output_name}}" |
| 317 |
dllfile = "$outprefix{{output_extension}}" |
| 318 |
libfile = "$outprefix.lib" |
| 319 |
pdbfile = "$outprefix.pdb" |
| 320 |
command = "$link /nologo /dll {{ldflags}} /out:$dllfile /implib:$libfile /pdb:$pdbfile {{inputs}} {{libs}} " |
| 321 |
description = "LINK $dllfile" |
| 322 |
link_output = libfile |
| 323 |
depend_output = libfile |
| 324 |
runtime_outputs = [ dllfile ] |
| 325 |
outputs = [ |
| 326 |
dllfile, |
| 327 |
libfile, |
| 328 |
] |
| 329 |
default_output_extension = ".dll" |
| 330 |
restat = true |
| 331 |
|
| 332 |
# Put dlls next to the executables in bin/ on Windows, since Windows |
| 333 |
# doesn't have a configurable rpath. This matches initialization of |
| 334 |
# module_dir to bin/ in AddLLVM.cmake's set_output_directory(). |
| 335 |
default_output_dir = "{{root_out_dir}}/bin" |
| 336 |
} |
| 337 |
|
| 338 |
# Plugins for opt and clang and so on don't work in LLVM's Windows build |
| 339 |
# since the code doesn't have export annotations, but there are a few |
| 340 |
# standalone loadable modules used for unit-testing LLVM's dynamic library |
| 341 |
# loading code. |
| 342 |
tool("solink_module") { |
| 343 |
outprefix = "{{output_dir}}/{{target_output_name}}" |
| 344 |
dllfile = "$outprefix{{output_extension}}" |
| 345 |
pdbfile = "$outprefix.pdb" |
| 346 |
command = "$link /nologo /dll {{ldflags}} /out:$dllfile /pdb:$pdbfile {{inputs}} {{libs}} " |
| 347 |
description = "LINK_MODULE $dllfile" |
| 348 |
outputs = [ dllfile ] |
| 349 |
runtime_outputs = outputs |
| 350 |
default_output_extension = ".dll" |
| 351 |
|
| 352 |
# No default_output_dir, all clients set output_dir. |
| 353 |
} |
| 354 |
|
| 355 |
tool("link") { |
| 356 |
outprefix = "{{output_dir}}/{{target_output_name}}" |
| 357 |
outfile = "$outprefix{{output_extension}}" |
| 358 |
pdbfile = "$outprefix.pdb" |
| 359 |
command = "$link /nologo {{ldflags}} /out:$outfile /pdb:$pdbfile {{inputs}} {{libs}}" |
| 360 |
description = "LINK $outfile" |
| 361 |
outputs = [ outfile ] |
| 362 |
default_output_extension = ".exe" |
| 363 |
|
| 364 |
# Setting this allows targets to override the default executable output by |
| 365 |
# setting output_dir. |
| 366 |
default_output_dir = "{{root_out_dir}}/bin" |
| 367 |
} |
| 368 |
|
| 369 |
tool("copy") { |
| 370 |
if (host_os == "win") { |
| 371 |
# GN hands out slash-using paths, but cmd's copy needs backslashes. |
| 372 |
# Use cmd's %foo:a=b% substitution feature to convert. |
| 373 |
command = "cmd /c set source=\"{{source}}\" & set output=\"{{output}}\" & call copy /Y %source:/=\% %output:\=/% > nul" |
| 374 |
} else { |
| 375 |
command = unix_copy_command |
| 376 |
} |
| 377 |
|
| 378 |
description = "COPY {{source}} {{output}}" |
| 379 |
} |
| 380 |
|
| 381 |
tool("stamp") { |
| 382 |
if (host_os == "win") { |
| 383 |
command = "cmd /c type nul > {{output}}" |
| 384 |
} else { |
| 385 |
command = "touch {{output}}" |
| 386 |
} |
| 387 |
description = "STAMP {{output}}" |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
win_toolchain("win") { |
| 393 |
toolchain_args = { |
| 394 |
current_os = "win" |
| 395 |
current_cpu = host_cpu |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
win_toolchain("stage2_win_x64") { |
| 400 |
toolchain_args = { |
| 401 |
current_os = "win" |
| 402 |
current_cpu = "x64" |
| 403 |
|
| 404 |
if (host_os != "win") { |
| 405 |
sysroot = win_sysroot |
| 406 |
} |
| 407 |
clang_base_path = root_build_dir |
| 408 |
} |
| 409 |
deps = [ |
| 410 |
"//:clang($host_toolchain)", |
| 411 |
"//:lld($host_toolchain)", |
| 412 |
] |
| 413 |
} |
| 414 |
|
| 415 |
win_toolchain("stage2_win_x86") { |
| 416 |
toolchain_args = { |
| 417 |
current_os = "win" |
| 418 |
current_cpu = "x86" |
| 419 |
|
| 420 |
if (host_os != "win") { |
| 421 |
sysroot = win_sysroot |
| 422 |
} |
| 423 |
clang_base_path = root_build_dir |
| 424 |
} |
| 425 |
deps = [ |
| 426 |
"//:clang($host_toolchain)", |
| 427 |
"//:lld($host_toolchain)", |
| 428 |
] |
| 429 |
} |