pkg-config.tcl 5.0 KB

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