Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --output-directory to forc build and related commands #796

Merged
merged 14 commits into from
Feb 17, 2022

Conversation

mitchmindtree
Copy link
Contributor

@mitchmindtree mitchmindtree commented Feb 14, 2022

Closes #764, #765.

Users can now specify a directory in which all compiler output artifacts
will be placed by forc upon forc build.

When unspecified, --output-directory defaults to <project>/out/.

More specifically, artifacts are placed in <project>/out/<profile>
where the profile subdirectory is in anticipation of support for
multiple different build profiles. Currently "debug" is assumed.

TODO

  • Add --output-directory flag to forc build and related commands.
  • Create output directory if it doesn't exit.
  • On forc build write compiled output binary to <project>/out/<profile>/<project-name>.bin
  • Add out to sway repo .gitignore.
  • Reminder to check related PR disallow generating a JSON ABI for non-contracts #527.
  • On forc build write contract ABI JSON to <project>/out/<profile>/<project-name>-abi.json. Generate ABI json file when running forc build #764.
  • Consider updating forc test to first forc build to make it easier to ensure necessary contract ABI files are available.
  • Update examples to check for generated ABI in default output directory.
  • On forc init, create .gitignore with out and target.
  • Add forc clean that removes out and target.

@mitchmindtree mitchmindtree force-pushed the mitchmindtree/forc-out branch 2 times, most recently from 466d2c9 to 05bfc82 Compare February 14, 2022 12:46
This is a start at addressing #765.

Users can now specify a directory in which all compiler output artifacts
will be placed by `forc` upon `forc build`.

When unspecified, `--output-directory` defaults to `<project>/out/`.

More specifically, artifacts are placed in `<project>/out/<profile>`
where the `profile` subdirectory is in anticipation of support for
multiple different build profiles. Currently "debug" is assumed.
This refactors the bodies of the `compile_to_asm` and
`compile_to_bytecode` functions into `ast_to_asm` and `asm_to_bytecode`
respectively. This allows `forc` to compile the AST (for producing the
ABI JSON) and then re-use the compiled AST to complete the rest of
compilation to bytecode in the same pass.
This updates `forc build` and related commands to compile the JSON ABI
and write it to a `<project-name>-abi.json` file in the output
directory.
We may want to consider removing the dedicated `forc json-abi` command
altogether in favour of a `--print-json-abi` flag to `forc build` to
make it clear that the output comes directly from `forc build`.
This ensures the project can build before attempting to run tests.

This may also make it easier to assume a location for the contract JSON
ABI in tests.
Now that `cargo test` does `cargo build` first, it's safer to assume the
contract JSON ABI is located in the `out` directory.
This ensures that forc and cargo's output artifact directories are
discluded from git tracking by default.
Removes the default forc compiler output directory (`<project>/out`).

Also runs `cargo clean` to remove the `target` directory that gets
created when running `cargo test` via `forc test`.
@mitchmindtree mitchmindtree marked this pull request as ready for review February 15, 2022 09:52
@mitchmindtree
Copy link
Contributor Author

OK I think this is about ready to go!

See the commit messages for details about new additions since PR was opened.

otrho
otrho previously approved these changes Feb 15, 2022
Copy link
Contributor

@otrho otrho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, though I'm not familiar with how the JSON ABI stuff should work, so...

@@ -53,7 +53,7 @@ pub(crate) fn default_program() -> String {
r#"script;

fn main() {

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, this whitespace has annoyed me for yonks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg I did it again #766 (comment)

I'm honestly not intending to do this, my editor is just accidentally "fixing" this and I keep missing it lol (I'm gonna leave it unless someone requests otherwise)

Human-friendly formatting by default, useful for checking/debugging
contract/library ABIs.

This is applied to both the `forc build` JSON of the output directory,
as well as the `forc json-abi` output.

A `--minimize-json-abi` option is added in order to allow for achieving
the original behaviour.
Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mitchmindtree
Copy link
Contributor Author

Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Generate ABI json file when running forc build
5 participants