annotate src/capnproto-git-20161025/doc/capnp-tool.md @ 55:284acf908dcd

Add source for PortAudio stable v190600_20161030
author Chris Cannam
date Tue, 03 Jan 2017 13:44:07 +0000
parents 9530b331f8c1
children
rev   line source
cannam@48 1 ---
cannam@48 2 layout: page
cannam@48 3 title: The capnp Tool
cannam@48 4 ---
cannam@48 5
cannam@48 6 # The `capnp` Tool
cannam@48 7
cannam@48 8 Cap'n Proto comes with a command-line tool called `capnp` intended to aid development and
cannam@48 9 debugging. This tool can be used to:
cannam@48 10
cannam@48 11 * Compile Cap'n Proto schemas to produce source code in multiple languages.
cannam@48 12 * Generate unique type IDs.
cannam@48 13 * Decode Cap'n Proto messages to human-readable text.
cannam@48 14 * Encode text representations of Cap'n Proto messages to binary.
cannam@48 15 * Evaluate and extract constants defined in Cap'n Proto schemas.
cannam@48 16
cannam@48 17 This page summarizes the functionality. A complete reference on the command's usage can be
cannam@48 18 found by typing:
cannam@48 19
cannam@48 20 capnp help
cannam@48 21
cannam@48 22 ## Compiling Schemas
cannam@48 23
cannam@48 24 capnp compile -oc++ myschema.capnp
cannam@48 25
cannam@48 26 This generates files `myschema.capnp.h` and `myschema.capnp.c++` which contain C++ source code
cannam@48 27 corresponding to the types defined in `myschema.capnp`. Options exist to control output location
cannam@48 28 and import paths.
cannam@48 29
cannam@48 30 The above example generates C++ code, but the tool is able to generate output in any language
cannam@48 31 for which a plugin is available. Compiler plugins are just regular programs named
cannam@48 32 `capnpc-language`. For example, the above command runs `capnpc-c++`. [More on how to write
cannam@48 33 compiler plugins](otherlang.html#how-to-write-compiler-plugins).
cannam@48 34
cannam@48 35 Note that some Cap'n Proto implementations (especially for interpreted languages) do not require
cannam@48 36 generating source code.
cannam@48 37
cannam@48 38 ## Decoding Messages
cannam@48 39
cannam@48 40 capnp decode myschema.capnp MyType < message.bin > message.txt
cannam@48 41
cannam@48 42 `capnp decode` reads a binary Cap'n Proto message from standard input and decodes it to a
cannam@48 43 human-readable text format (specifically, the format used for specifying constants and default
cannam@48 44 values in [the schema language](language.html)). By default it
cannam@48 45 expects an unpacked message, but you can decode a
cannam@48 46 [packed](encoding.html#packing) message with the `--packed` flag.
cannam@48 47
cannam@48 48 ## Encoding Messages
cannam@48 49
cannam@48 50 capnp encode myschema.capnp MyType < message.txt > message.bin
cannam@48 51
cannam@48 52 `capnp encode` is the opposite of `capnp decode`: it takes a text-format message on stdin and
cannam@48 53 encodes it to binary (possibly [packed](encoding.html#packing),
cannam@48 54 with the `--packed` flag).
cannam@48 55
cannam@48 56 This is mainly useful for debugging purposes, to build test data or to apply tweaks to data
cannam@48 57 decoded with `capnp decode`. You should not rely on `capnp encode` for encoding data written
cannam@48 58 and maintained in text format long-term -- instead, use `capnp eval`, which is much more powerful.
cannam@48 59
cannam@48 60 ## Evaluating Constants
cannam@48 61
cannam@48 62 capnp eval myschema.capnp myConstant
cannam@48 63
cannam@48 64 This prints the value of `myConstant`, a [const](language.html#constants) declaration, after
cannam@48 65 applying variable substitution. It can also output the value in binary format (`--binary` or
cannam@48 66 `--packed`).
cannam@48 67
cannam@48 68 At first glance, this may seem no more interesting that `capnp encode`: the syntax used to define
cannam@48 69 constants in schema files is the same as the format accepted by `capnp encode`, right? There is,
cannam@48 70 however, a big difference: constants in schema files may be defined in terms of other constants,
cannam@48 71 which may even be imported from other files.
cannam@48 72
cannam@48 73 As a result, `capnp eval` is a great basis for implementing config files. For example, a large
cannam@48 74 company might maintain a production server that serves dozens of clients and needs configuration
cannam@48 75 information about each one. Rather than maintaining the config as one enormous file, it can be
cannam@48 76 written as several separate files with a master file that imports the rest.
cannam@48 77
cannam@48 78 Such a configuration should be compiled to binary format using `capnp eval` before deployment,
cannam@48 79 in order to verify that there are no errors and to make deployment easier and faster. While you
cannam@48 80 could technically ship the text configs to production and have the servers parse them directly
cannam@48 81 (e.g. with `capnp::SchemaParser`), encoding before deployment is more efficient and robust.