Skip to content

Add optimization level flags (-Oz, -O1, etc.) and refactor debug/release to profiles in moon build system #1378

@peter-jerry-ye

Description

@peter-jerry-ye

Motivation

Currently, the moon build system uses debug and release options, which function as presets or profiles, each bundling a set of underlying flags. However, there is no granular control over optimization levels, such as those seen in other language toolchains (e.g., -O0, -O1, -O2, -O3, -Os, -Oz in C/C++, Rust, Swift, etc.). This limits power users and advanced build scenarios who want to tune builds for size, speed, or debugging fidelity.

Detailed Description

Investigate how other programming language compilers/build systems expose optimization control and profiles:

Survey Summary

  • C/C++ (GCC/Clang): Supports flags like -O0 (none), -O1, -O2, -O3 (increasing optimization), -Os (optimize for size), -Oz (aggressive size optimization), and -Og (debugging with some optimizations).
  • Rust (Cargo): Profiles include dev and release, which compose flags like opt-level (0–3, s, z), debug, lto, etc., and allow further refinement in Cargo.toml ([profile.*] tables).
  • Swift (swiftc): Similar optimization flags: -O, -Ounchecked, no optimization (debug), and profile concept via Xcode schemes.
  • Zig: Uses build profiles (ReleaseSafe, ReleaseFast, ReleaseSmall, Debug), each mapped to underlying options, and also exposes individualized flags.

Recommended Approach for Moon

  1. Refactor debug/release as profiles, not exclusive build modes.
  2. Introduce explicit optimization level flags (e.g., -O0, -O1, -O2, -O3, -Os, -Oz), mirroring conventions from C/Rust.
  3. Allow users to compose their own profiles with sets of flags, possibly in a config file (akin to [profile.*] in Cargo).
  4. Document how default profiles (debug, release) map to specific flag sets and how custom ones can be constructed.
  5. Ensure integration with current build graph and artifact naming logic.
  6. Update documentation and dev references.

Benefits

  • Improved user control over build output (speed, size, debug info)
  • More familiar developer experience for those from C/C++/Rust/Swift backgrounds
  • Extensibility for future flags/profiles

Related Issues

N/A (initial proposal)

Checklist

  • I have added all necessary details to make it easy for the maintainers to understand my request.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions