cmake-file-api.7.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. .. cmake-manual-description: CMake File-Based API
  2. cmake-file-api(7)
  3. *****************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. CMake provides a file-based API that clients may use to get semantic
  9. information about the buildsystems CMake generates. Clients may use
  10. the API by writing query files to a specific location in a build tree
  11. to request zero or more `Object Kinds`_. When CMake generates the
  12. buildsystem in that build tree it will read the query files and write
  13. reply files for the client to read.
  14. The file-based API uses a ``<build>/.cmake/api/`` directory at the top
  15. of a build tree. The API is versioned to support changes to the layout
  16. of files within the API directory. API file layout versioning is
  17. orthogonal to the versioning of `Object Kinds`_ used in replies.
  18. This version of CMake supports only one API version, `API v1`_.
  19. API v1
  20. ======
  21. API v1 is housed in the ``<build>/.cmake/api/v1/`` directory.
  22. It has the following subdirectories:
  23. ``query/``
  24. Holds query files written by clients.
  25. These may be `v1 Shared Stateless Query Files`_,
  26. `v1 Client Stateless Query Files`_, or `v1 Client Stateful Query Files`_.
  27. ``reply/``
  28. Holds reply files written by CMake whenever it runs to generate a build
  29. system. These are indexed by a `v1 Reply Index File`_ file that may
  30. reference additional `v1 Reply Files`_. CMake owns all reply files.
  31. Clients must never remove them.
  32. Clients may look for and read a reply index file at any time.
  33. Clients may optionally create the ``reply/`` directory at any time
  34. and monitor it for the appearance of a new reply index file.
  35. v1 Shared Stateless Query Files
  36. -------------------------------
  37. Shared stateless query files allow clients to share requests for
  38. major versions of the `Object Kinds`_ and get all requested versions
  39. recognized by the CMake that runs.
  40. Clients may create shared requests by creating empty files in the
  41. ``v1/query/`` directory. The form is::
  42. <build>/.cmake/api/v1/query/<kind>-v<major>
  43. where ``<kind>`` is one of the `Object Kinds`_, ``-v`` is literal,
  44. and ``<major>`` is the major version number.
  45. Files of this form are stateless shared queries not owned by any specific
  46. client. Once created they should not be removed without external client
  47. coordination or human intervention.
  48. v1 Client Stateless Query Files
  49. -------------------------------
  50. Client stateless query files allow clients to create owned requests for
  51. major versions of the `Object Kinds`_ and get all requested versions
  52. recognized by the CMake that runs.
  53. Clients may create owned requests by creating empty files in
  54. client-specific query subdirectories. The form is::
  55. <build>/.cmake/api/v1/query/client-<client>/<kind>-v<major>
  56. where ``client-`` is literal, ``<client>`` is a string uniquely
  57. identifying the client, ``<kind>`` is one of the `Object Kinds`_,
  58. ``-v`` is literal, and ``<major>`` is the major version number.
  59. Each client must choose a unique ``<client>`` identifier via its
  60. own means.
  61. Files of this form are stateless queries owned by the client ``<client>``.
  62. The owning client may remove them at any time.
  63. v1 Client Stateful Query Files
  64. ------------------------------
  65. Stateful query files allow clients to request a list of versions of
  66. each of the `Object Kinds`_ and get only the most recent version
  67. recognized by the CMake that runs.
  68. Clients may create owned stateful queries by creating ``query.json``
  69. files in client-specific query subdirectories. The form is::
  70. <build>/.cmake/api/v1/query/client-<client>/query.json
  71. where ``client-`` is literal, ``<client>`` is a string uniquely
  72. identifying the client, and ``query.json`` is literal. Each client
  73. must choose a unique ``<client>`` identifier via its own means.
  74. ``query.json`` files are stateful queries owned by the client ``<client>``.
  75. The owning client may update or remove them at any time. When a
  76. given client installation is updated it may then update the stateful
  77. query it writes to build trees to request newer object versions.
  78. This can be used to avoid asking CMake to generate multiple object
  79. versions unnecessarily.
  80. A ``query.json`` file must contain a JSON object:
  81. .. code-block:: json
  82. {
  83. "requests": [
  84. { "kind": "<kind>" , "version": 1 },
  85. { "kind": "<kind>" , "version": { "major": 1, "minor": 2 } },
  86. { "kind": "<kind>" , "version": [2, 1] },
  87. { "kind": "<kind>" , "version": [2, { "major": 1, "minor": 2 }] },
  88. { "kind": "<kind>" , "version": 1, "client": {} },
  89. { "kind": "..." }
  90. ],
  91. "client": {}
  92. }
  93. The members are:
  94. ``requests``
  95. A JSON array containing zero or more requests. Each request is
  96. a JSON object with members:
  97. ``kind``
  98. Specifies one of the `Object Kinds`_ to be included in the reply.
  99. ``version``
  100. Indicates the version(s) of the object kind that the client
  101. understands. Versions have major and minor components following
  102. semantic version conventions. The value must be
  103. * a JSON integer specifying a (non-negative) major version number, or
  104. * a JSON object containing ``major`` and (optionally) ``minor``
  105. members specifying non-negative integer version components, or
  106. * a JSON array whose elements are each one of the above.
  107. ``client``
  108. Optional member reserved for use by the client. This value is
  109. preserved in the reply written for the client in the
  110. `v1 Reply Index File`_ but is otherwise ignored. Clients may use
  111. this to pass custom information with a request through to its reply.
  112. For each requested object kind CMake will choose the *first* version
  113. that it recognizes for that kind among those listed in the request.
  114. The response will use the selected *major* version with the highest
  115. *minor* version known to the running CMake for that major version.
  116. Therefore clients should list all supported major versions in
  117. preferred order along with the minimal minor version required
  118. for each major version.
  119. ``client``
  120. Optional member reserved for use by the client. This value is
  121. preserved in the reply written for the client in the
  122. `v1 Reply Index File`_ but is otherwise ignored. Clients may use
  123. this to pass custom information with a query through to its reply.
  124. Other ``query.json`` top-level members are reserved for future use.
  125. If present they are ignored for forward compatibility.
  126. v1 Reply Index File
  127. -------------------
  128. CMake writes an ``index-*.json`` file to the ``v1/reply/`` directory
  129. whenever it runs to generate a build system. Clients must read the
  130. reply index file first and may read other `v1 Reply Files`_ only by
  131. following references. The form of the reply index file name is::
  132. <build>/.cmake/api/v1/reply/index-<unspecified>.json
  133. where ``index-`` is literal and ``<unspecified>`` is an unspecified
  134. name selected by CMake. Whenever a new index file is generated it
  135. is given a new name and any old one is deleted. During the short
  136. time between these steps there may be multiple index files present;
  137. the one with the largest name in lexicographic order is the current
  138. index file.
  139. The reply index file contains a JSON object:
  140. .. code-block:: json
  141. {
  142. "cmake": {
  143. "version": {
  144. "major": 3, "minor": 14, "patch": 0, "suffix": "",
  145. "string": "3.14.0", "isDirty": false
  146. },
  147. "paths": {
  148. "cmake": "/prefix/bin/cmake",
  149. "ctest": "/prefix/bin/ctest",
  150. "cpack": "/prefix/bin/cpack",
  151. "root": "/prefix/share/cmake-3.14"
  152. },
  153. "generator": {
  154. "name": "Unix Makefiles"
  155. }
  156. },
  157. "objects": [
  158. { "kind": "<kind>",
  159. "version": { "major": 1, "minor": 0 },
  160. "jsonFile": "<file>" },
  161. { "...": "..." }
  162. ],
  163. "reply": {
  164. "<kind>-v<major>": { "kind": "<kind>",
  165. "version": { "major": 1, "minor": 0 },
  166. "jsonFile": "<file>" },
  167. "<unknown>": { "error": "unknown query file" },
  168. "...": {},
  169. "client-<client>": {
  170. "<kind>-v<major>": { "kind": "<kind>",
  171. "version": { "major": 1, "minor": 0 },
  172. "jsonFile": "<file>" },
  173. "<unknown>": { "error": "unknown query file" },
  174. "...": {},
  175. "query.json": {
  176. "requests": [ {}, {}, {} ],
  177. "responses": [
  178. { "kind": "<kind>",
  179. "version": { "major": 1, "minor": 0 },
  180. "jsonFile": "<file>" },
  181. { "error": "unknown query file" },
  182. { "...": {} }
  183. ],
  184. "client": {}
  185. }
  186. }
  187. }
  188. }
  189. The members are:
  190. ``cmake``
  191. A JSON object containing information about the instance of CMake that
  192. generated the reply. It contains members:
  193. ``version``
  194. A JSON object specifying the version of CMake with members:
  195. ``major``, ``minor``, ``patch``
  196. Integer values specifying the major, minor, and patch version components.
  197. ``suffix``
  198. A string specifying the version suffix, if any, e.g. ``g0abc3``.
  199. ``string``
  200. A string specifying the full version in the format
  201. ``<major>.<minor>.<patch>[-<suffix>]``.
  202. ``isDirty``
  203. A boolean indicating whether the version was built from a version
  204. controlled source tree with local modifications.
  205. ``paths``
  206. A JSON object specifying paths to things that come with CMake.
  207. It has members for ``cmake``, ``ctest``, and ``cpack`` whose values
  208. are JSON strings specifying the absolute path to each tool,
  209. represented with forward slashes. It also has a ``root`` member for
  210. the absolute path to the directory containing CMake resources like the
  211. ``Modules/`` directory (see :variable:`CMAKE_ROOT`).
  212. ``generator``
  213. A JSON object describing the CMake generator used for the build.
  214. It has members:
  215. ``name``
  216. A string specifying the name of the generator.
  217. ``platform``
  218. If the generator supports :variable:`CMAKE_GENERATOR_PLATFORM`,
  219. this is a string specifying the generator platform name.
  220. ``objects``
  221. A JSON array listing all versions of all `Object Kinds`_ generated
  222. as part of the reply. Each array entry is a
  223. `v1 Reply File Reference`_.
  224. ``reply``
  225. A JSON object mirroring the content of the ``query/`` directory
  226. that CMake loaded to produce the reply. The members are of the form
  227. ``<kind>-v<major>``
  228. A member of this form appears for each of the
  229. `v1 Shared Stateless Query Files`_ that CMake recognized as a
  230. request for object kind ``<kind>`` with major version ``<major>``.
  231. The value is a `v1 Reply File Reference`_ to the corresponding
  232. reply file for that object kind and version.
  233. ``<unknown>``
  234. A member of this form appears for each of the
  235. `v1 Shared Stateless Query Files`_ that CMake did not recognize.
  236. The value is a JSON object with a single ``error`` member
  237. containing a string with an error message indicating that the
  238. query file is unknown.
  239. ``client-<client>``
  240. A member of this form appears for each client-owned directory
  241. holding `v1 Client Stateless Query Files`_.
  242. The value is a JSON object mirroring the content of the
  243. ``query/client-<client>/`` directory. The members are of the form:
  244. ``<kind>-v<major>``
  245. A member of this form appears for each of the
  246. `v1 Client Stateless Query Files`_ that CMake recognized as a
  247. request for object kind ``<kind>`` with major version ``<major>``.
  248. The value is a `v1 Reply File Reference`_ to the corresponding
  249. reply file for that object kind and version.
  250. ``<unknown>``
  251. A member of this form appears for each of the
  252. `v1 Client Stateless Query Files`_ that CMake did not recognize.
  253. The value is a JSON object with a single ``error`` member
  254. containing a string with an error message indicating that the
  255. query file is unknown.
  256. ``query.json``
  257. This member appears for clients using
  258. `v1 Client Stateful Query Files`_.
  259. If the ``query.json`` file failed to read or parse as a JSON object,
  260. this member is a JSON object with a single ``error`` member
  261. containing a string with an error message. Otherwise, this member
  262. is a JSON object mirroring the content of the ``query.json`` file.
  263. The members are:
  264. ``client``
  265. A copy of the ``query.json`` file ``client`` member, if it exists.
  266. ``requests``
  267. A copy of the ``query.json`` file ``requests`` member, if it exists.
  268. ``responses``
  269. If the ``query.json`` file ``requests`` member is missing or invalid,
  270. this member is a JSON object with a single ``error`` member
  271. containing a string with an error message. Otherwise, this member
  272. contains a JSON array with a response for each entry of the
  273. ``requests`` array, in the same order. Each response is
  274. * a JSON object with a single ``error`` member containing a string
  275. with an error message, or
  276. * a `v1 Reply File Reference`_ to the corresponding reply file for
  277. the requested object kind and selected version.
  278. After reading the reply index file, clients may read the other
  279. `v1 Reply Files`_ it references.
  280. v1 Reply File Reference
  281. ^^^^^^^^^^^^^^^^^^^^^^^
  282. The reply index file represents each reference to another reply file
  283. using a JSON object with members:
  284. ``kind``
  285. A string specifying one of the `Object Kinds`_.
  286. ``version``
  287. A JSON object with members ``major`` and ``minor`` specifying
  288. integer version components of the object kind.
  289. ``jsonFile``
  290. A JSON string specifying a path relative to the reply index file
  291. to another JSON file containing the object.
  292. v1 Reply Files
  293. --------------
  294. Reply files containing specific `Object Kinds`_ are written by CMake.
  295. The names of these files are unspecified and must not be interpreted
  296. by clients. Clients must first read the `v1 Reply Index File`_ and
  297. and follow references to the names of the desired response objects.
  298. Reply files (including the index file) will never be replaced by
  299. files of the same name but different content. This allows a client
  300. to read the files concurrently with a running CMake that may generate
  301. a new reply. However, after generating a new reply CMake will attempt
  302. to remove reply files from previous runs that it did not just write.
  303. If a client attempts to read a reply file referenced by the index but
  304. finds the file missing, that means a concurrent CMake has generated
  305. a new reply. The client may simply start again by reading the new
  306. reply index file.
  307. Object Kinds
  308. ============
  309. The CMake file-based API reports semantic information about the build
  310. system using the following kinds of JSON objects. Each kind of object
  311. is versioned independently using semantic versioning with major and
  312. minor components. Every kind of object has the form:
  313. .. code-block:: json
  314. {
  315. "kind": "<kind>",
  316. "version": { "major": 1, "minor": 0 },
  317. "...": {}
  318. }
  319. The ``kind`` member is a string specifying the object kind name.
  320. The ``version`` member is a JSON object with ``major`` and ``minor``
  321. members specifying integer components of the object kind's version.
  322. Additional top-level members are specific to each object kind.