DEV Community

Batuhan Ipci
Batuhan Ipci

Posted on

Makefiles can be helpful in your CI Workflow

While developing palpatine, I used Makefile to automate the process of building and running the project.

Currently, it looks something like this:

setup: 
    @if [ "$(shell uname)" = "Darwin" ]; then \
        brew install gcovr; \
        brew install lcov; \
    fi
    @if [ "$(shell uname)" = "Linux" ]; then \
        pip install gcovr; \
    fi

dependency:
    cd build && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png

prepare:
    @echo ">>> Building palpatine ...";
    @if [ -d ./build ]; then rm -rf ./build; fi
    @mkdir build; 
    @cd build; \
    cmake -S .. -B .; \
    make; \

    @echo ">>> Done";
    @echo ">>> Run ./palpatine [options] to start the application";


test:
    @echo ">>> Running tests ...";
    @cd build; \
    cmake --build . --target unit_tests ;
    @cd build/tests; \
    ./unit_tests;

codecov:
    @echo ">>> Running tests ...";
    @cd build; \
    cmake --build . --target unit_tests ;
    gcovr --html -o build/index.html ;
Enter fullscreen mode Exit fullscreen mode

Most of the set of rules of this Makefile are self-explanatory, also the echo logs I've set makes it easier to understand. Iโ€™m going to explain the ones that are not that obvious.

  • dependency rule is a interesting one. It initially changes the directory to build and then runs cmake with --graphviz=dependency.dot flag. This flag generates a dependency.dot file which is a graphviz file. Then, it runs dot command with -Tpng flag to generate a dependency.png file in build directory. This png file is a dependency graph of palpatine.

  • codecov rule builds unit_tests target just like test rule. Then, it runs gcovr command with --html flag to generate a index.html file in build directory. It helps to visualize the code coverage of the project.

Few weeks ago, I have added CI workflow to palpatine.

jobs:
  build:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v2
    - name: setup
      run: |
        make setup
    - name: configure
      run: |
        cmake -H. -Bbuild -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Debug"
    - name: building
      run: |
        cmake --build build --config Debug --target unit_tests -j4
    - name: testing
      run: |
        cd build/tests && ./unit_tests
        bash <(curl -s https://codecov.io/bash)
Enter fullscreen mode Exit fullscreen mode

This workflow runs on macOS and it does the following:

  • It checks out the code from the repository.
  • It runs make setup to install gcovr and lcov.
  • It runs cmake to configure the project.
  • It builds the project.
  • It runs the tests.
  • It uploads the code coverage report to codecov.

In the next releases, I am planning to automate the CI workflow with Makefile commands. I will update this post when I do that.

Top comments (0)