|
| 1 | +## Usage |
| 2 | + |
| 3 | +### Start a new project with Poac |
| 4 | + |
| 5 | +The `poac new` command lets you start a new Poac project: |
| 6 | + |
| 7 | +```console |
| 8 | +you:~$ poac new hello_world |
| 9 | + Created binary (application) `hello_world` package |
| 10 | +``` |
| 11 | + |
| 12 | +> [!TIP] |
| 13 | +> If you want to integrate your existing project with Poac, use the `init` command: |
| 14 | +> |
| 15 | +> ```console |
| 16 | +> you:~/your-pj$ poac init |
| 17 | +> Created binary (application) `your-pj` package |
| 18 | +> ``` |
| 19 | +> |
| 20 | +> This command just creates a `poac.toml` file not to break your project. |
| 21 | +
|
| 22 | +### Build the project |
| 23 | +
|
| 24 | +In most cases, you want to execute the generated binary as well as build the project. |
| 25 | +
|
| 26 | +```console |
| 27 | +you:~/hello_world$ poac run |
| 28 | + Compiling src/main.cc |
| 29 | + Linking hello_world |
| 30 | + Finished debug target(s) in 0.45386s |
| 31 | + Running poac-out/debug/hello_world |
| 32 | +Hello, world! |
| 33 | +``` |
| 34 | +
|
| 35 | +If you just want to build it, run the `build` command: |
| 36 | + |
| 37 | +```console |
| 38 | +you:~/hello_world$ poac build |
| 39 | + Finished debug target(s) in 0.00866317s |
| 40 | +``` |
| 41 | + |
| 42 | +Poac uses a cache since we executed the command with no changes. |
| 43 | + |
| 44 | +> [!TIP] |
| 45 | +> To use a different compiler, you can export a `CXX` environmental variable: |
| 46 | +> |
| 47 | +> ```shell |
| 48 | +> export CXX=g++-13 |
| 49 | +> ``` |
| 50 | +
|
| 51 | +### Install dependencies |
| 52 | +
|
| 53 | +Like Cargo does, Poac installs dependencies at build time. Poac currently supports Git and system dependencies. You can use the `poac add` command to add dependencies to your project. |
| 54 | +
|
| 55 | +The `poac add` command accepts the following arguments: |
| 56 | +
|
| 57 | +`poac add <package names ....> --<options>` |
| 58 | +
|
| 59 | +Options: |
| 60 | +- `--sys`: Marks the packages as system dependency (requires the `--version` argument) |
| 61 | +- `--version`: Specify dependency version. Only used with system dependencies |
| 62 | +- `--rev`: Specify revision for git dependencies |
| 63 | +- `--tag`: Specify tag for git dependencies |
| 64 | +- `--branch`: Specify branch for git dependencies |
| 65 | +
|
| 66 | +Example |
| 67 | +```bash |
| 68 | +poac add libgit2 --sys --version "1.1.0" |
| 69 | +poac add "ToruNiina/toml11" --rev "846abd9a49082fe51440aa07005c360f13a67bbf" |
| 70 | +``` |
| 71 | +
|
| 72 | +If `tag`, `branch`, or `rev` is unspecified for git dependencies, Poac will use the latest revision of the default branch. System dependency names must be acceptable by `pkg-config`. The version requirement syntax is specified in [src/VersionReq.hpp](https://github.com/poac-dev/poac/blob/main/src/VersionReq.hpp). |
| 73 | + |
| 74 | +After adding dependencies, executing the `build` command will install the package and its dependencies. |
| 75 | + |
| 76 | +```console |
| 77 | +you:~/hello_world$ poac build |
| 78 | +Downloaded ToruNiina/toml11 846abd9a49082fe51440aa07005c360f13a67bbf |
| 79 | + Compiling src/main.cc |
| 80 | + Linking hello_world |
| 81 | + Finished debug target(s) in 0.70s |
| 82 | +``` |
| 83 | + |
| 84 | +> [!WARNING] |
| 85 | +> Poac currently supports building a project with header-only dependencies. |
| 86 | +> Building with build-required dependencies will be soon supported. |
| 87 | +
|
| 88 | +### Unit tests |
| 89 | + |
| 90 | +You can write unit tests in any source files within the `src` directory. Create a new file like: |
| 91 | + |
| 92 | +`src/Lib.cc` |
| 93 | + |
| 94 | +```cpp |
| 95 | +int add(int a, int b) { |
| 96 | + return a + b; |
| 97 | +} |
| 98 | + |
| 99 | +#ifdef POAC_TEST |
| 100 | + |
| 101 | +# include <cassert> |
| 102 | + |
| 103 | +int main() { |
| 104 | + assert(add(1, 2) == 3); // ok |
| 105 | + assert(add(1, 2) == 4); // fail |
| 106 | +} |
| 107 | + |
| 108 | +#endif |
| 109 | +``` |
| 110 | +
|
| 111 | +Now, with the `test` command, you can run tests defined within `POAC_TEST`: |
| 112 | +
|
| 113 | +```console |
| 114 | +you:~/hello_world$ poac test |
| 115 | + Compiling src/Lib.cc |
| 116 | + Linking tests/test_Lib |
| 117 | + Testing Lib |
| 118 | +Assertion failed: (add(1, 2) == 4), function main, file Lib.cc, line 13. |
| 119 | +make: *** [test] Abort trap: 6 |
| 120 | +``` |
| 121 | + |
| 122 | +Unit tests with the `POAC_TEST` macro are useful when testing private functions. Integration testing with the `tests` directory has not yet been implemented. |
| 123 | + |
| 124 | +### Run linter |
| 125 | + |
| 126 | +Linting source code is essential to protect its quality. Poac supports linting your project by the `lint` command: |
| 127 | + |
| 128 | +```console |
| 129 | +you:~/hello_world$ poac lint |
| 130 | + Linting hello_world |
| 131 | +src/main.cc:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5] |
| 132 | +Done processing src/main.cc |
| 133 | +Total errors found: 1 |
| 134 | +Error: `cpplint` exited with status 1 |
| 135 | +``` |
| 136 | + |
| 137 | +> [!TIP] |
| 138 | +> If you do not have `cpplint`, install it with the following command: |
| 139 | +> |
| 140 | +> ```bash |
| 141 | +> pip install cpplint |
| 142 | +> ``` |
| 143 | +
|
| 144 | +The `lint` command works without configurations, and Poac would automatically opt out of unwanted lints by adjusting to each project. |
| 145 | +To customize the lint settings, try adding the `[lint.cpplint]` key in your `poac.toml` like [this](https://github.com/poac-dev/poac/blob/cc30b706fb49860903384df56d650a0955aca16c/poac.toml#L67-L83) |
| 146 | +or creating a [`CPPLINT.cfg`](https://github.com/poac-dev/poac/blob/5e7e3792e8818d165149214e94f30958fb0fef66/CPPLINT.cfg) file in the repository root. |
| 147 | +
|
| 148 | +### Run formatter |
| 149 | +
|
| 150 | +Poac also supports formatting your source code with `clang-format`. Ensure having installed `clang-format` before running this command. |
| 151 | +
|
| 152 | +```console |
| 153 | +you:~/hello_world$ poac fmt |
| 154 | + Formatting hello_world |
| 155 | +``` |
| 156 | +
|
| 157 | +> [!NOTE] |
| 158 | +> This command automatically detects what files we need to format to avoid getting bothered by commands like: |
| 159 | +> |
| 160 | +> ```console |
| 161 | +> $ # We need to avoid the `build` dir and such dirs ... |
| 162 | +> $ clang-format ./src/*.cpp -i |
| 163 | +> $ clang-format ./include/**/*.hpp -i |
| 164 | +> $ clang-format ./tests/**/*.cpp -i |
| 165 | +> $ ... |
| 166 | +> ``` |
| 167 | +
|
| 168 | +To customize the format settings, try creating a [`.clang-format`](https://github.com/poac-dev/poac/blob/main/.clang-format) file to the repository root. |
| 169 | +
|
| 170 | +### Run `clang-tidy` |
| 171 | +
|
| 172 | +Poac also supports running `clang-tidy` on your source code. Ensure having installed `clang-tidy` before running this command. |
| 173 | +
|
| 174 | +```console |
| 175 | +you:~/hello_world$ poac tidy |
| 176 | + Running clang-tidy |
| 177 | +``` |
| 178 | +
|
| 179 | +You can customize the tidy settings by creating a [`.clang-tidy`](https://github.com/poac-dev/poac/blob/main/.clang-tidy) file to the repository root. |
0 commit comments