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