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