15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright (c) 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import("//build/config/sysroot.gni")
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Defines a config specifying the result of running pkg-config for the given
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# packages. Put the package names you want to query in the "packages" variable
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# inside the template invocation.
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# You can also add defines via the "defines" variable. This can be useful to
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# add this to the config to pass defines that the library expects to get by
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# users of its headers.
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Example:
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#   pkg_config("mything") {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     packages = [ "mything1", "mything2" ]
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#     defines = [ "ENABLE_AWESOME" ]
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#   }
20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch# You can also use "extra args" to filter out results (see pkg-config.py):
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#   extra_args = [ "-v, "foo" ]
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)# To ignore libs and ldflags (only cflags/defines will be set, which is useful
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)# when doing manual dynamic linking), set:
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#   ignore_libs = true
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccideclare_args() {
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  # A pkg-config wrapper to call instead of trying to find and call the right
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  # pkg-config directly. Wrappers like this are common in cross-compilation
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  # environments.
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  # Leaving it blank defaults to searching PATH for 'pkg-config' and relying on
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  # the sysroot mechanism to find the right .pc files.
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pkg_config = ""
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template("pkg_config") {
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  assert(defined(invoker.packages),
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        "Variable |packages| must be defined to be a list in pkg_config.")
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  config(target_name) {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (sysroot != "") {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      # Pass the sysroot if we're using one (it requires the CPU arch also).
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      args = ["-s", sysroot, "-a", cpu_arch] + invoker.packages
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    } else if (pkg_config != "") {
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      args = ["-p", pkg_config] + invoker.packages
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      args = invoker.packages
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
48a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (defined(invoker.extra_args)) {
50a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      args += invoker.extra_args
51a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    }
52a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    pkgresult = exec_script("//build/config/linux/pkg-config.py",
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            args, "value")
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    include_dirs = pkgresult[0]
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cflags = pkgresult[1]
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!defined(invoker.ignore_libs) || !invoker.ignore_libs) {
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      libs = pkgresult[2]
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      lib_dirs = pkgresult[3]
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ldflags = pkgresult[4]
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (defined(invoker.defines)) {
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      defines = invoker.defines
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    if (defined(invoker.visibility)) {
68c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      visibility = invoker.visibility
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    }
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
72