diff --git a/bin/tools/vscode-settings b/bin/tools/vscode-settings new file mode 100755 index 000000000..2b6e851ea --- /dev/null +++ b/bin/tools/vscode-settings @@ -0,0 +1,148 @@ +#!/bin/sh +#------------------------------------------------------------------------------ +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | www.openfoam.com +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# Copyright (C) 2020 OpenCFD Ltd. +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. +# +# Script +# bin/tools/vscode-settings +# +# Description +# Emit some settings for Visual Studio Code + OpenFOAM +# +# Example +# bin/tools/vscode-settings > .vscode/settings.json +# etc/openfoam -spdp -int64 bin/tools/vscode-settings +# +# Environment +# WM_PROJECT_DIR, WM_PROJECT_USER_DIR, WM_OPTIONS +# +# More information: +# https://openfoamwiki.net/index.php/HowTo_Use_OpenFOAM_with_Visual_Studio_Code +# +#------------------------------------------------------------------------------ +# Values consistent with wmake-with-bear +cacheDirName="build/$WM_OPTIONS" + +printHelp() { + cat< .vscode/settings.json + etc/openfoam -spdp -int64 bin/tools/vscode-settings + +USAGE + exit 0 # clean exit +} + + +#------------------------------------------------------------------------------ + +unset outputDir + +# Parse options +while [ "$#" -gt 0 ] +do + case "$1" in + -h | -help*) + printHelp + ;; + *) + echo "Unknown option/argument: '$1'" 1>&2 + break + ;; + esac + shift +done + + +#------------------------------------------------------------------------------ +# Report settings and flag errors +projectDir="$WM_PROJECT_DIR" +session="$projectDir"/etc/openfoam + +echo "project: ${WM_PROJECT_DIR:?OpenFOAM environment not set?}" 1>&2 +echo "options: ${WM_OPTIONS:?not set}" 1>&2 +if [ -x "$session" ] +then + echo "session: $session" 1>&2 +else + echo "No session: $session" 1>&2 +fi + +if [ -z "$outputDir" ] +then + prefixDir="$projectDir" + if ! [ -w "$prefixDir" ] + then + echo "Non-writable directory: $prefixDir" 1>&2 + echo "Try with user location" 1>&2 + prefixDir="$WM_PROJECT_USER_DIR" + + if ! [ -w "$prefixDir" ] + then + echo "Non-writable directory: $prefixDir" 1>&2 + echo "Using home directory" 1>&2 + prefixDir="$HOME" + fi + fi + outputDir="$prefixDir/$cacheDirName" +fi + + +## echo "Output = $outputDir" 1>&2 + +# JSON output. Long entries are outdented. +cat << INFO 1>&2 +# ------------------------- +# Some settings for Visual Studio Code + OpenFOAM +# ------------------------- +INFO + +echo '{' # BEGIN_LIST + +# ccls integration +cat << JSON_CONTENT + "ccls.cache.directory": +"$outputDir/ccls-cache", + + "ccls.misc.compilationDatabaseDirectory": +"$outputDir", + +JSON_CONTENT + + +# Compilation via openfoam session +if [ -x "$session" ] +then +cat << JSON_CONTENT + "C_Cpp.default.compileCommands": +"$session wmake -with-bear -s -j", +JSON_CONTENT +fi + +cat << JSON_CONTENT + + "C_Cpp.autocomplete": "Disabled", + "C_Cpp.errorSquiggles": "Disabled", + "C_Cpp.formatting": "Disabled", + "C_Cpp.intelliSenseEngine": "Disabled" +JSON_CONTENT + +echo '}' # END_LIST + +#------------------------------------------------------------------------------ diff --git a/wmake/wmake b/wmake/wmake old mode 100755 new mode 100644 index 57f79af7f..d1eceba05 --- a/wmake/wmake +++ b/wmake/wmake @@ -126,6 +126,48 @@ useAllCores() } +#------------------------------------------------------------------------------ +# Check environment variables +#------------------------------------------------------------------------------ + +checkEnv + +# When compiling anything but a standalone exe WM_PROJECT and WM_PROJECT_DIR +# must be set +[ "$1" = exe ] || { [ "$WM_PROJECT" ] && [ "$WM_PROJECT_DIR" ]; } || { + echo "$Script error:" 1>&2 + echo " environment variable \$WM_PROJECT or " \ + "\$WM_PROJECT_DIR not set" 1>&2 + echo " while building project library" 1>&2 + exit 1 +} + + +#------------------------------------------------------------------------------ +# check if bear is installed and we are not already running under bear +#------------------------------------------------------------------------------ + +if [ -z "${RUNNING_UNDER_BEAR}" ] ; then + if ! bear --version > /dev/null ; then + echo "WARNING: bear is not installed -> There will be no compile_commands.json output." 1>&2 + elif printf '%s\n%s\n' "bear 3.0.0" "$(bear --version)" | sort -V -C ; then + #bear version >= 3.0.0 + export RUNNING_UNDER_BEAR=true + mkdir -p $FOAM_LIBBIN + bear --append --output $FOAM_LIBBIN/../compile_commands.json -- wmake $@ + exit $? + elif printf '%s\n%s\n' "bear 2.0.0" "$(bear --version)" | sort -V -C ; then + #bear version >= 2.0.0 + export RUNNING_UNDER_BEAR=true + mkdir -p $FOAM_LIBBIN + bear --append -o $FOAM_LIBBIN/../compile_commands.json wmake $@ + exit $? + else + echo "WARNING: bear version is below 2.0.0 -> There will be no compile_commands.json output." 1>&2 + fi +fi + + #------------------------------------------------------------------------------ # Parse arguments and options #------------------------------------------------------------------------------ @@ -195,23 +237,6 @@ do done -#------------------------------------------------------------------------------ -# Check environment variables -#------------------------------------------------------------------------------ - -checkEnv - -# When compiling anything but a standalone exe WM_PROJECT and WM_PROJECT_DIR -# must be set -[ "$1" = exe ] || { [ "$WM_PROJECT" ] && [ "$WM_PROJECT_DIR" ]; } || { - echo "$Script error:" 1>&2 - echo " environment variable \$WM_PROJECT or " \ - "\$WM_PROJECT_DIR not set" 1>&2 - echo " while building project library" 1>&2 - exit 1 -} - - #------------------------------------------------------------------------------ # Setup parallel compilation #------------------------------------------------------------------------------