| # Copyright 2018 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| # Defines a config specifying the result of running pkg-config for the given | |
| # packages. Put the package names you want to query in the "pkg_deps" variable | |
| # inside the template invocation. | |
| template("pkg_config") { | |
| config(target_name) { | |
| forward_variables_from(invoker, "*") | |
| assert(defined(pkg_deps), "pkg_deps must be set") | |
| _pkg_config_result = exec_script("//common-mk/pkg-config_wrapper.py", | |
| [ pkg_config ] + pkg_deps, | |
| "json") | |
| if (_pkg_config_result.cflags != []) { | |
| if (!defined(cflags)) { | |
| cflags = [] | |
| } | |
| cflags += _pkg_config_result.cflags | |
| } | |
| if (_pkg_config_result.libs != []) { | |
| if (!defined(libs)) { | |
| libs = [] | |
| } | |
| libs += _pkg_config_result.libs | |
| } | |
| if (_pkg_config_result.lib_dirs != []) { | |
| if (!defined(lib_dirs)) { | |
| lib_dirs = [] | |
| } | |
| lib_dirs += _pkg_config_result.lib_dirs | |
| } | |
| if (_pkg_config_result.ldflags != []) { | |
| if (!defined(ldflags)) { | |
| ldflags = [] | |
| } | |
| ldflags += _pkg_config_result.ldflags | |
| } | |
| } | |
| } | |
| # Generates a .pc file with given parameters. | |
| # All parameters are optional. If missing, empty value is used unless there is | |
| # explicit documentation. | |
| # | |
| # Parameters: | |
| # deps: Dependency of the generated action. | |
| # name: The name of library set in .pc file. If missing | |
| # |target_name| is used. | |
| # output_name: The stem (file name excluding the .pc extension) | |
| # of the generated .pc file. If missing, |name| is used. | |
| # descritption: Description to be embedded. | |
| # version: Version to be embedded. | |
| # requires: Package name list to be embedded into "Requires:". | |
| # requires_private: Similar to requires, but into "Requires.private:". | |
| # libs: Flags passed to ld to be embedded into "Libs:" E.g. [ "-lpthread" ]. | |
| # libs_private: Similar to libs, butinto "Libs.private:". | |
| # cflags: Compile flag list to be embedded into "Cflags:". | |
| # install(optional): Install the generated .pc file to ${libdir}/pkgconfig | |
| # if set to true. | |
| template("generate_pkg_config") { | |
| if (defined(invoker.output_name)) { | |
| _output_name = invoker.output_name | |
| } else if (defined(invoker.name)) { | |
| _output_name = invoker.name | |
| } else { | |
| _output_name = target_name | |
| } | |
| _pc = "${invoker.target_out_dir}/${_output_name}.pc" | |
| _install = defined(invoker.install) && invoker.install | |
| if (_install) { | |
| _install_config_name = "_${target_name}-install_config" | |
| install_config(_install_config_name) { | |
| sources = [ _pc ] | |
| install_path = "${libdir}/pkgconfig" | |
| } | |
| } | |
| action(target_name) { | |
| forward_variables_from(invoker, | |
| [ | |
| "deps", | |
| "testonly", | |
| "name", | |
| "description", | |
| "version", | |
| "requires", | |
| "requires_private", | |
| "libs", | |
| "libs_private", | |
| "cflags", | |
| "defines", | |
| ]) | |
| if (!defined(name)) { | |
| name = target_name | |
| } | |
| outputs = [ _pc ] | |
| script = "//common-mk/generate-pc.py" | |
| args = [ "--output" ] + rebase_path(outputs) + [ "--name=" + name ] | |
| if (defined(description)) { | |
| args += [ "--description=" + description ] | |
| } | |
| if (defined(version)) { | |
| args += [ "--version=" + version ] | |
| } | |
| if (defined(requires)) { | |
| foreach(pkg, requires) { | |
| args += [ "--requires=" + pkg ] | |
| } | |
| } | |
| if (defined(requires_private)) { | |
| foreach(pkg, requires_private) { | |
| args += [ "--requires-private=" + pkg ] | |
| } | |
| } | |
| if (defined(libs)) { | |
| foreach(lib, libs) { | |
| args += [ "--libs=" + lib ] | |
| } | |
| } | |
| if (defined(libs_private)) { | |
| foreach(lib, libs_private) { | |
| args += [ "--libs-private=" + lib ] | |
| } | |
| } | |
| if (defined(cflags)) { | |
| foreach(cflag, cflags) { | |
| args += [ "--cflags=" + cflag ] | |
| } | |
| } | |
| if (defined(defines)) { | |
| foreach(define, defines) { | |
| args += [ "--cflags=-D" + define ] | |
| } | |
| } | |
| if (_install) { | |
| deps = [ ":${_install_config_name}" ] | |
| } | |
| } | |
| } |