1
0

CSharpUtilities.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 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. cmake_policy(PUSH)
  132. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  133. function(csharp_get_filename_keys OUT)
  134. set(${OUT} "")
  135. foreach(f ${ARGN})
  136. get_filename_component(f ${f} REALPATH)
  137. string(TOUPPER ${f} f)
  138. list(APPEND ${OUT} ${f})
  139. endforeach()
  140. set(${OUT} "${${OUT}}" PARENT_SCOPE)
  141. endfunction()
  142. function(csharp_get_filename_key_base base key)
  143. get_filename_component(dir ${key} DIRECTORY)
  144. get_filename_component(fil ${key} NAME_WE)
  145. set(${base} "${dir}/${fil}" PARENT_SCOPE)
  146. endfunction()
  147. function(csharp_get_dependentupon_name out in)
  148. get_filename_component(${out} ${in} NAME)
  149. set(${out} ${${out}} PARENT_SCOPE)
  150. endfunction()
  151. function(csharp_set_windows_forms_properties)
  152. csharp_get_filename_keys(fileKeys ${ARGN})
  153. foreach(key ${fileKeys})
  154. get_filename_component(ext ${key} EXT)
  155. if(${ext} STREQUAL ".DESIGNER.CS" OR
  156. ${ext} STREQUAL ".RESX")
  157. csharp_get_filename_key_base(NAME_BASE ${key})
  158. list(FIND fileKeys "${NAME_BASE}.CS" FILE_INDEX)
  159. if(NOT ${FILE_INDEX} EQUAL -1)
  160. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  161. # set properties of main form file
  162. set_source_files_properties("${FILE_NAME}"
  163. PROPERTIES
  164. VS_CSHARP_SubType "Form")
  165. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  166. # set properties of designer file (if found)
  167. list(FIND fileKeys "${NAME_BASE}.DESIGNER.CS" FILE_INDEX)
  168. if(NOT ${FILE_INDEX} EQUAL -1)
  169. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  170. set_source_files_properties("${FILE_NAME}"
  171. PROPERTIES
  172. VS_CSHARP_DependentUpon "${LINK}"
  173. VS_CSHARP_DesignTime ""
  174. VS_CSHARP_AutoGen "")
  175. endif()
  176. # set properties of corresponding resource file (if found)
  177. list(FIND fileKeys "${NAME_BASE}.RESX" FILE_INDEX)
  178. if(NOT ${FILE_INDEX} EQUAL -1)
  179. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  180. set_source_files_properties("${FILE_NAME}"
  181. PROPERTIES
  182. VS_RESOURCE_GENERATOR ""
  183. VS_CSHARP_DependentUpon "${LINK}"
  184. VS_CSHARP_SubType "Designer")
  185. endif()
  186. endif()
  187. endif()
  188. endforeach()
  189. endfunction()
  190. function(csharp_set_designer_cs_properties)
  191. csharp_get_filename_keys(fileKeys ${ARGN})
  192. set(INDEX -1)
  193. foreach(key ${fileKeys})
  194. math(EXPR INDEX "${INDEX}+1")
  195. list(GET ARGN ${INDEX} source)
  196. get_filename_component(ext ${key} EXT)
  197. if(${ext} STREQUAL ".DESIGNER.CS")
  198. csharp_get_filename_key_base(NAME_BASE ${key})
  199. if("${NAME_BASE}.RESX" IN_LIST fileKeys)
  200. list(FIND fileKeys "${NAME_BASE}.RESX" FILE_INDEX)
  201. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  202. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  203. set_source_files_properties("${source}"
  204. PROPERTIES
  205. VS_CSHARP_AutoGen "True"
  206. VS_CSHARP_DesignTime "True"
  207. VS_CSHARP_DependentUpon "${LINK}")
  208. elseif("${NAME_BASE}.CS" IN_LIST fileKeys)
  209. list(FIND fileKeys "${NAME_BASE}.CS" FILE_INDEX)
  210. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  211. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  212. set_source_files_properties("${source}"
  213. PROPERTIES
  214. VS_CSHARP_DependentUpon "${LINK}")
  215. elseif("${NAME_BASE}.SETTINGS" IN_LIST fileKeys)
  216. list(FIND fileKeys "${NAME_BASE}.SETTINGS" FILE_INDEX)
  217. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  218. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  219. set_source_files_properties("${source}"
  220. PROPERTIES
  221. VS_CSHARP_AutoGen "True"
  222. VS_CSHARP_DesignTimeSharedInput "True"
  223. VS_CSHARP_DependentUpon "${LINK}")
  224. endif()
  225. endif()
  226. endforeach()
  227. endfunction()
  228. function(csharp_set_xaml_cs_properties)
  229. csharp_get_filename_keys(fileKeys ${ARGN})
  230. set(INDEX -1)
  231. foreach(key ${fileKeys})
  232. math(EXPR INDEX "${INDEX}+1")
  233. list(GET ARGN ${INDEX} source)
  234. get_filename_component(ext ${key} EXT)
  235. if(${ext} STREQUAL ".XAML.CS")
  236. csharp_get_filename_key_base(NAME_BASE ${key})
  237. if("${NAME_BASE}.XAML" IN_LIST fileKeys)
  238. list(FIND fileKeys "${NAME_BASE}.XAML" FILE_INDEX)
  239. list(GET ARGN ${FILE_INDEX} FILE_NAME)
  240. csharp_get_dependentupon_name(LINK "${FILE_NAME}")
  241. set_source_files_properties("${source}"
  242. PROPERTIES
  243. VS_CSHARP_DependentUpon "${LINK}")
  244. endif()
  245. endif()
  246. endforeach()
  247. endfunction()
  248. cmake_policy(POP)