diff --git a/modules/cmake/README.md b/modules/cmake/README.md new file mode 100644 index 00000000..a787bf44 --- /dev/null +++ b/modules/cmake/README.md @@ -0,0 +1,44 @@ +CMake +===== + +Initializes build directories for [CMake][1]. + +### Usage + +```sh +# In the root directory of a CMake project: +$ mb +# Or with additional flags: +$ mb -DSOME_EXTRA_FLAGS="foo" +``` + +### Options + +```sh +# Set the install prefix. +zstyle ':prezto:module:cmake' install-prefix '/usr' + +# Set the build prefix. +zstyle ':prezto:module:cmake' build-prefix '_build' + +# Set the build profiles. +zstyle ':prezto:module:cmake' profiles 'Debug' 'Release' + +# Whether to support clang as well. +zstyle ':prezto:module:cmake' support-clang 'yes' +``` + +### Aliases + +* `mb` initialize build directories. + + +Authors +------- + +*The authors of this module should be contacted via the [issue tracker][2].* + + - [Benjamin Chrétien](https://github.com/bchretien) + +[1]: https://cmake.org +[2]: https://github.com/sorin-ionescu/prezto/issues diff --git a/modules/cmake/init.zsh b/modules/cmake/init.zsh new file mode 100644 index 00000000..61b31b82 --- /dev/null +++ b/modules/cmake/init.zsh @@ -0,0 +1,74 @@ +# +# Initialize build directories for CMake. +# +# Authors: +# Benjamin Chrétien +# Thomas Moulard +# + +# Get the install prefix or use the default. +zstyle -s ':prezto:module:cmake' install-prefix '_cmake_install_prefix' \ + || _cmake_install_prefix='/usr' + +# Get the build prefix. +zstyle -s ':prezto:module:cmake' build-prefix '_cmake_build_prefix' \ + || _cmake_build_prefix='_build' + +# Get the profiles to consider or use the default. +zstyle -a ':prezto:module:cmake' profiles '_cmake_profiles' \ + || _cmake_profiles=(Debug Release) + +# Whether to look for clang as well. +zstyle -b ':prezto:module:cmake' support-clang '_cmake_support_clang' \ + || _cmake_support_clang=true + +# Check for clang +_cmake_has_clang=false +if (( ${_cmake_support_clang} && $+commands[clang] )); then + _cmake_has_clang=true +fi + +function makeBuildDirectory +{ + local extra_flags="$@" + + local d=`pwd` + if `test x$(find . -maxdepth 1 -name CMakeLists.txt) = x`; then + echo "Run this in your project's root directory" + return 1 + fi + + local common_flags="-DCMAKE_INSTALL_PREFIX=${_cmake_install_prefix}" + + # Create default GCC profiles. + for p in "${_cmake_profiles[@]}"; do + echo "*** Creating ${p:l} profile..." + local build_dir="${d}/${_cmake_build_prefix}/${p:l}" + mkdir -p "${build_dir}" + (cd "${build_dir}" && \ + cmake ${common_flags} ${extra_flags} -DCMAKE_BUILD_TYPE=${p} \ + "${d}") + echo "*** ...done!" + done + + # If clang is available, create clang profiles. + if ${_cmake_has_clang}; then + for p in "${_cmake_profiles[@]}"; do + echo "*** Creating ${p:l} profile (clang)..." + local build_dir="${d}/${_cmake_build_prefix}/clang+${p:l}" + mkdir -p "${build_dir}" + (cd "${build_dir}" && \ + CC=clang CXX=clang++ \ + cmake ${common_flags} ${extra_flags} -DCMAKE_BUILD_TYPE=${p} \ + "${d}") + echo "*** ...done!" + done + fi +} + +alias mb=makeBuildDirectory + +# Enable CMake completion +(( $+functions[compdef] )) && { + compdef _cmake makeBuildDirectory +}