CSharpUtilities.cmake 11 KB

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