running.rst 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\-runtime\-errors
  87. Disable the :ref:`no-log-runtime-errors` debugging feature
  88. \-\-no\-prints
  89. Disable the :ref:`no-print` debugging feature
  90. \-\-release
  91. Disable all debugging features for :ref:`release`
  92. \-\-config-file
  93. Read compiler configurations from a ``.toml`` file. The minimal fields required in the configuration file are:
  94. .. code-block:: toml
  95. [package]
  96. input_files = ["flipper.sol"] # Solidity files to compile
  97. [target]
  98. name = "solana" # Target name
  99. Fields not explicitly present in the .toml acquire the compiler's default value.
  100. If any other argument is provided in the command line, for example, ``solang compile --config-file --target polkadot``, the argument will be overridden.
  101. The priority for the args is given as follows:
  102. 1. Command line
  103. 2. Configuration file
  104. 3. Default values.
  105. 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.
  106. \-\-wasm-opt
  107. wasm-opt passes for Wasm targets (0, 1, 2, 3, 4, s or z; see the wasm-opt help for more details).
  108. \-\-contract-authors
  109. Specify authors for all contracts. If a `@author` tag is present, it will override this argument for the targeted contract.
  110. For specifying multiple authors, use this format: `--contract-authors author1,author2,..`
  111. .. note::
  112. This will only affect the metadata in case of substrate target.
  113. \-\-version
  114. 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.
  115. .. warning::
  116. If multiple Solidity source files define the same contract name, you will get a single
  117. compiled contract file for this contract name. As a result, you will only have a single
  118. contract with the duplicate name without knowing from which Solidity file it originated.
  119. Solang will not give a warning about this problem.
  120. Starting a new project
  121. ______________________________
  122. solang new \-\-target solana my_project
  123. A solang project is a directory in which there are one or more solidity files and a ``solang.toml`` file where
  124. the compilation options are defined. Given these two components, a user can run ``solang compile`` in a similar fashion as ``cargo build``.
  125. The ``solang new`` command creates a new solang project with an example `flipper <https://github.com/hyperledger-solang/solang/blob/main/examples/solana/flipper.sol>`_ contract,
  126. and a default ``solang.toml`` configuration file.
  127. Generating Documentation Usage
  128. ______________________________
  129. Generate documentation for the given Solidity files as a single html page. This uses the
  130. doccomment tags. The result is saved in ``soldoc.html``. See :ref:`tags` for
  131. further information.
  132. solang doc [OPTIONS]... [SOLIDITY SOURCE FILE]...
  133. This means that the command line is ``solang doc`` followed by any options described below,
  134. followed by one or more solidity source filenames.
  135. Options:
  136. \-\-target *target*
  137. This takes one argument, which can either be ``solana`` or ``polkadot``. The target
  138. must be specified.
  139. \-\-address\-length *length-in-bytes*
  140. Change the default address length on Polkadot. By default, Substate uses an address type of 32 bytes. This option
  141. is ignored for any other target.
  142. \-\-value\-length *length-in-bytes*
  143. Change the default value length on Polkadot. By default, Substate uses an value type of 16 bytes. This option
  144. is ignored for any other target.
  145. \-\-importpath *directory*
  146. When resolving ``import`` directives, search this directory. By default ``import``
  147. will only search the current working directory. This option can be specified multiple times
  148. and the directories will be searched in the order specified.
  149. \-\-importmap *map=directory*
  150. When resolving ``import`` directives, if the first part of the path matches *map*,
  151. search the directory provided for the file. This option can be specified multiple times
  152. with different values for map.
  153. \-\-help, -h
  154. This displays a short description of all the options
  155. .. _idl_command:
  156. Generate Solidity interface from IDL
  157. ____________________________________
  158. This command converts Anchor IDL into Solidity import files, so they can be used to call
  159. Anchor Programs from Solidity.
  160. solang idl [--output DIR] [IDLFILE]...
  161. For each idl file provided, a Solidity file is written. See :ref:`call_anchor`
  162. for an example of how to use this.
  163. .. note::
  164. There is only supported on Solana.
  165. Running Solang using a container
  166. ________________________________
  167. First pull the last Solang container from
  168. `solang containers <https://github.com/hyperledger-solang/solang/pkgs/container/solang>`_:
  169. .. code-block:: bash
  170. docker image pull ghcr.io/hyperledger/solang
  171. And if you are using podman:
  172. .. code-block:: bash
  173. podman image pull ghcr.io/hyperledger/solang
  174. Now you can run Solang like so:
  175. .. code-block:: bash
  176. docker run --rm -it ghcr.io/hyperledger/solang --version
  177. Or podman:
  178. .. code-block:: bash
  179. podman container run --rm -it ghcr.io/hyperledger/solang --version
  180. If you want to compile some Solidity files, the source files need to be
  181. available inside the container. You can do this via the ``-v`` docker command line.
  182. In this example ``/local/path`` should be replaced with the absolute path
  183. to your solidity files:
  184. .. code-block:: bash
  185. docker run --rm -it -v /local/path:/sources ghcr.io/hyperledger/solang compile -o /sources /sources/flipper.sol
  186. On Windows, you need to specify absolute paths:
  187. .. code-block:: text
  188. docker run --rm -it -v C:\Users\User:/sources ghcr.io/hyperledger/solang compile -o /sources /sources/flipper.sol