Skip to content

Commit 8f26a57

Browse files
Linux: Don't fail if glib is missing if we don't need it
This adds support for an `optional` property to the `pkg_config` template.
1 parent 8c8889b commit 8f26a57

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

build/chip/linux/BUILD.gn

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2020 Project CHIP Authors
1+
# Copyright (c) 2020-2024 Project CHIP Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -21,4 +21,5 @@ pkg_config("glib") {
2121
"glib-2.0",
2222
"gio-unix-2.0",
2323
]
24+
optional = true # Only certain conditionally-compiled modules depend on glib
2425
}

build/config/linux/pkg-config.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
3-
# Copyright (c) 2020 Project CHIP Authors
3+
# Copyright (c) 2020-2024 Project CHIP Authors
44
#
55
# Redistribution and use in source and binary forms, with or without
66
# modification, are permitted provided that the following conditions are
@@ -127,6 +127,7 @@ def RewritePath(path, strip_prefix, sysroot):
127127
def main():
128128
parser = OptionParser()
129129
parser.add_option('-d', '--debug', action='store_true')
130+
parser.add_option('-o', '--optional', action='store_true')
130131
parser.add_option('-p', action='store', dest='pkg_config', type='string',
131132
default='pkg-config')
132133
parser.add_option('-v', action='append', dest='strip_out', type='string')
@@ -209,6 +210,10 @@ def main():
209210
try:
210211
flag_string = subprocess.check_output(cmd).decode('utf-8')
211212
except Exception:
213+
if options.optional:
214+
sys.stderr.write('Ignoring failure to run pkg-config for optional library.\n')
215+
print(json.dumps([False])) # Output a GN array indicating missing optional packages
216+
return 0
212217
sys.stderr.write('Could not run pkg-config.\n')
213218
return 1
214219

@@ -248,10 +253,10 @@ def main():
248253
else:
249254
cflags.append(flag)
250255

251-
# Output a GN array, the first one is the cflags, the second are the libs. The
256+
# Output a GN array, indicating success and our output lists.
252257
# JSON formatter prints GN compatible lists when everything is a list of
253258
# strings.
254-
print(json.dumps([includes, cflags, libs, lib_dirs]))
259+
print(json.dumps([True, includes, cflags, libs, lib_dirs]))
255260
return 0
256261

257262

build/config/linux/pkg_config.gni

+33-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2-
# Copyright (c) 2020 Project CHIP Authors
2+
# Copyright (c) 2020-2024 Project CHIP Authors
33
#
44
# Redistribution and use in source and binary forms, with or without
55
# modification, are permitted provided that the following conditions are
@@ -43,9 +43,14 @@
4343
#
4444
# You can also use "extra args" to filter out results (see pkg-config.py):
4545
# extra_args = [ "-v, "foo" ]
46+
#
4647
# To ignore libs and ldflags (only cflags/defines will be set, which is useful
4748
# when doing manual dynamic linking), set:
4849
# ignore_libs = true
50+
#
51+
# To allow the build to proceed if (any of) the requested packages are absent, set:
52+
# optional = true
53+
# In this case the resulting config object will be empty.
4954

5055
import("//build_overrides/build.gni")
5156
import("${build_root}/config/sysroot.gni")
@@ -109,37 +114,42 @@ template("pkg_config") {
109114
} else {
110115
args = pkg_config_args + invoker.packages
111116
}
117+
if (defined(invoker.optional) && invoker.optional) {
118+
args += [ "-o" ]
119+
}
112120
if (defined(invoker.extra_args)) {
113121
args += invoker.extra_args
114122
}
115123

124+
# pkgresult = [present, includes, cflags, libs, lib_dirs]
116125
pkgresult = exec_script(pkg_config_script, args, "value")
117-
cflags = pkgresult[1]
126+
if (pkgresult[0]) {
127+
cflags = pkgresult[2]
118128

119-
foreach(include, pkgresult[0]) {
120-
cflags += [ "-I$include" ]
121-
}
129+
foreach(include, pkgresult[1]) {
130+
cflags += [ "-I$include" ]
131+
}
122132

123-
if (!defined(invoker.ignore_libs) || !invoker.ignore_libs) {
124-
libs = pkgresult[2]
125-
lib_dirs = pkgresult[3]
126-
}
133+
if (!defined(invoker.ignore_libs) || !invoker.ignore_libs) {
134+
libs = pkgresult[3]
135+
lib_dirs = pkgresult[4]
136+
}
127137

128-
# Link libraries statically for OSS-Fuzz fuzzer build
129-
if (oss_fuzz) {
130-
libs = []
131-
ldflags = [ "-Wl,-Bstatic" ]
132-
foreach(lib, pkgresult[2]) {
133-
ldflags += [ "-l$lib" ]
138+
# Link libraries statically for OSS-Fuzz fuzzer build
139+
if (oss_fuzz) {
140+
libs = []
141+
ldflags = [ "-Wl,-Bstatic" ]
142+
foreach(lib, pkgresult[3]) {
143+
ldflags += [ "-l$lib" ]
144+
}
145+
ldflags += [ "-Wl,-Bdynamic" ]
146+
lib_dirs = pkgresult[4]
134147
}
135-
ldflags += [ "-Wl,-Bdynamic" ]
136-
lib_dirs = pkgresult[3]
137-
}
138148

139-
forward_variables_from(invoker,
140-
[
141-
"defines",
142-
"visibility",
143-
])
149+
forward_variables_from(invoker, [ "defines" ])
150+
}
144151
}
152+
153+
# Always forward visibility
154+
forward_variables_from(invoker, [ "visibility" ])
145155
}

0 commit comments

Comments
 (0)