From 07642c3689edaaf04addb0853877bbd0938ecf7b Mon Sep 17 00:00:00 2001
From: Jack Jansen <jack.jansen@cwi.nl>
Date: Fri, 22 Sep 2000 23:26:55 +0000
Subject: More bits and pieces of project generation.

---
 Mac/Lib/mkcwproject/__init__.py                    | 59 +++++++++++++++
 Mac/Lib/mkcwproject/cwtalker.py                    | 26 +++++++
 Mac/Lib/mkcwproject/cwxmlgen.py                    | 87 ++++++++++++++++++++++
 Mac/Lib/mkcwproject/mkcwproject.py                 | 82 --------------------
 .../mkcwproject/template/template-searchdirs.xml   |  9 +++
 Mac/Lib/mkcwproject/template/template.prj.xml      | 20 +++--
 6 files changed, 196 insertions(+), 87 deletions(-)
 create mode 100644 Mac/Lib/mkcwproject/__init__.py
 create mode 100644 Mac/Lib/mkcwproject/cwtalker.py
 create mode 100644 Mac/Lib/mkcwproject/cwxmlgen.py
 delete mode 100644 Mac/Lib/mkcwproject/mkcwproject.py
 create mode 100644 Mac/Lib/mkcwproject/template/template-searchdirs.xml

diff --git a/Mac/Lib/mkcwproject/__init__.py b/Mac/Lib/mkcwproject/__init__.py
new file mode 100644
index 0000000..9664542
--- /dev/null
+++ b/Mac/Lib/mkcwproject/__init__.py
@@ -0,0 +1,59 @@
+import cwxmlgen
+import cwtalker
+
+def mkproject(outputfile, modulename, settings):
+	#
+	# Copy the dictionary
+	#
+	dictcopy = {}
+	for k, v in settings.items():
+		dictcopy[k] = v
+	#
+	# Fill in mac-specific values
+	#
+	dictcopy['mac_projectxmlname'] = outputfile + '.xml'
+	dictcopy['mac_exportname'] = os.path.split(outputfile)[1] + '.exp'
+	dictcopy['mac_outputdir'] = ':lib:'  # XXX Is this correct??
+	dictcopy['mac_dllname'] = modulename + '.ppc.slb'
+	dictcopy['mac_targetname'] = modulename + '.ppc'
+	of os.path.isabs(dictcopy['sysprefix']):
+		dictcopy['mac_sysprefixtype'] = 'Absolute'
+	else:
+		dictcopy['mac_sysprefixtype'] = 'Project' # XXX not sure this is right...
+	#
+	# Generate the XML for the project
+	#
+	xmlbuilder = cwxmlgen.ProjectBuilder(dictcopy)
+	xmlbuilder.generate()
+	fp = open(dictcopy['mac_projectxmlname'], "w")
+	fp.write(dict["tmp_projectxmldata"])
+	fp.close()
+	#
+	# Generate the export file
+	#
+	fp = open(outputfile + '.exp', 'w')
+	fp.write('init%s\n'%modulename)
+	fp.close()
+	#
+	# Generate the project from the xml
+	#
+	cw = cwtalker.MyCodeWarrior(start=1)
+	cw.send_timeout = AppleEvents.kNoTimeOut
+	xmlfss = macfs.FSSpec(dictcopy['mac_projectxmlname'])
+	prjfss = macfs.FSSpec(outputfile)
+	cw.my_mkproject(prjfss, xmlfss)
+	
+def buildproject(projectfile):
+	cw = cwtalker.MyCodeWarrior(start=1)
+	cw.send_timeout = AppleEvents.kNoTimeOut
+	prjfss = macfs.FSSpec(projectfile)
+	cw.open(prjfss)
+	cw.Make_Project()	# XXX Should set target
+	
+def cleanproject(projectfile):
+	cw = cwtalker.MyCodeWarrior(start=1)
+	cw.send_timeout = AppleEvents.kNoTimeOut
+	prjfss = macfs.FSSpec(projectfile)
+	cw.open(prjfss)
+	cw.Remove_Binaries()
+		
diff --git a/Mac/Lib/mkcwproject/cwtalker.py b/Mac/Lib/mkcwproject/cwtalker.py
new file mode 100644
index 0000000..fbce620
--- /dev/null
+++ b/Mac/Lib/mkcwproject/cwtalker.py
@@ -0,0 +1,26 @@
+import CodeWarrior
+
+class MyCodeWarrior(CodeWarrior.CodeWarrior):
+	# Bug in the CW OSA dictionary
+	def export(self, object, _attributes={}, **_arguments):
+		"""export: Export the project file as an XML file
+		Keyword argument _in: the XML file in which to export the project
+		Keyword argument _attributes: AppleEvent attribute dictionary
+		"""
+		_code = 'CWIE'
+		_subcode = 'EXPT'
+
+		aetools.keysubst(_arguments, self._argmap_export)
+		_arguments['----'] = _object
+
+
+		_reply, _arguments, _attributes = self.send(_code, _subcode,
+				_arguments, _attributes)
+		if _arguments.has_key('errn'):
+			raise aetools.Error, aetools.decodeerror(_arguments)
+		# XXXX Optionally decode result
+		if _arguments.has_key('----'):
+			return _arguments['----']
+
+	def my_mkproject(self, prjfile, xmlfile):
+		self.make(new=CodeWarrior.project_document, with_data=xmlfile, at=prjfile)
diff --git a/Mac/Lib/mkcwproject/cwxmlgen.py b/Mac/Lib/mkcwproject/cwxmlgen.py
new file mode 100644
index 0000000..b29786f
--- /dev/null
+++ b/Mac/Lib/mkcwproject/cwxmlgen.py
@@ -0,0 +1,87 @@
+# First attempt at automatically generating CodeWarior projects
+import os
+
+Error="gencwproject.Error"
+#
+# These templates are executed in-order.
+# 
+TEMPLATELIST= [
+	("tmp_allsources", "file", "template-allsources.xml", "sources"),
+	("tmp_linkorder", "file", "template-linkorder.xml", "sources"),
+	("tmp_grouplist", "file", "template-grouplist.xml", "sources"),
+	("tmp_extrasearchdirs", "file", "template-searchdirs.xml", "extrasearchdirs"),
+	("tmp_projectxmldata", "file", "template.prj.xml", None)
+]
+
+class ProjectBuilder:
+	def __init__(self, dict, templatelist=TEMPLATELIST, templatedir=None):
+		if templatedir == None:
+			try:
+				packagedir = os.path.split(__file__)[0]
+			except NameError:
+				packagedir = os.curdir
+			templatedir = os.path.join(packagedir, 'template')
+		if not os.path.exists(templatedir):
+			raise Error, "Cannot file templatedir"
+		self.dict = dict
+		self.templatelist = templatelist
+		self.templatedir = templatedir
+		
+	def generate(self):
+		for tmpl in self.templatelist:
+			self._generate_one_template(tmpl)
+		
+	def _generate_one_template(self, tmpl):
+		resultname, datasource, dataname, key = tmpl
+		result = ''
+		if key:
+			# This is a multi-element rule. Run for every item in dict[key]
+			if self.dict.has_key(key):
+				keyvalues = self.dict[key]
+				try:
+					if not type(keyvalues) in (type(()), type([])):
+						raise Error, "List or tuple expected for %s"%key
+					for curkeyvalue in keyvalues:
+						self.dict[key] = curkeyvalue
+						curkeyvalueresult = self._generate_one_value(datasource, dataname)
+						result = result + curkeyvalueresult
+				finally:
+					# Restore the list
+					self.dict[key] = keyvalues
+		else:
+			# Not a multi-element rule. Simply generate
+			result = self._generate_one_value(datasource, dataname)
+		# And store the result
+		self.dict[resultname] = result
+		
+	def _generate_one_value(self, datasource, dataname):
+		if datasource == 'file':
+			filepath = os.path.join(self.templatedir, dataname)
+			fp = open(filepath, "r")
+			format = fp.read()
+		elif datasource == 'string':
+			format = dataname
+		else:
+			raise Error, 'Datasource should be file or string, not %s'%datasource
+		return format % self.dict
+		
+def _test():
+	dict = {
+		"mac_projectxmlname" : "controlstrip.prj.xml",	# The XML filename (full path)
+		"mac_exportname" : "controlstrip.prj.exp",	# Export file (relative to project)
+		"mac_outputdir" : ":",	# The directory where the DLL is put (relative to project)
+		"mac_dllname" : "controlstrip.ppc.slb",	# The DLL filename (within outputdir)
+		"mac_targetname" : "controlstrip.ppc",	# The targetname within the project
+		"sysprefix" : sys.prefix,	# Where the Python sources live
+		"mac_sysprefixtype" : "Absolute",	# Type of previous pathname
+		"sources" : ["controlstripmodule.c"],
+		"extrasearchdirs": [],	# -I and -L, in unix terms
+	}
+	pb = ProjectBuilder(dict)
+	pb.generate()
+	fp = open(dict["mac_projectxmlname"], "w")
+	fp.write(dict["tmp_projectxmldata"])
+	
+if __name__ == '__main__':
+	_test()
+		
diff --git a/Mac/Lib/mkcwproject/mkcwproject.py b/Mac/Lib/mkcwproject/mkcwproject.py
deleted file mode 100644
index 7889b76..0000000
--- a/Mac/Lib/mkcwproject/mkcwproject.py
+++ /dev/null
@@ -1,82 +0,0 @@
-# First attempt at automatically generating CodeWarior projects
-import os
-
-Error="gencwproject.Error"
-#
-# These templates are executed in-order.
-# 
-TEMPLATELIST= [
-	("tmp_allsources", "file", "template-allsources.xml", "sources"),
-	("tmp_linkorder", "file", "template-linkorder.xml", "sources"),
-	("tmp_grouplist", "file", "template-grouplist.xml", "sources"),
-	("tmp_projectxmldata", "file", "template.prj.xml", None)
-]
-
-class ProjectBuilder:
-	def __init__(self, dict, templatelist=TEMPLATELIST, templatedir=None):
-		if templatedir == None:
-			try:
-				packagedir = os.path.split(__file__)[0]
-			except NameError:
-				packagedir = os.curdir
-			templatedir = os.path.join(packagedir, 'template')
-		if not os.path.exists(templatedir):
-			raise Error, "Cannot file templatedir"
-		self.dict = dict
-		self.templatelist = templatelist
-		self.templatedir = templatedir
-		
-	def generate(self):
-		for tmpl in self.templatelist:
-			self._generate_one_template(tmpl)
-		
-	def _generate_one_template(self, tmpl):
-		resultname, datasource, dataname, key = tmpl
-		result = ''
-		if key:
-			# This is a multi-element rule. Run for every item in dict[key]
-			if self.dict.has_key(key):
-				keyvalues = self.dict[key]
-				try:
-					if not type(keyvalues) in (type(()), type([])):
-						raise Error, "List or tuple expected for %s"%key
-					for curkeyvalue in keyvalues:
-						self.dict[key] = curkeyvalue
-						curkeyvalueresult = self._generate_one_value(datasource, dataname)
-						result = result + curkeyvalueresult
-				finally:
-					# Restore the list
-					self.dict[key] = keyvalues
-		else:
-			# Not a multi-element rule. Simply generate
-			result = self._generate_one_value(datasource, dataname)
-		# And store the result
-		self.dict[resultname] = result
-		
-	def _generate_one_value(self, datasource, dataname):
-		if datasource == 'file':
-			filepath = os.path.join(self.templatedir, dataname)
-			fp = open(filepath, "r")
-			format = fp.read()
-		elif datasource == 'string':
-			format = dataname
-		else:
-			raise Error, 'Datasource should be file or string, not %s'%datasource
-		return format % self.dict
-		
-def _test():
-	dict = {
-		"mac_projectxmlname" : "xxnew.prj.xml",
-		"mac_targetname" : "xxnew.ppc",
-		"mac_dllname" : "xxnew.ppc.slb",
-		"sources" : ["xxnewmodule.c"],
-		"mac_exportname" : "xxnew.prj.exp",
-	}
-	pb = ProjectBuilder(dict)
-	pb.generate()
-	fp = open(dict["mac_projectxmlname"], "w")
-	fp.write(dict["tmp_projectxmldata"])
-	
-if __name__ == '__main__':
-	_test()
-		
diff --git a/Mac/Lib/mkcwproject/template/template-searchdirs.xml b/Mac/Lib/mkcwproject/template/template-searchdirs.xml
new file mode 100644
index 0000000..3cb59a9
--- /dev/null
+++ b/Mac/Lib/mkcwproject/template/template-searchdirs.xml
@@ -0,0 +1,9 @@
+                    <SETTING>
+                        <SETTING><NAME>SearchPath</NAME>
+                            <SETTING><NAME>Path</NAME><VALUE>%(extrasearchdirs)s</VALUE></SETTING>
+                            <SETTING><NAME>PathFormat</NAME><VALUE>MacOS</VALUE></SETTING>
+                            <SETTING><NAME>PathRoot</NAME><VALUE>Absolute</VALUE></SETTING>
+                        </SETTING>
+                        <SETTING><NAME>Recursive</NAME><VALUE>false</VALUE></SETTING>
+                        <SETTING><NAME>HostFlags</NAME><VALUE>All</VALUE></SETTING>
+                    </SETTING>
diff --git a/Mac/Lib/mkcwproject/template/template.prj.xml b/Mac/Lib/mkcwproject/template/template.prj.xml
index c6e01e8..ac38544 100644
--- a/Mac/Lib/mkcwproject/template/template.prj.xml
+++ b/Mac/Lib/mkcwproject/template/template.prj.xml
@@ -92,20 +92,30 @@
                     </SETTING>
                     <SETTING>
                         <SETTING><NAME>SearchPath</NAME>
-                            <SETTING><NAME>Path</NAME><VALUE>:::</VALUE></SETTING>
+                            <SETTING><NAME>Path</NAME><VALUE>%(sysprefix)s:Mac:</VALUE></SETTING>
                             <SETTING><NAME>PathFormat</NAME><VALUE>MacOS</VALUE></SETTING>
-                            <SETTING><NAME>PathRoot</NAME><VALUE>Project</VALUE></SETTING>
+                            <SETTING><NAME>PathRoot</NAME><VALUE>%(mac_sysprefixtype)s</VALUE></SETTING>
+                        </SETTING>
+                        <SETTING><NAME>Recursive</NAME><VALUE>true</VALUE></SETTING>
+                        <SETTING><NAME>HostFlags</NAME><VALUE>All</VALUE></SETTING>
+                    </SETTING>
+                    <SETTING>
+                        <SETTING><NAME>SearchPath</NAME>
+                            <SETTING><NAME>Path</NAME><VALUE>%(sysprefix)s</VALUE></SETTING>
+                            <SETTING><NAME>PathFormat</NAME><VALUE>MacOS</VALUE></SETTING>
+                            <SETTING><NAME>PathRoot</NAME><VALUE>%(mac_sysprefixtype)s</VALUE></SETTING>
                         </SETTING>
                         <SETTING><NAME>Recursive</NAME><VALUE>true</VALUE></SETTING>
                         <SETTING><NAME>HostFlags</NAME><VALUE>All</VALUE></SETTING>
                     </SETTING>
                 </SETTING>
                 <SETTING><NAME>SystemSearchPaths</NAME>
+                    %(tmp_extrasearchdirs)s
                     <SETTING>
                         <SETTING><NAME>SearchPath</NAME>
-                            <SETTING><NAME>Path</NAME><VALUE>::::GUSI2:include:</VALUE></SETTING>
+                            <SETTING><NAME>Path</NAME><VALUE>%(sysprefix)s::GUSI2:include:</VALUE></SETTING>
                             <SETTING><NAME>PathFormat</NAME><VALUE>MacOS</VALUE></SETTING>
-                            <SETTING><NAME>PathRoot</NAME><VALUE>Project</VALUE></SETTING>
+                            <SETTING><NAME>PathRoot</NAME><VALUE>%(mac_sysprefixtype)s</VALUE></SETTING>
                         </SETTING>
                         <SETTING><NAME>Recursive</NAME><VALUE>false</VALUE></SETTING>
                         <SETTING><NAME>HostFlags</NAME><VALUE>All</VALUE></SETTING>
@@ -136,7 +146,7 @@
                 <SETTING><NAME>PostLinker</NAME><VALUE></VALUE></SETTING>
                 <SETTING><NAME>Targetname</NAME><VALUE>%(mac_targetname)s</VALUE></SETTING>
                 <SETTING><NAME>OutputDirectory</NAME>
-                    <SETTING><NAME>Path</NAME><VALUE>::PlugIns:</VALUE></SETTING>
+                    <SETTING><NAME>Path</NAME><VALUE>%(mac_outputdir)s</VALUE></SETTING>
                     <SETTING><NAME>PathFormat</NAME><VALUE>MacOS</VALUE></SETTING>
                     <SETTING><NAME>PathRoot</NAME><VALUE>Project</VALUE></SETTING>
                 </SETTING>
-- 
cgit v0.12

