From 3232fbd88398fd884e47d70add8d56f6b9a41f06 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Mon, 8 Apr 2024 13:06:44 -0700 Subject: [PATCH] Switch to Ninja (#140) * switch to ninja * update README * default jobs to 4 * make number of jobs configurable --- .github/workflows/lint.yml | 2 +- .github/workflows/test.yml | 2 +- CMakeLists.txt | 13 +++++++++++++ PreLoad.cmake | 2 +- README.md | 22 ++++++++++------------ 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 463640bd..75bf5072 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,4 +33,4 @@ jobs: run: cd "$HOME_DIR/obcpp" && mkdir build && cd build && sudo cmake -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" .. - name: Run cpplint - run: cd "$HOME_DIR/obcpp/build" && sudo make lint + run: cd "$HOME_DIR/obcpp/build" && sudo ninja lint diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8cfb0e69..a6a9a979 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,5 +35,5 @@ jobs: run: cd "$HOME_DIR/obcpp" && mkdir build && cd build && sudo cmake -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" .. - name: Run Tests - run: cd "$HOME_DIR/obcpp/build" && sudo make test + run: cd "$HOME_DIR/obcpp/build" && sudo ninja test -j 4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d839eed..77b41c05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,19 @@ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS}") SET(CMAKE_C_FLAGS "-D_GNU_SOURCE") +# Anecdotal Observations: +# four_jobs works with 12GB RAM +# eight_jobs requires more than 12GB RAM +# sixteen_jobs requires more than 16GB RAM (I don't know how much, since I only have 16GB) +# Therefore we default to 4 jobs, so it doesn't crash anyone's laptop with 12GB ram. +if (NOT DEFINED CMAKE_JOB_POOLS) + set_property(GLOBAL PROPERTY JOB_POOLS j=4) +else() + message("Using user defined number of pools: ${CMAKE_JOB_POOLS}") +endif() +set(CMAKE_JOB_POOL_COMPILE j) +set(CMAKE_JOB_POOL_LINK j) + project(obcpp VERSION 1.0) set(LOGURU_WITH_STREAMS TRUE) diff --git a/PreLoad.cmake b/PreLoad.cmake index 25aa9f9d..a333c891 100644 --- a/PreLoad.cmake +++ b/PreLoad.cmake @@ -1 +1 @@ -set (CMAKE_GENERATOR "Unix Makefiles" CACHE INTERNAL "" FORCE) \ No newline at end of file +set (CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE) \ No newline at end of file diff --git a/README.md b/README.md index c35c2941..6d333692 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ mkdir build && cd build cmake .. # configures the CMake build system ``` -Now you can use our Make targets. +Now you can use our build targets. - `ninja obcpp`: Makes the binary which you can run using `./bin/obcpp` - `ninja test`: Run the tests in `tests/unit` @@ -25,15 +25,19 @@ Now you can use our Make targets. ## A Note on Ninja -Ninja, the build tool we are using, tries to use a number of cores on your system based on how more cores your CPU has. It is possible that it will attempt to use too many cores, and you will run out of memory on your system and everything will freeze up. +Ninja, the build tool we are using, tries to use a number of cores on your system based on how more cores your CPU has. For our repo, it seems like this default almost always crashes/freezes your computer because it quickly runs out of memory. -If this happens, you should explicitly tell Ninja to use less cores. You can do this by passing the following argument: +To solve this, in our CMake config we limit the number of core ninja can use to 4. This hasn't crashed anyone's computer so far, but it is possible that you may need to reduce this number, or perhaps you want to increase it if your system can handle it so that you can get faster build times. +To change the number of cores, you have to pass a special flag when you run `cmake`, like so: ``` -ninja -j [# cores] +cmake -D CMAKE_JOB_POOLS="j=[# jobs]" .. ``` +where you replace `[# jobs]` with a number specifying the number of jobs. -Anecdotally, on a machine with 16 virtual cores and 16GB of RAM, `-j 8` appears to be a good balance between speed and resources usage. +If you do this once, CMake should remember how you specified it, so as long as you don't clear the CMake cache you won't need to enter this again. (I.e. you can just run `cmake ..` and you should still see the message at the top saying that it is using a user-defined number of jobs). + +Anecdotally, on a machine with 16 virtual cores and 16GB of RAM, `-D CMAKE_JOB_POOLS="j=8"` appears to be a good balance between speed and resources usage. ## Modules @@ -186,13 +190,7 @@ With that, everything should be set up correctly, and you should be good to go. ## Modifying `CMakeLists.txt` -The `CMakeLists.txt` file tells `cmake` how to build the program. It will need to be modified if any of the following occurs: - -1. A new `.cpp` file is created -2. A `.cpp` file is renamed -3. A new module is created - -Each module has its own folder in `include/` and `src/`. Currently all of the header files that we expect to need are planned out, but many do not have accompanying source files. As we add these source files, new libraries will need to be added to the CMake file. You can follow the example for the libraries already included to make this change. +There is a `CMakeLists.txt` folder inside of each of the obcpp's module directories. If you add a new file to, say, the `network` module inside of `src/network/`, then you will need to add that filename to `src/network/CMakeLists.txt`. Note: you may need to clear you CMake cache if things get messed up. `find -name "*Cache.txt" -delete`