PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SConscript.py
Go to the documentation of this file.
1 # Note the .py extension on this file, necessary so that
2 # Doxygen parses as a Python script (but not necessary
3 # for SCons).
4 
5 # Documentation for this file. If the \file tag isn't present,
6 # this file won't be documented.
7 ## \file
8 # This file builds various files from templates in the
9 # templates/ directory in which this file resides. It should
10 # be called from the main SConscript.py script.
11 #
12 
13 ## @{
14 # \name Setup for template building
15 ###############################################################################
16 
17 import os
18 import string
19 from string import Template
20 
21 # Import environment from calling SConstruct context
22 Import('env')
23 
24 ## Define the directory in which the templates reside
25 templateDir = './'
26 
27 ## Define the include directory destination for .h templates
28 includeDir = '../lib/include/'
29 
30 ## Define the common directory destination for .c templates
31 commonDir = '../lib/common/'
32 
33 ## @}
34 
35 ## @{
36 # \name Functions supporting template file builds
37 ###############################################################################
38 
39 ## A function to do search and replaces on a file, optionally
40 # appending the results to the output file.
41 # \param sourceFileName Name of the source file
42 # \param destFileName Name of the destination file
43 # \param mapping A dictionary of key=value pairs used by
44 # Template to perform the search and replace operation.
45 # \param openMode Mode with which to open the destination file.
46 # Use the default of 'wb' to overwrite the
47 # destination file with the replaced source file. Use 'ab' to
48 # append the replaced source file to the destination file.
49 # The 'b' at end end of 'wb'/'ab' keeps line endings Unix-style,
50 # even when run under Windows.
51 def searchAndReplace(sourceFile, destFileName, mapping, openMode='wb'):
52  outFile = open(destFileName, openMode)
53  template = Template(open(sourceFile).read())
54  outFile.write(template.substitute(mapping))
55 
56 ## A function to create a .c/.h file from multiple replaces through
57 # a template.
58 # \param templateFileName Name of the template file to use
59 # \param destFileName Name of the destination file to create
60 # by accumulating multiple passes through the template
61 # file
62 # \param iters Number of iterations (passes) through the file
63 # to make. During each iteration, $x is replaced by the
64 # iteration number.
65 def genFromTemplate(templateFileName, destFileName, iters):
66  openMode = 'wb'
67  for i in range(1, iters + 1):
68  searchAndReplace(templateFileName, destFileName,
69  {'x' : str(i)}, openMode)
70  openMode = 'ab'
71 
72 ## Builds a .c from a template -- SCons Builder function formation.
73 # Function will build the .c as requested. Has the proper
74 # inputs, outputs and returns value to be registered SCons environment
75 # as a builder function
76 # \param target Target file for this function to build
77 # \param source File on which target is built from
78 # \param env Environment (if needed)
79 def c_template_builder(target, source, env):
80  s=str(source[0])
81  t=str(target[0])
82  f=os.path.split(str(target[0]))[-1]
83  g=os.path.splitext(f)[0]
84  if (g == "pic24_uart"):
85  genFromTemplate(s, t, 4)
86  if (g == "pic24_i2c"):
87  genFromTemplate(s, t, 2)
88  if (g == "pic24_ecan"):
89  genFromTemplate(s, t, 2)
90  if (g == "pic24_spi"):
91  genFromTemplate(s, t, 2)
92  return None
93 
94 ## Builds a .h from a template -- SCons Builder function formation.
95 # Function will build the .c as requested. Has the proper
96 # inputs, outputs and returns value to be registered SCons environment
97 # as a builder function
98 # \param target Target file for this function to build
99 # \param source File on which target is built from
100 # \param env Environment (if needed)
101 def h_template_builder(target, source, env):
102  s=str(source[0])
103  t=str(target[0])
104  f=os.path.split(str(target[0]))[-1]
105  g=os.path.splitext(f)[0]
106  if ( g == "pic24_uart"):
107  genFromTemplate(s, t, 4)
108  if ( g == "pic24_i2c"):
109  genFromTemplate(s, t, 2)
110  if ( g == "pic24_ecan"):
111  genFromTemplate(s, t, 2)
112  return None
113 
114 ## Define and register a template-driven builder for .c files
115 cbldr = Builder(action = c_template_builder, suffix='.c', src_suffix='.c-template')
116 env.Append(BUILDERS = {'CTemplate' : cbldr})
117 
118 ## Define and register a template-driven builder for .h files
119 hbldr = Builder(action = h_template_builder, suffix='.h', src_suffix='.h-template')
120 env.Append(BUILDERS = {'HTemplate' : hbldr})
121 
122 ## @}
123 
124 
125 ## @{
126 # \name Calls to build templates
127 ###############################################################################
128 
129 # Specify which files are produced by templates
130 env.CTemplate('../lib/common/pic24_uart','pic24_uart')
131 env.HTemplate('../lib/include/pic24_uart','pic24_uart')
132 env.CTemplate('../lib/common/pic24_i2c','pic24_i2c')
133 env.HTemplate('../lib/include/pic24_i2c','pic24_i2c')
134 env.CTemplate('../lib/common/pic24_spi','pic24_spi')
135 env.CTemplate('../lib/common/pic24_ecan','pic24_ecan')
136 env.HTemplate('../lib/include/pic24_ecan','pic24_ecan')
137 
138 
139 ## @}