# 🚀 Windows Build WITHOUT Visual Studio

**Build SecondVoice using MinGW (GCC) - Only ~500MB instead of 10GB+!**

---

## ⚡ Why MinGW?

| | Visual Studio | MinGW |
|---|---|---|
| **Size** | 10-20 GB | ~500 MB |
| **Install Time** | 30-60 min | 5-10 min |
| **Compiler** | MSVC | GCC |
| **Result** | SecondVoice.exe | SecondVoice.exe |
| **Performance** | Same | Same |

**Conclusion**: MinGW is **20x smaller** and works perfectly! 🎉

---

## 📦 One-Click Setup (5 minutes)

### Step 1: Run Setup Script

Open **PowerShell as Administrator** and run:

```powershell
cd "E:\Users\Alexis Trouvé\Documents\Projets\secondvoice"
.\setup_mingw.bat
```

This will automatically install:
- ✅ Chocolatey (package manager)
- ✅ MinGW-w64 (GCC compiler)
- ✅ CMake (build system)
- ✅ Ninja (build tool)
- ✅ Git (if not installed)
- ✅ vcpkg (dependency manager)

**Total size**: ~500MB
**Install time**: 5-10 minutes

### Step 2: Restart Terminal

**Important**: Close and reopen PowerShell to reload PATH.

### Step 3: Create .env

```powershell
copy .env.example .env
notepad .env
```

Add your API keys:
```
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
```

### Step 4: Build!

```powershell
.\build_mingw.bat --release
```

### Step 5: Run!

```powershell
cd build\mingw-release
.\SecondVoice.exe
```

**Done!** 🎉

---

## 🔧 Manual Installation (Alternative)

If `setup_mingw.bat` doesn't work, install manually:

### 1. Install Chocolatey

Open **PowerShell as Administrator**:

```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
```

### 2. Install Tools

```powershell
choco install mingw cmake ninja git -y
```

### 3. Install vcpkg

```powershell
cd C:\
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
setx VCPKG_ROOT "C:\vcpkg"
```

### 4. Restart Terminal & Build

```powershell
# Close and reopen PowerShell
cd "E:\Users\Alexis Trouvé\Documents\Projets\secondvoice"
.\build_mingw.bat --release
```

---

## 🎯 Build Options

### Release Build (Optimized)
```powershell
.\build_mingw.bat --release
```

Output: `build\mingw-release\SecondVoice.exe`

### Debug Build (With symbols)
```powershell
.\build_mingw.bat --debug
```

Output: `build\mingw-debug\SecondVoice.exe`

### Clean Build (From scratch)
```powershell
.\build_mingw.bat --clean --release
```

---

## 📊 Comparison: MSVC vs MinGW

### Build Flags

| Compiler | Debug Flags | Release Flags |
|----------|-------------|---------------|
| MSVC (Visual Studio) | `/W4` | `/O2 /W4` |
| GCC (MinGW) | `-Wall -Wextra -g` | `-O3 -Wall -Wextra` |

### Optimizations

Both produce **highly optimized** executables:
- **MSVC `/O2`**: Full optimization
- **GCC `-O3`**: Aggressive optimization

**Performance**: Nearly identical in practice!

### Executable Size

- **MSVC build**: ~5-8 MB
- **MinGW build**: ~6-9 MB

Slightly larger with MinGW due to static linking, but negligible difference.

---

## 🐛 Troubleshooting

### Error: "gcc not found"

**Solution**:
```powershell
# Check if MinGW installed
where gcc

# If not found, install:
choco install mingw -y

# Restart terminal
```

### Error: "VCPKG_ROOT not set"

**Solution**:
```powershell
setx VCPKG_ROOT "C:\vcpkg"
# Restart terminal
echo %VCPKG_ROOT%
```

### Error: "Chocolatey not recognized"

**Solution**: Not running as Administrator
1. Right-click PowerShell → **Run as Administrator**
2. Re-run `setup_mingw.bat`

### Build fails with "undefined reference"

**Cause**: vcpkg dependencies not built for MinGW

**Solution**:
```powershell
# Clean vcpkg cache
rmdir /s /q build\mingw-release\vcpkg_installed

# Rebuild
.\build_mingw.bat --clean --release
```

### Error: "Cannot find -lportaudio"

**Cause**: vcpkg toolchain issue

**Solution**:
```powershell
# Force vcpkg integration
%VCPKG_ROOT%\vcpkg integrate install

# Rebuild
.\build_mingw.bat --clean --release
```

---

## 🔍 Verify Installation

### Check Tools

```powershell
# GCC
gcc --version
# Should show: gcc (x86_64-win32-seh-rev...)

# CMake
cmake --version
# Should show: cmake version 3.x.x

# Ninja
ninja --version
# Should show: 1.x.x

# vcpkg
echo %VCPKG_ROOT%
# Should show: C:\vcpkg
```

### Test Build

```powershell
.\build_mingw.bat --release

# Should complete without errors
# Output: build\mingw-release\SecondVoice.exe
```

---

## 📦 Distribution

The `.exe` built with MinGW is **standalone** and can be distributed to any Windows 10/11 machine.

**What to include**:
```
SecondVoice/
├── SecondVoice.exe  (from build\mingw-release\)
├── config.json
└── .env.example
```

**User needs**:
- Windows 10/11
- Microphone
- Their own API keys in `.env`

**No need for**:
- MinGW
- Visual Studio
- CMake
- vcpkg

---

## 💡 Why This Works

### Static Linking

vcpkg builds all dependencies as **static libraries** by default on Windows:
- PortAudio: `libportaudio.a`
- ImGui: `libimgui.a`
- etc.

These are **linked into** `SecondVoice.exe`, so no DLLs needed!

### MinGW Runtime

The MinGW C++ runtime (`libstdc++`, `libgcc`) is also statically linked, making the `.exe` truly portable.

---

## 🚀 Performance Tips

### Faster Builds

```powershell
# Use all CPU cores (default)
.\build_mingw.bat --release

# Ninja automatically uses -j$(nproc)
```

### Smaller Executable

Add to `CMakeLists.txt` (optional):
```cmake
if(MINGW)
    target_link_options(SecondVoice PRIVATE -s)  # Strip symbols
endif()
```

Then rebuild:
```powershell
.\build_mingw.bat --clean --release
```

Result: Executable ~4-5 MB instead of ~6-9 MB.

---

## 📚 Resources

### MinGW-w64
- Website: https://www.mingw-w64.org/
- GCC Docs: https://gcc.gnu.org/onlinedocs/

### Chocolatey
- Website: https://chocolatey.org/
- Packages: https://community.chocolatey.org/packages

### vcpkg
- GitHub: https://github.com/microsoft/vcpkg
- Docs: https://vcpkg.io/

---

## 🎉 Summary

### Before (Visual Studio)
```
Download: 10-20 GB
Install: 30-60 min
Total: 1 hour+ setup
```

### After (MinGW)
```
Download: ~500 MB
Install: 5-10 min
Total: 15 min setup
```

**20x faster setup, same result!** 🚀

---

## 🔄 Switching Between MSVC and MinGW

You can have **both** installed:

**Build with Visual Studio**:
```powershell
.\build.bat --release
# Output: build\windows-release\Release\SecondVoice.exe
```

**Build with MinGW**:
```powershell
.\build_mingw.bat --release
# Output: build\mingw-release\SecondVoice.exe
```

Both work perfectly! Use whichever you prefer.

---

*Last updated: 20 novembre 2025*
*Recommended for: Developers who want lightweight, fast setup*
