1 # First attempt at automatically generating CodeWarior projects
6 Error
="gencwproject.Error"
8 # These templates are executed in-order.
11 ("tmp_allsources", "file", "template-allsources.xml", "sources"),
12 ("tmp_linkorder", "file", "template-linkorder.xml", "sources"),
13 ("tmp_grouplist", "file", "template-grouplist.xml", "sources"),
14 ("tmp_alllibraries", "file", "template-alllibraries.xml", "libraries"),
15 ("tmp_linkorderlib", "file", "template-linkorderlib.xml", "libraries"),
16 ("tmp_grouplistlib", "file", "template-grouplistlib.xml", "libraries"),
17 ("tmp_extrasearchdirs", "file", "template-searchdirs.xml", "extrasearchdirs"),
18 ("tmp_projectxmldata", "file", "template.prj.xml", None)
22 def __init__(self
, dict, templatelist
=TEMPLATELIST
, templatename
=None):
23 self
._adddefaults
(dict)
24 if templatename
== None:
25 if hasattr(MacOS
, 'runtimemodel'):
26 templatename
= 'template-%s'%MacOS
.runtimemodel
28 templatename
= 'template'
29 if os
.sep
in templatename
:
30 templatedir
= templatename
33 packagedir
= os
.path
.split(__file__
)[0]
35 packagedir
= os
.curdir
36 templatedir
= os
.path
.join(packagedir
, templatename
)
37 if not os
.path
.exists(templatedir
):
38 raise Error
, "Cannot find templatedir %s"%templatedir
40 if not dict.has_key('prefixname'):
41 if hasattr(MacOS
, 'runtimemodel') and MacOS
.runtimemodel
== "carbon":
42 dict['prefixname'] = 'mwerks_shcarbon_pch'
44 dict['prefixname'] = 'mwerks_plugin_config.h'
45 self
.templatelist
= templatelist
46 self
.templatedir
= templatedir
48 def _adddefaults(self
, dict):
49 # Set all suitable defaults set for values which were omitted.
50 if not dict.has_key('mac_outputdir'):
51 dict['mac_outputdir'] = ':lib:'
52 if not dict.has_key('stdlibraryflags'):
53 dict['stdlibraryflags'] = 'Debug'
54 if not dict.has_key('libraryflags'):
55 dict['libraryflags'] = 'Debug'
56 if not dict.has_key('initialize'):
57 dict['initialize'] = '__initialize'
58 if not dict.has_key('mac_sysprefixtype'):
59 if os
.path
.isabs(dict['sysprefix']):
60 dict['mac_sysprefixtype'] = 'Absolute'
62 dict['mac_sysprefixtype'] = 'Project' # XXX not sure this is right...
65 for tmpl
in self
.templatelist
:
66 self
._generate
_one
_template
(tmpl
)
68 def _generate_one_template(self
, tmpl
):
69 resultname
, datasource
, dataname
, key
= tmpl
72 # This is a multi-element rule. Run for every item in dict[key]
73 if self
.dict.has_key(key
):
74 keyvalues
= self
.dict[key
]
76 if not type(keyvalues
) in (type(()), type([])):
77 raise Error
, "List or tuple expected for %s"%key
78 for curkeyvalue
in keyvalues
:
79 if string
.lower(curkeyvalue
[:10]) == '{compiler}':
80 curkeyvalue
= curkeyvalue
[10:]
81 self
.dict['pathtype'] = 'CodeWarrior'
82 elif string
.lower(curkeyvalue
[:9]) == '{project}':
83 curkeyvalue
= curkeyvalue
[9:]
84 self
.dict['pathtype'] = 'Project'
85 elif curkeyvalue
[0] == '{':
86 raise Error
, "Unknown {} escape in %s"%curkeyvalue
87 elif os
.path
.isabs(curkeyvalue
):
88 self
.dict['pathtype'] = 'Absolute'
90 self
.dict['pathtype'] = 'Project'
91 if curkeyvalue
[-2:] == ':*':
92 curkeyvalue
= curkeyvalue
[:-2]
93 self
.dict['recursive'] = 'true'
95 self
.dict['recursive'] = 'false'
96 self
.dict[key
] = curkeyvalue
97 curkeyvalueresult
= self
._generate
_one
_value
(datasource
, dataname
)
98 result
= result
+ curkeyvalueresult
101 self
.dict[key
] = keyvalues
102 self
.dict['pathtype'] = None
103 del self
.dict['pathtype']
104 self
.dict['recursive'] = None
105 del self
.dict['recursive']
107 # Not a multi-element rule. Simply generate
108 result
= self
._generate
_one
_value
(datasource
, dataname
)
109 # And store the result
110 self
.dict[resultname
] = result
112 def _generate_one_value(self
, datasource
, dataname
):
113 if datasource
== 'file':
114 filepath
= os
.path
.join(self
.templatedir
, dataname
)
115 fp
= open(filepath
, "r")
117 elif datasource
== 'string':
120 raise Error
, 'Datasource should be file or string, not %s'%datasource
121 return format
% self
.dict
125 "mac_projectxmlname" : "controlstrip.prj.xml", # The XML filename (full path)
126 "mac_exportname" : "controlstrip.prj.exp", # Export file (relative to project)
127 "mac_outputdir" : ":", # The directory where the DLL is put (relative to project)
128 "mac_dllname" : "controlstrip.ppc.slb", # The DLL filename (within outputdir)
129 "mac_targetname" : "controlstrip.ppc", # The targetname within the project
130 "sysprefix" : sys
.prefix
, # Where the Python sources live
131 "mac_sysprefixtype" : "Absolute", # Type of previous pathname
132 "sources" : ["controlstripmodule.c"],
133 "extrasearchdirs": [], # -I and -L, in unix terms
135 pb
= ProjectBuilder(dict)
137 fp
= open(dict["mac_projectxmlname"], "w")
138 fp
.write(dict["tmp_projectxmldata"])
140 if __name__
== '__main__':