diff --git a/bin/tools/vscode-settings b/bin/tools/vscode-settings new file mode 100755 index 0000000000..2b6e851eac --- /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/scripts/AllwmakeParseArguments b/wmake/scripts/AllwmakeParseArguments index fb3882bdc7..b67dc6e9e8 100644 --- a/wmake/scripts/AllwmakeParseArguments +++ b/wmake/scripts/AllwmakeParseArguments @@ -38,6 +38,13 @@ # -l | -log | -log=FILE # -prefix=... same as -module-prefix=... # +# Trapped options +# -with-bear +# +# Note +# Locally handled options (eg, -log) must preceeded trapped options +# such as -with-bear. This is non-intuitive, but not easily fixed. +# #------------------------------------------------------------------------------ # Check environment [ -d "$WM_PROJECT_DIR" ] || { @@ -80,6 +87,7 @@ USAGE #------------------------------------------------------------------------------ unset optDebug optLog optNonRecursive optPrefix optQueue +unset optWmakeFrontend for arg in "$@" do @@ -91,6 +99,12 @@ do usage ;; + -with-bear) + # Everything wrapped via 'bear' + optWmakeFrontend="-with-bear" + break # Stop now, continue with wmake + ;; + -no-recurs* | -fromWmake) # Avoid recursion (eg, if called from wmake) optNonRecursive=true @@ -183,12 +197,14 @@ if [ -z "$optNonRecursive" ] then if [ -z "$optLog" ] then - exec wmake -all $optDebug $optQueue $* + exec wmake $optWmakeFrontend -all \ + $optDebug $optQueue $* exit $? # Unneeded, but just in case something went wrong else echo "Logging wmake -all output to '$optLog'" 1>&2 echo 1>&2 - exec wmake -all $optDebug $optQueue $* 2>&1 | /usr/bin/tee $optLog + exec wmake $optWmakeFrontend -all \ + $optDebug $optQueue $* 2>&1 | /usr/bin/tee $optLog # Need to cleanup after the tee rc=$? # Error code from tee (not wmake), but not entirely important echo "Done logging to '$optLog'" 1>&2 @@ -211,6 +227,7 @@ fi # Cleanup local variables and functions #------------------------------------------------------------------------------ +unset optWmakeFrontend unset optNonRecursive optDebug optLog optPrefix optQueue unset -f usage diff --git a/wmake/scripts/wmake-with-bear b/wmake/scripts/wmake-with-bear new file mode 100755 index 0000000000..a723d80ba2 --- /dev/null +++ b/wmake/scripts/wmake-with-bear @@ -0,0 +1,158 @@ +#!/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 +# wmake/scripts/wmake-with-bear +# Backend for "wmake -with-bear" +# +# Usage +# wmake-with-bear [wmake options and args] +# +# Description +# Call wmake via 'bear' to create json output. +# +# Environment +# WM_PROJECT_DIR, WM_PROJECT_USER_DIR, WM_OPTIONS +# +#------------------------------------------------------------------------------- +cacheDirName="build/$WM_OPTIONS" + +printHelp() { + cat<}/$cacheDirName"} + +USAGE + exit 0 # A clean exit +} + + +#------------------------------------------------------------------------------ + +unset optVersion outputDir + +# Parse options +while [ "$#" -gt 0 ] +do + case "$1" in + '') ;; + --) shift; break ;; + + -h | -help*) + printHelp + ;; + + -version) + optVersion=true + break + ;; + + -bear-output-dir=*) + outputDir="${1#*=}" + ;; + *) + break + ;; + esac + shift +done + +#------------------------------------------------------------------------------ + +# Detect version. Seems to be stderr for some versions!? +if version="$(bear --version 2>&1)" +then + version="$(echo "$version" | sed -ne '1{ s/^[^0-9]*\([1-9]\)/\1/p }')" +else + unset version +fi + +if [ "$optVersion" = true ] +then + echo "bear=$(command -v bear)" + echo "version=${version:-missing}" + exit 0 +fi + +# Not found? Can stop immediately +if [ -z "$version" ] +then + echo "Warning: bear not found" 1>&2 + echo "Stopping" 1>&2 + exit 2 +fi + + +unset outputFile +setOutput() +{ + if [ -z "$outputDir" ] + then + prefixDir="$WM_PROJECT_DIR" + 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 + + mkdir -p "$outputDir" + outputFile="$outputDir/compile_commands.json" + echo "Output = $outputFile" 1>&2 +} + + +case "$version" in +(2.*) + # Version 2 + echo "Use bear $version" 1>&2 + setOutput + exec bear --append -o "$outputFile" wmake "$@" + ;; + +([3-9].* | [1-9][0-9].*) + # Version 3 or newer + echo "Use bear $version" 1>&2 + setOutput + exec bear --append --output "$outputFile" -- wmake "$@" + ;; + +(*) + # Unknown version or some other error + echo "Warning: bear $version" 1>&2 + echo "Stopping" 1>&2 + exit 1 + ;; +esac + +# Should never reach here +exit $? + +#------------------------------------------------------------------------------ diff --git a/wmake/wmake b/wmake/wmake index d7cabb880f..6a9dd18b3b 100755 --- a/wmake/wmake +++ b/wmake/wmake @@ -115,10 +115,11 @@ then cat<