-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
201 lines (156 loc) · 5.41 KB
/
build.sh
File metadata and controls
201 lines (156 loc) · 5.41 KB
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
#!
#
# "build.sh"
# (same as build.sh --build)
#
# "build.sh --clean" | "build.sh -c"
# "build.sh --build" | "build.sh -b"
#
COMMAND=$1
#
# Handling for command line arguement(s)
#
# Set default command if none provided
if [[ -z "${COMMAND}" ]]; then
COMMAND="build"
elif [[ "${COMMAND}" == "-b" ]]; then
COMMAND="build"
elif [[ "${COMMAND}" == "--build" ]]; then
COMMAND="build"
elif [[ "${COMMAND}" == "-c" ]]; then
COMMAND="clean"
elif [[ "${COMMAND}" == "--clean" ]]; then
COMMAND="clean"
else
echo "unrecognized command ${COMMAND}"
exit
fi
#
# Builds a cargo project
#
build_cargo_project() {
local BUILD_OUTPUT_ROOT=$OUTPUT_ROOT/build
cargo build
cargo build --release
local GENERATOR_BUILD_OUTPUT_ROOT=$BUILD_OUTPUT_ROOT/release
echo $GENERATOR_BUILD_OUTPUT_ROOT
}
#
# Copies the build outputs into a common location
#
copy_build_outputs() {
local INCLUDE_ROOT=include
local GENERATOR_BUILD_OUTPUT_ROOT=$1
local OUTPUT_ROOT=$2
echo "Looking for outputs from ${GENERATOR_BUILD_OUTPUT_ROOT}"
local LIBRARY_OUTPUT_ROOT=$OUTPUT_ROOT/lib
local EXECUTABLE_OUTPUT_ROOT=$OUTPUT_ROOT/bin
local INCLUDE_OUTPUT_ROOT=$OUTPUT_ROOT/include
mkdir $EXECUTABLE_OUTPUT_ROOT
mkdir $LIBRARY_OUTPUT_ROOT
mkdir $INCLUDE_OUTPUT_ROOT
# Copy build outputs into output directories
ls $GENERATOR_BUILD_OUTPUT_ROOT | while read OUT_FILENAME; do
if [[ "$OUT_FILENAME" =~ -exe$ ]]; then
cp $GENERATOR_BUILD_OUTPUT_ROOT/$OUT_FILENAME $EXECUTABLE_OUTPUT_ROOT
elif [[ "$OUT_FILENAME" =~ -exe\.exe$ ]]; then
cp $GENERATOR_BUILD_OUTPUT_ROOT/$OUT_FILENAME $EXECUTABLE_OUTPUT_ROOT
elif [[ "$OUT_FILENAME" =~ -lib$ ]]; then
cp $GENERATOR_BUILD_OUTPUT_ROOT/$OUT_FILENAME $LIBRARY_OUTPUT_ROOT
elif [[ "$OUT_FILENAME" =~ -lib\.lib$ ]]; then
cp $GENERATOR_BUILD_OUTPUT_ROOT/$OUT_FILENAME $LIBRARY_OUTPUT_ROOT
fi
done
# Check for include folder
if [[ -e $INCLUDE_ROOT ]]; then
ls -R $INCLUDE_ROOT | while read INCLUDE_FILE; do
if [[ "${INCLUDE_FILE}" =~ .h$ ]]; then
# C header file
echo "${INCLUDE_FILE}"
elif [[ "${INCLUDE_FILE}" =~ .hpp$ ]]; then
# C++ header file
echo "${INCLUDE_FILE}"
fi
done
fi
}
CMAKE_GENERATOR=""
OUTPUT_ROOT=out
BUILD_OUTPUT_ROOT=$OUTPUT_ROOT/build
if [[ "${COMMAND}" == "clean" ]]; then
# Remove output directory/directories
rm -r out
elif [[ "${COMMAND}" == "build" ]]; then
# Determine if this is a CMake or Rust project
if [[ -e "Cargo.toml" ]]; then
PROJECT_TYPE=cargo
fi
if [[ -e "CMakeLists.txt" ]]; then
if [[ -z "${PROJECT_TYPE}" ]]; then
PROJECT_TYPE=cmake
else
echo "Found a Cargo.toml AND CMakeLists.txt file, this is confusing me."
exit
fi
fi
# Build the project
if [[ "${PROJECT_TYPE}" == "cargo" ]]; then
echo "Building Cargo project"
GENERATOR_BUILD_OUTPUT_ROOT=$(build_cargo_project)
copy_build_outputs $GENERATOR_BUILD_OUTPUT_ROOT $OUTPUT_ROOT
elif [[ "${PROJECT_TYPE}" == "cmake" ]]; then
echo "Building CMake project"
#
# Builds a cmake project
#
if [[ -z "${CMAKE_GENERATOR}" ]]; then
# Determine cmake generator since none was provided
CMAKE_GENERATOR_LIST=$(cmake -G 2>&1 | grep "=")
if [[ "${CMAKE_GENERATOR_LIST}" =~ "Visual Studio 16 2019" ]]; then
# Use visual studio
CMAKE_GENERATOR="Visual Studio 16 2019"
elif [[ "${CMAKE_GENERATOR_LIST}" =~ "Ninja" ]]; then
# Use ninja
CMAKE_GENERATOR="Ninja"
else
echo "Failed to find a supported cmake generator. Please install ninja, visual studio, or other supported cmake generator."
echo "error"
exit
fi
fi
# Check that a cmake generator was provided
echo "Using cmake generator \"${CMAKE_GENERATOR}\""
# Generate the project files
cmake -G "${CMAKE_GENERATOR}" -S . -B $BUILD_OUTPUT_ROOT
#
# Handle outputs based on cmake generator
#
if [[ "${CMAKE_GENERATOR}" == "Ninja" ]]; then
# Handle ninja outputs
GENERATOR_CONFIG=""
GENERATOR_BUILD_OUTPUT_ROOT=$BUILD_OUTPUT_ROOT
elif [[ "${CMAKE_GENERATOR}" =~ "Visual Studio" ]]; then
# Handle visual studio outputs
GENERATOR_CONFIG="Release"
GENERATOR_BUILD_OUTPUT_ROOT=$BUILD_OUTPUT_ROOT/Release
else
echo "unhandled output from generator ${CMAKE_GENERATOR} (REPORT TO MAINTAINER)"
echo "error"
exit
fi
# Build generated project files
if [[ -z "${GENERATOR_CONFIG}" ]]; then
cmake --build $BUILD_OUTPUT_ROOT
else
cmake --build $BUILD_OUTPUT_ROOT --config $GENERATOR_CONFIG
fi
copy_build_outputs $GENERATOR_BUILD_OUTPUT_ROOT $OUTPUT_ROOT
else
# Weird situation afoot
echo "Unhandled but recognized project type \"${PROJECT_TYPE}\" (REPORT TO MAINTAINER)"
exit
fi
else
echo "unhandled but recognized command ${COMMAND} (REPORT TO MAINTAINER)"
exit
fi