-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_rules
47 lines (38 loc) · 1.2 KB
/
cpp_rules
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
#!/bin/bash
#
# 2021-11-14 J.Nider
realpath=$(readlink -f $0) # in case 'bake' was executed from a symbolic link
path=${realpath%/*} # full path of 'bake' script (minus the script name)
# include the linker rules automatically for convenience
source $path/ld_rules
#---------------------------
# compile .cpp file to .o file
#
# Depends on: CXX compiler name
# Optional: 'flags' additional flags to pass to compiler, or CXXFLAGS global
#
function rule_compile_cpp()
{
local target=$1
local flags=${2:-$CXXFLAGS}
local root=${target%.*}
local deps
local target_suffix=${target##*.}
# make sure target is of the right type
[[ "$target_suffix" =~ [oO] ]] || return -1
# the first dependency is the source file, otherwise guess at the name
_deps_of $target deps
local srcfile=${deps[0]:-$root.cpp}
local suffix=${srcfile##*.}
# make sure source file exists and is of the right type
[[ "$suffix" == "cpp" ]] && [[ -f "$srcfile" ]] || return -1
# make sure the compiler is set
[ $CXX ] || {
echo "${FUNCNAME[0]}: 'CXX' variable must be set to the name of the compiler"
exit
}
echo "Compiling $srcfile to $target"
local cmd="$CXX $flags -c $srcfile -o $target"
[[ $__verbose ]] && echo $cmd
$cmd
}