-
Notifications
You must be signed in to change notification settings - Fork 3
/
algolabVS.sh
executable file
·464 lines (402 loc) · 13.6 KB
/
algolabVS.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/bash
# Created by nicbauma, Autumn Semester 2018,
# modified by bittero, Autumn Semester 2019
VERSION="0.1"
# Edit command (executed in current working directory)
IDE_COMMAND='qtcreator CMakeLists.txt &>/dev/null &disown'
# Enable cmake generators for ide project files (not tested)
USE_CODEBLOCKS=false
USE_ECLIPSE=false
USE_KATE=false
# Usage
DOC=$(cat <<'EOF'
///////////////////////////////////
/// Algolab VS usage ///
///////////////////////////////////
## 1 Setup (Only do this once.)
### 1.1 Install C++ Extension For VS Code
1. Download the file `ms-vscode.cpptools-0.21.0.vsix` from the `algolab/tools/` directory on the judge.
2. cd to the directory where you saved `ms-vscode.cpptools-0.21.0.vsix`.
3. Run `code --install-extension ms-vscode.cpptools-0.21.0.vsix`
You should now be able to use c++ intellisense, goto definition etc.
REMARK: Be aware that it takes a while for parsing all the c++ libraries, so autocomplete won't be available immediately.
On the ETH lab computers it takes up 8-10 minutes, so be patient (you can keep coding meanwhile).
Also, check in the bottom right corner, when hovering over the little DB icon, whether it states 'Parsing Paused'. If yes, click the symbol and resume parsing.
### 1.2 Install algolabVS command
1. Download the this file and place it wherever you want.
2. Cd to the directory where you placed this file.
3. Run `chmod +x ./algolabVS.sh` to make the script executable.
4. Run `./algolabVS.sh install`
You can now use the algolabVS command in all directories.
## 2 Initialize a new project (Do this once for every task.)
1. cd to the folder where you want to init your project.
2. Run `algolabVS init`
3. Run `code .` to open VS code in the current directory.
Now VS code will be configured to run/debug/test within VS code.
### 2.1 How to run tasks
1. Open the VS code command prompt (`Ctrl + shift + P` on windows/linux, probabily `Cmd + shift + P` on OSX?).
2. Type `run task` and look for the command `Tasks: Run Task`.
3. Select the command and see a list of three tasks to run which are explained below:
- Run: Compiles in release mode and runs the program in the inegrated terminal (Runs 'algolab run' under the hood.)
- Debug: Compiles in debug mode but DOES NOT run the program. See section 'How to use the debugger' below. (Runs a slightly modified version of 'algolab debug' under the hood.)
- Test: Compiles in release mode, runs the program with all testcases and prints the diff to the integrated terminal. (Runs 'algolab test' under the hood.)
### 2.2 How to use the debugger
To use the debugger, do not run the debug task above directly. Simply go to the debug section of VS code and press the play button in the top left corner. This will run the above debug task under the hood. Alternatively one can also press `F5` which is the default keybinding in VS code.
You should now be able to use the GDB debugger within vs code!
As it is annoying to input the data manually while debugging, the input will be redirected from a file called 'debug_test_data.txt' inside the root directory. Simply write down your input there before running the debugger.
///////////////////////////////////
/// Vanilla algolab.sh usage ///
///////////////////////////////////
As algolabVS is simply a wrapper for the original algolab.sh script, the remaining options are still available.
Don't forget that the command name is `algolabVS` (not `algolab`).
install: "Install" the script into user home (including autocomplete for commands after shell restart).
After installation, script can be invoked in any directory using the alias 'algolabVS'.
init: Setup project (creates CMakeLists.txt, algorithm.cpp, build directories (both debug and release), vs code config files, git setup and runs cmake)
clean: Remove build directory and CMakeLists.txt
edit: Execute IDE_COMMAND
debug: Compile in debug mode and open the executable file with gdb (in current working dir)
memcheck: Compile in debug mode (if modified) and run with valgrind
compile: Compiles in release mode
run: Compiles in release mode (if modified) and launches the executable (in current working dir)
test: Compiles in relase mode (if modified) and runs all test cases (compares all .in files with .out files). If a test
fails, mismatching output file lines are highlighted in red (lines are truncated to fit terminal window)
version: Show version number
EOF
)
# Colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NC=$(tput sgr0)
# Used for test output truncation
minlen=7
maxlen=$(($(tput cols)/2-5))
cutlen="-$((maxlen-3))"
if [ -f ".algolabVS" ]; then
pname=$(cat ".algolabVS")
fi
pname=${pname:-algo}
pname=${2:-${pname}}
# File templates
CMAKE_FILE=$(cat <<'EOF'
cmake_minimum_required(VERSION 2.8)
project(pname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
find_package(CGAL REQUIRED COMPONENTS Core)
find_package(Boost REQUIRED)
include_directories(${CGAL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
include( ${CGAL_USE_FILE} )
add_executable(${PROJECT_NAME} "src/algorithm.cpp")
target_link_libraries(${PROJECT_NAME} ${CGAL_LIBRARIES} ${Boost_LIBRARIES} ${MPFR_LIBRARIES} ${GMP_LIBRARIES} ${THREAD_LIBRARIES})
EOF
)
CMAKE_FILE="${CMAKE_FILE/pname/${pname}}"
CXX_TEMPLATE=$(cat <<'EOF'
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
void testcase() {
// TODO:
return;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}
EOF
)
COMP_FILE=$(cat <<EOF
#/usr/bin/env bash
alias algolabVS='~/.algolabVS.sh'
complete -W "clean init edit debug memcheck run compile test version" algolabVS
EOF
)
#-------------------------------------------------------------------------------
# Config files for VS code
#-------------------------------------------------------------------------------
algolabVS_script="~/.algolabVS.sh"
TASKS=$(cat <<EOF
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run",
"type": "shell",
"command": "${algolabVS_script}",
"args": [
"run"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "test",
"type": "shell",
"command": "${algolabVS_script}",
"args": [
"test"
],
"group": "test"
},
{
"label": "debug",
"type": "shell",
"command": "${algolabVS_script}",
"args": [
"debug"
],
"group": "none"
},
]
}
EOF
)
LAUNCH=$(cat <<'EOF'
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/algo",
"args": [
"<",
"${workspaceFolder}/debug_test_data.txt"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "debug",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
EOF
)
GITIGNORE=$(cat <<'EOF'
.vscode/
build/
.algolabVS
EOF
)
# Helper function
function try_compile() {
local ok
local coutput
coutput=$(make 2>&1)
ok=$?
if [ "$ok" != 0 ]; then
echo "Compiler error!"
echo ""
echo "$coutput"
exit 1
fi
}
case "$1" in
version)
echo "${VERSION}"
;;
install)
cp "$0" ~/.algolabVS.sh
if [ ! -f ~/.bashrc ] || ! grep -q 'source ~/.algolabVS-bashrc.bash' ~/.bashrc; then
echo 'source ~/.algolabVS-bashrc.bash' >> ~/.bashrc
fi
if [ ! -f ~/.zshrc ] || ! grep -q 'source ~/.algolabVS-bashrc.bash' ~/.zshrc; then
echo 'source ~/.algolabVS-bashrc.bash' >> ~/.zshrc
fi
echo "$COMP_FILE" > ~/.algolabVS-bashrc.bash
exit 0
;;
clean)
if [ -f .algolabVS ]; then
rm -r build
rm CMakeLists.txt
echo "algo" > .algolabVS
else
echo "Not an algolabVS project directory."
fi
;;
init)
printf "Creating CMake project...\t"
echo "$pname" > .algolabVS
mkdir -p src
if [ ! -f src/algorithm.cpp ]; then
echo "$CXX_TEMPLATE" > src/algorithm.cpp
fi
echo "$CMAKE_FILE" > CMakeLists.txt
mkdir -p build
mkdir -p build/debug
mkdir -p build/release
#----------------------------------------------------------------------
# Config files for VS code
#----------------------------------------------------------------------
destination=$PWD
vscode_directory="${destination}/.vscode"
mkdir "$vscode_directory"
debug_test_data_filename="${destination}/debug_test_data.txt"
gitignore_filename="${destination}/.gitignore"
launch_filename="${vscode_directory}/launch.json"
tasks_filename="${vscode_directory}/tasks.json"
# Create files
if [ ! -f "${launch_filename}" ]; then
echo "$LAUNCH" > "$launch_filename"
fi
if [ ! -f "${tasks_filename}" ]; then
echo "$TASKS" > "$tasks_filename"
fi
if [ ! -f "${debug_test_data_filename}" ]; then
echo "0" > "${debug_test_data_filename}"
fi
if [ ! -f "${gitignore_filename}" ]; then
echo "$GITIGNORE" > "${gitignore_filename}"
fi
echo ""
echo "Created VS code config files"
echo ""
cd build/debug
cmake -DCMAKE_BUILD_TYPE=Debug -DCGAL_DONT_OVERRIDE_CMAKE_FLAGS=True ../..
echo ""
echo "Created Debug Makefile"
echo ""
cd ../release
cmake -DCMAKE_BUILD_TYPE=release ../..
echo ""
echo "Created Release Makefile"
echo ""
cd ../
mkdir -p project_files
cd project_files
if [ "$USE_CODEBLOCKS" == true ]; then
cmake ../.. -G"CodeBlocks - Unix Makefiles"
echo ""
echo "Created Code::Blocks project"
echo ""
fi
if [ "$USE_ECLIPSE" == true ]; then
cmake ../.. -G"Eclipse CDT4 - Unix Makefiles"
echo ""
echo "Created Eclipse project"
echo ""
fi
if [ "$USE_KATE" == true ]; then
cmake ../.. -G"Kate - Unix Makefiles"
echo ""
echo "Created Kate project"
echo ""
fi
cd ../..
echo ""
echo "Finished creating project files."
echo ""
git init
echo ""
echo "Git init."
echo ""
;;
edit)
eval "$IDE_COMMAND"
;;
debug)
cd build/debug
try_compile
cd ../..
# gdb --exec="build/debug/${pname}"
;;
memcheck)
cd build/debug
try_compile
cd ../..
valgrind "build/debug/${pname}"
;;
compile)
cd build/release
make
;;
run)
cd build/release
try_compile
cd ../..
echo "Running ${pname}:"
echo ""
"./build/release/${pname}"
;;
test)
cd build/release
try_compile
cd ../..
echo "Running all tests for ${pname}:"
echo ""
find . -name '*.in' | sort | while read line; do
tname=${line::-3}
printf "Test '${tname:2}': "
result=$("./build/release/${pname}" < "$tname.in")
ok=$?
if [ "$ok" != 0 ]
then
echo "Runtime error!"
else
expected=$(cat "$tname.out")
if [ "$result" == "$expected" ]
then
printf "${GREEN}PASSED${NC}\n"
else
# Truncate lines if longer than half terminal
len=$(echo "$expected" | awk '(NR==1||length>longest){longest=length} END {print longest}')
if [ "$len" -gt "$maxlen" ]; then
len="$maxlen"
fi
if [ "$len" -lt "$minlen" ]; then
len=$minlen
fi
len=$((len+3))
dashes=$(printf "%-$((len*2))s" "-")
printf "${RED}FAILED${NC}\n"
echo "${dashes// /-}"
printf "%-${len}s %-${len}s\n" "Expected" "Was"
echo "${dashes// /-}"
while read -u3 e; read -u4 w;
do
equal=0; [ "$e" == "$w" ] && equal=1
if [ "${#e}" -gt "$maxlen" ]; then
e="$(echo "$e" | cut -c $cutlen)..."
fi
if [ "${#w}" -gt "$maxlen" ]; then
w="$(echo "$w" | cut -c $cutlen)..."
fi
if [ "$equal" == 0 ]; then
printf "${RED}%-${len}s %-${len}s${NC}\n" "$e" "$w"
else
printf "%-${len}s %-${len}s\n" "$e" "$w"
fi
done 3< <(echo "$expected") 4< <(echo "$result")
echo "${dashes// /-}"
echo ""
fi
fi
done
;;
*)
echo "Usage: algolabVS (init|clean|edit|debug|memcheck|compile|run|test|version)"
echo ""
echo "$DOC"
echo ""
esac