Skip to content

cnruby/w3h1_cmake

 
 

Repository files navigation

How to Format the 'CMake's File

"basic_107"

@Gitter: gitter.im/cnruby
@Github: github.com/cnruby
@Twitter: twitter.com/cnruby
@Blogspot: cnruby.blogspot.com

TABLE of CONTENTS

Requirements

How to Disable Auto Formatting of the Code

image

Install The VS Code Extension 'cmake-format'

How to Install the Tool 'cmake_format'

# sudo apt install python-pip
pip install cmake_format
#pip install --user cmake_format
echo "# cmake-format" >> ~/.bash_profile
echo "export PATH="$HOME/Library/Python/3.7/bin:$PATH" >> ~/.bash_profile
. ~/.bash_profile

How to Install the Extension "cmake-format"

Why do we need the Tool 'cmake_format'?

  • keep the code base manageable
  • allow coders to use C++ language features productively

Get the Project

git clone https://github.com/cnruby/w3h1_cmake.git basic_107
cd basic_107
git checkout basic_106
code .
cmake -GNinja -Bbuild/
cmake --build build/

How to Use the Tool 'cmake_format'

Format the CMake's File without a Custom 'cmake_format' Config File

#cmake-format --help
cmake-format --version
#
cmake-format CMakeLists.txt | more
#cmake-format --in-place CMakeLists.txt
cmake-format -i CMakeLists.txt
# 
cmake-format --command-case upper CMakeLists.txt | more
cmake-format --tab-size 4 CMakeLists.txt | more

Create A Dump 'cmake_format's Config File

cmake-format --dump-config | more
# language yaml
cmake-format --dump-config --no-help > .cmake-format.yml
# or language json
cmake-format --dump-config json > .cmake-format.json
# or language python
cmake-format --dump-config python > .cmake-format.py
# ????
cmake-format --dump-config yaml > .cmake-format.yml

Specify a Config File to Format the 'CMake's File

#cmake-format -i CMakeLists.txt --config-file .cmake-format.yml
cmake-format -i CMakeLists.txt -c .cmake-format.yml

How to Format all 'CMake's Files

cmake-format -i $(find . -name "CMakeLists.txt")
cmake-format -i $(find . -name "*.cmake")

Support for Configure Inputs

image

How to Use the Extension 'cmake-format'

Set the Extension 'cmake-format', only if Specify a Config File

  • cmakeFormat.args: Additional command line arguments to be specified.
  • like "--config-file ./.cmake-format.yaml", to specify a custom configuration
--config-file ./.cmake-format.yaml

Use the Extension 'cmake-format' with and without a Config File

  • SHIFT+CMD+P
  • Enter: "Format Document"
  • OR
  • SHIFT+ALT+F

References

Example Codes: Format All CMake's Files

find . \( -name '*.cmake' -o -name 'CMakeLists.txt' \) \
          -exec cmake-format -i {} \;
# https://github.com/googleapis/google-cloud-cpp/pull/807
find . \( -path ./.git \
          -o -path ./third_party \
          -o -path './cmake-build-*' \
          -o -path ./build-output \
       \) -prune \
       -o \( -name 'CMakeLists.txt' \
             -o -name '*.cmake' \
          \) -print0 \
    | xargs -0 cmake-format -i