|
| 1 | +# Migration from .lzz to Standard C++ Files |
| 2 | + |
| 3 | +## Overview |
| 4 | +Migrate better-sqlite3 from using .lzz (Lazy C++) to standard .cpp/.hpp files. The current architecture uses #insert directives to create a single compilation unit, which requires a careful migration approach. |
| 5 | + |
| 6 | +## Critical Understanding |
| 7 | +- **Current Build**: All .lzz files are processed as ONE unit through `src/better_sqlite3.lzz` |
| 8 | +- **#insert != #include**: The #insert directive literally inserts file contents during preprocessing |
| 9 | +- **Single .cpp Output**: Currently produces only `better_sqlite3.cpp` and `better_sqlite3.hpp` |
| 10 | + |
| 11 | +## Migration Approach: Option A (Recommended - Low Risk) |
| 12 | +Preserve the single compilation unit architecture while removing lzz dependency. |
| 13 | + |
| 14 | +### Phase 1: Preparation |
| 15 | +- [ ] Create a backup branch for safety |
| 16 | +- [ ] Document the current #insert order from `better_sqlite3.lzz` |
| 17 | +- [ ] Verify lzz is installed and working |
| 18 | + |
| 19 | +### Phase 2: Generate Baseline |
| 20 | +- [ ] Run lzz WITHOUT #line directives: |
| 21 | + ```bash |
| 22 | + lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -e ./src/better_sqlite3.lzz |
| 23 | + ``` |
| 24 | +- [ ] Backup the generated files: |
| 25 | + ```bash |
| 26 | + cp src/better_sqlite3.hpp src/better_sqlite3.hpp.generated |
| 27 | + cp src/better_sqlite3.cpp src/better_sqlite3.cpp.generated |
| 28 | + ``` |
| 29 | +- [ ] Verify the build still works with these generated files |
| 30 | + |
| 31 | +### Phase 3: Split Generated Code |
| 32 | +- [ ] Create directory structure for split files: |
| 33 | + ```bash |
| 34 | + mkdir -p src/util src/objects |
| 35 | + ``` |
| 36 | +- [ ] Manually extract code from generated files into individual .hpp files: |
| 37 | + - [ ] `src/util/macros.hpp` - Extract macro definitions |
| 38 | + - [ ] `src/util/query-macros.hpp` - Extract query macros |
| 39 | + - [ ] `src/util/constants.hpp` - Extract constants |
| 40 | + - [ ] `src/util/bind-map.hpp` - Extract BindMap class |
| 41 | + - [ ] `src/objects/database.hpp` - Extract Database class |
| 42 | + - [ ] `src/objects/statement.hpp` - Extract Statement class |
| 43 | + - [ ] `src/objects/statement-iterator.hpp` - Extract StatementIterator |
| 44 | + - [ ] `src/objects/backup.hpp` - Extract Backup class |
| 45 | + - [ ] `src/util/data-converter.hpp` - Extract data conversion utilities |
| 46 | + - [ ] `src/util/custom-function.hpp` - Extract custom function code |
| 47 | + - [ ] `src/util/custom-aggregate.hpp` - Extract custom aggregate code |
| 48 | + - [ ] `src/util/custom-table.hpp` - Extract custom table code |
| 49 | + - [ ] `src/util/data.hpp` - Extract data handling code |
| 50 | + - [ ] `src/util/binder.hpp` - Extract binder utilities |
| 51 | + |
| 52 | +### Phase 4: Create New Main Files |
| 53 | +- [ ] Create new `src/better_sqlite3.hpp` that includes all headers in correct order: |
| 54 | + ```cpp |
| 55 | + #pragma once |
| 56 | + #include <climits> |
| 57 | + #include <cstdio> |
| 58 | + // ... other system includes ... |
| 59 | + |
| 60 | + #include "util/macros.hpp" |
| 61 | + #include "util/query-macros.hpp" |
| 62 | + #include "util/constants.hpp" |
| 63 | + #include "util/bind-map.hpp" |
| 64 | + // Forward declarations |
| 65 | + struct Addon; |
| 66 | + class Statement; |
| 67 | + class Backup; |
| 68 | + #include "objects/database.hpp" |
| 69 | + #include "objects/statement.hpp" |
| 70 | + #include "objects/statement-iterator.hpp" |
| 71 | + #include "objects/backup.hpp" |
| 72 | + #include "util/data-converter.hpp" |
| 73 | + #include "util/custom-function.hpp" |
| 74 | + #include "util/custom-aggregate.hpp" |
| 75 | + #include "util/custom-table.hpp" |
| 76 | + #include "util/data.hpp" |
| 77 | + #include "util/binder.hpp" |
| 78 | + // Addon struct definition |
| 79 | + ``` |
| 80 | +- [ ] Update `src/better_sqlite3.cpp` to use the new header structure |
| 81 | + |
| 82 | +### Phase 5: Verify and Test |
| 83 | +- [ ] Build the project: `npm run build-release` |
| 84 | +- [ ] Run the test suite: `npm test` |
| 85 | +- [ ] Run benchmarks to ensure no performance regression |
| 86 | +- [ ] Check that all exports work correctly |
| 87 | + |
| 88 | +### Phase 6: Update Build System |
| 89 | +- [ ] Update `package.json` to remove lzz script |
| 90 | +- [ ] Update documentation to remove lzz references |
| 91 | +- [ ] Update contribution guide |
| 92 | + |
| 93 | +### Phase 7: Cleanup |
| 94 | +- [ ] Once all tests pass, remove .lzz files |
| 95 | +- [ ] Remove lzz from development dependencies |
| 96 | +- [ ] Create a PR with the changes |
| 97 | + |
| 98 | +## Alternative: Option B (Future Enhancement) |
| 99 | +Convert to proper modular architecture with separate compilation units. |
| 100 | + |
| 101 | +### Additional Steps for Option B: |
| 102 | +- [ ] Analyze dependencies between modules |
| 103 | +- [ ] Add proper .cpp files for each module |
| 104 | +- [ ] Update binding.gyp to include all .cpp files |
| 105 | +- [ ] Resolve circular dependencies |
| 106 | +- [ ] Enable parallel compilation |
| 107 | + |
| 108 | +## Rollback Plan |
| 109 | +If issues arise: |
| 110 | +1. Git reset to backup branch |
| 111 | +2. Restore original .lzz files |
| 112 | +3. Rebuild with lzz |
| 113 | + |
| 114 | +## Success Criteria |
| 115 | +- [ ] All tests pass |
| 116 | +- [ ] No performance regression |
| 117 | +- [ ] No lzz dependency |
| 118 | +- [ ] Clean build without warnings |
| 119 | +- [ ] Documentation updated |
0 commit comments