Fix documentation tag-file build
[senf.git] / site_scons / SENFSCons.py
1 import os.path, glob
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     opts = [ '--tagfile-name', '"${MODULE}.tag"',
49              '--output-dir', '$OUTPUT_DIRECTORY' ]
50
51     # Rule to generate tagfile
52     # (need to exclude the 'clean' case, otherwise we'll have duplicate nodes)
53     if not env.GetOption('clean'):
54         tagfile = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfile' ],
55                               **vars(generate_tagfile='doc/${MODULE}.tag'))
56         env.Append(ALL_TAGFILES = [ tagfile[0].abspath ])
57         env.Depends(tagfile, [ env.File('#/site_scons/lib/doxygen.sh'), 
58                                env.File('#/site_scons/lib/tag-munge.xsl') ])
59
60         env.Install(env.Dir('$DOCINSTALLDIR').Dir(tagfile[0].dir.get_path(env.Dir('#'))),
61                     tagfile[0])
62
63     # Rule to generate HTML documentation
64     doc = env.Doxygen(doxyfile, DOXYOPTS = opts + [ '--tagfiles', '"$ALL_TAGFILES"', '--html' ],
65                       **vars(html='YES', tagfiles='$ALL_TAGFILES'))
66     env.Depends(doc, [ env.File('#/site_scons/lib/doxygen.sh'),
67                        env.File('#/site_scons/lib/html-munge.xsl') ])
68
69     # Copy the extra_sources (the images) into the documentation directory
70     # (need to exclude the 'clean' case otherwise there are multiple ways to clean the copies)
71     if extra_sources:
72         if env.GetOption('clean'):
73             env.Depends(doc, extra_sources)
74         else:
75             env.Depends(doc, env.CopyToDir(doc[0].dir, extra_sources))
76
77     # Install documentation into DOCINSTALLDIR
78     env.InstallDir(env.Dir('$DOCINSTALLDIR').Dir(doc[0].dir.dir.get_path(env.Dir('#'))), doc[0].dir,
79                    FILTER_SUFFIXES=['.html','.css','.png','.php','.idx'])
80
81     # Useful aliases
82     env.Alias('all_docs', doc)
83     env.Clean(env.Alias('all_docs'), doc)
84     env.Clean(env.Alias('all'), doc)
85
86     return doc
87
88 def AllIncludesHH(env, exclude=[]):
89     exclude = exclude[:] + ['all_includes.hh'] # Make a copy !!
90     headers = [ f for f in glob.glob("*.hh")
91                 if f not in exclude and not f.endswith('.test.hh') ]
92     headers.sort()
93     target = env.File("all_includes.hh")
94     file(target.abspath,"w").write("".join([ '#include "%s"\n' % f
95                                              for f in headers ]))
96     env.Clean(env.Alias('all'), target)
97
98 ###########################################################################
99 # The following functions serve as simple macros for most SConscript files
100 #
101 # If you need to customize these rules, copy-and-paste the code into the
102 # SConscript file and adjust at will (don't forget to replace the
103 # parameters with their actual value. Parameters are marked with ((name)) )
104
105 def AutoRules(env, exclude=[], subdirs=[], doc_extra_sources = []):
106     import SENFSCons
107
108     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
109     subscripts               = env.Glob("*/SConscript")
110     doxyfile                 = env.Glob("Doxyfile")
111
112     if sources               : env.Append(ALLOBJECTS = env.Object(sources))
113     if tests                 : env.BoostUnitTest('test', tests)
114     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
115     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
116     if subscripts            : SConscript(subscripts)
117
118
119 def AutoPacketBundle(env, name, exclude=[], subdirs=[], doc_extra_sources=[]):
120     import SENFSCons
121
122     sources, tests, includes = SENFSCons.Glob(env, exclude=((exclude)), subdirs=((subdirs)) )
123     subscripts               = env.Glob("*/SConscript")
124     doxyfile                 = env.Glob("Doxyfile")
125
126     objects = env.Object(sources)
127     cobject = env.CombinedObject('${LOCALLIBDIR}/${NAME}${OBJADDSUFFIX}', objects, NAME=((name)))
128
129     env.Default(cobject)
130     env.Append(ALLOBJECTS = objects, PACKET_BUNDLES = cobject)
131     env.Install('$OBJINSTALLDIR', cobject)
132
133     if tests                 : env.BoostUnitTest('test', tests + cobject)
134     if includes              : env.InstallSubdir('$INCLUDEINSTALLDIR', includes)
135     if doxyfile              : SENFSCons.Doxygen(env, extra_sources=((doc_extra_sources)) )
136     if subscripts            : SConscript(subscripts)