Purpose of no-codegen option on crystal build?

Viewed 95

What is purpose of no-codegen option when building crystal project ?

I have quite large codebase and when building without this option, it can take up to 20 seconds to build. When I use no-codegen option, it reduces build time to 6 seconds. But I don't understand can executable be debugged when that option is included (currently I debug using LLDB when building with --debug flag)?

2 Answers

In short, it allows to check things are alright, but without generating a binary.

In long, there are four main phases within the Crystal compiler:

  1. Typechecking and macro expansion
  2. Code generation to LLVM
  3. Optimization (by LLVM)
  4. Binary generation (by LLVM)

Each process takes some time. In the first phase, the compiler just checks all the types and expand the macros in your program. In the second phase, the dead code is thrown away, some other language-specific optimizations take place, and the program is generated as LLVM. In the fourth phase (only with --release), Crystal calls LLVM to peform many optimization of the generated code. In the last phase LLVM creates the platform-specific binary.

In your case, it's taking 6s to do typechecking, and ~14s to do all the other phases (but 3).

--no-codegen means no code is generated. It does not produce any binary.

The purpose of this flag is to check the validity of a program, without actually building it. This is useful when you can't execute the result anyway, then the binary generation can be skipped. For example, this is used for the code examples in the Crystal repo: They are built in CI with --no-codegen just to check that the code is valid, but verifying their execution is out of scope for automated tests.

Related