generate.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. import copy
  5. import xml.etree.ElementTree as etree
  6. import uuid
  7. import getopt
  8. import zipfile
  9. import shutil
  10. EXT_MAPPING = { ".c": "c", ".cc": "cpp", ".cpp": "cpp", ".h": "h", ".s": "s"}
  11. def parseError(str):
  12. print("*** error: \""+str+"\"\n");
  13. def platformCompare(a, b):
  14. return cmp(a.count('*'), b.count('*'))
  15. def platformSubseteqTest(a, b):
  16. a4=a.split("-")
  17. b4=b.split("-")
  18. ret=len(a4)==len(b4)
  19. i=0
  20. while i<len(a4) and i<len(b4):
  21. ret=ret and (a4[i]==b4[i] or b4[i]=="*")
  22. i=i+1
  23. # sys.stderr.write("platformSubseteqTest("+a+", "+b+")="+str(ret))
  24. return ret
  25. def parseFile(node, ctx, list):
  26. path=node.attrib["path"]
  27. abs_path=os.path.join(ctx["base"], ctx["root"], path)
  28. # sys.stderr.write "path="+abs_path
  29. filterMatch=True
  30. if "platform" in node.attrib:
  31. filter=node.attrib["platform"]
  32. filterMatch=platformSubseteqTest(ctx["platform"], filter)
  33. if filterMatch:
  34. file_ext=os.path.splitext(path)[1]
  35. if file_ext in EXT_MAPPING:
  36. ext=EXT_MAPPING[file_ext]
  37. if os.path.exists(abs_path):
  38. install=None
  39. if "install" in node.attrib and node.attrib["install"]=="yes":
  40. install=os.path.dirname(os.path.relpath(abs_path, ctx["root"]))
  41. ## sys.stderr.write install
  42. file_item={ "path": abs_path, "ext": ext, "install": install }
  43. if "x" in node.attrib:
  44. file_item["x"]=node.attrib["x"]
  45. list.append(file_item)
  46. else:
  47. parseError("file "+abs_path+" does not exist!")
  48. else:
  49. parseError("extension "+file_ext+" is unknown")
  50. def parseFiles(target, node, ctx):
  51. for child in list(node):
  52. if child.tag=="file":
  53. parseFile(child, ctx, target)
  54. else:
  55. parseError("unhandled element: "+child.tag)
  56. def setAttr(dict, node, tag):
  57. if tag in node.attrib:
  58. dict[tag]=node.attrib[tag]
  59. def fallbackCompare(a, b):
  60. return -cmp(a.count('-'), b.count('-'))
  61. def generateFallbackList(platform):
  62. ret=[]
  63. p4=platform.split("-")
  64. for i in range(1<<len(p4)):
  65. p=""
  66. for j in range(len(p4)):
  67. m=(i&(1<<j))==(1<<j)
  68. # print " "+str(i)+" "+str(j)+" "+str(m)+" "+p
  69. if m:
  70. if len(p)>0:
  71. p=p+"-"
  72. p=p+p4[j]
  73. if len(p)>0:
  74. ret.append(p)
  75. ret.sort(fallbackCompare)
  76. return ret
  77. def updateCtx(conf, ctx, node):
  78. if "root" in node.attrib:
  79. root=ctx["root"]
  80. ctx=copy.copy(ctx)
  81. rel_prj_path=os.path.split(ctx["root"])[1]
  82. prjconfigdir=os.path.join(ctx["config"], rel_prj_path, "config")
  83. # print "rel_prj_path="+rel_prj_path+" prjconfigdir="+prjconfigdir
  84. v=node.attrib["root"].format(platform=ctx["platform"], config=os.path.join(ctx["config"], "config"), prjconfig=prjconfigdir)
  85. f=node.attrib["root"].format(platform="{platform}", config=os.path.join(ctx["config"], "config"), prjconfig=prjconfigdir)
  86. if v!=f:
  87. conf["format"][os.path.join(root, v)]=os.path.join(root, f)
  88. ctx_root=os.path.join(root, v)
  89. if not os.path.exists(ctx_root):
  90. list=generateFallbackList(ctx["platform"])
  91. for p in list:
  92. v=node.attrib["root"].format(platform=p, config=os.path.join(ctx["config"], "config"), prjconfig=prjconfigdir)
  93. if os.path.exists(os.path.join(root, v)):
  94. map=os.path.join(root, v)
  95. conf["mapping"][map]=ctx_root
  96. ctx_root=map
  97. list=[]
  98. if not os.path.exists(ctx_root) and "platforms" in conf and ctx["platform"] in conf["platforms"] and "family" in conf["platforms"][ctx["platform"]]:
  99. family=conf["platforms"][ctx["platform"]]["family"]
  100. list=generateFallbackList(family)
  101. # print "list="+str(list)
  102. for p in list:
  103. v=node.attrib["root"].format(platform=p, config=os.path.join(ctx["config"], "config"), prjconfig= prjconfigdir)
  104. if os.path.exists(os.path.join(root, v)):
  105. map=os.path.join(root, v)
  106. conf["mapping"][map]=ctx_root
  107. ctx_root=map
  108. list=[]
  109. ctx["root"]=ctx_root
  110. return ctx
  111. def parseDefines(ctx, node, dict):
  112. value=None
  113. if "value" in node.attrib:
  114. value=node.attrib["value"]
  115. if "name" in node.attrib:
  116. name=node.attrib["name"]
  117. dict[name]=value
  118. else:
  119. parseError("define syntax invalid ("+nv+")");
  120. def parseSettings(conf, node, ctx, lib):
  121. if node.tag=="settings":
  122. tag="defines"
  123. elif node.tag=="export":
  124. tag="exports"
  125. else:
  126. parseError("unknown setting: "+node.tag);
  127. for child in list(node):
  128. if child.tag=="define":
  129. parseDefines(ctx, child, lib[tag])
  130. else:
  131. parseError("unhanded element: "+child.tag)
  132. def parseDeps(node):
  133. if "dep" in node.attrib:
  134. dep=node.attrib["dep"].split()
  135. return dep
  136. else:
  137. return []
  138. def parseLibrary(conf, node, ctx, seq_type):
  139. lib_name=node.attrib["name"]
  140. lib_uuid=uuid.uuid5(uuid.NAMESPACE_URL, "urn:lib:"+lib_name)
  141. lib={"name": lib_name, "uuid": lib_uuid, "source": { "files": []}, "header": { "files": [], "target": "", "includes": [] }, "defines": {}, "exports": {}, "deps": parseDeps(node), "mode": "c90", "align": "", "external": False }
  142. ctx=updateCtx(conf, ctx, node)
  143. setAttr(lib, node, "mode")
  144. setAttr(lib, node, "align")
  145. if not(lib_name in ctx["externals"]) or not("external" in ctx["externals"][lib_name]) or not(ctx["externals"][lib_name]["external"]):
  146. for child in list(node):
  147. if child.tag=="source":
  148. parseFiles(lib["source"]["files"], list(child), updateCtx(conf, ctx, child))
  149. elif child.tag=="header":
  150. setAttr(lib["header"], child, "target")
  151. child_ctx=updateCtx(conf, ctx, child)
  152. lib["header"]["includes"].append(child_ctx["root"])
  153. parseFiles(lib["header"]["files"], list(child), child_ctx)
  154. elif child.tag=="settings":
  155. parseSettings(conf, child, ctx, lib)
  156. elif child.tag=="export":
  157. parseSettings(conf, child, ctx, lib)
  158. else:
  159. parseError("unhanded element: "+child.tag)
  160. else:
  161. sys.stderr.write("using external "+lib_name)
  162. lib["external"]=True
  163. conf[seq_type].append(lib)
  164. def isExcluded(conf, ctx, lib):
  165. # sys.stderr.write conf["platforms"]
  166. if ctx["platform"] in conf["platforms"]:
  167. platform=conf["platforms"][ctx["platform"]]
  168. return lib in platform["exclude"]
  169. else:
  170. return False
  171. def parsePlatform(conf, node, ctx):
  172. if "name" in node.attrib:
  173. name=node.attrib["name"]
  174. platform={ "exclude": {}, "libs": [], "ldflags": "" }
  175. setAttr(platform, node, "family")
  176. setAttr(platform, node, "cc")
  177. setAttr(platform, node, "ar")
  178. setAttr(platform, node, "ld")
  179. setAttr(platform, node, "cflags")
  180. setAttr(platform, node, "cflags_c")
  181. setAttr(platform, node, "cflags_cpp")
  182. setAttr(platform, node, "cppflags")
  183. setAttr(platform, node, "ldflags")
  184. setAttr(platform, node, "host")
  185. if "exclude" in node.attrib:
  186. for prj in node.attrib["exclude"].split(" "):
  187. platform["exclude"][prj]=None
  188. # sys.stderr.write platform["exclude"]
  189. if "libs" in node.attrib:
  190. platform["libs"].extend(node.attrib["libs"].split(" "))
  191. conf["platforms"][name]=platform
  192. else:
  193. parseError("no name given for platform")
  194. def parseTargets(conf, node, ctx):
  195. for child in list(node):
  196. if child.tag=="include":
  197. path=os.path.abspath(os.path.join(ctx["root"], child.attrib["path"]))
  198. new_ctx=copy.copy(ctx)
  199. new_ctx["root"]=os.path.dirname(path)
  200. parseConfiguration(conf, path, new_ctx)
  201. elif child.tag=="library":
  202. parseLibrary(conf, child, ctx, "libraries")
  203. elif child.tag=="tool":
  204. parseLibrary(conf, child, ctx, "tools")
  205. elif child.tag=="platform":
  206. parsePlatform(conf, child, ctx)
  207. else:
  208. parseError("unhanded element: "+child.tag)
  209. def parseConfiguration(conf, filename, ctx):
  210. sys.stderr.write("parsing "+filename+"\n")
  211. tree=etree.parse(filename)
  212. if tree is not None:
  213. parseTargets(conf, tree.getroot(), ctx)
  214. else:
  215. parseError("failed to open file \""+filename+"\"")
  216. def depClosure2(conf, deps, ignoreExternal):
  217. ret=[]
  218. gray=set(deps)
  219. black=set()
  220. while len(gray)>0:
  221. for lib in conf["libraries"]:
  222. if lib["name"] in gray:
  223. gray.remove(lib["name"])
  224. black.add(lib["name"])
  225. if not(ignoreExternal) or not(lib["external"]):
  226. ret.append((lib, []))
  227. lib_deps=lib["deps"]
  228. for lib_dep in lib_deps:
  229. if not lib_dep in black:
  230. gray.add(lib_dep)
  231. return ret
  232. def depCompare(a, b):
  233. if a[0] in b[1]:
  234. return 1
  235. elif b[0] in a[1]:
  236. return -1
  237. else:
  238. return 0
  239. def depClosureStr(req_deps, deps):
  240. ret="depClosure("+str(req_deps)+") ="
  241. for lib in deps:
  242. ret=ret+" "+lib[0]["name"]+"<"
  243. for x in lib[1]:
  244. ret=ret+" "+x[0]["name"]
  245. ret=ret+" >"
  246. return ret
  247. def depClosure(conf, req_deps, ignoreExternal):
  248. deps=depClosure2(conf, req_deps, ignoreExternal)
  249. for lib in deps:
  250. lib[1].extend(depClosure2(conf, lib[0]["deps"], ignoreExternal))
  251. # print depClosureStr(req_deps, deps)
  252. # deps.sort(depCompare)
  253. # print depClosureStr(req_deps, deps)
  254. ret=[]
  255. for lib in deps:
  256. ret.append(lib[0]["name"])
  257. # print "\n"
  258. return ret
  259. def gatherIncludeDirs(conf, ctx, deps):
  260. ret=[]
  261. aux={}
  262. for lib in conf["libraries"]:
  263. if lib["name"] in deps:
  264. for file in lib["header"]["files"]:
  265. if file["install"] is not None:
  266. dir=os.path.dirname(file["path"])
  267. dir=os.path.abspath(dir.rstrip(file["install"]))
  268. dir=os.path.relpath(dir, ctx["base"])
  269. if dir not in aux:
  270. aux[dir]=None
  271. ret.append(dir)
  272. # sys.stderr.write ret
  273. return ret
  274. def gatherSources(conf, ctx, lib):
  275. sources=[]
  276. for file in lib["source"]["files"]:
  277. rel_source=os.path.relpath(file["path"], ctx["base"])
  278. sources.append((rel_source, file))
  279. return sources
  280. #def generateOBJS(conf, ctx, lib, out, obj_dir, cppflags):
  281. # objs=[]
  282. # for file in lib["source"]["files"]:
  283. # rel_source=os.path.relpath(file["path"], ctx["base"])
  284. # rel_obj=os.path.join(obj_dir, os.path.splitext(rel_source)[0]+".o")
  285. # out_dir=os.path.dirname(rel_obj)
  286. # out.write(rel_obj+": "+rel_source+"\n")
  287. # out.write("\t@mkdir -p "+out_dir+"\n")
  288. # specific=""
  289. # if file["ext"]=="c":
  290. # specific ="$(CFLAGS_C) "
  291. # elif file["ext"]=="cpp":
  292. # specific ="$(CFLAGS_CPP) "
  293. # out.write("\t$(CC) -c $(CFLAGS) "+ specific +"$(CPPFLAGS)"+cppflags+" "+ rel_source+" -o "+rel_obj+"\n")
  294. # objs.append(rel_obj)
  295. # return objs
  296. def generateDEPS(conf, ctx, lib, out, obj_dir, cppflags, filename):
  297. for file in lib["source"]["files"]:
  298. rel_source=os.path.relpath(file["path"], ctx["base"])
  299. specific=""
  300. if file["ext"]=="c":
  301. specific ="$(CFLAGS_C) "
  302. elif file["ext"]=="cpp":
  303. specific ="$(CFLAGS_CPP) "
  304. out.write("\tmakedepend -a -f"+filename+" $(CPPFLAGS)"+cppflags+" "+ rel_source+"\n")
  305. def generateCPPFLAGS(conf, ctx, lib, build_dir):
  306. cppflags=""
  307. for define in lib["defines"]:
  308. if lib["defines"][define] is None:
  309. cppflags=cppflags+" -D"+define
  310. else:
  311. cppflags=cppflags+" -D"+define+"="+lib["defines"][define]
  312. for dir in lib["header"]["includes"]:
  313. abs_dir=os.path.abspath(dir)
  314. rel_dir=os.path.relpath(abs_dir, os.path.join(ctx["base"], build_dir))
  315. # sys.stderr.write("abs_dir="+str(abs_dir)+" build_dir="+os.path.join(ctx["base"], build_dir)+" rel_dir="+str(rel_dir))
  316. cppflags=cppflags+" -I"+rel_dir
  317. dep_closure=depClosure(conf, lib["deps"], False)
  318. dep_dirs=gatherIncludeDirs(conf, ctx, dep_closure)
  319. for dir in dep_dirs:
  320. abs_dir=os.path.abspath(dir)
  321. rel_dir=os.path.relpath(abs_dir, os.path.join(ctx["base"], build_dir))
  322. # sys.stderr.write("abs_dir="+str(abs_dir)+" build_dir="+os.path.join(ctx["base"], build_dir)+" rel_dir="+str(rel_dir))
  323. cppflags=cppflags+" -I"+rel_dir
  324. for dep_lib in conf["libraries"]:
  325. if dep_lib["name"] in ctx["externals"]:
  326. ext_lib=ctx["externals"][dep_lib["name"]]
  327. if "external" in ext_lib and ext_lib["external"]:
  328. if "cppflags" in ext_lib:
  329. cppflags=cppflags+" "+ext_lib["cppflags"]
  330. return cppflags
  331. def generateVCXPROJ(conf, ctx, lib, type):
  332. filename=os.path.abspath("win32\\"+lib["name"]+"\\"+lib["name"]+".vcxproj")
  333. filepath=os.path.dirname(filename)
  334. prj_uuid=lib["uuid"]
  335. sys.stderr.write("generating "+filename+"\n")
  336. try:
  337. os.makedirs(os.path.dirname(filename))
  338. except OSError:
  339. pass
  340. out=open(filename, "w")
  341. out.write("<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  342. out.write("<ItemGroup Label=\"ProjectConfigurations\">\n");
  343. out.write(" <ProjectConfiguration Include=\"Debug|Win32\">\n");
  344. out.write(" <Configuration>Debug</Configuration>\n");
  345. out.write(" <Platform>Win32</Platform>\n");
  346. out.write(" </ProjectConfiguration>\n");
  347. out.write("<ProjectConfiguration Include=\"Release|Win32\">\n");
  348. out.write(" <Configuration>Release</Configuration>\n");
  349. out.write(" <Platform>Win32</Platform>\n");
  350. out.write("</ProjectConfiguration>\n");
  351. out.write("</ItemGroup>\n");
  352. out.write("<PropertyGroup Label=\"Globals\">\n");
  353. out.write(" <ProjectGuid>{"+str(prj_uuid)+"}</ProjectGuid>\n");
  354. out.write(" <Keyword>Win32Proj</Keyword>\n");
  355. out.write(" <RootNamespace>"+lib["name"]+"</RootNamespace>\n");
  356. out.write("</PropertyGroup>\n");
  357. out.write("<Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.default.props\" />\n");
  358. out.write(" <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n");
  359. out.write(" <ConfigurationType>"+type+"</ConfigurationType>\n");
  360. out.write(" <UseDebugLibraries>true</UseDebugLibraries>\n");
  361. out.write(" <CharacterSet>Unicode</CharacterSet>\n");
  362. out.write(" </PropertyGroup>\n");
  363. out.write(" <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n");
  364. out.write(" <ConfigurationType>"+type+"</ConfigurationType>\n");
  365. out.write(" <UseDebugLibraries>false</UseDebugLibraries>\n");
  366. out.write(" <WholeProgramOptimization>true</WholeProgramOptimization>\n");
  367. out.write(" <CharacterSet>Unicode</CharacterSet>\n");
  368. out.write(" </PropertyGroup>\n");
  369. out.write("<Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.props\" />\n");
  370. out.write("<ImportGroup Label=\"ExtensionSettings\" />\n");
  371. out.write("<ImportGroup Label=\"PropertySheets\" />\n");
  372. out.write("<PropertyGroup Label=\"UserMacros\" />\n");
  373. out.write("<PropertyGroup />\n");
  374. out.write("<ItemDefinitionGroup>\n");
  375. out.write("<ClCompile>\n");
  376. dep_closure=depClosure(conf, lib["deps"], False)
  377. cpp_defs="WIN32;"
  378. for define in lib["defines"]:
  379. if lib["defines"][define] is None:
  380. cpp_defs=cpp_defs+define+";"
  381. else:
  382. cpp_defs=cpp_defs+define+"="+lib["defines"][define]+";"
  383. for dep in dep_closure:
  384. for dep_lib in conf["libraries"]:
  385. if dep_lib["name"]==dep and not isExcluded(conf, ctx, dep):
  386. for define in dep_lib["exports"]:
  387. if dep_lib["exports"][define] is None:
  388. cpp_defs=cpp_defs+define+";"
  389. else:
  390. cpp_defs=cpp_defs+define+"="+dep_lib["defines"][define]+";"
  391. cpp_defs=cpp_defs+"_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)"
  392. out.write("<PreprocessorDefinitions>"+cpp_defs+"</PreprocessorDefinitions>\n" )
  393. AdditionalIncludeDirectories=""
  394. for dir in lib["header"]["includes"]:
  395. AdditionalIncludeDirectories=AdditionalIncludeDirectories+os.path.relpath(os.path.abspath(dir), filepath)+";"
  396. dep_dirs=gatherIncludeDirs(conf, ctx, dep_closure)
  397. for dir in dep_dirs:
  398. abs_path=os.path.relpath(os.path.abspath(dir), filepath)
  399. AdditionalIncludeDirectories=AdditionalIncludeDirectories+abs_path+";"
  400. AdditionalIncludeDirectories=AdditionalIncludeDirectories+"%(AdditionalIncludeDirectories)"
  401. out.write("<AdditionalIncludeDirectories>"+AdditionalIncludeDirectories+"</AdditionalIncludeDirectories>\n")
  402. if "c99"==lib["mode"]:
  403. out.write("<CompileAs>CompileAsCpp</CompileAs>\n");
  404. if ""!=lib["align"]:
  405. out.write("<StructMemberAlignment>"+lib["align"]+"Byte</StructMemberAlignment>\n");
  406. out.write("</ClCompile>\n");
  407. if "Application"==type:
  408. out.write("<Link>\n");
  409. out.write("<AdditionalDependencies>")
  410. if ctx["platform"] in conf["platforms"]:
  411. ext_libs=conf["platforms"][ctx["platform"]]["libs"]
  412. else:
  413. ext_libs={}
  414. # sys.stderr.write ext_libs
  415. for ext_lib in ext_libs:
  416. # sys.stderr.write ext_lib
  417. out.write(ext_lib+";")
  418. out.write("%(AdditionalDependencies)</AdditionalDependencies> \n")
  419. out.write("</Link>\n");
  420. out.write("</ItemDefinitionGroup>\n");
  421. out.write("<ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n")
  422. out.write("<ClCompile>\n");
  423. out.write("<WarningLevel>Level3</WarningLevel>\n")
  424. out.write("<Optimization>Disabled</Optimization>\n")
  425. out.write("<DisableSpecificWarnings>4996</DisableSpecificWarnings>\n")
  426. out.write("</ClCompile>\n");
  427. if "Application"==type:
  428. out.write("<Link>\n");
  429. out.write("<GenerateDebugInformation>true</GenerateDebugInformation>\n")
  430. out.write("<SubSystem>Console</SubSystem>\n")
  431. out.write("</Link>\n");
  432. out.write("</ItemDefinitionGroup>\n");
  433. out.write("<ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n")
  434. out.write("<ClCompile>\n")
  435. out.write("<Optimization>MaxSpeed</Optimization>\n")
  436. out.write("<DisableSpecificWarnings>4996</DisableSpecificWarnings>\n")
  437. out.write("<FunctionLevelLinking>true</FunctionLevelLinking> \n")
  438. out.write("<IntrinsicFunctions>true</IntrinsicFunctions> \n")
  439. out.write("</ClCompile>\n");
  440. if "Application"==type:
  441. out.write("<Link>\n");
  442. out.write("<GenerateDebugInformation>false</GenerateDebugInformation>\n")
  443. out.write("<SubSystem>Console</SubSystem>\n")
  444. out.write("<EnableCOMDATFolding>true</EnableCOMDATFolding>\n")
  445. out.write("<OptimizeReferences>true</OptimizeReferences>\n")
  446. out.write("</Link>\n");
  447. out.write("</ItemDefinitionGroup>\n");
  448. out.write("<ItemGroup>\n");
  449. for file in lib["header"]["files"]:
  450. if "h"==file["ext"]:
  451. out.write(" <ClInclude Include=\""+os.path.relpath(file["path"], filepath)+"\"/>\n");
  452. out.write("</ItemGroup>\n");
  453. out.write("<ItemGroup>\n");
  454. for file in lib["source"]["files"]:
  455. if "cpp"==file["ext"] or "c"==file["ext"] or "s"==file["ext"]:
  456. out.write(" <ClCompile Include=\""+os.path.relpath(file["path"], filepath)+"\">\n");
  457. out.write(" </ClCompile>\n");
  458. out.write("</ItemGroup>\n");
  459. out.write("<ItemGroup>\n");
  460. for dep in dep_closure:
  461. for dep_lib in conf["libraries"]:
  462. if dep_lib["name"]==dep and not isExcluded(conf, ctx, dep_lib["name"]):
  463. out.write("<ProjectReference Include=\"../"+dep_lib["name"]+"/"+dep_lib["name"]+".vcxproj\">\n")
  464. out.write("<Project>{"+str(dep_lib["uuid"])+"}</Project>\n")
  465. out.write("</ProjectReference>\n")
  466. out.write("</ItemGroup>\n")
  467. out.write("<Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.targets\" />\n");
  468. out.write("<ImportGroup Label=\"ExtensionTargets\" />\n");
  469. out.write("</Project>\n");
  470. out.close()
  471. def writeProject(out, solution_uuid, lib):
  472. out.write("Project(\"{"+str(solution_uuid)+"}\") = \""+lib["name"]+"\", \""+lib["name"]+"\\"+lib["name"]+".vcxproj\", \"{"+str(lib["uuid"])+"}\"\n")
  473. out.write("EndProject\n")
  474. def generatePlatformList(conf, ctx):
  475. add=[]
  476. for platform in conf["platforms"]:
  477. if platformSubseteqTest(ctx["platform"], platform):
  478. add.append(platform)
  479. add.sort(platformCompare)
  480. return add
  481. def generateConfiguration(ctx, includes, platform):
  482. conf={ "libraries": [], "tools": [], "platforms": {}, "mapping": {}, "format": {} }
  483. ctx["platform"]=platform
  484. for include in includes:
  485. parseConfiguration(conf, include, ctx)
  486. if ctx["platform"] not in conf["platforms"]:
  487. add=generatePlatformList(conf, ctx);
  488. if len(add)>0:
  489. conf["platforms"][ctx["platform"]]=conf["platforms"][add[0]]
  490. return conf
  491. def generateWin32(ctx, source):
  492. platform=ctx["platforms"][0]
  493. conf=generateConfiguration(ctx, [source], platform)
  494. sys.stderr.write("generate win32 project")
  495. for lib in conf["libraries"]:
  496. if not isExcluded(conf, ctx, lib["name"]):
  497. generateVCXPROJ(conf, ctx, lib, "StaticLibrary")
  498. for tool in conf["tools"]:
  499. if not isExcluded(conf, ctx, tool["name"]):
  500. generateVCXPROJ(conf, ctx, tool, "Application")
  501. solution_file=os.path.abspath("win32\\solution.sln")
  502. solution_uuid=uuid.uuid5(uuid.NAMESPACE_URL, solution_file)
  503. out=open(solution_file, "w")
  504. out.write("\n")
  505. out.write("Microsoft Visual Studio Solution File, Format Version 11.00\n")
  506. for lib in conf["libraries"]:
  507. if not isExcluded(conf, ctx, lib["name"]):
  508. writeProject(out, solution_uuid, lib)
  509. for tool in conf["tools"]:
  510. if not isExcluded(conf, ctx, tool["name"]):
  511. writeProject(out, solution_uuid, tool)
  512. out.write("\n")
  513. out.close()
  514. def writeSourceRules(conf, ctx, lib_type, out, lib, build_dir):
  515. dirs={}
  516. sources=gatherSources(conf, ctx, lib)
  517. out.write("objs_"+lib["name"]+"=\\\n")
  518. for source in sources:
  519. out.write(" $(BUILD_DIR)/"+os.path.splitext(source[0])[0]+".o\\\n")
  520. dirs[os.path.dirname(source[0])]=None
  521. out.write(" \n")
  522. out.write(lib["name"]+"_includes="+generateCPPFLAGS(conf, ctx, lib, build_dir)+"\n")
  523. out.write("\n")
  524. includes="$("+lib["name"]+"_includes)"
  525. lib_type_flags=""
  526. if lib_type=="shared":
  527. if platformSubseteqTest(ctx["platform"], "linux-*-gcc-*"):
  528. lib_type_flags=" -fPIC -DPIC"
  529. elif platformSubseteqTest(ctx["platform"], "darwin-*-gcc-*"):
  530. lib_type_flags=" -fno-common"
  531. # write special rules
  532. for source in sources:
  533. file=source[1]
  534. if "x" in file:
  535. special="-x "+file["x"]+" "
  536. out.write("$(BUILD_DIR)/"+os.path.splitext(source[0])[0]+".o: $(SRC_DIR)/"+source[0]+"\n")
  537. out.write("\t@mkdir -p $(dir $@)\n")
  538. out.write("\t$(CC) $(CFLAGS) $(CFLAGS_C) $(CPPFLAGS) "+special+includes+lib_type_flags+" -c \"$<\" -o \"$@\"\n")
  539. # write generic rules
  540. for dir in dirs:
  541. for ext in EXT_MAPPING:
  542. if EXT_MAPPING[ext]=='c' or EXT_MAPPING[ext]=='s':
  543. out.write("$(BUILD_DIR)/"+dir+"/%.o: $(SRC_DIR)/"+dir+"/%"+ext+"\n")
  544. out.write("\t@mkdir -p $(dir $@)\n")
  545. out.write("\t$(CC) $(CFLAGS) $(CFLAGS_C) $(CPPFLAGS) "+includes+lib_type_flags+" -c \"$<\" -o \"$@\"\n")
  546. out.write("\n")
  547. elif EXT_MAPPING[ext]=='cpp':
  548. out.write("$(BUILD_DIR)/"+dir+"/%.o: $(SRC_DIR)/"+dir+"/%"+ext+"\n")
  549. out.write("\t@mkdir -p $(dir $@)\n")
  550. out.write("\t$(CC) $(CFLAGS) $(CFLAGS_CPP) $(CPPFLAGS) "+includes+lib_type_flags+" -c \"$<\" -o \"$@\"\n")
  551. out.write("\n")
  552. def writeMakefileFlags(conf, ctx, out):
  553. platform=ctx["platform"]
  554. out.write("CC="+conf["platforms"][platform]["cc"]+"\n");
  555. out.write("AR="+conf["platforms"][platform]["ar"]+"\n");
  556. out.write("CFLAGS="+conf["platforms"][platform]["cflags"]+"\n")
  557. out.write("CFLAGS_C="+conf["platforms"][platform]["cflags_c"]+"\n")
  558. out.write("CFLAGS_CPP="+conf["platforms"][platform]["cflags_cpp"]+"\n")
  559. out.write("CPPFLAGS="+conf["platforms"][platform]["cppflags"]+"\n");
  560. out.write("LDFLAGS="+conf["platforms"][platform]["ldflags"]+"\n");
  561. def getSharedExt(conf, ctx):
  562. if platformSubseteqTest(ctx["platform"], "darwin-*-gcc-*"):
  563. return ".dylib"
  564. else:
  565. return ".so"
  566. def generateExtLDFlags(conf, ctx):
  567. ext_ldflags=""
  568. for dep_lib in conf["libraries"]:
  569. if dep_lib["name"] in ctx["externals"]:
  570. ext_lib=ctx["externals"][dep_lib["name"]]
  571. if "external" in ext_lib and ext_lib["external"]:
  572. if "ldflags" in ext_lib:
  573. ext_ldflags=ext_ldflags+" "+ext_lib["ldflags"]
  574. return ext_ldflags
  575. def generateLDFlags(conf, ctx, lib_type, initial_deps, exclude_deps, lib_form):
  576. ret=""
  577. deps=depClosure(conf, initial_deps, True)
  578. for dep in deps:
  579. if not(dep in exclude_deps):
  580. if lib_type=="static":
  581. ret=ret+" $(BUILD_DIR)/lib"+dep+".a"
  582. if lib_type=="shared":
  583. if lib_form:
  584. ret=ret+" -L$(BUILD_DIR) -l"+dep
  585. else:
  586. ret=ret+" $(BUILD_DIR)/lib"+dep+getSharedExt(conf, ctx)
  587. return ret
  588. def generateLibraryMakefile(conf, ctx, lib_type, lib, filename, build_dir, src_dir):
  589. sys.stderr.write("generating "+filename+"\n")
  590. if not os.path.exists(os.path.dirname(filename)):
  591. os.makedirs(os.path.dirname(filename))
  592. out=open(filename, "w")
  593. out.write("# Generated.\n")
  594. # compiler flags
  595. writeMakefileFlags(conf, ctx, out)
  596. out.write("BUILD_DIR=.\n")
  597. out.write("SRC_DIR="+src_dir+"\n")
  598. out.write("\n")
  599. out.write(".PHONY: all clean\n")
  600. out.write("\n")
  601. out.write("# library "+lib["name"]+"\n")
  602. out.write("all:")
  603. if lib_type=="static":
  604. out.write(" $(BUILD_DIR)/lib"+lib["name"]+".a")
  605. if lib_type=="shared":
  606. out.write(" $(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx))
  607. out.write("\n\n")
  608. writeSourceRules(conf, ctx, lib_type, out, lib, build_dir)
  609. if lib_type=="static":
  610. out.write("$(BUILD_DIR)/lib"+lib["name"]+".a: $(objs_"+lib["name"]+")\n")
  611. out.write("\t$(AR) -rcs $(BUILD_DIR)/lib"+lib["name"]+".a $(objs_"+lib["name"]+")\n")
  612. out.write("\n")
  613. if lib_type=="shared":
  614. ld_deps=generateLDFlags(conf, ctx, lib_type, lib["deps"], [lib["name"]], True)+generateExtLDFlags(conf, ctx)
  615. out.write("$(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx)+": $(objs_"+lib["name"]+")\n")
  616. if platformSubseteqTest(ctx["platform"], "darwin-*-gcc-*"):
  617. out.write("\t$(CC) -dynamiclib -install_name lib"+lib["name"]+getSharedExt(conf, ctx)+" -o $(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx)+" $(objs_"+lib["name"]+")"+ld_deps+"\n")
  618. else:
  619. out.write("\t$(CC) -shared -Wl,-soname,lib"+lib["name"]+getSharedExt(conf, ctx)+" -Wl,-z,defs -fPIC -o $(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx)+" $(objs_"+lib["name"]+")"+ld_deps+" -lc\n")
  620. out.write("\tstrip --remove-section=.comment --remove-section=.note --strip-unneeded $(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx))
  621. out.write("\n")
  622. out.write("clean:\n")
  623. out.write("\trm -rf $(BUILD_DIR)/lib"+lib["name"]+".a $(BUILD_DIR)/lib"+lib["name"]+getSharedExt(conf, ctx)+" $(objs_"+lib["name"]+")\n")
  624. out.close()
  625. def generateLibraryInclude(conf, ctx, lib, filename, build_dir, src_dir):
  626. sys.stderr.write("generating "+filename+"\n")
  627. if not os.path.exists(os.path.dirname(filename)):
  628. os.makedirs(os.path.dirname(filename))
  629. ret=[]
  630. lib_deps=lib["deps"]
  631. lib_deps.append(lib["name"])
  632. # out.write("deps="+str(lib["deps"])+"\n");
  633. dep_closure=depClosure(conf, lib_deps, False)
  634. dep_dirs=gatherIncludeDirs(conf, ctx, dep_closure)
  635. for dep in dep_dirs:
  636. dep_dir=os.path.join(ctx["base"], str(dep))
  637. if dep_dir in conf["mapping"]:
  638. dep_dir=conf["mapping"][dep_dir]
  639. if dep_dir in conf["format"]:
  640. ln_src=os.path.join(build_dir, "include", os.path.relpath(conf["format"][dep_dir], ctx["base"]).format(platform="platform").replace(os.sep, "_"))
  641. ln_dst=os.path.join(src_dir, dep)
  642. if os.path.exists(ln_src):
  643. os.remove(ln_src)
  644. print("symlink("+ln_dst+", "+ln_src+")"+str(os.path.exists(ln_src)))
  645. os.symlink(ln_dst, ln_src)
  646. ret.append(ln_src)
  647. else:
  648. ret.append(dep)
  649. out=open(filename, "w")
  650. for file in ret:
  651. out.write(file+"\n")
  652. out.close()
  653. def generateToolMakefile(conf, ctx, lib_type, tool, filename, build_dir, src_dir):
  654. sys.stderr.write("generating "+filename+"\n")
  655. if not os.path.exists(os.path.dirname(filename)):
  656. os.makedirs(os.path.dirname(filename))
  657. out=open(filename, "w")
  658. out.write("# Generated.\n")
  659. # compiler flags
  660. writeMakefileFlags(conf, ctx, out)
  661. out.write("BUILD_DIR=.\n")
  662. out.write("SRC_DIR="+src_dir+"\n")
  663. out.write("\n")
  664. out.write(".PHONY: all clean\n")
  665. out.write("\n")
  666. out.write("# tool "+tool["name"]+"\n")
  667. out.write("all: $(BUILD_DIR)/"+ tool["name"]+"\n\n")
  668. writeSourceRules(conf, ctx, "static", out, tool, build_dir)
  669. out.write(tool["name"]+"_ld="+generateLDFlags(conf, ctx, lib_type, tool["deps"], [], False)+"\n")
  670. # deps=depClosure(conf, tool["deps"], True)
  671. # for dep in deps:
  672. # if lib_type=="static":
  673. # out.write(" $(BUILD_DIR)/lib"+ dep +".a")
  674. # if lib_type=="shared":
  675. # out.write(" $(BUILD_DIR)/lib"+ dep +getSharedExt(conf, ctx))
  676. # out.write("\n")
  677. ext_ldflags=generateExtLDFlags(conf, ctx)
  678. # ext_ldflags=""
  679. # for dep_lib in conf["libraries"]:
  680. # if dep_lib["name"] in ctx["externals"]:
  681. # ext_lib=ctx["externals"][dep_lib["name"]]
  682. # if "external" in ext_lib and ext_lib["external"]:
  683. # if "ldflags" in ext_lib:
  684. # ext_ldflags=ext_ldflags+" "+ext_lib["ldflags"]
  685. out.write("$(BUILD_DIR)/"+tool["name"]+": $(objs_"+tool["name"]+") $("+tool["name"]+"_ld)\n")
  686. static_flag=""
  687. if lib_type=="static" and platformSubseteqTest(ctx["platform"], "linux-*-gcc-*"):
  688. static_flag=" --static"
  689. out.write("\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(BUILD_DIR)/"+tool["name"]+" $(objs_"+tool["name"]+") "+generateLDFlags(conf, ctx, lib_type, tool["deps"], [], True)+ ext_ldflags+" $(LDFLAGS)\n")
  690. out.write("\n")
  691. out.write("clean:\n")
  692. out.write("\trm -f $(BUILD_DIR)/"+tool["name"]+" $(objs_"+tool["name"]+")\n")
  693. def generateTypedMakefile(conf, ctx, lib_type):
  694. obj_dir=os.path.join("build", ctx["platform"], lib_type)
  695. filename=os.path.join(obj_dir, "Makefile")
  696. sys.stderr.write("generating "+filename+"\n")
  697. if not os.path.exists(os.path.dirname(filename)):
  698. os.makedirs(os.path.dirname(filename))
  699. out=open(filename, "w")
  700. out.write("# Generated.\n")
  701. # generate phony targets
  702. out.write(".PHONY: all clean tools dep")
  703. for lib in conf["libraries"]:
  704. if not lib["external"]:
  705. out.write(" "+lib["name"])
  706. for tool in conf["tools"]:
  707. out.write(" "+tool["name"])
  708. out.write("\n")
  709. # all
  710. out.write("all:")
  711. for lib in conf["libraries"]:
  712. if not lib["external"]:
  713. out.write(" "+lib["name"])
  714. out.write(" tools\n")
  715. # tools
  716. out.write("tools:")
  717. for tool in conf["tools"]:
  718. out.write(" "+tool["name"])
  719. out.write("\n")
  720. # generate libs
  721. for lib in conf["libraries"]:
  722. if not lib["external"]:
  723. makefile=os.path.join(obj_dir, "Makefile."+lib["name"])
  724. generateLibraryMakefile(conf, ctx, lib_type, lib, makefile, obj_dir, os.path.join("..", "..", ".."))
  725. libincludefile=os.path.join(obj_dir, "include", lib["name"]+".inc")
  726. generateLibraryInclude(conf, ctx, lib, libincludefile, obj_dir, os.path.join("..", "..", "..", ".."))
  727. out.write(lib["name"]+":")
  728. deps=depClosure(conf, lib["deps"], True)
  729. for dep in deps:
  730. if lib["name"]!=dep:
  731. out.write(" "+dep)
  732. out.write("\n")
  733. out.write("\t$(MAKE) -f "+"Makefile."+lib["name"]+"\n\n")
  734. # generate tools
  735. for tool in conf["tools"]:
  736. makefile=os.path.join(obj_dir, "Makefile."+tool["name"])
  737. generateToolMakefile(conf, ctx, lib_type, tool, makefile, obj_dir, os.path.join("..", "..", ".."))
  738. deps=depClosure(conf, tool["deps"], True)
  739. out.write(tool["name"]+":")
  740. for dep in deps:
  741. if tool["name"]!=dep:
  742. out.write(" "+dep)
  743. out.write("\n")
  744. out.write("\t$(MAKE) -f "+"Makefile."+ tool["name"]+"\n\n")
  745. out.write("clean:\n")
  746. for lib in conf["libraries"]:
  747. if not lib["external"]:
  748. out.write("\t$(MAKE) -f "+"Makefile."+lib["name"]+" clean\n")
  749. for tool in conf["tools"]:
  750. out.write("\t$(MAKE) -f "+"Makefile."+tool["name"]+" clean\n")
  751. # out.write("dep:\n")
  752. # for lib in conf["libraries"]:
  753. # generateDEPS(conf, ctx, lib, out, obj_dir, generateCPPFLAGS(conf, ctx, lib), filename)
  754. # for tool in conf["tools"]:
  755. # generateDEPS(conf, ctx, tool, out, obj_dir, generateCPPFLAGS(conf, ctx, tool), filename)
  756. out.write("package: all\n")
  757. out.write("\tcd ../../.. && ./generate.py --include Makefile.xml");
  758. for ext_lib in ctx["externals"]:
  759. if ctx["externals"][ext_lib]["external"]:
  760. name=ext_lib
  761. if "xml"==ext_lib:
  762. name="libxml"
  763. out.write(" --with-"+name+"=yes")
  764. if "cppflags" in ctx["externals"][ext_lib]:
  765. out.write(" --with-"+name+"-cppflags=\""+ctx["externals"][ext_lib]["cppflags"]+"\"")
  766. if "ldflags" in ctx["externals"][ext_lib]:
  767. out.write(" --with-"+name+"-ldflags=\""+ctx["externals"][ext_lib]["ldflags"]+"\"")
  768. out.write(" --package package_"+lib_type+".zip --type "+lib_type)
  769. out.write(" --config-dir "+ctx["config"]+" "+ctx["platform"])
  770. out.write("\n")
  771. obj_dir=os.path.join(ctx["base"], "build", ctx["platform"], lib_type)
  772. tool_ext=""
  773. if lib_type=="static":
  774. lib_ext=".a"
  775. else:
  776. lib_ext=getSharedExt(conf, ctx)
  777. lib_prefix="lib"
  778. base="$(DESTDIR)/usr/"
  779. out.write("install: all\n")
  780. for lib in conf["libraries"]:
  781. if not lib["external"] and not isExcluded(conf, ctx, lib["name"]):
  782. for include in lib["header"]["files"]:
  783. if None!=include["install"]:
  784. target=base+"include/"
  785. if len(include["install"])>0:
  786. target=target+include["install"]+"/"
  787. target=target+os.path.split(include["path"])[1]
  788. out.write("\t@mkdir -p "+os.path.dirname(target)+"\n")
  789. out.write("\t@cp "+include["path"]+" "+target+"\n")
  790. target=base+"lib/"+lib_prefix+lib["name"]+lib_ext
  791. out.write("\t@mkdir -p "+os.path.dirname(target)+"\n")
  792. if lib_type=="shared":
  793. out.write("\t@cp "+os.path.join(obj_dir, lib_prefix+lib["name"]+lib_ext)+" "+target+".1\n")
  794. out.write("\tldconfig -v -n "+os.path.dirname(target)+"\n")
  795. else:
  796. out.write("\t@cp "+os.path.join(obj_dir, lib_prefix+lib["name"]+lib_ext)+" "+target+"\n")
  797. for tool in conf["tools"]:
  798. if not isExcluded(conf, ctx, tool["name"]):
  799. target=base+"bin/"+tool["name"]+tool_ext
  800. out.write("\t@mkdir -p "+os.path.dirname(target)+"\n")
  801. out.write("\t@cp "+os.path.join(obj_dir, tool["name"]+tool_ext)+" "+target+"\n")
  802. target=base+"lib/pkgconfig/libopc.pc"
  803. out.write("\t@mkdir -p "+os.path.dirname(target)+"\n")
  804. out.write("\t@cp "+os.path.join(ctx["base"], "config", "libopc.pc")+" "+target+"\n")
  805. out.close()
  806. def generateTypedMakefiles(ctx, source, lib_types):
  807. for platform in ctx["platforms"]:
  808. conf=generateConfiguration(ctx, [source], platform)
  809. if ctx["platform"] in conf["platforms"]:
  810. for lib_type in lib_types:
  811. generateTypedMakefile(conf, ctx, lib_type)
  812. else:
  813. parseError("platform "+ctx["platform"]+" is unknown.")
  814. sys.stderr.write("available platforms:");
  815. for platform in conf["platforms"]:
  816. sys.stderr.write(platform+"\n")
  817. out=open("Makefile", "w")
  818. out.write(".PHONY: all clean");
  819. for platform in ctx["platforms"]:
  820. for lib_type in lib_types:
  821. out.write(" "+platform+"."+lib_type);
  822. for lib_type in lib_types:
  823. out.write(" "+lib_type);
  824. out.write("\n");
  825. out.write("all:");
  826. for lib_type in lib_types:
  827. out.write(" "+lib_type);
  828. out.write("\n");
  829. for lib_type in lib_types:
  830. out.write(lib_type+":");
  831. for platform in ctx["platforms"]:
  832. out.write(" "+platform+"."+lib_type);
  833. out.write("\n");
  834. for lib_type in lib_types:
  835. for platform in ctx["platforms"]:
  836. out.write(platform+"."+lib_type+": build"+os.sep+platform+os.sep+lib_type+os.sep+"Makefile\n");
  837. out.write("\t@$(MAKE) -C build"+os.sep+platform+os.sep+lib_type+"\n");
  838. out.write("clean:\n")
  839. for lib_type in lib_types:
  840. for platform in ctx["platforms"]:
  841. out.write("\t@$(MAKE) -C build"+os.sep+platform+os.sep+lib_type+" clean\n");
  842. out.write("package:\n")
  843. for lib_type in lib_types:
  844. for platform in ctx["platforms"]:
  845. out.write("\t@$(MAKE) -C build"+os.sep+platform+os.sep+lib_type+" package\n")
  846. out.write("install:\n")
  847. for lib_type in lib_types:
  848. for platform in ctx["platforms"]:
  849. out.write("\t@$(MAKE) -C build"+os.sep+platform+os.sep+lib_type+" install\n")
  850. out.close()
  851. def dumpEnvironment(ctx, includes, platform):
  852. conf=generateConfiguration(ctx, includes, platform)
  853. if platform in conf["platforms"]:
  854. p=conf["platforms"][platform]
  855. # print(str(p))
  856. host=""
  857. if "host" in p:
  858. host="HOST=\""+p["host"]+"\" "
  859. sys.stdout.write("export CC=\""+p["cc"]+"\" "+
  860. "export AR=\""+p["ar"]+"\" "+
  861. "export LD=\""+p["ld"]+"\" "
  862. "export CPPFLAGS=\""+p["cppflags"]+"\" "
  863. "export CFLAGS=\""+p["cflags"]+"\" "+
  864. host
  865. )
  866. else:
  867. parseError("platform "+platform+" is unknown.")
  868. sys.stderr.write("available platforms:");
  869. for platform in conf["platforms"]:
  870. sys.stderr.write(platform)
  871. def set_external_flag(ctx, lib, flag, value):
  872. # sys.stderr.write("SETTING "+lib+" "+flag+" "+str(value)+"\n")
  873. if lib in ctx["externals"]:
  874. ctx["externals"][lib][flag]=value
  875. else:
  876. ctx["externals"][lib]={flag: value}
  877. def is_external(ctx, lib):
  878. if lib in ctx["externals"]:
  879. if "external" in ctx["externals"][lib]:
  880. return ctx["externals"][lib]["external"]
  881. else:
  882. return False
  883. else:
  884. return False
  885. def generateZipPackage(ctx, source, lib_type, install_zip):
  886. zip=zipfile.ZipFile(install_zip, 'w', zipfile.ZIP_DEFLATED)
  887. base=os.path.splitext(install_zip)[0]+"/"
  888. platform=ctx["platforms"][0]
  889. conf=generateConfiguration(ctx, [source], platform)
  890. if platformSubseteqTest(ctx["platforms"][0], "win32-*-*"):
  891. obj_dir=os.path.join("win32", ctx["platform"].split('-')[1])
  892. tool_ext=".exe"
  893. lib_exts=[".lib"]
  894. if (lib_type=="shared"):
  895. shared_exts.append(".dll")
  896. lib_prefix=""
  897. else:
  898. obj_dir=os.path.join("build", ctx["platform"], lib_type)
  899. tool_ext=""
  900. if lib_type=="shared":
  901. lib_exts=[".dylib"]
  902. else:
  903. lib_exts=[".a"]
  904. lib_prefix="lib"
  905. for lib in conf["libraries"]:
  906. if not lib["external"] and not isExcluded(conf, ctx, lib["name"]):
  907. for include in lib["header"]["files"]:
  908. if None!=include["install"]:
  909. target=base+"include/"
  910. if len(include["install"])>0:
  911. target=target+include["install"]+"/"
  912. target=target+os.path.split(include["path"])[1]
  913. zip.write(include["path"], target)
  914. for lib_ext in lib_exts:
  915. target=base+"lib/"+lib_prefix+lib["name"]+lib_ext
  916. # print "ADD "+os.path.join(obj_dir, lib_prefix+lib["name"]+lib_ext)
  917. zip.write(os.path.join(obj_dir, lib_prefix+lib["name"]+lib_ext), target)
  918. for tool in conf["tools"]:
  919. if not isExcluded(conf, ctx, tool["name"]):
  920. target=base+"bin/"+tool["name"]+tool_ext
  921. zip.write(os.path.join(obj_dir, tool["name"]+tool_ext), target)
  922. zip.write(os.path.join("third_party", "LICENSE"), base+"/LICENSE.THIRD_PARTY")
  923. zip.write("LICENSE", base+"/LICENSE")
  924. zip.close()
  925. def installFiles(ctx, source, lib_type, base):
  926. copy_ctx=None
  927. platform=ctx["platforms"][0]
  928. conf=generateConfiguration(ctx, [source], platform)
  929. if platformSubseteqTest(ctx["platforms"][0], "win32-*-*"):
  930. obj_dir=os.path.join("win32", ctx["platform"].split('-')[1])
  931. tool_ext=".exe"
  932. lib_exts=[".lib"]
  933. lib_prefix=""
  934. else:
  935. obj_dir=os.path.join("build", ctx["platform"], lib_type)
  936. tool_ext=""
  937. lib_exts=[".a", getSharedExt(conf, ctx)]
  938. lib_prefix="lib"
  939. for lib in conf["libraries"]:
  940. if not lib["external"] and not isExcluded(conf, ctx, lib["name"]):
  941. for include in lib["header"]["files"]:
  942. if None!=include["install"]:
  943. target=base+"include/"
  944. if len(include["install"])>0:
  945. target=target+include["install"]+"/"
  946. target=target+os.path.split(include["path"])[1]
  947. copyFile(copy_ctx, include["path"], target)
  948. for lib_ext in lib_exts:
  949. target=base+"lib/"+lib_prefix+lib["name"]+lib_ext
  950. copyFile(copy_ctx, os.path.join(obj_dir, lib_prefix+lib["name"]+lib_ext), target)
  951. for tool in conf["tools"]:
  952. if not isExcluded(conf, ctx, tool["name"]):
  953. target=base+"bin/"+tool["name"]+tool_ext
  954. copyFile(copy_ctx, os.path.join(obj_dir, tool["name"]+tool_ext), target)
  955. copyFile(copy_ctx, os.path.join("third_party", "LICENSE"), base+"/LICENSE.THIRD_PARTY")
  956. copyFile(copy_ctx, "LICENSE", base+"/LICENSE")
  957. def usage():
  958. print("usage:")
  959. print(" generate [--include \"Makefile.xml\"]")
  960. print(" [--config-dir \"config/\"]")
  961. print(" [--print-env \"linux-release-gcc\"]")
  962. print(" [--package \"xyz.zip/\"]")
  963. if __name__ == "__main__":
  964. try:
  965. opts, args = getopt.getopt(sys.argv[1:], "h", ["help", "include=", "config-dir=", "print-env=",
  966. "package=", "install=",
  967. "with-zlib-cppflags=", "with-zlib-ldflags=", "with-zlib=",
  968. "with-libxml-cppflags=", "with-libxml-ldflags=", "with-libxml=",
  969. "type="])
  970. except(getopt.GetoptError, err):
  971. # print help information and exit:
  972. print(str(err)) # will print something like "option -a not recognized"
  973. usage()
  974. sys.exit(2)
  975. ctx={ "base": os.path.abspath(os.curdir), "root": os.path.abspath(os.curdir), "platform": "?-?-?", "platforms": [], "externals": {} }
  976. ctx["config"]=ctx["base"]
  977. includes=[]
  978. dump_env=[]
  979. install_zip=None
  980. target_type="static"
  981. for o, a in opts:
  982. if o in ("-h", "--help"):
  983. usage()
  984. sys.exit(0)
  985. elif o in ("--include"):
  986. includes.append(a)
  987. elif o in ("--config-dir"):
  988. ctx["config"]=os.path.join(ctx["base"], a)
  989. elif o in ("--print-env"):
  990. args=[]
  991. dump_env=[a]
  992. elif o in ("--package"):
  993. install_zip=a
  994. elif o in ("--install"):
  995. install_files=a
  996. elif o in ("--with-zlib"):
  997. set_external_flag(ctx, "zlib", "external", "yes"==a)
  998. elif o in ("--with-zlib-cppflags"):
  999. set_external_flag(ctx, "zlib", "cppflags", a)
  1000. elif o in ("--with-zlib-ldflags"):
  1001. set_external_flag(ctx, "zlib", "ldflags", a)
  1002. elif o in ("--with-libxml"):
  1003. set_external_flag(ctx, "xml", "external", "yes"==a)
  1004. elif o in ("--with-libxml-cppflags"):
  1005. set_external_flag(ctx, "xml", "cppflags", a)
  1006. elif o in ("--with-libxml-ldflags"):
  1007. set_external_flag(ctx, "xml", "ldflags", a)
  1008. elif o in ("--type"):
  1009. target_type=a
  1010. for platform in args:
  1011. ctx["platforms"].append(platform)
  1012. if 1==len(ctx["platforms"]) and None!=install_zip and 1==len(includes):
  1013. ctx["root"]=os.path.abspath(os.path.split(includes[0])[0])
  1014. generateZipPackage(ctx, includes[0], target_type, install_zip)
  1015. elif 1==len(ctx["platforms"]) and platformSubseteqTest(ctx["platforms"][0], "win32-*-msvc-*") and 1==len(includes):
  1016. ctx["root"]=os.path.abspath(os.path.split(includes[0])[0])
  1017. generateWin32(ctx, includes[0])
  1018. else:
  1019. if is_external(ctx, "zlib") and is_external(ctx, "xml"):
  1020. lib_types=["static", "shared"]
  1021. else:
  1022. lib_types=["static"]
  1023. if not os.path.exists("build"):
  1024. os.makedirs("build")
  1025. f=open(os.path.join("build", "configure.ctx"), "w")
  1026. f.write(str(ctx))
  1027. f.close()
  1028. for include in includes:
  1029. ctx["root"]=os.path.abspath(os.path.split(include)[0])
  1030. generateTypedMakefiles(ctx, include, lib_types)
  1031. for platform in dump_env:
  1032. dumpEnvironment(ctx, includes, platform)