【问题标题】:How to include a shared library in chromium's gn file?如何在铬的 gn 文件中包含共享库?
【发布时间】:2018-08-12 15:55:05
【问题描述】:

(Q1)我有一个测试。所以有一些我需要使用的功能。我调查了一段时间但没有答案。任何人都可以对如何在铬项目的 gn 文件中包含共享库有一些建议吗?非常感谢。

下面是我的gn文件内容:

import("//third_party/WebKit/Source/core/core.gni")

blink_core_sources("frame") {
 sources = [
    "csp/CSPSource.h",
    "csp/ContentSecurityPolicy.cpp",
    "csp/ContentSecurityPolicy.h",
    "csp/MediaListDirective.cpp",
    "csp/MediaListDirective.h",
    "csp/SourceListDirective.cpp",
    "csp/SourceListDirective.h",

    // my created file
    "HelloWorld.h",
    "HelloWorld.cpp",   // Will use the function of provided in add.so
    "add.h"
  ]

  deps = [ ":add.so" ]

}

(Q2)另外一个问题是:如果我有add.so的源代码,我应该如何在gn中编写来使用共享库的源代码?谢谢。

【问题讨论】:

    标签: shared-libraries static-libraries chromium ninja gn


    【解决方案1】:

    (Q1)有没有人可以建议如何在 chromium 项目的 gn 文件中包含共享库?

    通常您可以使用lib_dirs 指定库目录,使用libs 指定库。你的BUILD.gn 文件可以是这样的:

    import("//third_party/WebKit/Source/core/core.gni")
    
    blink_core_sources("frame") {
     sources = [
        "csp/CSPSource.h",
        "csp/ContentSecurityPolicy.cpp",
        "csp/ContentSecurityPolicy.h",
        "csp/MediaListDirective.cpp",
        "csp/MediaListDirective.h",
        "csp/SourceListDirective.cpp",
        "csp/SourceListDirective.h",
    
        // my created file
        "HelloWorld.h",
        "HelloWorld.cpp",   // Will use the function of provided in add.so
        "add.h"
      ]
    
      lib_dirs = [ "//path/to/add.so" ]
      libs = [ "add" ]
    
    }
    

    (Q2)如果我有add.so的源代码,我应该如何在gn中编写使用共享库的源代码?

    如果你想从add.so的源代码中获取共享库,你可以写一个BUILD.gn这样的文件:

    shared_library("libadd.so") {
      include_dirs = []
      sources = [
        "/path/to/sources",
      ]
    }
    

    您可以使用gn help shared_library了解更多详情。

    然后你就可以像 Q(1) 一样使用共享库了。

    最后,我建议您使用gn help 来查看有关 gn 构建系统的更多详细信息。

    【讨论】:

      【解决方案2】:

      更新 2021 年 10 月 25 日导入库的模板

      # imported_library.gni
      declare_args() {
          copy_third_library = is_win
      }
      
      template("imported_library") {
          ## Part1 public config
          ##--------------------
          config("${invoker.target_name}_imported") {
              ldflags = []
              if (defined(invoker.ldflags)) {
                  ldflags = invoker.ldflags
              }
              if (defined(invoker.input_exec)) {
                  inputs = [invoker.input_exec]
              }
              if (defined(invoker.include_dirs)) {
                  include_dirs = invoker.include_dirs
                  # print("INCLUDE: $include_dirs")
              }
              if (defined(invoker.lib_dirs)) {
                  lib_dirs = invoker.lib_dirs
                  foreach(p, invoker.lib_dirs) {
                      if (is_linux || is_mac) {
                          if (build_type == "debug") {
                              rpath_new = rebase_path(".", root_out_dir)
                              ldflags += ["-Wl,-rpath=\$ORIGIN/" + rpath_new + "/" + p]
                              # print(ldflags)
                          } else {
                              print("Note: do not set RUNPATH for $target_name when build_type=$build_type.")
                          }
                      }
                  }
              }
              if (defined(invoker.libs)) {
                  libs = invoker.libs
              }
              if (defined(invoker.defines)) {
                  defines = invoker.defines
              }
              if (defined(invoker.cflags)) {
                  cflags = invoker.cflags
              }
              if (defined(invoker.cflags_c)) {
                  cflags_c = invoker.cflags_c
              }
              if (defined(invoker.cflags_cc)) {
                  cflags_cc = invoker.cflags_cc
              }
              
              visibility = [ ":${invoker.target_name}" ]
          } # end config({$target_name}_imported)
      
      
          ## Part2 copy action
          ##------------------
          require_copy_action = defined(invoker.runtimes) && copy_third_library
      
          if (defined(invoker.runtime_dest)) { print("${invoker.runtime_dest}") }
      
          if (require_copy_action) {
              copy("$target_name" + "_copy") {
                  sources = invoker.runtimes
                  if (defined(invoker.runtime_dest)){
                      folder = invoker.runtime_dest
                      outputs = [ "$root_out_dir/$folder/{{source_file_part}}" ]
                  }else {
                      outputs = [ "$root_out_dir/{{source_file_part}}" ]
                  }
                  visibility = [ ":$target_name" ]
              }
          }
      
      
          ## Part3 
          ##------------------
          group(target_name) {
              public_configs = [":$target_name" + "_imported"]
              # if (copy_third_library && defined(invoker.lib_dirs)) {
              #     deps = [":$target_name" + "_copy"]
              # }
              if (defined(invoker.public_deps)) {
                  public_deps = invoker.public_deps
              }
              if (defined(invoker.runtimes)) {
                  data = invoker.runtimes
              }
              if (defined(invoker.metadata)) {
                  metadata = invoker.metadata
              }
              if (require_copy_action) {
                  deps = [":$target_name" + "_copy"]
              }
          }
      
      } ## end template
      

      在windows上使用

      imported_library("tbb") {
          include_dirs = ["tbb-2020.3/tbb/include"]
          lib_dirs = ["tbb-2020.3/tbb/lib/intel64/vc14"]
          libs = ["tbb.lib"]
          runtimes = [
              "tbb-2020.3/tbb/bin/intel64/vc14/tbb.dll",
              "tbb-2020.3/tbb/bin/intel64/vc14/tbb.pdb"
          ]
      }
      

      由于 linux 中的 rpath,我们这里不需要 runtimes[] 参数。


      原始答案:

      我找到了一个解决方案,灵感来自https://github.com/matlo607/conan-gn-generator

      config("myadd_import") {
        include_dirs = ["./mypath/include"]
        lib_dirs = [ "./mypath/lib" ]
        libs = ["libmyadd.so", "pthread"] # or use full path directly
        visibility = [ ":myadd" ]
      }
      
      group("myadd") {
        public_configs = [":myadd_import"]
      }
      
      

      那么就可以在deps扇区中使用,比如:

      executable("test") {
        sources = [
          "test.cpp"
        ]
        deps = ["//third:myadd"]
      }
      

      顺便说一句:要从源代码导入第三个库,我们可以使用它

      config("sqlite3_export") {
          include_dirs = ["sqlite-amalgamation-3330000"]
          visibility = [":sqlite3"]
      }
      static_library("sqlite3") {
          _base_dir = "sqlite-amalgamation-3330000"
          sources = ["$_base_dir/sqlite3.c", "$_base_dir/sqlite3.h", "$_base_dir/sqlite3ext.h"]
          include_dirs = ["$_base_dir"]
          # public = ["$_base_dir/sqlite3.h", "$_base_dir/sqlite3ext.h"]
          public_configs = [":sqlite3_export"]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多