From 34988afc046199e93f0721b7075db42003229b3d Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Mon, 16 Dec 2019 14:22:00 -0800 Subject: [PATCH] Build env setup scripts + initial skeleton of cross-plat scripts for clang-format (#11) * Build env setup scripts * Remove sqlite for now * Reduce the scope of .clang-format .gitattributes and .gitignore changes * Fix code comment --- .clang-format | 58 +++++++++++++++++++++++++++++++ .gitattributes | 42 ++++++++++++++++++++++ .gitignore | 1 + docs/using-clang-format.md | 37 ++++++++++++++++++++ tools/git-cl.cmd | 20 +++++++++++ tools/git-cl.sh | 15 ++++++++ tools/setup-buildtools-mac.sh | 28 +++++++++++++++ tools/setup-buildtools.sh | 65 +++++++++++++++++++++++++++++++++++ tools/setup-devenv.cmd | 6 ++++ tools/setup-devenv.sh | 28 +++++++++++++++ 10 files changed, 300 insertions(+) create mode 100644 .clang-format create mode 100644 .gitattributes create mode 100644 docs/using-clang-format.md create mode 100644 tools/git-cl.cmd create mode 100644 tools/git-cl.sh create mode 100644 tools/setup-buildtools-mac.sh create mode 100644 tools/setup-buildtools.sh create mode 100644 tools/setup-devenv.cmd create mode 100644 tools/setup-devenv.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..8d7c41ac90 --- /dev/null +++ b/.clang-format @@ -0,0 +1,58 @@ +# See Clang docs: http://clang.llvm.org/docs/ClangFormatStyleOptions.html +BasedOnStyle: Chromium + +# Allow double brackets such as std::vector>. +Standard: Cpp11 + +# Indent 2 spaces at a time. +IndentWidth: 2 + +# Keep lines under 100 columns long. +ColumnLimit: 100 + +# Always break before braces +BreakBeforeBraces: Custom +BraceWrapping: +# TODO(lujc) wait for clang-format-9 support in Chromium tools +# AfterCaseLabel: true + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false + + # Keeps extern "C" blocks unindented. + AfterExternBlock: false + +# Indent case labels. +IndentCaseLabels: true + +# Right-align pointers and references +PointerAlignment: Right + +# ANGLE likes to align things as much as possible. +AlignOperands: true +AlignConsecutiveAssignments: true + +# Use 2 space negative offset for access modifiers +AccessModifierOffset: -2 + +# TODO(jmadill): Decide if we want this on. Doesn't have an "all or none" mode. +AllowShortCaseLabelsOnASingleLine: false + +# Useful for spacing out functions in classes +KeepEmptyLinesAtTheStartOfBlocks: true + +# Indent nested PP directives. +IndentPPDirectives: AfterHash + +# Include blocks style +IncludeBlocks: Preserve diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..c1c540735f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,42 @@ +##### Source code ##### + +## C++ and C source files +*.c text eol=lf diff=cpp +*.h text eol=lf diff=cpp +*.cpp text eol=lf diff=cpp +*.cxx text eol=lf diff=cpp +*.hpp text eol=lf diff=cpp + +## Python scripts +*.py text eol=lf diff=python + +## Perl scripts/libraries/modules +*.perl text eol=lf diff=perl +*.pl text eol=lf diff=perl +*.pm text eol=lf diff=perl + +## Shell scripts +*.sh text eol=lf +*.bash text eol=lf + +## Windows batch and PowerShell scripts +*.bat text eol=crlf +*.cmd text eol=crlf +*.ps1 text eol=crlf + +##### Other file types ##### + +## Text files and documentation +*.txt text +README* text +INSTALL* text +LICENSE* text + +## Non-text documentation +*.html text diff=html +*.pdf binary +*.rtf binary + +## Self-reference =) +.gitignore text +.gitattributes text diff --git a/.gitignore b/.gitignore index 259148fa18..de186d6072 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +# Ref. https://github.com/github/gitignore/blob/master/C%2B%2B.gitignore # Prerequisites *.d diff --git a/docs/using-clang-format.md b/docs/using-clang-format.md new file mode 100644 index 0000000000..2f8ca92022 --- /dev/null +++ b/docs/using-clang-format.md @@ -0,0 +1,37 @@ +# Using clang-format + +## Command-line use + +To format a file according to Coding style using command line, please setup the build tools environment first. + +For Windows - cmd.exe command: + +```call tools\setup-devenv.cmd``` + +For Linux and Mac: + +```. tools/setup-devenv.sh``` + +Command will add the tools from repo *tools* directory to PATH environment variable. + +Then run: + +```git cl format ``` + +At the moment the tool requires you to specify the path to file you want to format. +Long-term goal is to integrate the [clang-format from Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/clang_format.md) to automatically format all source code files being changed. + +## Editor integrations + +For further guidance on editor integration, see these specific pages: +* [Download link for LLVM tools for Windows](https://releases.llvm.org/9.0.0/LLVM-9.0.0-win64.exe) +* [LLVM tools extension for Visual Studio](https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain) +* [Visual Studio code extension](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) +* [CppStyle Eclipse CDT extension](https://marketplace.eclipse.org/content/cppstyle) + +## Are robots taking over my freedom to choose where newlines go? + +No. For the project as a whole, using clang-format is just one optional way to format your code. +While it will produce style-guide conformant code, other formattings would also satisfy the style +guide. For certain modules it may be appropriate to use alternate coding style. In those scenarios +a local directory *.clang-format* settings file takes precedence over the one at top-level. diff --git a/tools/git-cl.cmd b/tools/git-cl.cmd new file mode 100644 index 0000000000..f48a15f239 --- /dev/null +++ b/tools/git-cl.cmd @@ -0,0 +1,20 @@ +@echo off +setlocal enabledelayedexpansion +if "%1" == "format" ( + if NOT "%2" == "" ( + where clang-format > NUL + if %ERRORLEVEL% neq 0 ( + echo clang-format.exe not found in PATH! + echo Assuming default path for LLVM tools... + set PATH="C:\Program Files\LLVM\bin;!PATH!" + ) + REM Assume if file exists - it's a full path, else - it's a path relative to git root. + if exist %2 ( + set "FILEPATH=%2" + ) else ( + set "FILEPATH=%GIT_PREFIX%%2" + ) + clang-format -style=file -i --verbose !FILEPATH! + ) +) +endlocal diff --git a/tools/git-cl.sh b/tools/git-cl.sh new file mode 100644 index 0000000000..5f0cca9e1b --- /dev/null +++ b/tools/git-cl.sh @@ -0,0 +1,15 @@ +#!/bin/sh +if [ "format" = "$1" ]; then + if [ -z "$2" ]; then + echo Please specify file name. + exit + fi + + if [ -f "$2" ]; then + FILEPATH=$2 + else + FILEPATH=${GIT_PREFIX}$2 + fi + echo Formatting $FILEPATH + clang-format -style=file -i $FILEPATH +fi diff --git a/tools/setup-buildtools-mac.sh b/tools/setup-buildtools-mac.sh new file mode 100644 index 0000000000..335fd8e3e4 --- /dev/null +++ b/tools/setup-buildtools-mac.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# TODO: it's not ideal experience, but we use have to use brew-provided deps. +# Sometimes we might run into a situation where a different user takes over +# control of the brew dirs. That causes the brew update to fail. +# Temporarily allow the user to take over control of brew files. + +echo *** +echo *** You may need to enter your admin password to update the brew files: +echo *** + +which -s brew +if [[ $? != 0 ]] ; then + # Install Homebrew + ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +else + # FIXME: do we always require the brew update?? + brew update +fi + +sudo chown -R $(whoami) /usr/local/Cellar +sudo chown -R $(whoami) /usr/local/Homebrew +sudo chown -R $(whoami) /usr/local/var/homebrew +sudo chown -R $(whoami) /usr/local/etc/bash_completion.d /usr/local/include /usr/local/lib/pkgconfig /usr/local/share/aclocal /usr/local/share/locale /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew/locks + +brew install cmake +brew install wget +brew install clang-format diff --git a/tools/setup-buildtools.sh b/tools/setup-buildtools.sh new file mode 100644 index 0000000000..f118e16070 --- /dev/null +++ b/tools/setup-buildtools.sh @@ -0,0 +1,65 @@ +#!/bin/sh + +if [ -f /bin/yum ]; then +# Prefer yum over apt-get +yum -y install automake +yum -y install autoconf +yum -y install libtool +yum -y install make gcc gcc-c++ +yum -y install wget +yum -y install libcurl +yum -y install zlib-devel +yum -y install git +yum -y install gperftools-libs +yum -y install libcurl-devel +yum -y install rpm-build + +# Install gcc-7 +# FIXME: current tooling is CentOS-centric :-/ +yum -y install centos-release-scl +yum -y install devtoolset-7 +yum -y install devtoolset-7-valgrind + +yum-config-manager --enable rhel-server-rhscl-7-rpms + +if [ `gcc --version | grep 7` == "" ]; then +echo "*********************************************************" +echo "*** Please make sure you start the build with gcc-7 ***" +echo "*** > scl enable devtoolset-7 ./build.sh ***" +echo "*********************************************************" +exit 3 +fi + +if [ `cmake --version | grep 3` == "" ]; then +yum -y remove cmake +wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz +tar -zxvf cmake-3.6.2.tar.gz +cd cmake-3.6.2 +./bootstrap --prefix=/usr/local +make +make install +cd .. +fi + +else +# Use apt-get +export DEBIAN_FRONTEND=noninteractive +apt-get update -y +apt-get install -qq automake +apt-get install -qq libtool-bin +apt-get install -qq cmake +apt-get install -qq curl +apt-get install -qq libcurl4-openssl-dev +apt-get install -qq zlib1g-dev +apt-get install -qq git +apt-get install -qq build-essential +apt-get install -qq libssl-dev +apt-get install -qq libsqlite3-dev +# Stock sqlite may be too old +#apt install libsqlite3-dev +apt-get install -qq wget +apt-get install -qq clang-format +fi + +## Change owner from root to current dir owner +chown -R `stat . -c %u:%g` * diff --git a/tools/setup-devenv.cmd b/tools/setup-devenv.cmd new file mode 100644 index 0000000000..44808997ce --- /dev/null +++ b/tools/setup-devenv.cmd @@ -0,0 +1,6 @@ +endlocal +set "PATH=%~dp0;%PATH%" +set "TOOLS_PATH=%~dp0" +set "TOOLS_PATH_UNIX=%TOOLS_PATH:\=/%" +git config alias.cl !%TOOLS_PATH_UNIX%git-cl.cmd +exit /b 0 diff --git a/tools/setup-devenv.sh b/tools/setup-devenv.sh new file mode 100644 index 0000000000..b851e211a7 --- /dev/null +++ b/tools/setup-devenv.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# Try to autodetect the tools dir +if [ "$BASH_SOURCE" != "" ]; then +TOOLS_PATH=`dirname ${BASH_SOURCE[0]}` +else +TOOLS_PATH=`pwd`/tools +fi +echo "Tools directory: $TOOLS_PATH" + +# Install build tools if not installed yet +FILE=.buildtools +OS_NAME=`uname -a` +if [ ! -f $FILE ]; then +case "$OS_NAME" in + *Darwin*) tools/setup-buildtools-mac.sh ;; + *Linux*) [[ -z "$NOROOT" ]] && sudo $TOOLS_PATH/setup-buildtools.sh || echo "No root: skipping build tools installation." ;; + *) echo "WARNING: unsupported OS $OS_NAME , skipping build tools installation.." +esac +# Assume that the build tools have been successfully installed +echo > $FILE +else +echo Build tools have been already installed, skipping installation. +fi + +# Configure git aliases for current session +export PATH=$TOOLS_PATH:$PATH +git config alias.cl '!git-cl.sh'