-
Notifications
You must be signed in to change notification settings - Fork 44
/
build.sh
100 lines (72 loc) · 2.65 KB
/
build.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
#!/usr/bin/env bash
# This is the Linux build script for the NASM assembly tutorials.
# usage: ./build.sh [debug|release|clean] <project name>
StartTime=`date +%T`;
echo "Build script started executing at $StartTime...";
# Process command line arguments
BuildType=$1;
ProjectName=$2;
if [ "$BuildType" == "" ]; then
BuildType=release;
fi;
if [ "$ProjectName" == "" ]; then
ProjectName=hello_world;
fi;
echo "Building $ProjectName in $BuildType mode ...";
RED='\033[0;31m';
GREEN='\033[0;32m';
NC='\033[0m'; # No Color
# Create a build directory to store artifacts
BuildDir=$PWD/linuxbuild;
# If cleaning builds, just delete build artifacts and exit immediately
if [ "$BuildType" == "clean" ]; then
echo "Cleaning build from directory: $BuildDir. Files will be deleted!";
read -p "Continue? (Y/N)" ConfirmCleanBuild;
if [ $ConfirmCleanBuild == [Yy] ]; then
echo "Removing files in: $BuildDir...";
rm -rf $BuildDir;
fi;
exit 0;
fi;
echo "Building in directory: $BuildDir";
if [ ! -d $BuildDir ]; then
mkdir -p $BuildDir;
fi;
EntryPoint=$PWD/$ProjectName.asm;
IntermediateObj=$BuildDir/$ProjectName.o;
OutExe=$BuildDir/$ProjectName;
CommonCompilerFlags="-f elf64 -I$PWD -l $BuildDir/$ProjectName.lst";
DebugCompilerFlags="-g";
CommonLinkerFlags="-v -fuse-ld=lld -Wl,-e,main";
DebugLinkerFlags="-O2 -ggdb";
ReleaseLinkerFlags="-O3";
if [ "$BuildType" == "debug" ]; then
CompileCommand="nasm $CommonCompilerFlags $DebugCompilerFlags -o $IntermediateObj $EntryPoint";
LinkCommand="clang $CommonLinkerFlags $DebugLinkerFlags -o $OutExe $IntermediateObj";
else
CompileCommand="nasm $CommonCompilerFlags -o $IntermediateObj $EntryPoint";
LinkCommand="clang $CommonLinkerFlags $ReleaseLinkerFlags -o $OutExe $IntermediateObj";
fi;
echo "Compiling (command follows below)...";
echo "$CompileCommand";
$CompileCommand;
if [ $? -ne 0 ]; then
echo -e "${RED}***************************************${NC}";
echo -e "${RED}* !!! An error occurred!!! *${NC}";
echo -e "${RED}***************************************${NC}";
exit 1;
fi;
echo "Linking (command follows below)...";
echo "$LinkCommand";
$LinkCommand;
if [ $? -ne 0 ]; then
echo -e "${RED}***************************************${NC}";
echo -e "${RED}* !!! An error occurred!!! *${NC}";
echo -e "${RED}***************************************${NC}";
exit 2;
fi;
echo -e "${GREEN}***************************************${NC}";
echo -e "${GREEN}* Build completed successfully! *${NC}";
echo -e "${GREEN}***************************************${NC}";
EndTime=`date +%T`;
echo "Build script finished execution at $EndTime.";