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