-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm4.sh
executable file
·105 lines (83 loc) · 2.93 KB
/
cm4.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
#!/bin/bash
#~ START:Sun,14-Apr-2024,00:49:38 IST
#~ STOPP:Sun,14-Apr-2024,03:26:01 IST
#~ AuTHoR: Rajesh Pandian M | mrprajesh.co.in
# alias cm='$HOME/my-setup/cm4.sh' # Append ~/.bashrc
# cm -l # complies last modified file in pwd. c/cpp/cu for now
# cm file.cpp # complies based on the cmds on first line/comment
# cm file.cpp v4 # complies & outputs a suffixed v4 executable
# cm -l v3 # complies last modified file and produce *v3.out file
# Assumes the first line contains one of the below
# g++ file.cpp -o file.out # gcc/g++/nvcc
# g++ -Wall filename.cpp # This creates filename.out
# Function to display usage message
display_usage() {
echo "Usage: $0 [-l [<version>]] [<filename> [<version>]]"
echo "Options:"
echo " -l Use the last modified file as the filename"
echo " <filename> Specify the filename"
echo " <version> Specify the version number (optional)"
echo "=== v.4 === mrprajesh.co.in ==="
}
# Check if no arguments are provided
if [ "$#" -eq 0 ]; then
display_usage
exit 1
fi
# Check if the user provided the '-l' flag
if [ "$1" = "-l" ]; then
# Find the last modified file with .c, .cpp, or .cu extension in the current directory
filename=$(ls -t | grep -E "\.(c|cpp|cu)$" | head -n 1)
# Check if a matching file is not found
if [ -z "$filename" ]; then
echo "Error: No .c, .cpp, or .cu files found in the current directory."
exit 1
fi
# Shift the arguments to remove the '-l' flag
shift
# If version is provided, use it
version="$1"
shift
else
# Check if a filename is provided
if [ -f "$1" ]; then
filename="$1"
shift
else
display_usage
exit 1
fi
fi
# If version is provided, use it
if [ "$#" -gt 0 ]; then
version="$1"
fi
# Get the first line of the file
first_line=$(head -n 1 "$filename")
# Check if the first line contains a comment
if ! echo "$first_line" | grep -q "//"; then
echo "Error: '$filename' missing firstline comment/cmds."
exit 1
fi
# Extract the command after single line comment "//"
cmd=$(echo "$first_line" | sed 's/.*\/\/\(.*\)/\1/' | xargs)
# Extract *.out from the cmd.
# outfile_name_noext=$(echo $cmd | grep -o '\b[[:alnum:]_]*\(?=\.out\b\)')
outfile_name_noext=$(echo $cmd | awk '{match($0, /[[:alnum:]]+\.out/); print substr($0, RSTART, RLENGTH-4)}')
# echo " ="$outfile_name_noext
#if *.out NOT found. USE FILE.out and append the cmd
if [ -z "$outfile_name_noext" ]; then
outfile_name_noext="${filename%.*}"
# echo $cmd
cmd=" $cmd -o $outfile_name_noext.out"
fi
# If version is provided, modify the output executable name in the cmd
if [ -n "$version" ]; then
output_name="${outfile_name_noext}-$version.out"
# Replace the placeholder with the new output name
cmd="${cmd/${outfile_name_noext}.out/$output_name}"
fi
#show the cmd
echo "$cmd"
# Execute the modified or original command
`$cmd`