expr: prevent stack overflow
[jimtcl.git] / autosetup / pkg-config.tcl
blob0883e4c32f0cd33547ce4d2e35aa0358f415058f
1 # Copyright (c) 2016 WorkWare Systems http://www.workware.net.au/
2 # All rights reserved
4 # @synopsis:
6 # The 'pkg-config' module allows package information to be found via 'pkg-config'.
8 # If not cross-compiling, the package path should be determined automatically
9 # by 'pkg-config'.
10 # If cross-compiling, the default package path is the compiler sysroot.
11 # If the C compiler doesn't support '-print-sysroot', the path can be supplied
12 # by the '--sysroot' option or by defining 'SYSROOT'.
14 # 'PKG_CONFIG' may be set to use an alternative to 'pkg-config'.
16 use cc
18 module-options {
19 sysroot:dir => "Override compiler sysroot for pkg-config search path"
22 # @pkg-config-init ?required?
24 # Initialises the 'pkg-config' system. Unless '$required' is set to 0,
25 # it is a fatal error if a usable 'pkg-config' is not found .
27 # This command will normally be called automatically as required,
28 # but it may be invoked explicitly if lack of 'pkg-config' is acceptable.
30 # Returns 1 if ok, or 0 if 'pkg-config' not found/usable (only if '$required' is 0).
32 proc pkg-config-init {{required 1}} {
33 if {[is-defined HAVE_PKG_CONFIG]} {
34 return [get-define HAVE_PKG_CONFIG]
36 set found 0
38 define PKG_CONFIG [get-env PKG_CONFIG pkg-config]
39 msg-checking "Checking for pkg-config..."
41 if {[catch {exec [get-define PKG_CONFIG] --version} version]} {
42 msg-result "[get-define PKG_CONFIG] (not found)"
43 if {$required} {
44 user-error "No usable pkg-config"
46 } else {
47 msg-result $version
48 define PKG_CONFIG_VERSION $version
50 set found 1
52 if {[opt-str sysroot o]} {
53 define SYSROOT [file-normalize $o]
54 msg-result "Using specified sysroot [get-define SYSROOT]"
55 } elseif {[get-define build] ne [get-define host]} {
56 if {[catch {exec-with-stderr [get-define CC] -print-sysroot} result errinfo] == 0} {
57 # Use the compiler sysroot, if there is one
58 define SYSROOT $result
59 msg-result "Found compiler sysroot $result"
60 } else {
61 set msg "pkg-config: Cross compiling, but no compiler sysroot and no --sysroot supplied"
62 if {$required} {
63 user-error $msg
64 } else {
65 msg-result $msg
67 set found 0
70 if {[is-defined SYSROOT]} {
71 set sysroot [get-define SYSROOT]
73 # XXX: It's possible that these should be set only when invoking pkg-config
74 global env
75 set env(PKG_CONFIG_DIR) ""
76 # Do we need to try /usr/local as well or instead?
77 set env(PKG_CONFIG_LIBDIR) $sysroot/usr/lib/pkgconfig:$sysroot/usr/share/pkgconfig
78 set env(PKG_CONFIG_SYSROOT_DIR) $sysroot
81 define HAVE_PKG_CONFIG $found
82 return $found
85 # @pkg-config module ?requirements?
87 # Use 'pkg-config' to find the given module meeting the given requirements.
88 # e.g.
90 ## pkg-config pango >= 1.37.0
92 # If found, returns 1 and sets 'HAVE_PKG_PANGO' to 1 along with:
94 ## PKG_PANGO_VERSION to the found version
95 ## PKG_PANGO_LIBS to the required libs (--libs-only-l)
96 ## PKG_PANGO_LDFLAGS to the required linker flags (--libs-only-L)
97 ## PKG_PANGO_CFLAGS to the required compiler flags (--cflags)
99 # If not found, returns 0.
101 proc pkg-config {module args} {
102 set ok [pkg-config-init]
104 msg-checking "Checking for $module $args..."
106 if {!$ok} {
107 msg-result "no pkg-config"
108 return 0
111 if {[catch {exec [get-define PKG_CONFIG] --modversion "$module $args"} version]} {
112 msg-result "not found"
113 configlog "pkg-config --modversion $module $args: $version"
114 return 0
116 msg-result $version
117 set prefix [feature-define-name $module PKG_]
118 define HAVE_${prefix}
119 define ${prefix}_VERSION $version
120 define ${prefix}_LIBS [exec pkg-config --libs-only-l $module]
121 define ${prefix}_LDFLAGS [exec pkg-config --libs-only-L $module]
122 define ${prefix}_CFLAGS [exec pkg-config --cflags $module]
123 return 1
126 # @pkg-config-get module setting
128 # Convenience access to the results of 'pkg-config'.
130 # For example, '[pkg-config-get pango CFLAGS]' returns
131 # the value of 'PKG_PANGO_CFLAGS', or '""' if not defined.
132 proc pkg-config-get {module name} {
133 set prefix [feature-define-name $module PKG_]
134 get-define ${prefix}_${name} ""