diff --git a/bin/setupVScodeFOAM b/bin/setupVScodeFOAM new file mode 100755 index 0000000000..b9792b876d --- /dev/null +++ b/bin/setupVScodeFOAM @@ -0,0 +1,102 @@ +#!/bin/python3 +#------------------------------------------------------------------------------ +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | www.openfoam.com +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# Copyright (C) 2011-2016 OpenFOAM Foundation +# Copyright (C) 2017-2020 OpenCFD Ltd. +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM. +# +# OpenFOAM is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with OpenFOAM. If not, see . +# +# Script +# setupVScode +# +# Description +# Sets up Visual Studio Code for use with OpenFOAM by writing correct +# settings into .vscode/.settings . +# You can see the settings that we set in the good_settings variable below. +# +#------------------------------------------------------------------------------ +import os +import sys +import json +import argparse + +parser = argparse.ArgumentParser(description="Sets this directory or a workspace up for using Visual Studio Code with Openfoam. Take a look at https://openfoamwiki.net/index.php/HowTo_Use_OpenFOAM_with_Visual_Studio_Code for more information.") +parser.add_argument("--generate-workspace", action="store_true", help="Generates a openfoam.code-workspace file instead of setting up the current working directory") +args = parser.parse_args() + +workspacegen = args.generate_workspace + +assert "FOAM_LIBBIN" in os.environ, os.path.basename(sys.argv[0]) + " error: environment variable FOAM_LIBBIN not set" + +if not workspacegen: + if not os.path.exists(".vscode"): + os.mkdir(".vscode") + assert os.path.isdir(".vscode"), "The path .vscode already exists and it is not a directory." + +outpath = "openfoam.code-workspace" if workspacegen else ".vscode/settings.json" +if os.path.exists(outpath): + assert os.path.isfile(outpath), outpath + " is not a file." + try: + jsondata = json.load(open(outpath, "r")) + except json.decoder.JSONDecodeError as exc: + print(exc) + print(outpath + " is not a valid json file (see exception above). Remove or fix this file then run setupVSCode again.") + sys.exit(1) +else: + jsondata = json.loads("{}") + +if workspacegen and "settings" not in jsondata: + jsondata["settings"] = json.loads("{}") + +if workspacegen: + settings = jsondata["settings"] +else: + settings = jsondata + +good_settings = json.loads("{}") +good_settings["ccls.cache.directory"] = os.path.join(os.environ["FOAM_LIBBIN"], "..", "ccls-cache") +good_settings["ccls.misc.compilationDatabaseDirectory"] = os.path.join(os.getenv("FOAM_LIBBIN"), "..") +good_settings["C_Cpp.autocomplete"] = "Disabled" +good_settings["C_Cpp.formatting"] = "Disabled" +good_settings["C_Cpp.errorSquiggles"] = "Disabled" +good_settings["C_Cpp.intelliSenseEngine"] = "Disabled" + +settings_already_good = True +for el in good_settings: + if el not in settings or settings[el] != good_settings[el]: + settings_already_good = False + settings[el] = good_settings[el] + +if workspacegen: + jsondata["settings"] = settings + +if settings_already_good: + if workspacegen: + print("The vs-code settings for " + outpath + " are already set correctly.") + else: + print("The vs-code settings for this folder are already set correctly.") +else: + json.dump(jsondata, open(outpath, "w"), indent=4, sort_keys=False) + if workspacegen: + print("The vs-code settings for " + outpath + " are now set correctly.") + else: + print("The vs-code settings for this folder are now set correctly.") diff --git a/wmake/wmake b/wmake/wmake index 572fea8256..6b350fb119 100755 --- a/wmake/wmake +++ b/wmake/wmake @@ -190,6 +190,47 @@ allCores() } +#------------------------------------------------------------------------------ +# Check environment variables +#------------------------------------------------------------------------------ + +checkEnv + +# Require WM_PROJECT for anything except a standalone exe. +# The WM_PROJECT_DIR was already tested in checkEnv. +if [ -z "$WM_PROJECT" ] && [ "$1" != exe ] +then + exec 1>&2 + echo "$Script error:" + echo " environment variable \$WM_PROJECT not set" + echo " while building project library" + exit 1 +fi + + +#------------------------------------------------------------------------------ +# check if bear is installed and we are not already running under bear +#------------------------------------------------------------------------------ + +if [ -z "${INTERCEPT_REPORT_COMMAND}" ] ; then + if ! bear --version > /dev/null ; then + echo "WARNING: bear is not installed -> There will be no compile_commands.json output." + elif printf '%s\n%s\n' "bear 3.0.0" "$(bear --version)" | sort -V -C ; then + bear version >= 3.0.0 + 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 + mkdir -p $FOAM_LIBBIN + bear --append --output $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." + fi +fi + + #------------------------------------------------------------------------------ # Parse arguments and options #------------------------------------------------------------------------------ @@ -349,24 +390,6 @@ then fi -#------------------------------------------------------------------------------ -# Check environment variables -#------------------------------------------------------------------------------ - -checkEnv - -# Require WM_PROJECT for anything except a standalone exe. -# The WM_PROJECT_DIR was already tested in checkEnv. -if [ -z "$WM_PROJECT" ] && [ "$1" != exe ] -then - exec 1>&2 - echo "$Script error:" - echo " environment variable \$WM_PROJECT not set" - echo " while building project library" - exit 1 -fi - - #------------------------------------------------------------------------------ # Setup parallel compilation #------------------------------------------------------------------------------