contrib: add a script to initialize VS Code configuration
[git] / contrib / vscode / init.sh
1 #!/bin/sh
2
3 die () {
4         echo "$*" >&2
5         exit 1
6 }
7
8 cd "$(dirname "$0")"/../.. ||
9 die "Could not cd to top-level directory"
10
11 mkdir -p .vscode ||
12 die "Could not create .vscode/"
13
14 # General settings
15
16 cat >.vscode/settings.json <<\EOF ||
17 {
18     "C_Cpp.intelliSenseEngine": "Default",
19     "C_Cpp.intelliSenseEngineFallback": "Disabled",
20     "files.associations": {
21         "*.h": "c",
22         "*.c": "c"
23     }
24 }
25 EOF
26 die "Could not write settings.json"
27
28 # Infer some setup-specific locations/names
29
30 GCCPATH="$(which gcc)"
31 GDBPATH="$(which gdb)"
32 MAKECOMMAND="make -j5 DEVELOPER=1"
33 OSNAME=
34 X=
35 case "$(uname -s)" in
36 MINGW*)
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         ;;
44 Linux)
45         OSNAME=Linux
46         ;;
47 Darwin)
48         OSNAME=macOS
49         ;;
50 esac
51
52 # Default build task
53
54 cat >.vscode/tasks.json <<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 }
71 EOF
72 die "Could not install default build task"
73
74 # Debugger settings
75
76 cat >.vscode/launch.json <<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 }
106 EOF
107 die "Could not write launch configuration"
108
109 # C/C++ extension settings
110
111 make -f - OSNAME=$OSNAME GCCPATH="$GCCPATH" vscode-init \
112         >.vscode/c_cpp_properties.json <<\EOF ||
113 include Makefile
114
115 vscode-init:
116         @mkdir -p .vscode && \
117         incs= && defs= && \
118         for e in $(ALL_CFLAGS); do \
119                 case "$$e" in \
120                 -I.) \
121                         incs="$$(printf '% 16s"$${workspaceRoot}",\n%s' \
122                                 "" "$$incs")" \
123                         ;; \
124                 -I/*) \
125                         incs="$$(printf '% 16s"%s",\n%s' \
126                                 "" "$${e#-I}" "$$incs")" \
127                         ;; \
128                 -I*) \
129                         incs="$$(printf '% 16s"$${workspaceRoot}/%s",\n%s' \
130                                 "" "$${e#-I}" "$$incs")" \
131                         ;; \
132                 -D*) \
133                         defs="$$(printf '% 16s"%s",\n%s' \
134                                 "" "$$(echo "$${e#-D}" | sed 's/"/\\&/g')" \
135                                 "$$defs")" \
136                         ;; \
137                 esac; \
138         done && \
139         echo '{' && \
140         echo '    "configurations": [' && \
141         echo '        {' && \
142         echo '            "name": "$(OSNAME)",' && \
143         echo '            "intelliSenseMode": "clang-x64",' && \
144         echo '            "includePath": [' && \
145         echo "$$incs" | sort | sed '$$s/,$$//' && \
146         echo '            ],' && \
147         echo '            "defines": [' && \
148         echo "$$defs" | sort | sed '$$s/,$$//' && \
149         echo '            ],' && \
150         echo '            "browse": {' && \
151         echo '                "limitSymbolsToIncludedHeaders": true,' && \
152         echo '                "databaseFilename": "",' && \
153         echo '                "path": [' && \
154         echo '                    "$${workspaceRoot}"' && \
155         echo '                ]' && \
156         echo '            },' && \
157         echo '            "cStandard": "c11",' && \
158         echo '            "cppStandard": "c++17",' && \
159         echo '            "compilerPath": "$(GCCPATH)"' && \
160         echo '        }' && \
161         echo '    ],' && \
162         echo '    "version": 4' && \
163         echo '}'
164 EOF
165 die "Could not write settings for the C/C++ extension"