Use pkg-config to determine compile flags
compile.sh currently assumes all dependencies are in gcc's search paths. A more portable approach is to use pkg-config to determine the compile flags appropriate for the build platform (including additional flags specific to the platform), and fall back on hardcoded defaults if it's not available.
Problem to solve
Different environments have the expected dependencies in different locations, and/or have more up-to-date instances in non-default locations. For instance, the CLT gcc on my macOS Sonoma box can't find the SSL libraries without help:
% echo "int main(){}" | gcc -xc - -lssl -lcrypto -o /dev/null && echo YES
ld: library 'ssl' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
% echo "int main(){}" | gcc -xc - $(pkg-config --libs libssl) $(pkg-config --libs libcrypto) -o /dev/null && echo YES
YES
Proposal
Define a couple of functions that return the necessary flags (with defaults):
# env var to be overridden if needed
pkg_config=${PKG_CONFIG:-pkg-config}
# cflags <pkgname> [<default_flags>...]
cflags() {
local pkg=$1; shift
if ! command -v "$pkg_config" &>/dev/null || ! "$pkg_config" --cflags "$pkg" 2>/dev/null; then
echo "$*"
fi
}
# ldflags <pkgname> [<default_flags>...]
ldflags() {
local pkg=$1; shift
if ! command -v "$pkg_config" &>/dev/null || ! "$pkg_config" --libs "$pkg" 2>/dev/null; then
echo "$*"
fi
}
Example usage:
# auto-enable SSL support for build if OpenSSL can be found on the system
if echo "int main(){}" | gcc -xc - $(ldflags libssl -lssl) $(ldflags libcrypto -lcrypto) -o /dev/null 2>/dev/null
then
echo $' OpenSSL lib: \e[32mfound\e[0m'
links="$links $(ldflags libssl -lssl) $(ldflags libcrypto -lcrypto)"
flags="$flags $(cflags libssl) $(cflags libcrypto) -DSSL_SUPPORT"
[...]