CSharpUtilities.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CSharpUtilities
  5. ---------------
  6. Functions to make configuration of CSharp/.NET targets easier.
  7. A collection of CMake utility functions useful for dealing with CSharp
  8. targets for Visual Studio generators from version 2010 and later.
  9. The following functions are provided by this module:
  10. **Main functions**
  11. - :command:`csharp_set_windows_forms_properties`
  12. - :command:`csharp_set_designer_cs_properties`
  13. - :command:`csharp_set_xaml_cs_properties`
  14. **Helper functions**
  15. - :command:`csharp_get_filename_keys`
  16. - :command:`csharp_get_filename_key_base`
  17. - :command:`csharp_get_dependentupon_name`
  18. Main functions provided by the module
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. .. command:: csharp_set_windows_forms_properties
  21. Sets source file properties for use of Windows Forms. Use this, if your CSharp
  22. target uses Windows Forms::
  23. csharp_set_windows_forms_properties([<file1> [<file2> [...]]])
  24. ``<fileN>``
  25. List of all source files which are relevant for setting the
  26. :prop_sf:`VS_CSHARP_<tagname>` properties (including ``.cs``, ``.resx`` and
  27. ``.Designer.cs`` extensions).
  28. In the list of all given files for all files ending with ``.Designer.cs`` and
  29. ``.resx`` is searched. For every *designer* or *resource* file a file with the
  30. same base name but only ``.cs`` as extension is searched. If this is found, the
  31. :prop_sf:`VS_CSHARP_<tagname>` properties are set as follows:
  32. for the **.cs** file:
  33. - VS_CSHARP_SubType "Form"
  34. for the **.Designer.cs** file (if it exists):
  35. - VS_CSHARP_DependentUpon <cs-filename>
  36. - VS_CSHARP_DesignTime "" (delete tag if previously defined)
  37. - VS_CSHARP_AutoGen ""(delete tag if previously defined)
  38. for the **.resx** file (if it exists):
  39. - VS_RESOURCE_GENERATOR "" (delete tag if previously defined)
  40. - VS_CSHARP_DependentUpon <cs-filename>
  41. - VS_CSHARP_SubType "Designer"
  42. .. command:: csharp_set_designer_cs_properties
  43. Sets source file properties of ``.Designer.cs`` files depending on
  44. sibling filenames. Use this, if your CSharp target does **not**
  45. use Windows Forms (for Windows Forms use
  46. :command:`csharp_set_designer_cs_properties` instead)::
  47. csharp_set_designer_cs_properties([<file1> [<file2> [...]]])
  48. ``<fileN>``
  49. List of all source files which are relevant for setting the
  50. :prop_sf:`VS_CSHARP_<tagname>` properties (including ``.cs``,
  51. ``.resx``, ``.settings`` and ``.Designer.cs`` extensions).
  52. In the list of all given files for all files ending with
  53. ``.Designer.cs`` is searched. For every *designer* file all files
  54. with the same base name but different extensions are searched. If
  55. a match is found, the source file properties of the *designer* file
  56. are set depending on the extension of the matched file:
  57. if match is **.resx** file:
  58. - VS_CSHARP_AutoGen "True"
  59. - VS_CSHARP_DesignTime "True"
  60. - VS_CSHARP_DependentUpon <resx-filename>
  61. if match is **.cs** file:
  62. - VS_CSHARP_DependentUpon <cs-filename>
  63. if match is **.settings** file:
  64. - VS_CSHARP_AutoGen "True"
  65. - VS_CSHARP_DesignTimeSharedInput "True"
  66. - VS_CSHARP_DependentUpon <settings-filename>
  67. .. note::
  68. Because the source file properties of the ``.Designer.cs`` file are set according
  69. to the found matches and every match sets the **VS_CSHARP_DependentUpon**
  70. property, there should only be one match for each ``Designer.cs`` file.
  71. .. command:: csharp_set_xaml_cs_properties
  72. Sets source file properties for use of Windows Presentation Foundation (WPF) and
  73. XAML. Use this, if your CSharp target uses WPF/XAML::
  74. csharp_set_xaml_cs_properties([<file1> [<file2> [...]]])
  75. ``<fileN>``
  76. List of all source files which are relevant for setting the
  77. :prop_sf:`VS_CSHARP_<tagname>` properties (including ``.cs``,
  78. ``.xaml``, and ``.xaml.cs`` extensions).
  79. In the list of all given files for all files ending with
  80. ``.xaml.cs`` is searched. For every *xaml-cs* file, a file
  81. with the same base name but extension ``.xaml`` is searched.
  82. If a match is found, the source file properties of the ``.xaml.cs``
  83. file are set:
  84. - VS_CSHARP_DependentUpon <xaml-filename>
  85. Helper functions which are used by the above ones
  86. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  87. .. command:: csharp_get_filename_keys
  88. Helper function which computes a list of key values to identify
  89. source files independently of relative/absolute paths given in cmake
  90. and eliminates case sensitivity::
  91. csharp_get_filename_keys(OUT [<file1> [<file2> [...]]])
  92. ``OUT``
  93. Name of the variable in which the list of keys is stored
  94. ``<fileN>``
  95. filename(s) as given to to CSharp target using :command:`add_library`
  96. or :command:`add_executable`
  97. In some way the function applies a canonicalization to the source names.
  98. This is necessary to find file matches if the files have been added to
  99. the target with different directory prefixes:
  100. .. code-block:: cmake
  101. add_library(lib
  102. myfile.cs
  103. ${CMAKE_CURRENT_SOURCE_DIR}/myfile.Designer.cs)
  104. set_source_files_properties(myfile.Designer.cs PROPERTIES
  105. VS_CSHARP_DependentUpon myfile.cs)
  106. # this will fail, because in cmake
  107. # - ${CMAKE_CURRENT_SOURCE_DIR}/myfile.Designer.cs
  108. # - myfile.Designer.cs
  109. # are not the same source file. The source file property is not set.
  110. .. command:: csharp_get_filename_key_base
  111. Returns the full filepath and name **without** extension of a key.
  112. KEY is expected to be a key from csharp_get_filename_keys. In BASE
  113. the value of KEY without the file extension is returned::
  114. csharp_get_filename_key_base(BASE KEY)
  115. ``BASE``
  116. Name of the variable with the computed "base" of ``KEY``.
  117. ``KEY``
  118. The key of which the base will be computed. Expected to be a
  119. upper case full filename.
  120. .. command:: csharp_get_dependentupon_name
  121. Computes a string which can be used as value for the source file property
  122. :prop_sf:`VS_CSHARP_<tagname>` with *target* being ``DependentUpon``::
  123. csharp_get_dependentupon_name(NAME FILE)
  124. ``NAME``
  125. Name of the variable with the result value
  126. ``FILE``
  127. Filename to convert to ``<DependentUpon>`` value
  128. Actually this is only the filename without any path given at the moment.
  129. #]=======================================================================]
  130. function(csharp_get_filename_keys OUT)
  131. set(${OUT} "")
  132. foreach(f ${ARGN})
  133. get_filename_component(f ${f} REALPATH)
  134. string(TOUPPER ${f} f)
  135. list(APPEND ${OUT} ${f})
  136. endforeach()
  137. set(${OUT} "${${OUT}}" PARENT_SCOPE)
  138. endfunction()
  139. function(csharp_get_filename_key_base base key)
  140. get_filename_component(dir ${key} DIRECTORY)
  141. get_filename_component(fil ${key} NAME_WE)
  142. set(${base} "${dir}/${fil}" PARENT_SCOPE)
  143. endfunction()
  144. function(csharp_get_dependentupon_name out in)
  145. get_filename_component(${out} ${in} NAME)
  146. set(${out} ${${out}} PARENT_SCOPE)
  147. endfunction()
  148. function(csharp_set_windows_forms_properties)
  149. csharp_get_filename_keys(fileKeys ${ARGN})
  150. foreach(key ${fileKeys})
  151. get_filename_component(ext ${key} EXT)
  152. if(${ext} STREQUAL ".DESIGNER.CS" OR
  153. ${ext} STREQUAL ".RESX")
  154. csharp_get_filename_key_base(NAME_BASE ${key})
  155. list(FIND fileKeys "${NAME_BASE}.CS" FILE_INDEX)
  156. if(NOT ${FILE_INDEX} EQUAL -1)
  157. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  158. # set properties of main form file
  159. set_source_files_properties("${FILE_NAME}"
  160. PROPERTIES
  161. VS_CSHARP_SubType "Form")
  162. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  163. # set properties of designer file (if found)
  164. list(FIND fileKeys "${NAME_BASE}.DESIGNER.CS" FILE_INDEX)
  165. if(NOT ${FILE_INDEX} EQUAL -1)
  166. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  167. set_source_files_properties("${FILE_NAME}"
  168. PROPERTIES
  169. VS_CSHARP_DependentUpon "${LINK}"
  170. VS_CSHARP_DesignTime ""
  171. VS_CSHARP_AutoGen "")
  172. endif()
  173. # set properties of corresponding resource file (if found)
  174. list(FIND fileKeys "${NAME_BASE}.RESX" FILE_INDEX)
  175. if(NOT ${FILE_INDEX} EQUAL -1)
  176. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  177. set_source_files_properties("${FILE_NAME}"
  178. PROPERTIES
  179. VS_RESOURCE_GENERATOR ""
  180. VS_CSHARP_DependentUpon "${LINK}"
  181. VS_CSHARP_SubType "Designer")
  182. endif()
  183. endif()
  184. endif()
  185. endforeach()
  186. endfunction()
  187. function(csharp_set_designer_cs_properties)
  188. csharp_get_filename_keys(fileKeys ${ARGN})
  189. set(INDEX -1)
  190. foreach(key ${fileKeys})
  191. math(EXPR INDEX "${INDEX}+1")
  192. list(GET ARGN ${INDEX} source)
  193. get_filename_component(ext ${key} EXT)
  194. if(${ext} STREQUAL ".DESIGNER.CS")
  195. csharp_get_filename_key_base(NAME_BASE ${key})
  196. if("${NAME_BASE}.RESX" IN_LIST fileKeys)
  197. list(FIND fileKeys "${NAME_BASE}.RESX" FILE_INDEX)
  198. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  199. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  200. set_source_files_properties("${source}"
  201. PROPERTIES
  202. VS_CSHARP_AutoGen "True"
  203. VS_CSHARP_DesignTime "True"
  204. VS_CSHARP_DependentUpon "${LINK}")
  205. elseif("${NAME_BASE}.CS" IN_LIST fileKeys)
  206. list(FIND fileKeys "${NAME_BASE}.CS" FILE_INDEX)
  207. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  208. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  209. set_source_files_properties("${source}"
  210. PROPERTIES
  211. VS_CSHARP_DependentUpon "${LINK}")
  212. elseif("${NAME_BASE}.SETTINGS" IN_LIST fileKeys)
  213. list(FIND fileKeys "${NAME_BASE}.SETTINGS" FILE_INDEX)
  214. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  215. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  216. set_source_files_properties("${source}"
  217. PROPERTIES
  218. VS_CSHARP_AutoGen "True"
  219. VS_CSHARP_DesignTimeSharedInput "True"
  220. VS_CSHARP_DependentUpon "${LINK}")
  221. endif()
  222. endif()
  223. endforeach()
  224. endfunction()
  225. function(csharp_set_xaml_cs_properties)
  226. csharp_get_filename_keys(fileKeys ${ARGN})
  227. set(INDEX -1)
  228. foreach(key ${fileKeys})
  229. math(EXPR INDEX "${INDEX}+1")
  230. list(GET ARGN ${INDEX} source)
  231. get_filename_component(ext ${key} EXT)
  232. if(${ext} STREQUAL ".XAML.CS")
  233. csharp_get_filename_key_base(NAME_BASE ${key})
  234. if("${NAME_BASE}.XAML" IN_LIST fileKeys)
  235. list(FIND fileKeys "${NAME_BASE}.XAML" FILE_INDEX)
  236. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  237. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  238. set_source_files_properties("${source}"
  239. PROPERTIES
  240. VS_CSHARP_DependentUpon "${LINK}")
  241. endif()
  242. endif()
  243. endforeach()
  244. endfunction()