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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script for compiling a C benchmark.
#
# Usage: compile_c_benchmark <filepath>
#
# Arguments:
#
# filepath Benchmark file path.
#
#
# Environment Variables:
#
# OS Host operating system. Default: `linux`.
# NODE Command for running Node.js. Default: `node`.
# NODE_PATH Path for resolving node modules.
# C_COMPILER C compiler. Default: `gcc`.
# BLAS BLAS library to use.
# BLAS_DIR BLAS library path (if custom).
# CEPHES Cephes mathematical library path.
# CEPHES_SRC List of Cephes source files.
# INCLUDE Includes (e.g., `-I /foo/bar -I /a/b`).
# SOURCE_FILES Source file list.
# LIBRARIES Linked libraries (e.g., `-lopenblas -lpthreads`).
# LIBPATH Library paths (e.g., `-L /foo/bar -L /a/b`).
#
# VARIABLES #
# Set the benchmark file path:
file_path="$1"
# Define the host operating system:
os="${OS}"
if [[ "${os}" = "Darwin" ]]; then
os='mac'
elif [[ "${os}" = "WINNT" ]]; then
os='win'
else
os='linux'
fi
# Define the command for running Node.js:
node_cmd="${NODE}"
if [[ -z "${node_cmd}" ]]; then
node_cmd='node'
fi
# Define the node path:
node_path="${NODE_PATH}"
# Define the path to a C compiler:
c_compiler="${C_COMPILER}"
if [[ -z "${c_compiler}" ]]; then
c_compiler='gcc'
fi
# Define the BLAS library to use:
blas="${BLAS}"
# Define the BLAS directory:
blas_dir="${BLAS_DIR}"
# Define the path to the Cephes mathematical library:
cephes="${CEPHES}"
# List of Cephes source files:
cephes_src="${CEPHES_SRC}"
# Define a list of `include` directories (e.g., `-I /foo/bar -I /beep/boop/include`):
include="${INCLUDE}"
# Define a list of source files:
source_files="${SOURCE_FILES}"
# Define a list of libraries (e.g., `-lopenblas -lpthreads`):
libraries="${LIBRARIES}"
# Define a list of library paths (e.g., `-L /foo/bar -L /beep/boop`):
libpath="${LIBPATH}"
# FUNCTIONS #
# Defines an error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
echo '' >&2
}
# Prints a success message.
print_success() {
echo 'Success!' >&2
}
# Prints usage information.
usage() {
echo '' >&2
echo 'Usage: compile_c_benchmark <filepath>' >&2
echo '' >&2
}
# Resolves a package path.
#
# $1 - source directory
resolve_pkg_path() {
local pkg_path
local script
# Generate the script for resolving a package path:
script='"'"var resolve = require('@stdlib/fs/resolve-parent-path').sync; var dirname = require('@stdlib/utils/dirname'); var opts = {'dir':'$1'}; var path = resolve('package.json', opts); console.log(dirname(path));"'"'
# Resolve package path:
pkg_path=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}")
echo "${pkg_path}"
}
# Resolves a package `manifest.json`.
#
# $1 - package path
resolve_pkg_manifest() {
if [[ -e "$1/manifest.json" ]]; then
return 0
fi
return 1
}
# Resolves `include` directories.
#
# $1 - package directory
resolve_includes() {
local includes
local script
local opts
# WARNING: the following assumes knowledge of the `manifest.json` options for BLAS packages. Were those options to change, the following would need to be updated.
if [[ -n "${blas}" ]]; then
opts="{'os':'${os}','blas':'${blas}','task':'benchmark'}"
else
opts="{'task':'benchmark'}"
fi
# Generate the script for resolving `include` directories:
script='"'"var path = require('path'); var arr = require('@stdlib/utils/library-manifest')(path.join('$1','manifest.json'),${opts},{'basedir':'$1','paths':'posix'}).include; var str = ''; for (var i = 0; i < arr.length; i++){var p = path.resolve('$1', arr[i]); str += '-I '+p+' ';}; console.log(str.substring(0, str.length-1));"'"'
# Resolve `include` directories:
includes=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}")
if [[ -n "${blas}" ]]; then
if [[ -n "${blas_dir}" ]]; then
echo "${includes} -I ${blas_dir}"
else
echo "${includes}"
fi
else
echo "${includes}"
fi
}
# Resolves source files.
#
# $1 - package directory
resolve_source_files() {
local source_files
local script
local opts
# WARNING: the following assumes knowledge of the `manifest.json` options for BLAS packages. Were those options to change, the following would need to be updated.
if [[ -n "${blas}" ]]; then
opts="{'os':'${os}','blas':'${blas}','task':'benchmark'}"
else
opts="{'task':'benchmark'}"
fi
# Generate the script for resolving source files:
script='"'"var path = require('path'); var arr = require('@stdlib/utils/library-manifest')(path.join('$1','manifest.json'),${opts},{'basedir':'$1','paths':'posix'}).src; var str = ''; for (var i = 0; i < arr.length; i++){var p = path.resolve('$1', arr[i]); str += p+' ';}; console.log(str.substring(0, str.length-1));"'"'
# Resolve files:
source_files=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}")
echo "${source_files}"
}
# Resolves libraries.
#
# $1 - package directory
resolve_libraries() {
local libraries
local script
local opts
# WARNING: the following assumes knowledge of the `manifest.json` options for BLAS packages. Were those options to change, the following would need to be updated.
if [[ -n "${blas}" ]]; then
opts="{'os':'${os}','blas':'${blas}','task':'benchmark'}"
else
opts="{'task':'benchmark'}"
fi
# Generate the script for resolving libraries:
script='"'"var path = require('path'); var arr = require('@stdlib/utils/library-manifest')(path.join('$1','manifest.json'),${opts},{'basedir':'$1','paths':'posix'}).libraries; var str = ''; for (var i = 0; i < arr.length; i++){str += arr[i]+' ';}; console.log(str.substring(0, str.length-1));"'"'
# Resolve libraries:
libraries=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}")
echo "${libraries}"
}
# Resolves library paths.
#
# $1 - package directory
resolve_libpaths() {
local libpath
local script
local opts
# WARNING: the following assumes knowledge of the `manifest.json` options for BLAS packages. Were those options to change, the following would need to be updated.
if [[ -n "${blas}" ]]; then
opts="{'os':'${os}','blas':'${blas}','task':'benchmark'}"
else
opts="{'task':'benchmark'}"
fi
# Generate the script for resolving library paths:
script='"'"var path = require('path'); var arr = require('@stdlib/utils/library-manifest')(path.join('$1','manifest.json'),${opts},{'basedir':'$1','paths':'posix'}).libpath; var str = ''; for (var i = 0; i < arr.length; i++){var p = path.resolve('$1', arr[i]); str += '-L '+p+' ';}; console.log(str.substring(0, str.length-1));"'"'
# Resolve library paths:
libpath=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}")
if [[ -n "${blas}" ]]; then
if [[ -n "${blas_dir}" ]]; then
echo "${libpath} -L ${blas_dir}"
else
echo "${libpath}"
fi
else
echo "${libpath}"
fi
}
# Compiles benchmark.
#
# $1 - source directory
compile() {
cd "$1" && C_COMPILER="${c_compiler}" INCLUDE="${include}" SOURCE_FILES="${source_files}" LIBRARIES="${libraries}" LIBPATH="${libpath}" CEPHES="${cephes}" CEPHES_SRC="${cephes_src}" make 2>&1
if [[ "$?" -ne 0 ]]; then
echo 'Error when attempting to compile benchmark.' >&2
return 1
fi
echo 'Successfully compiled benchmark.' >&2
return 0
}
# MAIN #
# Main execution sequence.
main() {
local pkg_path
local manifest
local src_dir
echo 'Resolving package path...' >&2
pkg_path=$(resolve_pkg_path "${src_dir}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
echo "Package path: ${pkg_path}" >&2
echo 'Resolving package manifest...' >&2
manifest=$(resolve_pkg_manifest "${pkg_path}")
if [[ "$?" -eq 0 ]]; then
echo 'Successfully resolved package manifest.' >&2
if [[ -z "${include}" ]]; then
echo 'Resolving include directories...' >&2
include=$(resolve_includes "${pkg_path}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
fi
if [[ -z "${source_files}" ]]; then
echo 'Resolving source files...' >&2
source_files=$(resolve_source_files "${pkg_path}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
fi
if [[ -z "${libraries}" ]]; then
echo 'Resolving libraries...' >&2
libraries=$(resolve_libraries "${pkg_path}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
fi
if [[ -z "${libpath}" ]]; then
echo 'Resolving library paths...' >&2
libpath=$(resolve_libpaths "${pkg_path}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
fi
else
echo 'Unable to resolve package manifest.' >&2
fi
echo 'Compiling benchmark...' >&2
src_dir=$(dirname "${file_path}")
compile "${src_dir}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
print_success
cleanup
exit 0
}
# Handle arguments...
if [[ "$#" -eq 0 ]]; then
usage
exit 0
elif [[ "$#" -gt 1 ]]; then
echo 'ERROR: unrecognized arguments. Must provide a file path.' >&2
exit 1
fi
# Run main:
main
|