From ce786f30a43554a5f5ec0934589deb9e3b6244e8 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Wed, 5 May 2021 16:35:14 -0700 Subject: [PATCH] [gn build] Support compiler-rt/profile on Windows Reviewed By: thakis Differential Revision: https://reviews.llvm.org/D101961 --- utils/gn/build/toolchain/BUILD.gn | 226 +++++++++++--------- utils/gn/secondary/BUILD.gn | 4 +- utils/gn/secondary/compiler-rt/BUILD.gn | 6 +- utils/gn/secondary/compiler-rt/lib/BUILD.gn | 14 +- utils/gn/secondary/compiler-rt/target.gni | 3 + 5 files changed, 142 insertions(+), 111 deletions(-) diff --git a/utils/gn/build/toolchain/BUILD.gn b/utils/gn/build/toolchain/BUILD.gn index e1aaae2c35e..63e61db2d16 100644 --- a/utils/gn/build/toolchain/BUILD.gn +++ b/utils/gn/build/toolchain/BUILD.gn @@ -239,113 +239,137 @@ if (host_os == "mac") { } } -toolchain("win") { - cl = "cl" - link = "link" +template("win_toolchain") { + toolchain(target_name) { + # https://groups.google.com/a/chromium.org/d/msg/gn-dev/F_lv5T-tNDM + forward_variables_from(invoker.toolchain_args, "*") + not_needed("*") - if (clang_base_path != "") { - cl = rebase_path(clang_base_path, root_build_dir) + "/bin/clang-cl" - if (use_lld) { - link = rebase_path(clang_base_path, root_build_dir) + "/bin/lld-link" + forward_variables_from(invoker, "*") + + cl = "cl" + link = "link" + + if (clang_base_path != "") { + cl = rebase_path(clang_base_path, root_build_dir) + "/bin/clang-cl" + if (use_lld) { + link = rebase_path(clang_base_path, root_build_dir) + "/bin/lld-link" + } + } + + if (use_goma) { + cl = "$goma_dir/gomacc $cl" + } + + tool("cc") { + command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}" + depsformat = "msvc" + description = "CC {{output}}" + outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] + } + + tool("cxx") { + command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}" + depsformat = "msvc" + description = "CXX {{output}}" + outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] + } + + tool("alink") { + command = "$link /lib /nologo {{arflags}} /out:{{output}} {{inputs}}" + description = "LIB {{output}}" + outputs = [ "{{output_dir}}/{{target_output_name}}.lib" ] + default_output_dir = "{{root_out_dir}}/lib" + } + + tool("solink") { + outprefix = "{{output_dir}}/{{target_output_name}}" + dllfile = "$outprefix{{output_extension}}" + libfile = "$outprefix.lib" + pdbfile = "$outprefix.pdb" + command = "$link /nologo /dll {{ldflags}} /out:$dllfile /implib:$libfile /pdb:$pdbfile {{inputs}} {{libs}} " + description = "LINK $dllfile" + link_output = libfile + depend_output = libfile + runtime_outputs = [ dllfile ] + outputs = [ + dllfile, + libfile, + ] + lib_switch = "" + default_output_extension = ".dll" + restat = true + + # Put dlls next to the executables in bin/ on Windows, since Windows + # doesn't have a configurable rpath. This matches initialization of + # module_dir to bin/ in AddLLVM.cmake's set_output_directory(). + default_output_dir = "{{root_out_dir}}/bin" + } + + # Plugins for opt and clang and so on don't work in LLVM's Windows build + # since the code doesn't have export annotations, but there are a few + # standalone loadable modules used for unit-testing LLVM's dynamic library + # loading code. + tool("solink_module") { + outprefix = "{{output_dir}}/{{target_output_name}}" + dllfile = "$outprefix{{output_extension}}" + pdbfile = "$outprefix.pdb" + command = "$link /nologo /dll {{ldflags}} /out:$dllfile /pdb:$pdbfile {{inputs}} {{libs}} " + description = "LINK_MODULE $dllfile" + outputs = [ dllfile ] + lib_switch = "" + runtime_outputs = outputs + default_output_extension = ".dll" + + # No default_output_dir, all clients set output_dir. + } + + tool("link") { + outprefix = "{{output_dir}}/{{target_output_name}}" + outfile = "$outprefix{{output_extension}}" + pdbfile = "$outprefix.pdb" + command = "$link /nologo {{ldflags}} /out:$outfile /pdb:$pdbfile {{inputs}} {{libs}}" + description = "LINK $outfile" + outputs = [ outfile ] + lib_switch = "" + default_output_extension = ".exe" + + # Setting this allows targets to override the default executable output by + # setting output_dir. + default_output_dir = "{{root_out_dir}}/bin" + } + + tool("copy") { + # GN hands out slash-using paths, but cmd's copy needs backslashes. + # Use cmd's %foo:a=b% substitution feature to convert. + command = "cmd /c set source=\"{{source}}\" & set output=\"{{output}}\" & call copy /Y %source:/=\% %output:\=/% > nul" + description = "COPY {{source}} {{output}}" + } + + tool("stamp") { + command = "cmd /c type nul > {{output}}" + description = "STAMP {{output}}" } } +} - if (use_goma) { - cl = "$goma_dir/gomacc $cl" - } - - tool("cc") { - command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}" - depsformat = "msvc" - description = "CC {{output}}" - outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] - } - - tool("cxx") { - command = "$cl /nologo /showIncludes /Fo{{output}} /c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}" - depsformat = "msvc" - description = "CXX {{output}}" - outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.obj" ] - } - - tool("alink") { - command = "$link /lib /nologo {{arflags}} /out:{{output}} {{inputs}}" - description = "LIB {{output}}" - outputs = [ "{{output_dir}}/{{target_output_name}}.lib" ] - default_output_dir = "{{root_out_dir}}/lib" - } - - tool("solink") { - outprefix = "{{output_dir}}/{{target_output_name}}" - dllfile = "$outprefix{{output_extension}}" - libfile = "$outprefix.lib" - pdbfile = "$outprefix.pdb" - command = "$link /nologo /dll {{ldflags}} /out:$dllfile /implib:$libfile /pdb:$pdbfile {{inputs}} {{libs}} " - description = "LINK $dllfile" - link_output = libfile - depend_output = libfile - runtime_outputs = [ dllfile ] - outputs = [ - dllfile, - libfile, - ] - lib_switch = "" - default_output_extension = ".dll" - restat = true - - # Put dlls next to the executables in bin/ on Windows, since Windows - # doesn't have a configurable rpath. This matches initialization of - # module_dir to bin/ in AddLLVM.cmake's set_output_directory(). - default_output_dir = "{{root_out_dir}}/bin" - } - - # Plugins for opt and clang and so on don't work in LLVM's Windows build - # since the code doesn't have export annotations, but there are a few - # standalone loadable modules used for unit-testing LLVM's dynamic library - # loading code. - tool("solink_module") { - outprefix = "{{output_dir}}/{{target_output_name}}" - dllfile = "$outprefix{{output_extension}}" - pdbfile = "$outprefix.pdb" - command = "$link /nologo /dll {{ldflags}} /out:$dllfile /pdb:$pdbfile {{inputs}} {{libs}} " - description = "LINK_MODULE $dllfile" - outputs = [ dllfile ] - lib_switch = "" - runtime_outputs = outputs - default_output_extension = ".dll" - - # No default_output_dir, all clients set output_dir. - } - - tool("link") { - outprefix = "{{output_dir}}/{{target_output_name}}" - outfile = "$outprefix{{output_extension}}" - pdbfile = "$outprefix.pdb" - command = "$link /nologo {{ldflags}} /out:$outfile /pdb:$pdbfile {{inputs}} {{libs}}" - description = "LINK $outfile" - outputs = [ outfile ] - lib_switch = "" - default_output_extension = ".exe" - - # Setting this allows targets to override the default executable output by - # setting output_dir. - default_output_dir = "{{root_out_dir}}/bin" - } - - tool("copy") { - # GN hands out slash-using paths, but cmd's copy needs backslashes. - # Use cmd's %foo:a=b% substitution feature to convert. - command = "cmd /c set source=\"{{source}}\" & set output=\"{{output}}\" & call copy /Y %source:/=\% %output:\=/% > nul" - description = "COPY {{source}} {{output}}" - } - - tool("stamp") { - command = "cmd /c type nul > {{output}}" - description = "STAMP {{output}}" - } - +win_toolchain("win") { toolchain_args = { current_os = "win" current_cpu = host_cpu } } + +win_toolchain("stage2_win") { + toolchain_args = { + current_os = host_os + current_cpu = host_cpu + + clang_base_path = root_build_dir + use_goma = false + } + deps = [ + "//:clang($host_toolchain)", + "//:lld($host_toolchain)", + ] +} diff --git a/utils/gn/secondary/BUILD.gn b/utils/gn/secondary/BUILD.gn index 95afb4e7c11..50f1c4f26d6 100644 --- a/utils/gn/secondary/BUILD.gn +++ b/utils/gn/secondary/BUILD.gn @@ -8,14 +8,12 @@ group("default") { "//clang-tools-extra/test", "//clang/test", "//clang/tools/scan-build", + "//compiler-rt", "//compiler-rt/include", "//compiler-rt/lib/scudo", "//lld/test", "//llvm/test", ] - if (current_os == "linux" || current_os == "mac") { - deps += [ "//compiler-rt" ] - } if (current_os == "linux") { deps += [ "//libcxx", diff --git a/utils/gn/secondary/compiler-rt/BUILD.gn b/utils/gn/secondary/compiler-rt/BUILD.gn index 31c3c7266e4..6d7b54fdd01 100644 --- a/utils/gn/secondary/compiler-rt/BUILD.gn +++ b/utils/gn/secondary/compiler-rt/BUILD.gn @@ -5,7 +5,11 @@ import("//llvm/utils/gn/build/toolchain/compiler.gni") # In the GN build, compiler-rt is always built by just-built clang and lld. # FIXME: For macOS and iOS builds, depend on lib in all needed target arch # toolchains and then lipo them together for the final output. -supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ] +if (current_os == "win") { + supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_win" ] +} else { + supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ] +} if (android_ndk_path != "") { supported_toolchains += [ "//llvm/utils/gn/build/toolchain:stage2_android_aarch64", diff --git a/utils/gn/secondary/compiler-rt/lib/BUILD.gn b/utils/gn/secondary/compiler-rt/lib/BUILD.gn index 8eae2cdb397..b98b85e3b77 100644 --- a/utils/gn/secondary/compiler-rt/lib/BUILD.gn +++ b/utils/gn/secondary/compiler-rt/lib/BUILD.gn @@ -1,8 +1,10 @@ group("lib") { - deps = [ - "//compiler-rt/lib/asan", - "//compiler-rt/lib/builtins", - "//compiler-rt/lib/profile", - "//compiler-rt/lib/tsan", - ] + deps = [ "//compiler-rt/lib/profile" ] + if (current_os != "win") { + deps += [ + "//compiler-rt/lib/asan", + "//compiler-rt/lib/builtins", + "//compiler-rt/lib/tsan", + ] + } } diff --git a/utils/gn/secondary/compiler-rt/target.gni b/utils/gn/secondary/compiler-rt/target.gni index ead515fa501..ac273372cee 100644 --- a/utils/gn/secondary/compiler-rt/target.gni +++ b/utils/gn/secondary/compiler-rt/target.gni @@ -28,6 +28,9 @@ if (clang_enable_per_target_runtime_dir) { } } else if (current_os == "ios" || current_os == "mac") { crt_current_out_dir = "$clang_resource_dir/lib/darwin" +} else if (current_os == "win") { + crt_current_out_dir = "$clang_resource_dir/lib/windows" + crt_current_target_suffix = "-$crt_current_target_arch" } else { assert(false, "unimplemented current_os " + current_os) }