configure.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/usr/bin/python
  2. import optparse
  3. import os
  4. import platform
  5. import socket
  6. import subprocess
  7. import sys
  8. PROG = "r" + "$Rev: 17 $".strip("$ ").replace("Rev: ", "")
  9. PYTHON = os.path.basename(sys.executable)
  10. build_type = ""
  11. vs_target = ""
  12. s60_target = ""
  13. no_test = False
  14. no_pjsua_test = False
  15. #
  16. # Get gcc version
  17. #
  18. def gcc_version(gcc):
  19. proc = subprocess.Popen(gcc + " -v", stdout=subprocess.PIPE,
  20. stderr=subprocess.STDOUT, shell=True)
  21. ver = ""
  22. while True:
  23. s = proc.stdout.readline()
  24. if not s:
  25. break
  26. if s.find("gcc version") >= 0:
  27. ver = s.split(None, 3)[2]
  28. break
  29. proc.wait()
  30. return "gcc" + ver
  31. #
  32. # Get Visual Studio info
  33. #
  34. class VSVersion:
  35. def __init__(self):
  36. self.version = "8"
  37. self.release = "2005"
  38. proc = subprocess.Popen("cl", stdout=subprocess.PIPE,
  39. stderr=subprocess.STDOUT)
  40. while True:
  41. s = proc.stdout.readline()
  42. if s=="":
  43. break
  44. pos = s.find("Version")
  45. if pos > 0:
  46. proc.wait()
  47. s = s[pos+8:]
  48. ver = s.split(None, 1)[0]
  49. major = ver[0:2]
  50. if major=="12":
  51. self.version = "6"
  52. self.release = "98"
  53. break
  54. elif major=="13":
  55. self.version = "7"
  56. self.release = "2003"
  57. break
  58. elif major=="14":
  59. self.version = "8"
  60. self.release = "2005"
  61. break
  62. elif major=="15":
  63. self.version = "9"
  64. self.release = "2008"
  65. break
  66. elif major=="16":
  67. self.version = "10"
  68. self.release = "2010"
  69. break
  70. else:
  71. self.version = "11"
  72. self.release = "2012"
  73. break
  74. proc.wait()
  75. self.vs_version = "vs" + self.version
  76. self.vs_release = "vs" + self.release
  77. #
  78. # Get S60 SDK info
  79. #
  80. class S60SDK:
  81. def __init__(self):
  82. self.epocroot = ""
  83. self.sdk = ""
  84. self.device = ""
  85. # Check that EPOCROOT is set
  86. if not "EPOCROOT" in os.environ:
  87. sys.stderr.write("Error: EPOCROOT environment variable is not set\n")
  88. sys.exit(1)
  89. epocroot = os.environ["EPOCROOT"]
  90. # EPOCROOT must have trailing backslash
  91. if epocroot[-1] != "\\":
  92. epocroot = epocroot + "\\"
  93. os.environ["EPOCROOT"] = epocroot
  94. self.epocroot = epocroot
  95. self.sdk = sdk1 = epocroot.split("\\")[-2]
  96. self.device = "@" + self.sdk + ":com.nokia.s60"
  97. # Check that correct device is set
  98. proc = subprocess.Popen("devices", stdout=subprocess.PIPE,
  99. stderr=subprocess.STDOUT, shell=True)
  100. sdk2 = ""
  101. while True:
  102. line = proc.stdout.readline()
  103. if line.find("- default") > 0:
  104. sdk2 = line.split(":",1)[0]
  105. break
  106. proc.wait()
  107. if sdk1 != sdk2:
  108. sys.stderr.write("Error: default SDK in device doesn't match EPOCROOT\n")
  109. sys.stderr.write("Default device SDK = '" + sdk2 + "'\n")
  110. sys.stderr.write("EPOCROOT SDK = '" + sdk1 + "'\n")
  111. sys.exit(1)
  112. self.name = sdk2.replace("_", "-")
  113. def replace_vars(text):
  114. global vs_target, s60_target, build_type, no_test, no_pjsua_test
  115. suffix = ""
  116. os_info = platform.system() + platform.release() + "-" + platform.machine()
  117. # osinfo
  118. s60sdk_var = None
  119. if build_type == "s60":
  120. s60sdk_var = S60SDK()
  121. os_info = s60sdk_var.name
  122. elif platform.system().lower() == "windows" or platform.system().lower() == "microsoft":
  123. if platform.system().lower() == "microsoft":
  124. os_info = platform.release() + "-" + platform.version() + "-" + platform.win32_ver()[2]
  125. elif platform.system().lower() == "linux":
  126. os_info = "-" + "-".join(platform.linux_distribution()[0:2])
  127. # vs_target
  128. if not vs_target and text.find("$(VSTARGET)") >= 0:
  129. if build_type != "vs":
  130. sys.stderr.write("Warning: $(VSTARGET) only valid for Visual Studio\n")
  131. print "Enter Visual Studio vs_target name (e.g. Release, Debug) [Release]: ",
  132. vs_target = sys.stdin.readline().replace("\n", "").replace("\r", "")
  133. if not vs_target:
  134. vs_target = "Release"
  135. # s60_target
  136. if not s60_target and text.find("$(S60TARGET)") >= 0:
  137. if build_type != "s60":
  138. sys.stderr.write("Warning: $(S60TARGET) only valid for S60\n")
  139. print "Enter S60 target name (e.g. \"gcce urel\") [gcce urel]: ",
  140. s60_target = sys.stdin.readline().replace("\n", "").replace("\r", "")
  141. if not s60_target:
  142. s60_target = "gcce urel"
  143. # Suffix
  144. if build_type == "vs":
  145. suffix = "i386-Win32-vc8-" + vs_target
  146. elif build_type == "s60":
  147. suffix = s60sdk_var.name + "-" + s60_target.replace(" ", "-")
  148. elif build_type == "gnu":
  149. proc = subprocess.Popen("sh config.guess", cwd="../..",
  150. shell=True, stdout=subprocess.PIPE)
  151. suffix = proc.stdout.readline().rstrip(" \r\n")
  152. else:
  153. sys.stderr.write("Error: unsupported build type '" + build_type + "'\n")
  154. sys.exit(1)
  155. while True:
  156. if text.find("$(PJSUA-TESTS)") >= 0:
  157. if no_test==False and no_pjsua_test==False:
  158. # Determine pjsua exe to use
  159. exe = "../../pjsip-apps/bin/pjsua-" + suffix
  160. proc = subprocess.Popen(PYTHON + " runall.py --list-xml -e " + exe,
  161. cwd="../pjsua",
  162. shell=True, stdout=subprocess.PIPE)
  163. content = proc.stdout.read()
  164. else:
  165. content = ""
  166. text = text.replace("$(PJSUA-TESTS)", content)
  167. elif text.find("$(GCC)") >= 0:
  168. text = text.replace("$(GCC)", gcc_version("gcc"))
  169. elif text.find("$(VS)") >= 0:
  170. vsver = VSVersion()
  171. text = text.replace("$(VS)", VSVersion().vs_release)
  172. elif text.find("$(VSTARGET)") >= 0:
  173. text = text.replace("$(VSTARGET)", vs_target)
  174. elif text.find("$(S60TARGET)") >= 0:
  175. text = text.replace("$(S60TARGET)", s60_target)
  176. elif text.find("$(S60TARGETNAME)") >= 0:
  177. text = text.replace("$(S60TARGETNAME)", s60_target.replace(" ", "-"))
  178. elif text.find("$(S60DEVICE)") >= 0:
  179. text = text.replace("$(S60DEVICE)", s60sdk_var.device)
  180. elif text.find("$(EPOCROOT)") >= 0:
  181. text = text.replace("$(EPOCROOT)", s60sdk_var.epocroot)
  182. elif text.find("$(DISABLED)") >= 0:
  183. text = text.replace("$(DISABLED)", "0")
  184. elif text.find("$(IPPROOT)") >= 0:
  185. if not os.environ.has_key("IPPROOT"):
  186. sys.stderr.write("Error: environment variable IPPROOT is needed but not set\n")
  187. sys.exit(1)
  188. text = text.replace("$(IPPROOT)", os.environ["IPPROOT"])
  189. elif text.find("$(IPPSAMPLES)") >= 0:
  190. if not os.environ.has_key("IPPSAMPLES"):
  191. sys.stderr.write("Error: environment variable IPPSAMPLES is needed but not set\n")
  192. sys.exit(1)
  193. text = text.replace("$(IPPSAMPLES)", os.environ["IPPSAMPLES"])
  194. elif text.find("$(IPPARCH)") >= 0:
  195. if not os.environ.has_key("IPPARCH"):
  196. text = text.replace("$(IPPARCH)", "")
  197. else:
  198. text = text.replace("$(IPPARCH)", os.environ["IPPARCH"])
  199. elif text.find("$(OS)") >= 0:
  200. text = text.replace("$(OS)", os_info)
  201. elif text.find("$(SUFFIX)") >= 0:
  202. text = text.replace("$(SUFFIX)", suffix)
  203. elif text.find("$(HOSTNAME)") >= 0:
  204. text = text.replace("$(HOSTNAME)", socket.gethostname())
  205. elif text.find("$(PJDIR)") >= 0:
  206. wdir = os.path.join(os.getcwd(), "../..")
  207. wdir = os.path.normpath(wdir)
  208. text = text.replace("$(PJDIR)", wdir)
  209. elif text.find("$(NOP)") >= 0:
  210. if platform.system().lower() == "windows" or platform.system().lower() == "microsoft":
  211. cmd = "CMD /C echo Success"
  212. else:
  213. cmd = "echo Success"
  214. text = text.replace("$(NOP)", cmd)
  215. elif text.find("$(NOTEST)") >= 0:
  216. if no_test:
  217. str = '"1"'
  218. else:
  219. str = '"0"'
  220. text = text.replace("$(NOTEST)", str)
  221. else:
  222. break
  223. return text
  224. def main(args):
  225. global vs_target, s60_target, build_type, no_test, no_pjsua_test
  226. output = sys.stdout
  227. usage = """Usage: configure.py [OPTIONS] scenario_template_file
  228. Where OPTIONS:
  229. -o FILE Output to file, otherwise to stdout.
  230. -t TYPE Specify build type. If not specified, it will be
  231. asked if necessary. Values are:
  232. vs: Visual Studio
  233. gnu: Makefile based
  234. s60: Symbian S60
  235. -vstarget TARGETNAME Specify Visual Studio target name if build type is set
  236. to vs. If not specified then it will be asked.
  237. Sample target names:
  238. - Debug
  239. - Release
  240. - or any other target in the project file
  241. -s60target TARGETNAME Specify S60 target name if build type is set to s60.
  242. If not specified then it will be asked. Sample target
  243. names:
  244. - "gcce udeb"
  245. - "gcce urel"
  246. -notest Disable all tests in the scenario.
  247. -nopjsuatest Disable pjsua tests in the scenario.
  248. """
  249. args.pop(0)
  250. while len(args):
  251. if args[0]=='-o':
  252. args.pop(0)
  253. if len(args):
  254. output = open(args[0], "wt")
  255. args.pop(0)
  256. else:
  257. sys.stderr.write("Error: needs value for -o\n")
  258. sys.exit(1)
  259. elif args[0]=='-vstarget':
  260. args.pop(0)
  261. if len(args):
  262. vs_target = args[0]
  263. args.pop(0)
  264. else:
  265. sys.stderr.write("Error: needs value for -vstarget\n")
  266. sys.exit(1)
  267. elif args[0]=='-s60target':
  268. args.pop(0)
  269. if len(args):
  270. s60_target = args[0]
  271. args.pop(0)
  272. else:
  273. sys.stderr.write("Error: needs value for -s60target\n")
  274. sys.exit(1)
  275. elif args[0]=='-t':
  276. args.pop(0)
  277. if len(args):
  278. build_type = args[0].lower()
  279. args.pop(0)
  280. else:
  281. sys.stderr.write("Error: needs value for -t\n")
  282. sys.exit(1)
  283. if not ["vs", "gnu", "s60"].count(build_type):
  284. sys.stderr.write("Error: invalid -t argument value\n")
  285. sys.exit(1)
  286. elif args[0]=='-notest' or args[0]=='-notests':
  287. args.pop(0)
  288. no_test = True
  289. elif args[0]=='-nopjsuatest' or args[0]=='-nopjsuatests':
  290. args.pop(0)
  291. no_pjsua_test = True
  292. else:
  293. break
  294. if len(args) != 1:
  295. sys.stderr.write(usage + "\n")
  296. return 1
  297. if not build_type:
  298. defval = "vs"
  299. if "SHELL" in os.environ:
  300. shell = os.environ["SHELL"]
  301. if shell.find("sh") > -1:
  302. defval = "gnu"
  303. print "Enter the build type (values: vs, gnu, s60) [%s]: " % (defval),
  304. build_type = sys.stdin.readline().replace("\n", "").replace("\r", "")
  305. if not build_type:
  306. build_type = defval
  307. tpl_file = args[len(args)-1]
  308. if not os.path.isfile(tpl_file):
  309. print "Error: unable to find template file '%s'" % (tpl_file)
  310. return 1
  311. f = open(tpl_file, "r")
  312. tpl = f.read()
  313. f.close()
  314. tpl = replace_vars(tpl)
  315. output.write(tpl)
  316. if output != sys.stdout:
  317. output.close()
  318. return 0
  319. if __name__ == "__main__":
  320. rc = main(sys.argv)
  321. sys.exit(rc)