00001 # Note the .py extension on this file, necessary so that 00002 # Doxygen parses as a Python script (but not necessary 00003 # for SCons). 00004 00005 # Documentation for this file. If the \file tag isn't present, 00006 # this file won't be documented. 00007 ## \file 00008 # This file builds various files from templates in the 00009 # templates/ directory in which this file resides. It should 00010 # be called from the main SConscript.py script. 00011 # 00012 00013 ## @{ 00014 # \name Setup for template building 00015 ############################################################################### 00016 00017 import os 00018 import string 00019 from string import Template 00020 00021 # Import environment from calling SConstruct context 00022 Import('env') 00023 00024 ## Define the directory in which the templates reside 00025 templateDir = './' 00026 00027 ## Define the include directory destination for .h templates 00028 includeDir = '../include/' 00029 00030 ## Define the common directory destination for .c templates 00031 commonDir = '../common/' 00032 00033 ## @} 00034 00035 ## @{ 00036 # \name Functions supporting template file builds 00037 ############################################################################### 00038 00039 ## A function to do search and replaces on a file, optionally 00040 # appending the results to the output file. 00041 # \param sourceFileName Name of the source file 00042 # \param destFileName Name of the destination file 00043 # \param mapping A dictionary of key=value pairs used by 00044 # Template to perform the search and replace operation. 00045 # \param openMode Mode with which to open the destination file. 00046 # Use the default of 'w' to base the overwrite the 00047 # destination file with the replaced source file. Use 'a' to 00048 # append the replaced source file to the destination file. 00049 def searchAndReplace(sourceFile, destFileName, mapping, openMode='w'): 00050 outFile = open(destFileName, openMode) 00051 template = Template(open(sourceFile).read()) 00052 outFile.write(template.substitute(mapping)) 00053 00054 ## A function to create a .c/.h file from multiple replaces through 00055 # a template. 00056 # \param templateFileName Name of the template file to use 00057 # \param destFileName Name of the destination file to create 00058 # by accumulating multiple passes through the template 00059 # file 00060 # \param iters Number of iterations (passes) through the file 00061 # to make. During each iteration, $x is replaced by the 00062 # iteration number. 00063 def genFromTemplate(templateFileName, destFileName, iters): 00064 openMode = 'w' 00065 for i in range(1, iters + 1): 00066 searchAndReplace(templateFileName, destFileName, 00067 {'x' : str(i)}, openMode) 00068 openMode = 'a' 00069 00070 ## Builds a .c from a template -- SCons Builder function formation. 00071 # Function will build the .c as requested. Has the proper 00072 # inputs, outputs and returns value to be registered SCons environment 00073 # as a builder function 00074 # \param target Target file for this function to build 00075 # \param source File on which target is built from 00076 # \param env Environment (if needed) 00077 def c_template_builder(target, source, env): 00078 s=str(source[0]) 00079 t=str(target[0]) 00080 f=os.path.split(str(target[0]))[-1] 00081 g=os.path.splitext(f)[0] 00082 if (g == "pic24_uart"): 00083 genFromTemplate(s, t, 4) 00084 if (g == "pic24_i2c"): 00085 genFromTemplate(s, t, 2) 00086 if (g == "pic24_ecan"): 00087 genFromTemplate(s, t, 2) 00088 if (g == "pic24_spi"): 00089 genFromTemplate(s, t, 2) 00090 return None 00091 00092 ## Builds a .h from a template -- SCons Builder function formation. 00093 # Function will build the .c as requested. Has the proper 00094 # inputs, outputs and returns value to be registered SCons environment 00095 # as a builder function 00096 # \param target Target file for this function to build 00097 # \param source File on which target is built from 00098 # \param env Environment (if needed) 00099 def h_template_builder(target, source, env): 00100 s=str(source[0]) 00101 t=str(target[0]) 00102 f=os.path.split(str(target[0]))[-1] 00103 g=os.path.splitext(f)[0] 00104 if ( g == "pic24_uart"): 00105 genFromTemplate(s, t, 4) 00106 if ( g == "pic24_i2c"): 00107 genFromTemplate(s, t, 2) 00108 if ( g == "pic24_ecan"): 00109 genFromTemplate(s, t, 2) 00110 return None 00111 00112 ## Define and register a template-driven builder for .c files 00113 cbldr = Builder(action = c_template_builder, suffix='.c', src_suffix='.c-template') 00114 env.Append(BUILDERS = {'CTemplate' : cbldr}) 00115 00116 ## Define and register a template-driven builder for .h files 00117 hbldr = Builder(action = h_template_builder, suffix='.h', src_suffix='.h-template') 00118 env.Append(BUILDERS = {'HTemplate' : hbldr}) 00119 00120 ## @} 00121 00122 00123 ## @{ 00124 # \name Calls to build templates 00125 ############################################################################### 00126 00127 # Specify which files are produced by templates 00128 env.CTemplate('../common/pic24_uart','pic24_uart') 00129 env.HTemplate('../include/pic24_uart','pic24_uart') 00130 env.CTemplate('../common/pic24_i2c','pic24_i2c') 00131 env.HTemplate('../include/pic24_i2c','pic24_i2c') 00132 env.CTemplate('../common/pic24_spi','pic24_spi') 00133 env.CTemplate('../common/pic24_ecan','pic24_ecan') 00134 env.HTemplate('../include/pic24_ecan','pic24_ecan') 00135 00136 00137 ## @}