Skip to content

Instantly share code, notes, and snippets.

@TheVice
Last active November 16, 2015 15:27
Show Gist options
  • Save TheVice/b14a2a0f0bd64969917f to your computer and use it in GitHub Desktop.
Save TheVice/b14a2a0f0bd64969917f to your computer and use it in GitHub Desktop.
Configure scripts for C\C++ projects based on CMAKE build tool
:: The MIT License (MIT)
:: Copyright (c) 2013 - 2015 https://github.com/TheVice/
:: Permission is hereby granted, free of charge, to any person obtaining a copy
:: of this software and associated documentation files (the "Software"), to deal
:: in the Software without restriction, including without limitation the rights
:: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
:: copies of the Software, and to permit persons to whom the Software is
:: furnished to do so, subject to the following conditions:
:: The above copyright notice and this permission notice shall be included in all
:: copies or substantial portions of the Software.
:: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
:: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
:: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
:: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
:: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
:: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
:: SOFTWARE.
@echo off
if [%1] == [] goto using
if %1 == clean goto CLEAN_BINARIES
cmake > nul
if %ERRORLEVEL% NEQ 0 goto NO_CMAKE_IN_PATH
cmake -version 2>&1 | findstr /C:"version 3.4" > nul && goto CMAKE34
cmake -version 2>&1 | findstr /C:"version 3.3" > nul && goto CMAKE33
cmake -version 2>&1 | findstr /C:"version 3.2" > nul && goto CMAKE32
cmake -version 2>&1 | findstr /C:"version 3.1" > nul && goto CMAKE31
cmake -version 2>&1 | findstr /C:"version 3.0" > nul && goto CMAKE30
cmake -version 2>&1 | findstr /C:"version 2.8" > nul && goto CMAKE28
goto UNKNOWN_CMAKE_VERSION
:CMAKE34
echo Found cmake v3.4
set CMAKE_VER=cmake-3.4
goto CMAKE_VERSION_DETECTED
:CMAKE33
echo Found cmake v3.3
set CMAKE_VER=cmake-3.3
goto CMAKE_VERSION_DETECTED
:CMAKE32
echo Found cmake v3.2
set CMAKE_VER=cmake-3.2
goto CMAKE_VERSION_DETECTED
:CMAKE31
echo Found cmake v3.1
set CMAKE_VER=cmake-3.1
goto CMAKE_VERSION_DETECTED
:CMAKE30
echo Found cmake v3.0
set CMAKE_VER=cmake-3.0
goto CMAKE_VERSION_DETECTED
:CMAKE28
echo Found cmake v2.8
set CMAKE_VER=cmake-2.8
goto CMAKE_VERSION_DETECTED
:UNKNOWN_CMAKE_VERSION
echo "*****************************************************************************"
echo Warning: Unknown CMAKE version
echo "*****************************************************************************"
set CMAKE_VER=cmake-XX
:CMAKE_VERSION_DETECTED
if %1 == MinGW goto MinGW
if %1 == MSVC19 goto MSVC19
if %1 == MSVC18 goto MSVC18
if %1 == MSVC17 goto MSVC17
if %1 == MSVC16 goto MSVC16
if %1 == MSVC15 goto MSVC15
if %1 == MSVC14 goto MSVC14
goto using
:MSVC14
echo "MSVC14 (Visual Studio 2005)"
set TOOL="MSVC14"
set CMAKE_GENERATOR="Visual Studio 8 2005"
goto MSVC
:MSVC15
echo "MSVC15 (Visual Studio 2008)"
set TOOL="MSVC15"
set CMAKE_GENERATOR="Visual Studio 9 2008"
goto MSVC
:MSVC16
echo "MSVC16 (Visual Studio 2010)"
set TOOL="MSVC16"
if NOT %CMAKE_VER% == cmake-2.8 goto MSVC16_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 10"
goto MSVC
:MSVC16_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 10 2010"
goto MSVC
:MSVC17
echo "MSVC17 (Visual Studio 2012)"
set TOOL="MSVC17"
if NOT %CMAKE_VER% == cmake-2.8 goto MSVC17_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 11"
goto MSVC
:MSVC17_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 11 2012"
goto MSVC
:MSVC18
echo "MSVC18 (Visual Studio 2013)"
set TOOL="MSVC18"
if NOT %CMAKE_VER% == cmake-2.8 goto MSVC18_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 12"
goto MSVC
:MSVC18_4_CMAKE3
set CMAKE_GENERATOR="Visual Studio 12 2013"
goto MSVC
:MSVC19
if CMAKE_VER == cmake-2.8 goto using
if CMAKE_VER == cmake-3.0 goto using
echo "MSVC19 (Visual Studio 2015)"
set TOOL="MSVC19"
set CMAKE_GENERATOR="Visual Studio 14 2015"
goto MSVC
:MSVC
set CURRENT_DIR=%CD%
if not exist "%CURRENT_DIR%\build" mkdir "%CURRENT_DIR%\build"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Ansi" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Ansi"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Unicode" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Unicode"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Ansi"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR% -D"CHARACTER_SET=1"
echo "*****************************************************************************"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Unicode"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR%
echo "*****************************************************************************"
cd "%CURRENT_DIR%"
if %CMAKE_VER% == cmake-2.8 goto end
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\x64" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\x64"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Ansi" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Ansi"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Unicode" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Unicode"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Ansi"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR:~0,-1% Win64" -D"CHARACTER_SET=1"
echo "*****************************************************************************"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\x64\%TOOL%\Unicode"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR:~0,-1% Win64"
echo "*****************************************************************************"
cd "%CURRENT_DIR%"
goto end
:MinGW
gcc -dumpversion > nul
if %ERRORLEVEL% NEQ 0 goto NO_GCC_IN_PATH
for /f "delims=" %%a in ('gcc -dumpversion') do @set MIGW_VERSION=%%a
echo "MinGW_%MIGW_VERSION%"
set TOOL="MinGW_%MIGW_VERSION%"
set CMAKE_GENERATOR="Eclipse CDT4 - MinGW Makefiles"
set CURRENT_DIR=%CD%
if not exist "%CURRENT_DIR%\build" mkdir "%CURRENT_DIR%\build"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Debug" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Debug"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Release" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Release"
if not exist "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\RelWithDebInfo" mkdir "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\RelWithDebInfo"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Debug"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j%NUMBER_OF_PROCESSORS% -s" -D"CHARACTER_SET=1"
echo "*****************************************************************************"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\Release"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Release -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j%NUMBER_OF_PROCESSORS% -s" -D"CHARACTER_SET=1"
echo "*****************************************************************************"
cd "%CURRENT_DIR%\build\%CMAKE_VER%\Win32\%TOOL%\RelWithDebInfo"
echo "*****************************************************************************"
cmake %~dp0\src -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j%NUMBER_OF_PROCESSORS% -s" -D"CHARACTER_SET=1"
echo "*****************************************************************************"
cd "%CURRENT_DIR%"
goto end
:NO_CMAKE_IN_PATH
echo "*****************************************************************************"
echo Path to cmake bin directory not in the PATH enviroment variable
echo "*****************************************************************************"
goto end
:NO_GCC_IN_PATH
echo "*****************************************************************************"
echo Path to MinGW or MinGW-w64 bin directory not in the PATH enviroment variable
echo "*****************************************************************************"
goto end
:CLEAN_BINARIES
set CURRENT_DIR=%CD%
del /s /a %CURRENT_DIR%\bin\*.a %CURRENT_DIR%\bin\*.lib %CURRENT_DIR%\bin\*.exe %CURRENT_DIR%\bin\CMakeCache.txt
goto end
:using
echo "*****************************************************************************"
echo "%0 using with clean|MinGW|MSVC14|MSVC15|MSVC16|MSVC17|MSVC18|MSVC19"
echo clean for deleting binaries files (*.a *.lib *.exe) from bin directory
echo MinGW for Eclipse project with Minimalist GNU for Windows
echo MSVC14 for Visual Studio 2005
echo MSVC15 for Visual Studio 2008
echo MSVC16 for Visual Studio 2010
echo MSVC17 for Visual Studio 2012
echo MSVC18 for Visual Studio 2013
echo MSVC19 for Visual Studio 2015 (only for cmake 3.1 and above)
echo "*****************************************************************************"
goto end
:end
# The MIT License (MIT)
# Copyright (c) 2013 - 2015 https://github.com/TheVice/
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
if [ ! "$1" ]; then
echo "$0 using with <clean>|<gcc>"
echo "clean for deleting binaries files (*.a, executable) from bin directory"
echo "gcc for Eclipse project with gcc"
fi
if [ "$1" = "gcc" ]; then
command -v cmake >/dev/null 2>&1 || { echo "Path to cmake bin directory not in the PATH enviroment variable" >&2; exit 1; }
CMAKE_VERSION=$(cmake -version)
length=`expr match "$CMAKE_VERSION" 'cmake version [0-9.]*'`
let "length = $length - 14"
CMAKE_VERSION=cmake-${CMAKE_VERSION:14:$length}
CMAKE_GENERATOR="Eclipse CDT4 - Unix Makefiles"
NUMBER_OF_PROCESSORS=$(nproc)
CURRENT_DIR=$PWD
SOURCE_DIR=$(dirname "$0")
GCC_VERSION=$(gcc -dumpversion)
echo gcc_$GCC_VERSION
if [ ! -d $CURRENT_DIR/build ]; then mkdir $CURRENT_DIR/build; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION/linux ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION/linux; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Debug ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Debug; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Release ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Release; fi
if [ ! -d $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/RelWithDebInfo ]; then mkdir $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/RelWithDebInfo; fi
cd $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Debug
echo "********************************************************************************"
cmake "${SOURCE_DIR// /\ }"/src -G"$CMAKE_GENERATOR" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j$NUMBER_OF_PROCESSORS -s"
echo "********************************************************************************"
cd $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/Release
echo "********************************************************************************"
cmake "${SOURCE_DIR// /\ }"/src -G"$CMAKE_GENERATOR" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j$NUMBER_OF_PROCESSORS -s"
echo "********************************************************************************"
cd $CURRENT_DIR/build/$CMAKE_VERSION/linux/gcc$GCC_VERSION/RelWithDebInfo
echo "********************************************************************************"
cmake "${SOURCE_DIR// /\ }"/src -G"$CMAKE_GENERATOR" -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DCMAKE_ECLIPSE_MAKE_ARGUMENTS="-j$NUMBER_OF_PROCESSORS -s"
echo "********************************************************************************"
cd $CURRENT_DIR
fi
if [ "$1" = "clean" ]; then
echo "********************************************************************************"
CURRENT_DIR=$PWD
find $CURRENT_DIR/build -type f -name *.a -exec rm -rf {} \;
find $CURRENT_DIR/build -type f -executable -exec rm -rf {} \;
find $CURRENT_DIR/build -type f -name CMakeCache.txt -exec rm -rf {} \;
echo "********************************************************************************"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment