CSharpUtilities.cmake 11 KB

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