From 5bf5c06d6a46ec3af5b81de92f911efaca3fc3c2 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Wed, 16 Oct 2024 20:41:04 +0100 Subject: [PATCH] Add std/pkgconfig module A module for collecting compile-time information from pkg-config. --- lib/std/pkgconfig.nim | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/std/pkgconfig.nim diff --git a/lib/std/pkgconfig.nim b/lib/std/pkgconfig.nim new file mode 100644 index 000000000000..7df3ae794dff --- /dev/null +++ b/lib/std/pkgconfig.nim @@ -0,0 +1,43 @@ +# +# +# Nim's Runtime (and compile-time) Library +# (c) Copyright 2024 Nim contributors +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module implements a compile-time procedure for calling ``pkg-config``. +## +## https://www.freedesktop.org/wiki/Software/pkg-config/ + +from std/envvars import getEnv + +proc pkgConfig*(args: string): string {.compiletime.} = + ## Call **pkg-config** with ``args``. + ## If ``$PKG_CONFIG`` is set in the environment then it will + ## be used instead of ``pkg-config`` from ``$PATH``. + ## + ## ```nim + ## import std/pkgconfig + ## + ## discard pkgConfig" --atleast-version=3 openssl" + ## # If the available openssl version is less than 3.0.0 + ## # then pkg-config returns non-zero and a defect is raised. + ## + ## {.passC: pkgConfig" --cflags openssl".} + ## {.passL: pkgConfig" --libs openssl".} + ## # Pass command-line arguments from pkg-config to the compiler. + ## ``` + var cmd = getEnv("PKG_CONFIG", "pkg-config") & " " & args + var code: int + (result, code) = gorgeEx(cmd) + if code != 0: + var msg = cmd + msg.add "\nPKG_CONFIG=" + msg.add getEnv("PKG_CONFIG") + msg.add "\nPKG_CONFIG_PATH=" + msg.add getEnv("PKG_CONFIG_PATH") + msg.add '\n' + msg.add result + raise newException(AssertionDefect, msg)