|
| 1 | +# MaixCam & MaixCam2 Auto Build & Pack Script |
| 2 | +A bash script to **automatically compile MaixCDK projects for MaixCam and MaixCam2 platforms**, package the compiled binaries into a portable ZIP file, and generate a self-executing `main.py` for one-click deployment on both devices. |
| 3 | + |
| 4 | +## Features |
| 5 | +- **Two Compilation Modes**: Batch compile all valid projects in a directory, or compile a single project independently. |
| 6 | +- **Platform Selection**: Choose to build for MaixCam only, MaixCam2 only, or both platforms (default). |
| 7 | +- **Project Exclusion**: Configure an exclusion list to skip specific projects during batch compilation. |
| 8 | +- **Cross-Platform Support**: Compiles binaries for both MaixCam and MaixCam2 in one run. |
| 9 | +- **Smart Packaging**: |
| 10 | + - Separates platform-specific binaries into dedicated folders (`maixcam/`, `maixcam2/`). |
| 11 | + - Copies shared resources (assets, app.yaml, README) to the root of the package. |
| 12 | + - Generates an auto-executing `main.py` that **auto-detects the device model** (when building both) and runs the corresponding binary. |
| 13 | +- **Auto-Clean**: Clears compilation caches after each build to avoid version conflicts. |
| 14 | +- **Error Handling**: Stops execution on critical errors (batch mode skips failed projects and continues). |
| 15 | +- **Colorful Logs**: Clear visual feedback with colored info/warn/error messages for easy debugging. |
| 16 | +- **Timestamped ZIP**: Generates uniquely named ZIP packages with timestamps to avoid overwriting. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | +1. **MaixCDK Installed**: The script relies on the `maixcdk` command-line tool for compilation and cleaning. |
| 20 | + Install MaixCDK following the official guide: [MaixCDK Documentation](https://wiki.sipeed.com/maixcdk) |
| 21 | +2. **Bash Environment**: Works on Linux/macOS (native bash) or Windows (WSL2, Git Bash, or MSYS2). |
| 22 | +3. **Basic Dependencies**: Ensure `zip`, `find`, `sed` are installed (pre-installed on most Linux/macOS systems). |
| 23 | +4. **Valid MaixCDK Projects**: Projects must contain either `app.yaml` (MaixCDK app) or `CMakeLists.txt` (CMake-based project) to be recognized. |
| 24 | + |
| 25 | +## Quick Start |
| 26 | +### 1. Get the Script |
| 27 | +Save the script as `build_and_pack.sh` and grant execution permission: |
| 28 | +```bash |
| 29 | +chmod +x build_and_pack.sh |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Configure Exclusion List (Optional) |
| 33 | +Edit the script to exclude specific projects from batch compilation: |
| 34 | +```bash |
| 35 | +# Open the script and modify line 14-15 |
| 36 | +EXCLUDE_PROJECTS=("project1" "project2" "project3") |
| 37 | +``` |
| 38 | +Leave the array empty `EXCLUDE_PROJECTS=()` to compile all projects. |
| 39 | + |
| 40 | +### 3. Show Help Information |
| 41 | +```bash |
| 42 | +./build_and_pack.sh -h |
| 43 | +# or |
| 44 | +./build_and_pack.sh --help |
| 45 | +``` |
| 46 | + |
| 47 | +## Usage |
| 48 | +### Mode 1: Batch Compile (Default) |
| 49 | +Compile **all valid MaixCDK projects** in a target directory (skips non-project folders and a `build/` subdirectory if present). |
| 50 | +The compiled ZIP packages are output to a `build/` folder in the target directory. |
| 51 | + |
| 52 | +#### Syntax |
| 53 | +```bash |
| 54 | +./build_and_pack.sh [PROJECTS_DIRECTORY] [--platform maixcam|maixcam2|both] |
| 55 | +``` |
| 56 | +- `[PROJECTS_DIRECTORY]`: Optional, path to the folder containing all MaixCDK projects (defaults to the **current working directory** if not specified). |
| 57 | +- `--platform` or `-p`: Optional, specify target platform(s) to build (defaults to `both`). |
| 58 | + |
| 59 | +#### Examples |
| 60 | +```bash |
| 61 | +# Batch compile all projects in the current directory (both platforms) |
| 62 | +./build_and_pack.sh |
| 63 | + |
| 64 | +# Batch compile only for MaixCam |
| 65 | +./build_and_pack.sh --platform maixcam |
| 66 | + |
| 67 | +# Batch compile only for MaixCam2 |
| 68 | +./build_and_pack.sh --platform maixcam2 |
| 69 | + |
| 70 | +# Batch compile all projects in a specified directory for both platforms |
| 71 | +./build_and_pack.sh /root/MaixCDK/projects --platform both |
| 72 | +``` |
| 73 | + |
| 74 | +### Mode 2: Single Project Compile |
| 75 | +Compile a **single MaixCDK project** (outputs the ZIP package directly to the project directory by default). |
| 76 | + |
| 77 | +#### Syntax |
| 78 | +```bash |
| 79 | +./build_and_pack.sh --single [PROJECT_PATH] [--platform maixcam|maixcam2|both] |
| 80 | +# or short version |
| 81 | +./build_and_pack.sh -s [PROJECT_PATH] [-p maixcam|maixcam2|both] |
| 82 | +``` |
| 83 | +- `[PROJECT_PATH]`: Optional, path to the single MaixCDK project (defaults to the **current working directory** if not specified). |
| 84 | +- `--platform` or `-p`: Optional, specify target platform(s) to build (defaults to `both`). |
| 85 | + |
| 86 | +#### Examples |
| 87 | +```bash |
| 88 | +# Compile the project in the current directory (both platforms) |
| 89 | +./build_and_pack.sh --single |
| 90 | + |
| 91 | +# Compile only for MaixCam |
| 92 | +./build_and_pack.sh --single --platform maixcam |
| 93 | + |
| 94 | +# Compile a specified single project only for MaixCam2 |
| 95 | +./build_and_pack.sh --single /root/MaixCDK/projects/app_camera --platform maixcam2 |
| 96 | +``` |
| 97 | + |
| 98 | +## Output File Structure |
| 99 | +### Compiled ZIP Package Content |
| 100 | +The generated ZIP file (e.g., `app_camera_release_20260129_153000.zip`) structure depends on the platform selection: |
| 101 | + |
| 102 | +**When building both platforms (`--platform both` or default):** |
| 103 | +``` |
| 104 | +app_camera_release_20260129_153000/ |
| 105 | +├── main.py # Auto-execution script (auto-detects device & runs binary) |
| 106 | +├── app.yaml # Project configuration (shared) |
| 107 | +├── assets/ # Resource files (images/fonts, shared) |
| 108 | +├── README.md # Project documentation (shared) |
| 109 | +├── README_EN.md # English documentation (shared, if exists) |
| 110 | +├── maixcam/ # MaixCam-specific files |
| 111 | +│ ├── [binary] # Compiled binary for MaixCam |
| 112 | +│ └── dl_lib/ # Deep learning libraries (if exists) |
| 113 | +└── maixcam2/ # MaixCam2-specific files |
| 114 | + ├── [binary] # Compiled binary for MaixCam2 |
| 115 | + └── dl_lib/ # Deep learning libraries (if exists) |
| 116 | +``` |
| 117 | + |
| 118 | +**When building single platform (`--platform maixcam` or `--platform maixcam2`):** |
| 119 | +``` |
| 120 | +app_camera_release_20260129_153000/ |
| 121 | +├── main.py # Direct execution script (runs platform-specific binary) |
| 122 | +├── app.yaml # Project configuration |
| 123 | +├── assets/ # Resource files (images/fonts) |
| 124 | +├── README.md # Project documentation |
| 125 | +├── README_EN.md # English documentation (if exists) |
| 126 | +└── maixcam/ # Only the selected platform folder |
| 127 | + ├── [binary] # Compiled binary |
| 128 | + └── dl_lib/ # Deep learning libraries (if exists) |
| 129 | +``` |
| 130 | + |
| 131 | +### Key Output Files |
| 132 | +- **ZIP Package**: Named as `<PROJECT_NAME>_release_<TIMESTAMP>.zip` (timestamp format: `YYYYMMDD_HHMMSS`). |
| 133 | + - Batch mode: Stored in `[PROJECTS_DIRECTORY]/build/`. |
| 134 | + - Single mode: Stored in the project root directory by default. |
| 135 | +- **`main.py`**: The core execution script—**no manual modification needed** (auto-replaces the binary name during compilation). |
| 136 | + |
| 137 | +## How to Deploy on MaixCam/MaixCam2 |
| 138 | +1. Copy the generated ZIP file to the root directory of the MaixCam/MaixCam2 device (via SSH/SCP or SD card). |
| 139 | +2. Unzip the package on the device: |
| 140 | + ```bash |
| 141 | + unzip <PROJECT_NAME>_release_<TIMESTAMP>.zip -d <PROJECT_FOLDER> |
| 142 | + ``` |
| 143 | +3. Enter the project folder and run the script: |
| 144 | + ```bash |
| 145 | + cd <PROJECT_FOLDER> |
| 146 | + python main.py |
| 147 | + ``` |
| 148 | + The script will **automatically detect the device model** (MaixCam/MaixCam2), grant execute permission to the binary, and run it. |
| 149 | + |
| 150 | +## Script Workflow |
| 151 | +### For Single Project Compilation |
| 152 | +1. Clean up previous build outputs (if any). |
| 153 | +2. Based on `--platform` parameter: |
| 154 | + - `both` (default): Compile for **MaixCam** first, then **MaixCam2** |
| 155 | + - `maixcam`: Compile only for **MaixCam** |
| 156 | + - `maixcam2`: Compile only for **MaixCam2** |
| 157 | +3. Copy shared resources (assets, app.yaml, README) to the output directory. |
| 158 | +4. Generate platform-appropriate `main.py`: |
| 159 | + - For `both`: Auto-detects device and runs corresponding binary |
| 160 | + - For single platform: Directly runs the platform-specific binary |
| 161 | +5. Package all files into a timestamped ZIP and clean up temporary build files. |
| 162 | + |
| 163 | +### For Batch Compilation |
| 164 | +1. Create a `build/` directory for output ZIP packages. |
| 165 | +2. Traverse all subdirectories in the target folder, skip non-project folders, the `build/` directory, and projects in the exclusion list. |
| 166 | +3. Compile each valid project with the **single project workflow** using the specified platform, outputting ZIPs to the `build/` directory. |
| 167 | +4. Skip failed projects and continue compiling the rest. |
| 168 | +5. Print a summary (total/succeeded/failed projects) and list failed projects (if any). |
0 commit comments