running.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. Using Solang on the command line
  2. ================================
  3. The Solang compiler is run on the command line. The solidity source file
  4. names are provided as command line arguments; the output is an optimized
  5. WebAssembly or Solana SBF file which is ready for deployment on a chain, and an metadata
  6. file (also known as the abi).
  7. The following targets are supported right now:
  8. `Solana <https://www.solana.com/>`_ and
  9. `Polkadot <https://substrate.io/>`_ (via the ``contracts`` pallet runtime).
  10. Solang supports auto-completion for multiple shells. Use ``solang shell-complete --help`` to
  11. learn whether your favorite shell is supported. If so, evaluate the output of
  12. ``solang shell-complete <SHELL>`` in order to activate it. Example installation with ``bash``:
  13. .. code-block:: bash
  14. echo 'source <(solang shell-complete bash)' >> ~/.bashrc
  15. Compiler Usage
  16. ______________
  17. solang compile [OPTIONS]... [SOLIDITY SOURCE FILE]...
  18. This means that the command line is ``solang compile`` followed by any options described below,
  19. followed by one or more solidity source filenames.
  20. Options:
  21. -v, \-\-verbose
  22. Make the output more verbose. The compiler tell you what contracts have been
  23. found in the source, and what files are generated. Without this option Solang
  24. will be silent if there are no errors or warnings.
  25. \-\-target *target*
  26. This takes one argument, which can either be ``solana`` or ``polkadot``. The target
  27. must be specified.
  28. \-\-address\-length *length-in-bytes*
  29. Change the default address length on Polkadot. By default, Substate uses an address type of 32 bytes. This option
  30. is ignored for any other target.
  31. \-\-value\-length *length-in-bytes*
  32. Change the default value length on Polkadot. By default, Substate uses an value type of 16 bytes. This option
  33. is ignored for any other target.
  34. -o, \-\-output *directory*
  35. Sets the directory where the output should be saved. This defaults to the current working directory if not set.
  36. \-\-output\-meta *directory*
  37. Sets the directory where metadata should be saved. For Solana, the metadata is the Anchor IDL file,
  38. and, for Polkadot, the .contract file. If this option is not set, the directory specified by ``--output``
  39. is used, and if that is not set either, the current working directory is used.
  40. \-\-contract *contract-name* [, *contract-name*]...
  41. Only compile the code for the specified contracts. If any those contracts cannot be found, produce an error.
  42. -O *optimization level*
  43. This takes one argument, which can either be ``none``, ``less``, ``default``,
  44. or ``aggressive``. These correspond to llvm optimization levels.
  45. \-\-importpath *directory*
  46. When resolving ``import`` directives, search this directory. By default ``import``
  47. will only search the current working directory. This option can be specified multiple times
  48. and the directories will be searched in the order specified.
  49. \-\-importmap *map=directory*
  50. When resolving ``import`` directives, if the first part of the path matches *map*,
  51. search the directory provided for the file. This option can be specified multiple times
  52. with different values for map.
  53. \-\-help, -h
  54. This displays a short description of all the options
  55. \-\-standard-json
  56. This option causes Solang to emulate the behaviour of Solidity
  57. `standard json output <https://solidity.readthedocs.io/en/v0.5.13/using-the-compiler.html#output-description>`_. No output files are written, all the
  58. output will be in json on stdout.
  59. \-\-emit *phase*
  60. This option is can be used for debugging Solang itself. This is used to
  61. output early phases of compilation.
  62. Phase:
  63. ast-dot
  64. Output Abstract Syntax Tree as a graphviz dot file. This can be viewed with xdot
  65. or any other tool that can visualize graphviz dot files.
  66. cfg
  67. Output control flow graph.
  68. llvm-ir
  69. Output llvm IR as text.
  70. llvm-bc
  71. Output llvm bitcode as binary file.
  72. asm
  73. Output assembly text file.
  74. object
  75. Output wasm object file; this is the contract before final linking.
  76. \-\-no\-constant\-folding
  77. Disable the :ref:`constant-folding` codegen optimization
  78. \-\-no\-strength\-reduce
  79. Disable the :ref:`strength-reduce` codegen optimization
  80. \-\-no\-dead\-storage
  81. Disable the :ref:`dead-storage` optimization
  82. \-\-no\-vector\-to\-slice
  83. Disable the :ref:`vector-to-slice` optimization
  84. \-\-no\-cse
  85. Disable the :ref:`common-subexpression-elimination` optimization
  86. \-\-no\-log\-api\-return\-codes
  87. Disable the :ref:`no-log-api-return-codes` debugging feature
  88. \-\-no\-log\-runtime\-errors
  89. Disable the :ref:`no-log-runtime-errors` debugging feature
  90. \-\-no\-prints
  91. Disable the :ref:`no-print` debugging feature
  92. \-\-release
  93. Disable all debugging features for :ref:`release`
  94. \-\-config-file
  95. Read compiler configurations from a ``.toml`` file. The minimal fields required in the configuration file are:
  96. .. code-block:: toml
  97. [package]
  98. input_files = ["flipper.sol"] # Solidity files to compile
  99. [target]
  100. name = "solana" # Target name
  101. Fields not explicitly present in the .toml acquire the compiler's default value.
  102. If any other argument is provided in the command line, for example, ``solang compile --config-file --target polkadot``, the argument will be overridden.
  103. The priority for the args is given as follows:
  104. 1. Command line
  105. 2. Configuration file
  106. 3. Default values.
  107. The default name for the toml file is "solang.toml". If two configuration files exist in the same directory, priority will be given to the one passed explicitly to this argument.
  108. \-\-wasm-opt
  109. wasm-opt passes for Wasm targets (0, 1, 2, 3, 4, s or z; see the wasm-opt help for more details).
  110. \-\-contract-authors
  111. Specify authors for all contracts. If a `@author` tag is present, it will override this argument for the targeted contract.
  112. For specifying multiple authors, use this format: `--contract-authors author1,author2,..`
  113. .. note::
  114. This will only affect the metadata in case of substrate target.
  115. \-\-version
  116. Specify contracts version. According to `semver <https://semver.org/>`_, a normal version number must take the form X.Y.Z where X, Y, and Z are non-negative integers, and must not contain leading zeroes.
  117. .. warning::
  118. If multiple Solidity source files define the same contract name, you will get a single
  119. compiled contract file for this contract name. As a result, you will only have a single
  120. contract with the duplicate name without knowing from which Solidity file it originated.
  121. Solang will not give a warning about this problem.
  122. Starting a new project
  123. ______________________________
  124. solang new \-\-target solana my_project
  125. A solang project is a directory in which there are one or more solidity files and a ``solang.toml`` file where
  126. the compilation options are defined. Given these two components, a user can run ``solang compile`` in a similar fashion as ``cargo build``.
  127. The ``solang new`` command creates a new solang project with an example `flipper <https://github.com/hyperledger/solang/blob/main/examples/solana/flipper.sol>`_ contract,
  128. and a default ``solang.toml`` configuration file.
  129. Generating Documentation Usage
  130. ______________________________
  131. Generate documentation for the given Solidity files as a single html page. This uses the
  132. doccomment tags. The result is saved in ``soldoc.html``. See :ref:`tags` for
  133. further information.
  134. solang doc [OPTIONS]... [SOLIDITY SOURCE FILE]...
  135. This means that the command line is ``solang doc`` followed by any options described below,
  136. followed by one or more solidity source filenames.
  137. Options:
  138. \-\-target *target*
  139. This takes one argument, which can either be ``solana`` or ``polkadot``. The target
  140. must be specified.
  141. \-\-address\-length *length-in-bytes*
  142. Change the default address length on Polkadot. By default, Substate uses an address type of 32 bytes. This option
  143. is ignored for any other target.
  144. \-\-value\-length *length-in-bytes*
  145. Change the default value length on Polkadot. By default, Substate uses an value type of 16 bytes. This option
  146. is ignored for any other target.
  147. \-\-importpath *directory*
  148. When resolving ``import`` directives, search this directory. By default ``import``
  149. will only search the current working directory. This option can be specified multiple times
  150. and the directories will be searched in the order specified.
  151. \-\-importmap *map=directory*
  152. When resolving ``import`` directives, if the first part of the path matches *map*,
  153. search the directory provided for the file. This option can be specified multiple times
  154. with different values for map.
  155. \-\-help, -h
  156. This displays a short description of all the options
  157. .. _idl_command:
  158. Generate Solidity interface from IDL
  159. ____________________________________
  160. This command converts Anchor IDL into Solidity import files, so they can be used to call
  161. Anchor Programs from Solidity.
  162. solang idl [--output DIR] [IDLFILE]...
  163. For each idl file provided, a Solidity file is written. See :ref:`call_anchor`
  164. for an example of how to use this.
  165. .. note::
  166. There is only supported on Solana.
  167. Running Solang using a container
  168. ________________________________
  169. First pull the last Solang container from
  170. `solang containers <https://github.com/hyperledger/solang/pkgs/container/solang>`_:
  171. .. code-block:: bash
  172. docker image pull ghcr.io/hyperledger/solang
  173. And if you are using podman:
  174. .. code-block:: bash
  175. podman image pull ghcr.io/hyperledger/solang
  176. Now you can run Solang like so:
  177. .. code-block:: bash
  178. docker run --rm -it ghcr.io/hyperledger/solang --version
  179. Or podman:
  180. .. code-block:: bash
  181. podman container run --rm -it ghcr.io/hyperledger/solang --version
  182. If you want to compile some Solidity files, the source files need to be
  183. available inside the container. You can do this via the ``-v`` docker command line.
  184. In this example ``/local/path`` should be replaced with the absolute path
  185. to your solidity files:
  186. .. code-block:: bash
  187. docker run --rm -it -v /local/path:/sources ghcr.io/hyperledger/solang compile -o /sources /sources/flipper.sol
  188. On Windows, you need to specify absolute paths:
  189. .. code-block:: text
  190. docker run --rm -it -v C:\Users\User:/sources ghcr.io/hyperledger/solang compile -o /sources /sources/flipper.sol