• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# A very simple wrapper around clang-format:
16# Compare the original file with what clang-format thinks it should
17# look like and output corresponding diff. Exit with the same code as diff.
18#
19# USAGE:
20# scripts/run-clang-format /path/to/clang-format /path/to/checked/file
21#
22# The only reason of this script's existence is to avoid messing with
23# escaping special chatracters in CMake files.
24
25CLANG_FORMAT_BIN=`which $1`
26FILE_NAME=$2
27
28if [ ! -x "$CLANG_FORMAT_BIN" ]; then
29    echo "FATAL: clang-format binary '$CLANG_FORMAT_BIN' is not found or not executable"
30    exit 1
31fi
32
33if [ ! -f "$FILE_NAME" ]; then
34    echo "FATAL: Input file '$FILE_NAME' is not found"
35    exit 1
36fi
37
38# Here we rely on diff's exit code:
39diff -u --color=auto \
40    --label="$FILE_NAME (original)" \
41    --label="$FILE_NAME (reformatted)" \
42    "$FILE_NAME" <($CLANG_FORMAT_BIN $FILE_NAME)
43