generate_findpackage_file 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env ruby
  2. # Simple script to generate simple cmake modules for finding
  3. # libraries (packages)
  4. #
  5. # usage: generate_findpackage_file
  6. # then you will be prompted to enter the required parameters
  7. #
  8. #####################################################################
  9. #
  10. # Copyright (c) 2006 Alexander Neundorf <neundorf@kde.org>
  11. # Copyright (c) 2006 Andreas Schneider <asn@cryptomilk.org>
  12. #
  13. # This program is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU General Public License
  15. # as published by the Free Software Foundation; either version 2
  16. # of the License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin Street, Fifth Floor,
  26. # Boston, MA 02110-1301, USA.
  27. #
  28. require 'readline'
  29. package=Readline.readline("Name of package: ")
  30. name=Readline.readline("\nYour Name (for copyright): ")
  31. email=Readline.readline("\nYour mail (for copyright): ")
  32. pkgconfig=Readline.readline("\npkgconfig package name (e.g. \"libxml-2.0\", leave empty to skip pkgconfig): ")
  33. header=Readline.readline("\nLook for header (e.g. \"jpeglib.h\" or \"libxml/xpath.h\"): ")
  34. incSubDir=Readline.readline("\nLook for header subdir (e.g. \"libxml2\", empty to skip ): ")
  35. libs=Readline.readline("\nLook for library (e.g. \"xml2\" or \"avcodec avutil\"): ")
  36. t = Time.now
  37. cmakeIncDirName=package.upcase+"_INCLUDE_DIR"
  38. cmakeIncDirNames=package.upcase+"_INCLUDE_DIRS"
  39. cmakeLibNames=package.upcase+"_LIBRARIES"
  40. cmakeDefsName=package.upcase+"_DEFINITIONS"
  41. cmakeFoundName=package.upcase+"_FOUND"
  42. cmakeQuietName=package+"_FIND_QUIETLY"
  43. cmakeRequiredName=package+"_FIND_REQUIRED"
  44. file=File.new("Find#{package}.cmake", "w+")
  45. file.printf("# - Try to find #{package}\n")
  46. file.printf("# Once done this will define\n")
  47. file.printf("#\n")
  48. file.printf("# #{cmakeFoundName} - system has #{package}\n")
  49. file.printf("# #{cmakeIncDirNames} - the #{package} include directory\n")
  50. file.printf("# #{cmakeLibNames} - Link these to use #{package}\n")
  51. file.printf("# #{cmakeDefsName} - Compiler switches required for using #{package}\n")
  52. file.printf("#\n")
  53. file.printf("# Copyright (c) #{t.year} #{name} <#{email}>\n")
  54. file.printf("#\n")
  55. file.printf("# Redistribution and use is allowed according to the terms of the New\n")
  56. file.printf("# BSD license.\n")
  57. file.printf("# For details see the accompanying COPYING-CMAKE-SCRIPTS file.\n")
  58. file.printf("#\n")
  59. file.printf("\n")
  60. file.printf("\n")
  61. file.printf("if (#{cmakeLibNames} AND #{cmakeIncDirNames})\n")
  62. file.printf(" # in cache already\n")
  63. file.printf(" set(#{cmakeFoundName} TRUE)\n")
  64. file.printf("else (#{cmakeLibNames} AND #{cmakeIncDirNames})\n")
  65. if not pkgconfig.empty?
  66. file.printf(" find_package(PkgConfig)\n")
  67. file.printf(" if (PKG_CONFIG_FOUND)\n")
  68. file.printf(" pkg_check_modules(_#{package.upcase} #{pkgconfig})\n")
  69. file.printf(" endif (PKG_CONFIG_FOUND)\n")
  70. end
  71. file.printf("\n")
  72. file.printf(" find_path(#{cmakeIncDirName}\n")
  73. file.printf(" NAMES\n")
  74. file.printf(" #{header}\n")
  75. file.printf(" PATHS\n")
  76. if not pkgconfig.empty?
  77. file.printf(" ${_#{package.upcase}_INCLUDEDIR}\n")
  78. end
  79. file.printf(" /usr/include\n")
  80. file.printf(" /usr/local/include\n")
  81. file.printf(" /opt/local/include\n")
  82. file.printf(" /sw/include\n")
  83. if not incSubDir.empty?
  84. file.printf(" PATH_SUFFIXES\n")
  85. file.printf(" #{incSubDir}\n")
  86. end
  87. file.printf(" )\n")
  88. file.printf("\n")
  89. libs.split(" ").each do |lib|
  90. file.printf(" find_library(#{lib.upcase}_LIBRARY\n")
  91. file.printf(" NAMES\n")
  92. file.printf(" #{lib}\n")
  93. file.printf(" PATHS\n")
  94. if not pkgconfig.empty?
  95. file.printf(" ${_#{package.upcase}_LIBDIR}\n")
  96. end
  97. file.printf(" /usr/lib\n")
  98. file.printf(" /usr/local/lib\n")
  99. file.printf(" /opt/local/lib\n")
  100. file.printf(" /sw/lib\n")
  101. file.printf(" )\n")
  102. file.printf("\n")
  103. end
  104. file.printf(" set(#{cmakeIncDirNames}\n")
  105. file.printf(" ${#{cmakeIncDirName}}\n")
  106. file.printf(" )\n")
  107. file.printf("\n")
  108. libs.split(" ").each do |lib|
  109. file.printf(" if (#{lib.upcase}_LIBRARY)\n")
  110. file.printf(" set(#{cmakeLibNames}\n")
  111. file.printf(" ${#{cmakeLibNames}}\n")
  112. file.printf(" ${#{lib.upcase}_LIBRARY}\n")
  113. file.printf(" )\n")
  114. file.printf(" endif (#{lib.upcase}_LIBRARY)\n")
  115. file.printf("\n")
  116. end
  117. file.printf(" include(FindPackageHandleStandardArgs)\n")
  118. file.printf(" find_package_handle_standard_args(#{package} DEFAULT_MSG #{cmakeLibNames} #{cmakeIncDirNames})\n")
  119. file.printf("\n")
  120. file.printf(" # show the #{cmakeIncDirNames} and #{cmakeLibNames} variables only in the advanced view\n")
  121. file.printf(" mark_as_advanced(#{cmakeIncDirNames} #{cmakeLibNames})\n\n")
  122. file.printf("endif (#{cmakeLibNames} AND #{cmakeIncDirNames})\n\n")
  123. printf("Done, generated Find#{package}.cmake\n")