7ff62727c576dd10f288ea0127005c08e8efabf3
[senf.git] / site_scons / SENFSCons.py
1 import os.path, glob, yaptu
2 import SCons.Options, SCons.Environment, SCons.Script.SConscript, SCons.Node.FS
3 import SCons.Defaults, SCons.Action
4 from SCons.Script import *
5
6 def Glob(env, exclude=[], subdirs=[]):
7     testSources = glob.glob("*.test.cc")
8     sources = [ x for x in glob.glob("*.cc") if x not in testSources and x not in exclude ]
9     for subdir in subdirs:
10         testSources += glob.glob(os.path.join(subdir,"*.test.cc"))
11         sources += [ x for x in glob.glob(os.path.join(subdir,"*.cc"))
12                      if x not in testSources and x not in exclude ]
13     includes = []
14     for d in [ '.' ] + subdirs:
15         for f in os.listdir(d):
16             ext = '.' + f.split('.',1)[-1]
17             p = os.path.join(d,f)
18             if ext in env['CPP_INCLUDE_EXTENSIONS'] \
19                and ext not in env['CPP_EXCLUDE_EXTENSIONS'] \
20                and p not in exclude:
21                 includes.append(p)
22     return ( sources, testSources, includes )
23
24 def Doxygen(env, doxyfile = "Doxyfile", extra_sources = [], output_directory = "doc"):
25     # There is one small problem we need to solve with this builder: The Doxygen builder reads
26     # the Doxyfile and thus depends on the environment variables set by site_scons/lib/doxygen.sh.
27     # We thus have to provide all necessary definitions here manually via DOXYENV !
28
29     if type(doxyfile) is type(""):
30         doxyfile = env.File(doxyfile)
31
32     # Module name is derived from the doxyfile path
33     # Utils/Console/Doxyfile -> Utils_Console
34     module = doxyfile.dir.get_path(env.Dir('#')).replace('/','_')
35     if module == '.' : module = "Main"
36
37     # Standard doc build vars and opts
38     def vars(env=env, **kw):
39         denv = { 'TOPDIR'          : env.Dir('#').abspath,
40                  'LIBDIR'          : env.Dir('#/site_scons/lib').abspath,
41                  'output_dir'      : '$OUTPUT_DIRECTORY',
42                  'html_dir'        : 'html',
43                  'html'            : 'NO' }
44         denv.update(kw)
45         return { 'DOXYENV'         : denv,
46                  'MODULE'          : module,
47                  'OUTPUT_DIRECTORY': output_directory,
48                  'DOXYGENCOM'      : "site_scons/lib/doxygen.sh $DOXYOPTS $SOURCE",
49                  };
50     opts = [ '--tagfile-name', '"${MODULE}.tag"',
51              '--output-dir', '$OUTPUT_DIRECTORY' ]
52
53     # Rule to generate tagfile
54     # (need to exclude the 'clean' case, otherwise we'll have duplicate nodes)
55     if not env.GetOption('clean'):
56         tagfile = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfile' ],
57                               **vars(generate_tagfile='${OUTPUT_DIRECTORY}/${MODULE}.tag'))
58         env.Append(ALL_TAGFILES = [ tagfile[0].abspath ])
59         env.Depends(tagfile, [ env.File('#/site_scons/lib/doxygen.sh'), 
60                                env.File('#/site_scons/lib/tag-munge.xsl') ])
61
62         env.Install(env.Dir('$DOCINSTALLDIR').Dir(tagfile[0].dir.get_path(env.Dir('#'))),
63                     tagfile[0])
64
65     # Rule to generate HTML documentation
66     doc = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfiles', '"$ALL_TAGFILES"', '--html' ],
67                       **vars(html='YES', tagfiles='$ALL_TAGFILES'))
68     env.Depends(doc, [ env.File('#/site_scons/lib/doxygen.sh'),
69                        env.File('#/site_scons/lib/html-munge.xsl') ])
70
71     # Copy the extra_sources (the images) into the documentation directory
72     # (need to exclude the 'clean' case otherwise there are multiple ways to clean the copies)
73     if extra_sources:
74         if env.GetOption('clean'):
75             env.Depends(doc, extra_sources)
76         else:
77             env.Depends(tagfile, env.CopyToDir(doc[0].dir, extra_sources))
78
79     # Install documentation into DOCINSTALLDIR
80     env.InstallDir(env.Dir('$DOCINSTALLDIR').Dir(doc[0].dir.dir.get_path(env.Dir('#'))), doc[0].dir,
81                    FILTER_SUFFIXES=['.html','.css','.png','.php','.idx'])
82
83     # Useful aliases
84     env.Alias('all_docs', doc)
85     env.Clean(env.Alias('all_docs'), doc)
86     env.Clean(env.Alias('all'), doc)
87
88     return doc
89
90 def AllIncludesHH(env, exclude=[]):
91     exclude = exclude[:] + ['all_includes.hh'] # Make a copy !!
92     headers = [ f for f in glob.glob("*.hh")
93                 if f not in exclude and not f.endswith('.test.hh') ]
94     headers.sort()
95     target = env.File("all_includes.hh")
96     file(target.abspath,"w").write("".join([ '#include "%s"\n' % f
97                                              for f in headers ]))
98     env.Clean(env.Alias('all'), target)
99
100
101 INDEXPAGE="""
102 /** \mainpage ${TITLE}
103
104     ${TEXT}
105
106     \htmlonly
107     <dl>
108
109 {{  for name, title in SUBPAGES:
110       <dt><a href="../../${name}/doc/html/index.html">${name}</a></dt><dd>${title}</a></dd>
111 }}
112
113     </dl>
114     \endhtmlonly
115  */
116 """
117
118 def IndexPage(env, name, title, text=""):
119     SUBPAGES = []
120     for dox in sorted(glob.glob("*/Mainpage.dox")):
121         subtitle = ([None] + [ line.split('\\mainpage',1)[-1].strip() for line in file(dox)
122                                if '\\mainpage' in line ])[-1]
123         if subtitle:
124             SUBPAGES.append( (dox.split('/',1)[0], subtitle) )
125     file(name,"w").write(yaptu.process(
126             INDEXPAGE, globals(), { 'TITLE': title, 'TEXT': text, 'SUBPAGES': SUBPAGES }))
127     env.Clean('all',name)
128     env.Clean('all_docs',name)
129
130
131 ###########################################################################
132 # The following functions serve as simple macros for most SConscript files
133 #
134 # If you need to customize these rules, copy-and-paste the code into the
135 # SConscript file and adjust at will (don't forget to replace the
136 # parameters with their actual value. Parameters are marked with ((name)) )
137
138 def AutoRules(env, exclude=[], subdirs=[], doc_extra_sources = []):
139     import SENFSCons
140
141     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
142     subscripts               = env.Glob("*/SConscript")
143     doxyfile                 = env.Glob("Doxyfile")
144
145     if sources               : env.Append(ALLOBJECTS = env.Object(sources))
146     if tests                 : env.BoostUnitTest('test', tests)
147     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
148     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
149     if subscripts            : SConscript(subscripts)
150
151
152 def AutoPacketBundle(env, name, exclude=[], subdirs=[], doc_extra_sources=[]):
153     import SENFSCons
154
155     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
156     subscripts               = env.Glob("*/SConscript")
157     doxyfile                 = env.Glob("Doxyfile")
158
159     objects = env.Object(sources)
160     cobject = env.CombinedObject('${LOCALLIBDIR}/${NAME}${OBJADDSUFFIX}', objects, NAME=((name)))
161
162     env.Default(cobject)
163     env.Append(ALLOBJECTS = objects, PACKET_BUNDLES = cobject)
164     env.Install('$OBJINSTALLDIR', cobject)
165
166     if tests                 : env.BoostUnitTest('test', tests + cobject)
167     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
168     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
169     if subscripts            : SConscript(subscripts)