CSharpUtilities.cmake 11 KB

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