forked from shadow/shadow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup
executable file
·233 lines (187 loc) · 8.44 KB
/
setup
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
#!/usr/bin/env python
'''
/*
* The Shadow Simulator
* Copyright (c) 2010-2011, Rob Jansen
* See LICENSE for licensing information
*/
'''
import sys, os, argparse, subprocess, multiprocessing, shlex, shutil, urllib2, tarfile, gzip, stat, time
from datetime import datetime
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
BUILD_PREFIX="./build"
INSTALL_PREFIX=os.path.expanduser("~/.shadow")
def main():
parser_main = argparse.ArgumentParser(
description='Utility to help setup the Shadow simulator',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# setup our commands
subparsers_main = parser_main.add_subparsers(
help='run a subcommand (for help use <subcommand> --help)')
# configure build subcommand
parser_build = subparsers_main.add_parser('build',
help='configure and build Shadow',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_build.set_defaults(func=build,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# add building options
parser_build.add_argument('-p', '--prefix',
help="configure PATH as Shadow root installation directory",
metavar="PATH",
action="store", dest="prefix",
default=INSTALL_PREFIX)
parser_build.add_argument('-i', '--include',
help="append PATH to the list of paths searched for headers. useful if dependencies are installed to non-standard locations, or when compiling custom libraries.",
metavar="PATH",
action="append", dest="extra_includes",
default=[INSTALL_PREFIX+ "/include"])
parser_build.add_argument('-l', '--library',
help="append PATH to the list of paths searched for libraries. useful if dependencies are installed to non-standard locations, or when compiling custom libraries.",
metavar="PATH",
action="append", dest="extra_libraries",
default=[INSTALL_PREFIX+ "/lib"])
parser_build.add_argument('-c', '--clean',
help="force a full rebuild of Shadow by removing build cache",
action="store_true", dest="do_force_rebuild",
default=False)
parser_build.add_argument('-g', '--debug',
help="build in extra memory checks and debugging symbols when running Shadow",
action="store_true", dest="do_debug",
default=False)
parser_build.add_argument('-v', '--verbose',
help="print verbose output from the compiler",
action="store_true", dest="do_verbose",
default=False)
parser_build.add_argument('-j', '--jobs',
help="number of jobs to run simultaneously during the build",
metavar="N", type=int,
action="store", dest="njobs",
default=multiprocessing.cpu_count())
parser_build.add_argument('-o', '--profile',
help="build in gprof profiling information when running Shadow",
action="store_true", dest="do_profile",
default=False)
parser_build.add_argument('-t', '--test',
help="build tests",
action="store_true", dest="do_test",
default=False)
parser_build.add_argument('--export-libraries',
help="export Shadow's plug-in service libraries and headers",
action="store_true", dest="export_libraries",
default=False)
parser_build.add_argument('--disable-plugin-tgen',
help="do not build the built-in traffic generator plug-in (tgen)",
action="store_true", dest="disable_tgen",
default=False)
# configure install subcommand
parser_install = subparsers_main.add_parser('install', help='install Shadow',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_install.set_defaults(func=install,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# get arguments, accessible with args.value
args = parser_main.parse_args()
# run chosen command
r = args.func(args)
logging.info("returning code '{0}'".format(r))
def build(args):
# get absolute paths
if args.prefix is not None: args.prefix = getfullpath(args.prefix)
filepath=getfullpath(__file__)
rootdir=filepath[:filepath.rfind("/")]
builddir=getfullpath(BUILD_PREFIX)
shadowdir=builddir+"/shadow"
installdir=getfullpath(args.prefix)
# clear cmake cache
if args.do_force_rebuild and os.path.exists(shadowdir): shutil.rmtree(shadowdir)
# create directories
if not os.path.exists(shadowdir): os.makedirs(shadowdir)
if not os.path.exists(installdir): os.makedirs(installdir)
# build up args string for the cmake command
cmake_cmd = "cmake " + rootdir + " -DCMAKE_INSTALL_PREFIX=" + installdir
# other cmake options
if args.do_debug: cmake_cmd += " -DSHADOW_DEBUG=ON"
if args.do_verbose: os.putenv("VERBOSE", "1")
if args.do_test: cmake_cmd += " -DSHADOW_TEST=ON"
if args.do_profile: cmake_cmd += " -DSHADOW_PROFILE=ON"
if args.export_libraries: cmake_cmd += " -DSHADOW_EXPORT=ON"
if args.disable_tgen: cmake_cmd += " -DBUILD_TGEN=OFF"
# we will run from build directory
calledDirectory = os.getcwd()
# run build tasks
os.chdir(shadowdir)
# hack to make passing args to CMAKE work... doesnt seem to like the first arg
args.extra_includes.insert(0, "./")
args.extra_libraries.insert(0, "./")
# add extra library and include directories as absolution paths
make_paths_absolute(args.extra_includes)
make_paths_absolute(args.extra_libraries)
# make sure we can access them from cmake
cmake_cmd += " -DCMAKE_EXTRA_INCLUDES=" + ';'.join(args.extra_includes)
cmake_cmd += " -DCMAKE_EXTRA_LIBRARIES=" + ';'.join(args.extra_libraries)
# look for the clang/clang++ compilers
clangccpath = which("clang")
if clangccpath is None:
logging.error("can't find 'clang' compiler in your PATH! Is it installed?")
clangcxxpath = which("clang++")
if clangcxxpath is None:
logging.error("can't find 'clang++' compiler in your PATH! Is it installed?")
if clangccpath is None or clangcxxpath is None: return -1
# set clang/llvm as compiler
os.putenv("CC", clangccpath)
os.putenv("CXX", clangcxxpath)
#cmake_cmd += " -D_CMAKE_TOOLCHAIN_PREFIX=llvm-"
# call cmake to configure the make process, wait for completion
logging.info("running \'{0}\' from \'{1}\'".format(cmake_cmd, os.getcwd()))
retcode = subprocess.call(cmake_cmd.strip().split())
logging.info("cmake returned " + str(retcode))
if retcode == 0:
# call make, wait for it to finish
make = "make -j{0}".format(args.njobs)
logging.info("calling " + make)
retcode = subprocess.call(shlex.split(make))
logging.info("make returned " + str(retcode))
if retcode == 0: logging.info("now run \'./setup install\'")
else: logging.error("Non-zero return code from make.")
else: logging.error(" Non-zero return code from cmake.")
# go back to where we came from
os.chdir(calledDirectory)
return retcode
def install(args):
builddir=getfullpath(BUILD_PREFIX)
shadowdir=builddir+"/shadow"
if not os.path.exists(shadowdir):
logging.error("please build before installing!")
return
# go to build dir and install from makefile
calledDirectory = os.getcwd()
os.chdir(shadowdir)
# call make install, wait for it to finish
makeCommand = "make install"
logging.info("calling \'"+makeCommand+"\'")
retcode = subprocess.call(makeCommand.strip().split())
logging.info("make install returned " + str(retcode))
if retcode == 0: logging.info("now run \'shadow\' from \'PREFIX/bin\' (check your PATH)")
# go back to where we came from
os.chdir(calledDirectory)
return retcode
def getfullpath(path):
return os.path.abspath(os.path.expanduser(path))
def make_paths_absolute(list):
for i in xrange(len(list)): list[i] = getfullpath(list[i])
## helper - test if program is in path
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
if __name__ == '__main__':
main()