Home | History | Annotate | Line # | Download | only in autosetup
      1  1.1  christos # Copyright (c) 2016 WorkWare Systems http://www.workware.net.au/
      2  1.1  christos # All rights reserved
      3  1.1  christos 
      4  1.1  christos # @synopsis:
      5  1.1  christos #
      6  1.1  christos # The 'pkg-config' module allows package information to be found via 'pkg-config'.
      7  1.1  christos #
      8  1.1  christos # If not cross-compiling, the package path should be determined automatically
      9  1.1  christos # by 'pkg-config'.
     10  1.1  christos # If cross-compiling, the default package path is the compiler sysroot.
     11  1.1  christos # If the C compiler doesn't support '-print-sysroot', the path can be supplied
     12  1.1  christos # by the '--sysroot' option or by defining 'SYSROOT'.
     13  1.1  christos #
     14  1.1  christos # 'PKG_CONFIG' may be set to use an alternative to 'pkg-config'.
     15  1.1  christos 
     16  1.1  christos use cc
     17  1.1  christos 
     18  1.1  christos options {
     19  1.1  christos 	sysroot:dir => "Override compiler sysroot for pkg-config search path"
     20  1.1  christos }
     21  1.1  christos 
     22  1.1  christos # @pkg-config-init ?required?
     23  1.1  christos #
     24  1.1  christos # Initialises the 'pkg-config' system. Unless '$required' is set to 0,
     25  1.1  christos # it is a fatal error if a usable 'pkg-config' is not found .
     26  1.1  christos #
     27  1.1  christos # This command will normally be called automatically as required,
     28  1.1  christos # but it may be invoked explicitly if lack of 'pkg-config' is acceptable.
     29  1.1  christos #
     30  1.1  christos # Returns 1 if ok, or 0 if 'pkg-config' not found/usable (only if '$required' is 0).
     31  1.1  christos #
     32  1.1  christos proc pkg-config-init {{required 1}} {
     33  1.1  christos 	if {[is-defined HAVE_PKG_CONFIG]} {
     34  1.1  christos 		return [get-define HAVE_PKG_CONFIG]
     35  1.1  christos 	}
     36  1.1  christos 	set found 0
     37  1.1  christos 
     38  1.1  christos 	define PKG_CONFIG [get-env PKG_CONFIG pkg-config]
     39  1.1  christos 	msg-checking "Checking for pkg-config..."
     40  1.1  christos 
     41  1.1  christos 	if {[catch {exec [get-define PKG_CONFIG] --version} version]} {
     42  1.1  christos 		msg-result "[get-define PKG_CONFIG] (not found)"
     43  1.1  christos 		if {$required} {
     44  1.1  christos 			user-error "No usable pkg-config"
     45  1.1  christos 		}
     46  1.1  christos 	} else {
     47  1.1  christos 		msg-result $version
     48  1.1  christos 		define PKG_CONFIG_VERSION $version
     49  1.1  christos 
     50  1.1  christos 		set found 1
     51  1.1  christos 
     52  1.1  christos 		if {[opt-str sysroot o]} {
     53  1.1  christos 			define SYSROOT [file-normalize $o]
     54  1.1  christos 			msg-result "Using specified sysroot [get-define SYSROOT]"
     55  1.1  christos 		} elseif {[get-define build] ne [get-define host]} {
     56  1.1  christos 			if {[catch {exec-with-stderr {*}[get-define CC] -print-sysroot} result errinfo] == 0} {
     57  1.1  christos 				# Use the compiler sysroot, if there is one
     58  1.1  christos 				define SYSROOT $result
     59  1.1  christos 				msg-result "Found compiler sysroot $result"
     60  1.1  christos 			} else {
     61  1.1  christos 				configlog "[get-define CC] -print-sysroot: $result"
     62  1.1  christos 				set msg "pkg-config: Cross compiling, but no compiler sysroot and no --sysroot supplied"
     63  1.1  christos 				if {$required} {
     64  1.1  christos 					user-error $msg
     65  1.1  christos 				} else {
     66  1.1  christos 					msg-result $msg
     67  1.1  christos 				}
     68  1.1  christos 				set found 0
     69  1.1  christos 			}
     70  1.1  christos 		}
     71  1.1  christos 		if {[is-defined SYSROOT]} {
     72  1.1  christos 			set sysroot [get-define SYSROOT]
     73  1.1  christos 
     74  1.1  christos 			# XXX: It's possible that these should be set only when invoking pkg-config
     75  1.1  christos 			global env
     76  1.1  christos 			set env(PKG_CONFIG_DIR) ""
     77  1.1  christos 			# Supposedly setting PKG_CONFIG_LIBDIR means that PKG_CONFIG_PATH is ignored,
     78  1.1  christos 			# but it doesn't seem to work that way in practice
     79  1.1  christos 			set env(PKG_CONFIG_PATH) ""
     80  1.1  christos 			# Do we need to try /usr/local as well or instead?
     81  1.1  christos 			set env(PKG_CONFIG_LIBDIR) $sysroot/usr/lib/pkgconfig:$sysroot/usr/share/pkgconfig
     82  1.1  christos 			set env(PKG_CONFIG_SYSROOT_DIR) $sysroot
     83  1.1  christos 		}
     84  1.1  christos 	}
     85  1.1  christos 	define HAVE_PKG_CONFIG $found
     86  1.1  christos 	return $found
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos # @pkg-config module ?requirements?
     90  1.1  christos #
     91  1.1  christos # Use 'pkg-config' to find the given module meeting the given requirements.
     92  1.1  christos # e.g.
     93  1.1  christos #
     94  1.1  christos ## pkg-config pango >= 1.37.0
     95  1.1  christos #
     96  1.1  christos # If found, returns 1 and sets 'HAVE_PKG_PANGO' to 1 along with:
     97  1.1  christos #
     98  1.1  christos ## PKG_PANGO_VERSION to the found version
     99  1.1  christos ## PKG_PANGO_LIBS    to the required libs (--libs-only-l)
    100  1.1  christos ## PKG_PANGO_LDFLAGS to the required linker flags (--libs-only-L)
    101  1.1  christos ## PKG_PANGO_CFLAGS  to the required compiler flags (--cflags)
    102  1.1  christos #
    103  1.1  christos # If not found, returns 0.
    104  1.1  christos #
    105  1.1  christos proc pkg-config {module args} {
    106  1.1  christos 	set ok [pkg-config-init]
    107  1.1  christos 
    108  1.1  christos 	msg-checking "Checking for $module $args..."
    109  1.1  christos 
    110  1.1  christos 	if {!$ok} {
    111  1.1  christos 		msg-result "no pkg-config"
    112  1.1  christos 		return 0
    113  1.1  christos 	}
    114  1.1  christos 
    115  1.1  christos 	set pkgconfig [get-define PKG_CONFIG]
    116  1.1  christos 
    117  1.1  christos 	set ret [catch {exec $pkgconfig --modversion "$module $args"} version]
    118  1.1  christos 	configlog "$pkgconfig --modversion $module $args: $version"
    119  1.1  christos 	if {$ret} {
    120  1.1  christos 		msg-result "not found"
    121  1.1  christos 		return 0
    122  1.1  christos 	}
    123  1.1  christos 	# Sometimes --modversion succeeds but because of dependencies it isn't usable
    124  1.1  christos 	# This seems to show up with --cflags
    125  1.1  christos 	set ret [catch {exec $pkgconfig --cflags $module} cflags]
    126  1.1  christos 	if {$ret} {
    127  1.1  christos 		msg-result "unusable ($version - see config.log)"
    128  1.1  christos 		configlog "$pkgconfig --cflags $module"
    129  1.1  christos 		configlog $cflags
    130  1.1  christos 		return 0
    131  1.1  christos 	}
    132  1.1  christos 	msg-result $version
    133  1.1  christos 	set prefix [feature-define-name $module PKG_]
    134  1.1  christos 	define HAVE_${prefix}
    135  1.1  christos 	define ${prefix}_VERSION $version
    136  1.1  christos 	define ${prefix}_CFLAGS $cflags
    137  1.1  christos 	define ${prefix}_LIBS [exec $pkgconfig --libs-only-l $module]
    138  1.1  christos 	define ${prefix}_LDFLAGS [exec $pkgconfig --libs-only-L $module]
    139  1.1  christos 	return 1
    140  1.1  christos }
    141  1.1  christos 
    142  1.1  christos # @pkg-config-get module setting
    143  1.1  christos #
    144  1.1  christos # Convenience access to the results of 'pkg-config'.
    145  1.1  christos #
    146  1.1  christos # For example, '[pkg-config-get pango CFLAGS]' returns
    147  1.1  christos # the value of 'PKG_PANGO_CFLAGS', or '""' if not defined.
    148  1.1  christos proc pkg-config-get {module name} {
    149  1.1  christos 	set prefix [feature-define-name $module PKG_]
    150  1.1  christos 	get-define ${prefix}_${name} ""
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos # @pkg-config-get-var module variable
    154  1.1  christos #
    155  1.1  christos # Return the value of the given variable from the given pkg-config module.
    156  1.1  christos # The module must already have been successfully detected with pkg-config.
    157  1.1  christos # e.g.
    158  1.1  christos #
    159  1.1  christos ## if {[pkg-config harfbuzz >= 2.5]} {
    160  1.1  christos ##   define harfbuzz_libdir [pkg-config-get-var harfbuzz libdir]
    161  1.1  christos ## }
    162  1.1  christos #
    163  1.1  christos # Returns the empty string if the variable isn't defined.
    164  1.1  christos proc pkg-config-get-var {module variable} {
    165  1.1  christos 	set pkgconfig [get-define PKG_CONFIG]
    166  1.1  christos 	set prefix [feature-define-name $module HAVE_PKG_]
    167  1.1  christos 	exec $pkgconfig $module --variable $variable
    168  1.1  christos }
    169