Conan-center-index: [infrastructure] add cmake-format style checks to CI

Created on 17 Sep 2019  路  2Comments  路  Source: conan-io/conan-center-index

i think one could add cmake-format to the pipeline i.o. to only merge cmake scripts which are uniformly formatted.

for example this one: https://github.com/conan-io/conan-center-index/blob/master/recipes/boost/all/test_package/CMakeLists.txt uses capital case, lower case, different indentation etc.

this can be automatically done by putting a cmake-format.yml file into the repo and formatting according to this style

ref:
https://github.com/cheshirekow/cmake_format

my format file:

# https://github.com/cheshirekow/cmake_format

# How wide to allow formatted cmake files
line_width: 120

# How many spaces to tab for indent
tab_size: 2

# Format command names consistently as 'lower' or 'upper' case
command_case: "lower"

first_comment_is_literal: False

# enable comment markup parsing and reflow
enable_markup: False

# If arglists are longer than this, break them always
max_subargs_per_line: 1

script I run in gitlab ci:

#!/bin/bash

FAILED=0
echo "Script name: $0"
work_dir=$(pwd)
if [[ -z "${CI_PROJECT_DIR}" ]]; then
  echo "CI_PROJECT_DIR is not defined"
else
  work_dir="${CI_PROJECT_DIR}"
  echo "CI_PROJECT_DIR is defined: $work_dir" 
fi
echo "Working directory is: $work_dir"
echo ""

cmakeFormat="cmake-format"

path_to_cmake_format="$(which $cmakeFormat)"
echo "path_to_cmake_format :$path_to_cmake_format"
echo "version :$($cmakeFormat --version)"

if [ -z "$path_to_cmake_format" ]
then
    echo "$cmakeFormat is not installed. Cannot format cmake files..."
    echo "run: pip3 install cmake-format"
    exit 1
fi

echo "$cmakeFormat was found, going to format your cmake scripts..." >&2

for i in `
find "$work_dir"/ \
    -not \( -path "*/build/*" -prune \) \
    -not \( -path "*/_build/*" -prune \) \
    -not \( -path "*/_build_target/*" -prune \) \
    -not \( -path "*/config/*" -prune \) \
    -not \( -path "*/scripts/*" -prune \) \
    -not \( -path "*/.vscode/*" -prune \) \
    -not \( -path "*/third_party/*" -prune \) \
    -not \( -path "*/cmake-build-debug/*" -prune \) \
    -not \( -path '*/cmake/conan.cmake' -prune \) \
    -not \( -path '*/cmake/debug.cmake' -prune \) \
    -not \( -path '*/cmake/grpc_helper.cmake' -prune \) \
    -not \( -path '*/cmake/gtest_helper.cmake' -prune \) \
    -not \( -path '*/cmake/openssl_helper.cmake' -prune \) \
    -not \( -path '*/cmake/protobuf_helper.cmake' -prune \) \
    -not \( -path '*/cmake/zlib_helper.cmake' -prune \) \
    -not \( -path '*/cmake/FindGRPC.cmake' -prune \) \
    -not \( -path '*/cmake/FindProtobuf.cmake' -prune \) \
    \( -type f -name '*.cmake' -o -name 'CMakeLists.txt'  \)`
do
    $cmakeFormat -c cmake-format.yaml -i "$i" >/dev/null
  diff=(`git diff --name-only`)

  for line in "${diff[@]}"
  do
    fullPath=$work_dir/$line

    if [[ $fullPath == *"$i"* ]]; then
      printf "\033[0;31m"
      echo "Not correctly formatted: $fullPath"
      printf "\033[0m"
      FAILED=1
    fi
  done
done

if [ "$FAILED" -eq "1" ]
then
  echo ""
  echo "linted with cmake-format version: $($cmakeFormat --version)"
  echo "run: pip3 install cmake-format --upgrade"
  echo "run: ./format_code.sh"
  echo "Failing..." 
  echo ""

  exit 1
fi
echo "done..."
enhancement

Most helpful comment

I think this is interesting and could be something useful for the future. Right now I feel it would be another thing for contributors to do, and the purpose of this repo is to lower the barrier to contribute new recipes for Conan center. Let's keep this open for the future and see if it gets some support from the community 馃槃

All 2 comments

I think this is interesting and could be something useful for the future. Right now I feel it would be another thing for contributors to do, and the purpose of this repo is to lower the barrier to contribute new recipes for Conan center. Let's keep this open for the future and see if it gets some support from the community 馃槃

Maybe we can use a linter instead of forcing cmake format for now.

Was this page helpful?
0 / 5 - 0 ratings