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