contrib / vscode / init.shon commit vscode: only overwrite C/C++ settings (0f47f78)
   1#!/bin/sh
   2
   3die () {
   4        echo "$*" >&2
   5        exit 1
   6}
   7
   8cd "$(dirname "$0")"/../.. ||
   9die "Could not cd to top-level directory"
  10
  11mkdir -p .vscode ||
  12die "Could not create .vscode/"
  13
  14# General settings
  15
  16cat >.vscode/settings.json.new <<\EOF ||
  17{
  18    "C_Cpp.intelliSenseEngine": "Default",
  19    "C_Cpp.intelliSenseEngineFallback": "Disabled",
  20    "files.associations": {
  21        "*.h": "c",
  22        "*.c": "c"
  23    }
  24}
  25EOF
  26die "Could not write settings.json"
  27
  28# Infer some setup-specific locations/names
  29
  30GCCPATH="$(which gcc)"
  31GDBPATH="$(which gdb)"
  32MAKECOMMAND="make -j5 DEVELOPER=1"
  33OSNAME=
  34X=
  35case "$(uname -s)" in
  36MINGW*)
  37        GCCPATH="$(cygpath -am "$GCCPATH")"
  38        GDBPATH="$(cygpath -am "$GDBPATH")"
  39        MAKE_BASH="$(cygpath -am /git-cmd.exe) --command=usr\\\\bin\\\\bash.exe"
  40        MAKECOMMAND="$MAKE_BASH -lc \\\"$MAKECOMMAND\\\""
  41        OSNAME=Win32
  42        X=.exe
  43        ;;
  44Linux)
  45        OSNAME=Linux
  46        ;;
  47Darwin)
  48        OSNAME=macOS
  49        ;;
  50esac
  51
  52# Default build task
  53
  54cat >.vscode/tasks.json.new <<EOF ||
  55{
  56    // See https://go.microsoft.com/fwlink/?LinkId=733558
  57    // for the documentation about the tasks.json format
  58    "version": "2.0.0",
  59    "tasks": [
  60        {
  61            "label": "make",
  62            "type": "shell",
  63            "command": "$MAKECOMMAND",
  64            "group": {
  65                "kind": "build",
  66                "isDefault": true
  67            }
  68        }
  69    ]
  70}
  71EOF
  72die "Could not install default build task"
  73
  74# Debugger settings
  75
  76cat >.vscode/launch.json.new <<EOF ||
  77{
  78    // Use IntelliSense to learn about possible attributes.
  79    // Hover to view descriptions of existing attributes.
  80    // For more information, visit:
  81    // https://go.microsoft.com/fwlink/?linkid=830387
  82    "version": "0.2.0",
  83    "configurations": [
  84        {
  85            "name": "(gdb) Launch",
  86            "type": "cppdbg",
  87            "request": "launch",
  88            "program": "\${workspaceFolder}/git$X",
  89            "args": [],
  90            "stopAtEntry": false,
  91            "cwd": "\${workspaceFolder}",
  92            "environment": [],
  93            "externalConsole": true,
  94            "MIMode": "gdb",
  95            "miDebuggerPath": "$GDBPATH",
  96            "setupCommands": [
  97                {
  98                    "description": "Enable pretty-printing for gdb",
  99                    "text": "-enable-pretty-printing",
 100                    "ignoreFailures": true
 101                }
 102            ]
 103        }
 104    ]
 105}
 106EOF
 107die "Could not write launch configuration"
 108
 109# C/C++ extension settings
 110
 111make -f - OSNAME=$OSNAME GCCPATH="$GCCPATH" vscode-init \
 112        >.vscode/c_cpp_properties.json <<\EOF ||
 113include Makefile
 114
 115vscode-init:
 116        @mkdir -p .vscode && \
 117        incs= && defs= && \
 118        for e in $(ALL_CFLAGS) \
 119                        '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
 120                        '-DGIT_LOCALE_PATH="$(localedir_relative_SQ)"' \
 121                        '-DBINDIR="$(bindir_relative_SQ)"' \
 122                        '-DFALLBACK_RUNTIME_PREFIX="$(prefix_SQ)"' \
 123                        '-DDEFAULT_GIT_TEMPLATE_DIR="$(template_dir_SQ)"' \
 124                        '-DETC_GITCONFIG="$(ETC_GITCONFIG_SQ)"' \
 125                        '-DETC_GITATTRIBUTES="$(ETC_GITATTRIBUTES_SQ)"' \
 126                        '-DGIT_LOCALE_PATH="$(localedir_relative_SQ)"' \
 127                        '-DCURL_DISABLE_TYPECHECK', \
 128                        '-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
 129                        '-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
 130                        '-DGIT_INFO_PATH="$(infodir_relative_SQ)"'; do \
 131                case "$$e" in \
 132                -I.) \
 133                        incs="$$(printf '% 16s"$${workspaceRoot}",\n%s' \
 134                                "" "$$incs")" \
 135                        ;; \
 136                -I/*) \
 137                        incs="$$(printf '% 16s"%s",\n%s' \
 138                                "" "$${e#-I}" "$$incs")" \
 139                        ;; \
 140                -I*) \
 141                        incs="$$(printf '% 16s"$${workspaceRoot}/%s",\n%s' \
 142                                "" "$${e#-I}" "$$incs")" \
 143                        ;; \
 144                -D*) \
 145                        defs="$$(printf '% 16s"%s",\n%s' \
 146                                "" "$$(echo "$${e#-D}" | sed 's/"/\\&/g')" \
 147                                "$$defs")" \
 148                        ;; \
 149                esac; \
 150        done && \
 151        echo '{' && \
 152        echo '    "configurations": [' && \
 153        echo '        {' && \
 154        echo '            "name": "$(OSNAME)",' && \
 155        echo '            "intelliSenseMode": "clang-x64",' && \
 156        echo '            "includePath": [' && \
 157        echo "$$incs" | sort | sed '$$s/,$$//' && \
 158        echo '            ],' && \
 159        echo '            "defines": [' && \
 160        echo "$$defs" | sort | sed '$$s/,$$//' && \
 161        echo '            ],' && \
 162        echo '            "browse": {' && \
 163        echo '                "limitSymbolsToIncludedHeaders": true,' && \
 164        echo '                "databaseFilename": "",' && \
 165        echo '                "path": [' && \
 166        echo '                    "$${workspaceRoot}"' && \
 167        echo '                ]' && \
 168        echo '            },' && \
 169        echo '            "cStandard": "c11",' && \
 170        echo '            "cppStandard": "c++17",' && \
 171        echo '            "compilerPath": "$(GCCPATH)"' && \
 172        echo '        }' && \
 173        echo '    ],' && \
 174        echo '    "version": 4' && \
 175        echo '}'
 176EOF
 177die "Could not write settings for the C/C++ extension"
 178
 179for file in .vscode/settings.json .vscode/tasks.json .vscode/launch.json
 180do
 181        if test -f $file
 182        then
 183                if git diff --no-index --quiet --exit-code $file $file.new
 184                then
 185                        rm $file.new
 186                else
 187                        printf "The file $file.new has these changes:\n\n"
 188                        git --no-pager diff --no-index $file $file.new
 189                        printf "\n\nMaybe \`mv $file.new $file\`?\n\n"
 190                fi
 191        else
 192                mv $file.new $file
 193        fi
 194done