From b2e77cd095a677bcd7a3ba4fb3da5c811e994cc9 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Mon, 23 Nov 2020 11:45:06 -0800 Subject: [PATCH] gn build: Build libclang.so and libLTO.so on ELF platforms. This requires changing the ELF build to enable -fPIC, consistent with other platforms. Differential Revision: https://reviews.llvm.org/D108223 --- llvm/utils/gn/build/BUILD.gn | 3 ++ .../secondary/clang/tools/libclang/BUILD.gn | 48 ++++++++----------- .../gn/secondary/llvm/tools/lto/BUILD.gn | 14 +----- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn index 8df8be1ca070..b0f02cf6a937 100644 --- a/llvm/utils/gn/build/BUILD.gn +++ b/llvm/utils/gn/build/BUILD.gn @@ -377,6 +377,9 @@ config("llvm_code") { "//llvm/include", "$root_gen_dir/llvm/include", ] + if (current_os != "win") { + cflags = [ "-fPIC" ] + } } config("lld_code") { diff --git a/llvm/utils/gn/secondary/clang/tools/libclang/BUILD.gn b/llvm/utils/gn/secondary/clang/tools/libclang/BUILD.gn index 556c8783ba30..6f2c19c613a3 100644 --- a/llvm/utils/gn/secondary/clang/tools/libclang/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/tools/libclang/BUILD.gn @@ -5,33 +5,24 @@ import("//llvm/version.gni") # This build file is just enough to get check-clang to pass, it's missing # several things from the CMake build: # - a build target copying the Python bindings -# - the GN linux build always builds without -fPIC (as if LLVM_ENABLE_PIC=OFF -# in the CMake build), so libclang is always a static library on linux # - the GN build doesn't have LIBCLANG_BUILD_STATIC -libclang_target_type = "shared_library" -if (host_os != "win" && host_os != "mac") { - # ELF targets need -fPIC to build shared libs but they aren't on by default. - # For now, make libclang a static lib there. - libclang_target_type = "static_library" -} else { - action("linker_script_to_exports") { - script = "linker-script-to-export-list.py" - inputs = [ "libclang.map" ] - outputs = [ "$target_gen_dir/libclang.exports" ] - args = [ - rebase_path(inputs[0], root_build_dir), - rebase_path(outputs[0], root_build_dir), - ] - } - - symbol_exports("exports") { - deps = [ ":linker_script_to_exports" ] - exports_file = "$target_gen_dir/libclang.exports" - } +action("linker_script_to_exports") { + script = "linker-script-to-export-list.py" + inputs = [ "libclang.map" ] + outputs = [ "$target_gen_dir/libclang.exports" ] + args = [ + rebase_path(inputs[0], root_build_dir), + rebase_path(outputs[0], root_build_dir), + ] } -target(libclang_target_type, "libclang") { +symbol_exports("exports") { + deps = [ ":linker_script_to_exports" ] + exports_file = "$target_gen_dir/libclang.exports" +} + +shared_library("libclang") { configs += [ "//llvm/utils/gn/build:clang_code" ] deps = [ "//clang/include/clang/Config", @@ -48,14 +39,17 @@ target(libclang_target_type, "libclang") { "//llvm/lib/Support", "//llvm/lib/Target:TargetsToBuild", ] + if (current_os == "win" || current_os == "mac") { + deps += [ ":exports" ] + } else { + inputs = [ "libclang.map" ] + ldflags = + [ "-Wl,--version-script," + rebase_path(inputs[0], root_build_dir) ] + } if (clang_enable_arcmt) { deps += [ "//clang/lib/ARCMigrate" ] } - if (libclang_target_type == "shared_library") { - deps += [ ":exports" ] - } - defines = [] if (host_os == "win") { diff --git a/llvm/utils/gn/secondary/llvm/tools/lto/BUILD.gn b/llvm/utils/gn/secondary/llvm/tools/lto/BUILD.gn index 418bdeb4fc06..590e8951686e 100644 --- a/llvm/utils/gn/secondary/llvm/tools/lto/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/tools/lto/BUILD.gn @@ -1,20 +1,14 @@ import("//llvm/utils/gn/build/symbol_exports.gni") import("//llvm/version.gni") -lto_target_type = "shared_library" -if (host_os != "mac" && host_os != "win") { - # ELF targets need -fPIC to build shared libs but they aren't on by default. - # For now, make libclang a static lib there. - lto_target_type = "static_library" -} - symbol_exports("exports") { exports_file = "lto.exports" } -target(lto_target_type, "lto") { +shared_library("lto") { output_name = "LTO" deps = [ + ":exports", "//llvm/lib/Bitcode/Reader", "//llvm/lib/IR", "//llvm/lib/LTO", @@ -29,10 +23,6 @@ target(lto_target_type, "lto") { "lto.cpp", ] - if (lto_target_type == "shared_library") { - deps += [ ":exports" ] - } - if (host_os == "mac") { ldflags = [ "-Wl,-compatibility_version,1",