File: builder.py

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (255 lines) | stat: -rwxr-xr-x 7,907 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import subprocess
import sys
import time

class BuildError(Exception):
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return repr(self.value)

def runInDir(command, dir=None, verbose=True):
    if dir:
        olddir = os.getcwd()
        os.chdir(dir)

    commandStr = " ".join(command)
    if verbose:
        print(commandStr)
    result = os.system(commandStr)

    if dir:
        os.chdir(olddir)

    return result

class Builder:
    """
    Base class exposing the Builder interface.
    """

    def __init__(self, formatName="", commandName="", programDir=None):
        """
        formatName = human readable name for project format (should correspond with Bakefile names)
        commandName = name of command line program used to invoke builder
        programDir = directory program is located in, if not on the path
        """

        self.dir = dir
        self.name = commandName
        self.formatName = formatName
        self.programDir = programDir
        self.doSetup()

    def doSetup(self):
        """
        Do anything special needed to configure the environment to build with this builder.
        """

        pass

    def isAvailable(self):
        """
        Run sanity checks before attempting to build with this format
        """
        # Make sure the builder program exists
        programPath = self.getProgramPath()
        if os.path.exists(programPath):
            return True
        else:
            # check the PATH for the program
            # TODO: How do we check if we're in Cygwin?
            if sys.platform.startswith("win"):
                result = os.system(self.name)
                if result == 0:
                    return True
                dirs = os.environ["PATH"].split(":")

                for dir in dirs:
                    if os.path.isfile(os.path.join(dir, self.name)):
                        return True  

            else:
                result = os.system("which %s" % self.name)

                if result == 0:
                    return True

        return False

    def getProgramPath(self):
        if self.programDir:
            path = os.path.join(self.programDir, self.name)
            if sys.platform.startswith("win"):
                path = '"%s"' % path
            return path

        return self.name

    def getProjectFileArg(self, projectFile = None):
        result = []
        if projectFile:
            result.append(projectFile)
        return result
    
    def clean(self, dir=None, projectFile=None, options=[]):
        """
        dir = the directory containing the project file
        projectFile = Some formats need to explicitly specify the project file's name
        """
        if self.isAvailable():
            args = [self.getProgramPath()]
            pfArg = self.getProjectFileArg(projectFile)
            if pfArg:
                args.extend(pfArg)
            args.append("clean")
            if options:
                args.extend(options)
            result = runInDir(args, dir)
            return result

        return False

    def configure(self, dir=None, options=[]):
        # if we don't have configure, just report success
        return 0

    def build(self, dir=None, projectFile=None, targets=None, options=[]):
        if self.isAvailable():
            args = [self.getProgramPath()]
            pfArg = self.getProjectFileArg(projectFile)
            if pfArg:
                args.extend(pfArg)
            # Important Note: if extending args, check it first!
            # NoneTypes are not iterable and will crash the clean, build, or install!
            # Very very irritating when this happens right at the end.
            if options: 
                args.extend(options)
            result = runInDir(args, dir)
            return result

        return 1

    def install(self, dir=None, projectFile=None, options=[]):
        if self.isAvailable():
            args = [self.getProgramPath()]
            pfArg = self.getProjectFileArg(projectFile)
            if pfArg:
                args.extend(pfArg)
            args.append("install")
            if options:
                args.extend(options)
            result = runInDir(args, dir)
            return result

        return 1


# Concrete subclasses of abstract Builder interface

class GNUMakeBuilder(Builder):
    def __init__(self, commandName="make", formatName="GNUMake"):
        Builder.__init__(self, commandName=commandName, formatName=formatName)


class XcodeBuilder(Builder):
    def __init__(self, commandName="xcodebuild", formatName="Xcode"):
        Builder.__init__(self, commandName=commandName, formatName=formatName)


class AutoconfBuilder(GNUMakeBuilder):
    def __init__(self, formatName="autoconf"):
        GNUMakeBuilder.__init__(self, formatName=formatName)

    def configure(self, dir=None, options=None):
        #olddir = os.getcwd()
        #os.chdir(dir)

        configdir = dir
        if not dir:
            configdir = os.getcwd()

        configure_cmd = ""
        while os.path.exists(configdir):
            config_cmd = os.path.join(configdir, "configure")
            if not os.path.exists(config_cmd):
                parentdir = os.path.abspath(os.path.join(configdir, ".."))
                if configdir == parentdir:
                    break

                configdir = parentdir 
            else:
                configure_cmd = config_cmd
                break

        if not configure_cmd:
            sys.stderr.write("Could not find configure script at %r. Have you run autoconf?\n" % dir)
            return 1

        optionsStr = " ".join(options) if options else ""
        command = "%s %s" % (configure_cmd, optionsStr)
        print(command)
        result = os.system(command)
        #os.chdir(olddir)
        return result


class MSVCBuilder(Builder):
    def __init__(self, commandName="nmake.exe"):
        Builder.__init__(self, commandName=commandName, formatName="msvc")

    def isAvailable(self):
        PATH = os.environ['PATH'].split(os.path.pathsep)
        for p in PATH:
            if os.path.exists(os.path.join(p, self.name)):
                return True
        return False

    def getProjectFileArg(self, projectFile = None):
        result = []
        if projectFile:
            result.extend(['-f', projectFile])
            
        return result

        
class MSVCProjectBuilder(Builder):
    def __init__(self):
        Builder.__init__(self, commandName="VCExpress.exe", formatName="msvcProject")
        for key in ["VS90COMNTOOLS", "VC80COMNTOOLS", "VC71COMNTOOLS"]:
            if os.environ.has_key(key):
                self.programDir = os.path.join(os.environ[key], "..", "IDE")

        if self.programDir == None:
            for version in ["9.0", "8", ".NET 2003"]:
                msvcDir = "C:\\Program Files\\Microsoft Visual Studio %s\\Common7\\IDE" % version
                if os.path.exists(msvcDir):
                    self.programDir = msvcDir

    def isAvailable(self):
        if self.programDir:
            path = os.path.join(self.programDir, self.name)
            if os.path.exists(path):
                return True
            else:
                # I don't have commercial versions of MSVC so I can't test this
                name = "devenv.com"
                path = os.path.join(self.programDir, name)
                if os.path.exists(path):
                    self.name = "devenv.com"
                    return True

        return False

builders = [GNUMakeBuilder, XcodeBuilder, AutoconfBuilder, MSVCBuilder, MSVCProjectBuilder]

def getAvailableBuilders():
    availableBuilders = {}
    for symbol in builders:
        thisBuilder = symbol()
        if thisBuilder.isAvailable():
            availableBuilders[thisBuilder.formatName] = symbol

    return availableBuilders