rust.rst 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. .. _rust:
  2. ==============================
  3. Including Rust Code in Firefox
  4. ==============================
  5. The build system has support for building and linking Rust crates.
  6. Rust code is built using ``cargo`` in the typical way, so it is
  7. straightforward to take an existing Rust crate and integrate it
  8. into Firefox.
  9. .. important::
  10. Rust code is not currently enabled by default in Firefox builds.
  11. This should change soon (`bug 1283898 <https://bugzilla.mozilla.org/show_bug.cgi?id=1283898>`_),
  12. but the option to build without Rust code will likely last a little longer
  13. (`bug 1284816 <https://bugzilla.mozilla.org/show_bug.cgi?id=1284816>`_),
  14. so Rust code cannot currently be used for required components.
  15. Linking Rust Crates into libxul
  16. ===============================
  17. Rust crates that you want to link into libxul should be listed in the
  18. ``dependencies`` section of `toolkit/library/rust/shared/Cargo.toml <https://dxr.mozilla.org/mozilla-central/source/toolkit/library/rust/shared/Cargo.toml>`_.
  19. You'll also need to add an ``extern crate`` reference to `toolkit/library/rust/shared/lib.rs <https://dxr.mozilla.org/mozilla-central/source/toolkit/library/rust/shared/lib.rs>`_.
  20. This ensures that the Rust code will be linked properly into libxul as well
  21. as the copy of libxul used for gtests.
  22. Linking Rust Crates into something else
  23. =======================================
  24. There currently is not any Rust code being linked into binaries other than
  25. libxul. If you would like to do so, you'll need to create a directory with
  26. a ``Cargo.toml`` file for your crate, and a ``moz.build`` file that contains:
  27. .. code-block:: python
  28. RustLibrary('crate_name')
  29. Where *crate_name* matches the name from the ``[package]`` section of your
  30. ``Cargo.toml``. You can refer to `the moz.build file <https://dxr.mozilla.org/mozilla-central/rev/3f4c3a3cabaf94958834d3a8935adfb4a887942d/toolkit/library/rust/moz.build#7>`_ and `the Cargo.toml file <https://dxr.mozilla.org/mozilla-central/rev/3f4c3a3cabaf94958834d3a8935adfb4a887942d/toolkit/library/rust/Cargo.toml>`_ that are used for libxul.
  31. You can then add ``USE_LIBS += ['crate_name']`` to the ``moz.build`` file
  32. that defines the binary as you would with any other library in the tree.
  33. .. important::
  34. You cannot link a Rust crate into an intermediate library that will wind
  35. up being linked into libxul. The build system enforces that only a single
  36. ``RustLibrary`` may be linked into a binary. If you need to do this, you
  37. will have to add a ``RustLibrary`` to link to any standalone binaries that
  38. link the intermediate library, and also add the Rust crate to the libxul
  39. dependencies as in `linking Rust Crates into libxul`_.
  40. Where Should I put my Crate?
  41. ============================
  42. If your crate's canonical home is mozilla-central, you can put it next to the
  43. other code in the module it belongs to.
  44. If your crate is mirrored into mozilla-central from another repository, and
  45. will not be actively developed in mozilla-central, you can simply list it
  46. as a ``crates.io``-style dependency with a version number, and let it be
  47. vendored into the ``third_party/rust`` directory.
  48. If your crate is mirrored into mozilla-central from another repository, but
  49. will be actively developed in both locations, you should send mail to the
  50. dev-builds mailing list to start a discussion on how to meet your needs.
  51. Crate dependencies
  52. ==================
  53. All dependencies for in-tree Rust crates are vendored into the
  54. ``third_party/rust`` directory. Currently if you add a dependency on a new
  55. crate you must run ``mach vendor rust`` to vendor the dependencies into
  56. that directory. In the future we hope to make it so that you only need to
  57. vendor the dependencies in order to build your changes in a CI push.