Skip to content

Commit c85415d

Browse files
committed
Refactored build.sh
- Moved most of the code to functions - Added DebugPrint stuff - FindSingleFileTarget() is now passing parameters to a perl script using command line
1 parent 3416cc2 commit c85415d

File tree

1 file changed

+147
-93
lines changed

1 file changed

+147
-93
lines changed

build.sh

Lines changed: 147 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -38,116 +38,157 @@ while [ "$1" ]; do
3838
esac
3939
done
4040

41+
42+
function DebugPrint()
43+
{
44+
# echo ">> Debug: $*" # uncomment this line to see debug stuff
45+
: # just allow commented 'echo' above
46+
}
47+
48+
#-------------------------------------------------------------
49+
# Setup default project when running this script directly
50+
51+
function SetupDefaultProject()
52+
{
53+
[ "$project" ] || project="UmodelTool/umodel" # setup default project name
54+
[ "$root" ] || root="."
55+
[ "$render" ] || render=1
56+
}
57+
4158
#-------------------------------------------------------------
4259
# Get revision number from Git
4360

44-
revision="(unversioned)" # this value will be used in a case of missing git
45-
version_file="UmodelTool/Version.h"
46-
if [ -d .git ]; then
47-
git=`type -p git` # equals to `which git`
48-
if [ -z "$git" ]; then
49-
if [ "$OSTYPE" == "msys" ]; then
50-
# assume Windows, find local git distribution
51-
# progs="${PROGRAMFILES//\\//}" # get from environment with slash replacement
52-
# progs="/${progs//:/}" # for msys: convert "C:/Program Files" to "/C/Program Files"
53-
[ -d "$PROGRAMFILES/Git" ] && gitpath="$PROGRAMFILES/Git/bin"
54-
[ -d "$PROGRAMW6432/Git" ] && gitpath="$PROGRAMW6432/Git/bin"
55-
! [ "$gitpath" ] && [ -d "$PROGRAMFILES/SmartGitHg/git" ] && gitpath="$PROGRAMFILES/SmartGitHg/git/bin"
56-
! [ "$gitpath" ] && [ -d "$LOCALAPPDATA/Atlassian/SourceTree/git_local" ] && gitpath="$LOCALAPPDATA/Atlassian/SourceTree/git_local/bin"
57-
[ "$gitpath" ] && PATH="$PATH:$gitpath"
58-
# find git
59-
git=`type -p git`
61+
function GetBuildNumber()
62+
{
63+
local revision="(unversioned)" # this value will be used in a case of missing git
64+
local version_file="UmodelTool/Version.h"
65+
if [ -d .git ]; then
66+
local git=`type -p git` # equals to `which git`
67+
if [ -z "$git" ]; then
68+
if [ "$OSTYPE" == "msys" ]; then
69+
# assume Windows, find local git distribution
70+
# progs="${PROGRAMFILES//\\//}" # get from environment with slash replacement
71+
# progs="/${progs//:/}" # for msys: convert "C:/Program Files" to "/C/Program Files"
72+
[ -d "$PROGRAMFILES/Git" ] && gitpath="$PROGRAMFILES/Git/bin"
73+
[ -d "$PROGRAMW6432/Git" ] && gitpath="$PROGRAMW6432/Git/bin"
74+
! [ "$gitpath" ] && [ -d "$PROGRAMFILES/SmartGitHg/git" ] && gitpath="$PROGRAMFILES/SmartGitHg/git/bin"
75+
! [ "$gitpath" ] && [ -d "$LOCALAPPDATA/Atlassian/SourceTree/git_local" ] && gitpath="$LOCALAPPDATA/Atlassian/SourceTree/git_local/bin"
76+
[ "$gitpath" ] && PATH="$PATH:$gitpath"
77+
# find git
78+
git=`type -p git`
79+
fi
6080
fi
81+
[ "$git" ] && revision=`git rev-list --count HEAD`
82+
DebugPrint "Git revision: $revision"
6183
fi
62-
[ "$git" ] && revision=`git rev-list --count HEAD`
63-
fi
6484

65-
# update tool version
66-
# read current revision
67-
[ -f "$version_file" ] && [ "$revision" ] && read last_revision < $version_file
68-
last_revision=${last_revision##* } # cut "#define ..."
69-
# write back to a file if value differs or if file doesn't exist (only for UModel project, i.e. when $project is empty)
70-
[ -z "$project" ] && [ "$last_revision" != "$revision" ] && echo "#define GIT_REVISION $revision" > $version_file
85+
# update tool version
86+
# read current revision
87+
[ -f "$version_file" ] && [ "$revision" ] && read last_revision < $version_file
88+
local last_revision=${last_revision##* } # cut "#define ..."
89+
# write back to a file if value differs or if file doesn't exist (only for UModel project, i.e. when $project is empty)
90+
[ -z "$project" ] && [ "$last_revision" != "$revision" ] && echo "#define GIT_REVISION $revision" > $version_file
91+
}
7192

7293
#-------------------------------------------------------------
7394

74-
[ "$PLATFORM" ] || PLATFORM="vc-win32"
75-
76-
# force PLATFORM=linux under Linux OS
77-
if [ "$OSTYPE" == "linux-gnu" ] || [ "$OSTYPE" == "linux" ]; then
78-
# PLATFORM="linux" - old case, now we'll recognize 32 or 64 bit OS for proper use of oodle.project
79-
if [ $(uname -m) == 'x86_64' ]; then
80-
PLATFORM="linux64"
95+
function DetectBuildPlatform()
96+
{
97+
# force PLATFORM=linux under Linux OS
98+
if [ "$OSTYPE" == "linux-gnu" ] || [ "$OSTYPE" == "linux" ]; then
99+
# PLATFORM="linux" - old case, now we'll recognize 32 or 64 bit OS for proper use of oodle.project
100+
if [ $(uname -m) == 'x86_64' ]; then
101+
PLATFORM="linux64"
102+
else
103+
PLATFORM="linux32"
104+
fi
105+
elif [ "$OSTYPE" == "darwin"* ]; then
106+
PLATFORM=osx
81107
else
82-
PLATFORM="linux32"
108+
[ "$PLATFORM" ] || PLATFORM="vc-win32"
83109
fi
84-
elif [ "$OSTYPE" == "darwin"* ]; then
85-
PLATFORM=osx
86-
fi
87-
#[ "$PLATFORM" == "linux" ] && PLATFORM="linux64"
110+
DebugPrint "Detected platform: $PLATFORM"
111+
}
88112

89-
if [ "${PLATFORM:0:3}" == "vc-" ]; then
90-
# Visual C++ compiler
113+
#-------------------------------------------------------------
114+
# We have some makefile dependency on Visual Studio compiler version, so we should detect it
115+
116+
function DetectVisualStudioVersion()
117+
{
91118
# setup default compiler version
92119
[ "$vc_ver" ] || vc_ver=latest
93120
# Find Visual Studio
94121
. vc32tools $VC32TOOLS_OPTIONS --version=$vc_ver --check
95122
[ -z "$found_vc_year" ] && exit 1 # nothing found
96123
# Adjust vc_ver to found one
97124
vc_ver=$found_vc_year
98-
# echo "Found: $found_vc_year $workpath [$vc_ver]"
125+
DebugPrint "Found: Visual Studio $found_vc_year at \"$workpath\", Version = $vc_ver"
99126
GENMAKE_OPTIONS=VC_VER=$vc_ver # specify compiler for genmake script
100-
fi
127+
}
101128

102-
[ "$project" ] || project="UmodelTool/umodel" # setup default prohect name
103-
[ "$root" ] || root="."
104-
[ "$render" ] || render=1
129+
#-------------------------------------------------------------
105130

106-
# build shader includes before call to genmake
107-
if [ $render -eq 1 ]; then
108-
# 'cd' calls below won't work if we're not calling from the project's root
109-
if [ "$root" != "." ]; then
110-
echo "Bad 'root'"
111-
exit 1
131+
function ProcessShaderFiles()
132+
{
133+
# build shader includes before call to genmake
134+
if [ $render -eq 1 ]; then
135+
# 'cd' calls below won't work if we're not calling from the project's root
136+
if [ "$root" != "." ]; then
137+
echo "Bad 'root'"
138+
exit 1
139+
fi
140+
# build shaders
141+
#?? move this command to makefile
142+
Unreal/Shaders/make.pl
143+
fi
144+
}
145+
146+
#-------------------------------------------------------------
147+
148+
function GenerateMakefile()
149+
{
150+
# prepare makefile parameters, store in obj directory
151+
local projectName=${project##*/}
152+
makefile="$root/obj/$projectName-$PLATFORM"
153+
if ! [ -d $root/obj ]; then
154+
mkdir $root/obj
155+
fi
156+
# debugging options
157+
if [ "$debug" ]; then
158+
makefile="${makefile}-debug"
159+
GENMAKE_OPTIONS+=" DEBUG=1"
160+
elif [ "$profile" ]; then
161+
makefile="${makefile}-profile"
162+
GENMAKE_OPTIONS+=" TRACY=1"
112163
fi
113-
# build shaders
114-
#?? move to makefile
115-
Unreal/Shaders/make.pl
116-
fi
117-
118-
# prepare makefile parameters, store in obj directory
119-
projectName=${project##*/}
120-
makefile="$root/obj/$projectName-$PLATFORM"
121-
if ! [ -d $root/obj ]; then
122-
mkdir $root/obj
123-
fi
124-
# debugging options
125-
if [ "$debug" ]; then
126-
makefile="${makefile}-debug"
127-
GENMAKE_OPTIONS+=" DEBUG=1"
128-
elif [ "$profile" ]; then
129-
makefile="${makefile}-profile"
130-
GENMAKE_OPTIONS+=" TRACY=1"
131-
fi
132-
makefile="${makefile}.mak"
133-
134-
# update makefile when needed
135-
# [ $makefile -ot $project ] &&
136-
$root/Tools/genmake $project.project TARGET=$PLATFORM $GENMAKE_OPTIONS > $makefile
137-
138-
if [ "$single_file" ]; then
139-
# Using perl with HEREDOC for parsing of makefile to find object file matching required target.
140-
# Code layout: target=`perl << 'EOF'
141-
# EOF
142-
# `
143-
# 1) using quoted 'EOF' to prevent variable expansion
144-
# 2) passing parameters to a script using 'export <variable', return value - as output capture
145-
# 3) putting perl command into `` (inverse quotes)
146-
# 4) s/// command in perl code has extra quote for '$'
147-
export makefile
148-
export single_file
149-
target=`perl <<'EOF'
150-
open(FILE, $ENV{"makefile"}) or die;
164+
makefile="${makefile}.mak"
165+
DebugPrint "Using makefile: $makefile"
166+
167+
# update makefile when needed
168+
# [ $makefile -ot $project ] &&
169+
$root/Tools/genmake $project.project TARGET=$PLATFORM $GENMAKE_OPTIONS > $makefile
170+
}
171+
172+
173+
#-------------------------------------------------------------
174+
# Parse generated makefile to find an obj file which is built from provided c or cpp file $single_file.
175+
# Function parameters are passed via global variables $makefile, $single_file
176+
177+
function FindSingleFileTarget()
178+
{
179+
# Using perl with HEREDOC for parsing of makefile to find object file matching required target.
180+
# Code layout: target=`perl << 'EOF'
181+
# EOF
182+
# `
183+
# 1) using quoted 'EOF' to prevent variable expansion
184+
# 2) passing parameters to a script using command line, return value as output capture
185+
# 3) putting perl command into `` (inverse quotes)
186+
# 4) s/// command in perl code has extra quote for '$'
187+
188+
target=`perl -w - "$makefile" "$single_file" <<'EOF'
189+
my $makefile = $ARGV[0];
190+
my $single_file = $ARGV[1];
191+
open(FILE, $makefile) or die;
151192
$defines = ();
152193
while ($line = <FILE>)
153194
{
@@ -163,7 +204,7 @@ target=`perl <<'EOF'
163204
# parse target
164205
($target, $cpp) = $line =~ /^(\S+)\s*\:\s*(\S+)(\s|$)/;
165206
if (defined($target) && defined($cpp)) {
166-
next if $cpp ne $ENV{"single_file"}; # match with single_file value
207+
next if $cpp ne $single_file; # match with single_file value
167208
# print("$cpp -> $target\n");
168209
for my $key (keys(%defines)) {
169210
my $value = $defines{$key};
@@ -177,12 +218,25 @@ target=`perl <<'EOF'
177218
}
178219
EOF
179220
`
180-
#echo "[$target]"
181-
if [ -z "$target" ]; then echo "Error: failed to find build target for '$single_file'"; exit; fi
182-
# end of parsing
183-
fi
221+
DebugPrint "SingleFile target: $target"
222+
if [ -z "$target" ]; then echo "Error: failed to find a build target for '$single_file'"; exit; fi
223+
}
224+
225+
#-------------------------------------------------------------
226+
227+
SetupDefaultProject
228+
GetBuildNumber
229+
DetectBuildPlatform
230+
231+
[ "${PLATFORM:0:3}" == "vc-" ] && DetectVisualStudioVersion
232+
233+
ProcessShaderFiles
234+
235+
GenerateMakefile
236+
237+
[ "$single_file" ] && FindSingleFileTarget
184238

185-
# build
239+
# Perform a build
186240
# if $target is empty, whole project will be built, otherwise a single file
187241
case "$PLATFORM" in
188242
"vc-win32")

0 commit comments

Comments
 (0)