4

(Q1)I have a test.so with some functions that I need to use.I've surveyed for a while but no answers. Could anyone have some advise of how to include a shared library in gn file of chromium project? Many thanks.

Below is the content of my gn file:

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)Another question is: If I have source code of add.so, how should I write in gn to use the source code of the shared library? Thanks.

hsnu_976
  • 71
  • 1
  • 5

2 Answers2

3

(Q1)Could anyone have some advise of how to include a shared library in gn file of chromium project?

Typicall you can specify library directory with lib_dirs and libraries with libs. Your BUILD.gn file can be like this:

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)If I have source code of add.so, how should I write in gn to use the source code of the shared library?

If you want to get a shared library from source code of add.so, you can write a BUILD.gn file like this:

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

You can use gn help shared_library for more details.

And then you can use the shared library just like Q(1).

Finally, I recommand you to use gn help to see more details about gn build system.

Wechar Yu
  • 237
  • 3
  • 12
1

Upd Oct 25, 2021 template of imported library

# 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

Usage on 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"
    ]
}

Since rpath in linux, we don't need runtimes[] argument here.


The Original answer:

I found a solution, inspired by 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"]
}

Then it can be used in deps sector like:

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

BTW: to import 3rd-library from source code, we can use that

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"]
}
vrqq
  • 448
  • 3
  • 8