My CMake modules, scripts, templates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UseVala.cmake 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ##
  2. # Compile vala files to their c equivalents for further processing.
  3. #
  4. # The "vala_precompile" function takes care of calling the valac executable on
  5. # the given source to produce c files which can then be processed further using
  6. # default cmake functions.
  7. #
  8. # The first parameter provided is a variable, which will be filled with a list
  9. # of c files outputted by the vala compiler. This list can than be used in
  10. # conjuction with functions like "add_executable" or others to create the
  11. # neccessary compile rules with CMake.
  12. #
  13. # The following sections may be specified afterwards to provide certain options
  14. # to the vala compiler:
  15. #
  16. # SOURCES
  17. # A list of .vala files to be compiled. Please take care to add every vala
  18. # file belonging to the currently compiled project or library as Vala will
  19. # otherwise not be able to resolve all dependencies.
  20. #
  21. # PACKAGES
  22. # A list of vala packages/libraries to be used during the compile cycle. The
  23. # package names are exactly the same, as they would be passed to the valac
  24. # "--pkg=" option.
  25. #
  26. # OPTIONS
  27. # A list of optional options to be passed to the valac executable. This can be
  28. # used to pass "--thread" for example to enable multi-threading support.
  29. #
  30. # CUSTOM_VAPIS
  31. # A list of custom vapi files to be included for compilation. This can be
  32. # useful to include freshly created vala libraries without having to install
  33. # them in the system.
  34. #
  35. # GENERATE_VAPI
  36. # Pass all the needed flags to the compiler to create an internal vapi for
  37. # the compiled library. The provided name will be used for this and a
  38. # <provided_name>.vapi file will be created.
  39. #
  40. # GENERATE_HEADER
  41. # Let the compiler generate a header file for the compiled code. There will
  42. # be a header file as well as an internal header file being generated called
  43. # <provided_name>.h and <provided_name>_internal.h
  44. #
  45. # The following call is a simple example to the vala_precompile macro showing
  46. # an example to every of the optional sections:
  47. #
  48. # find_package(Vala "0.12" REQUIRED)
  49. # inlcude(${VALA_USE_FILE})
  50. #
  51. # vala_precompile(VALA_C
  52. # SOURCES
  53. # source1.vala
  54. # source2.vala
  55. # source3.vala
  56. # PACKAGES
  57. # gtk+-2.0
  58. # gio-1.0
  59. # posix
  60. # DIRECTORY
  61. # gen
  62. # OPTIONS
  63. # --thread
  64. # CUSTOM_VAPIS
  65. # some_vapi.vapi
  66. # GENERATE_VAPI
  67. # myvapi
  68. # GENERATE_HEADER
  69. # myheader
  70. # )
  71. #
  72. # Most important is the variable VALA_C which will contain all the generated c
  73. # file names after the call.
  74. ##
  75. ##
  76. # Copyright 2009-2010 Jakob Westhoff. All rights reserved.
  77. # Copyright 2010-2011 Daniel Pfeifer
  78. #
  79. # Redistribution and use in source and binary forms, with or without
  80. # modification, are permitted provided that the following conditions are met:
  81. #
  82. # 1. Redistributions of source code must retain the above copyright notice,
  83. # this list of conditions and the following disclaimer.
  84. #
  85. # 2. Redistributions in binary form must reproduce the above copyright notice,
  86. # this list of conditions and the following disclaimer in the documentation
  87. # and/or other materials provided with the distribution.
  88. #
  89. # THIS SOFTWARE IS PROVIDED BY JAKOB WESTHOFF ``AS IS'' AND ANY EXPRESS OR
  90. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  91. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  92. # EVENT SHALL JAKOB WESTHOFF OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  93. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  94. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  95. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  96. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  97. # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  98. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  99. #
  100. # The views and conclusions contained in the software and documentation are those
  101. # of the authors and should not be interpreted as representing official policies,
  102. # either expressed or implied, of Jakob Westhoff
  103. ##
  104. include(CMakeParseArguments)
  105. function(vala_precompile output)
  106. cmake_parse_arguments(ARGS "" "DIRECTORY;GENERATE_HEADER;GENERATE_VAPI"
  107. "SOURCES;PACKAGES;OPTIONS;CUSTOM_VAPIS" ${ARGN})
  108. if(ARGS_DIRECTORY)
  109. set(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGS_DIRECTORY})
  110. else(ARGS_DIRECTORY)
  111. set(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  112. endif(ARGS_DIRECTORY)
  113. include_directories(${DIRECTORY})
  114. set(vala_pkg_opts "")
  115. foreach(pkg ${ARGS_PACKAGES})
  116. list(APPEND vala_pkg_opts "--pkg=${pkg}")
  117. endforeach(pkg ${ARGS_PACKAGES})
  118. set(in_files "")
  119. set(out_files "")
  120. foreach(src ${ARGS_SOURCES} ${ARGS_UNPARSED_ARGUMENTS})
  121. list(APPEND in_files "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
  122. string(REPLACE ".vala" ".c" src ${src})
  123. string(REPLACE ".gs" ".c" src ${src})
  124. set(out_file "${DIRECTORY}/${src}")
  125. list(APPEND out_files "${DIRECTORY}/${src}")
  126. endforeach(src ${ARGS_SOURCES} ${ARGS_UNPARSED_ARGUMENTS})
  127. set(custom_vapi_arguments "")
  128. if(ARGS_CUSTOM_VAPIS)
  129. foreach(vapi ${ARGS_CUSTOM_VAPIS})
  130. if(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
  131. list(APPEND custom_vapi_arguments ${vapi})
  132. else (${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
  133. list(APPEND custom_vapi_arguments ${CMAKE_CURRENT_SOURCE_DIR}/${vapi})
  134. endif(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
  135. endforeach(vapi ${ARGS_CUSTOM_VAPIS})
  136. endif(ARGS_CUSTOM_VAPIS)
  137. set(vapi_arguments "")
  138. if(ARGS_GENERATE_VAPI)
  139. list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_VAPI}.vapi")
  140. set(vapi_arguments "--internal-vapi=${ARGS_GENERATE_VAPI}.vapi")
  141. # Header and internal header is needed to generate internal vapi
  142. if (NOT ARGS_GENERATE_HEADER)
  143. set(ARGS_GENERATE_HEADER ${ARGS_GENERATE_VAPI})
  144. endif(NOT ARGS_GENERATE_HEADER)
  145. endif(ARGS_GENERATE_VAPI)
  146. set(header_arguments "")
  147. if(ARGS_GENERATE_HEADER)
  148. list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_HEADER}.h")
  149. list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_HEADER}_internal.h")
  150. list(APPEND header_arguments "--header=${DIRECTORY}/${ARGS_GENERATE_HEADER}.h")
  151. list(APPEND header_arguments "--internal-header=${DIRECTORY}/${ARGS_GENERATE_HEADER}_internal.h")
  152. endif(ARGS_GENERATE_HEADER)
  153. add_custom_command(OUTPUT ${out_files}
  154. COMMAND
  155. ${VALA_EXECUTABLE}
  156. ARGS
  157. "-C"
  158. ${header_arguments}
  159. ${vapi_arguments}
  160. "-b" ${CMAKE_CURRENT_SOURCE_DIR}
  161. "-d" ${DIRECTORY}
  162. ${vala_pkg_opts}
  163. ${ARGS_OPTIONS}
  164. ${in_files}
  165. ${custom_vapi_arguments}
  166. DEPENDS
  167. ${in_files}
  168. ${ARGS_CUSTOM_VAPIS}
  169. )
  170. set(${output} ${out_files} PARENT_SCOPE)
  171. endfunction(vala_precompile)