Source

SCons / src / engine / SCons / Tool / xcode.py

The default branch has multiple heads

Full commit
  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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""SCons.Tool.xcodeproj

Tool-specific initialization for Xcode project files.

There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.

"""

#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os

import SCons.Builder
import SCons.Node.FS

contents_xcworkspacedata = """\
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
   version = "1.0">
   <FileRef
      location = "self:%s">
   </FileRef>
</Workspace>
"""

project_pbxproj_header = """\
// !$*UTF8*$!
{
    archiveVersion = 1;
    classes = {
    };
    objectVersion = 46;
    objects = {

"""

project_pbxproj_footer = """\
    };
    rootObject = %s;
}
"""

MAP_EXT = {
    '.h' :  "sourcecode.c.h",

    '.hh':  "sourcecode.cpp.h",
    '.inl': "sourcecode.cpp.h",
    '.hpp': "sourcecode.cpp.h",

    '.c':   "sourcecode.c.c",

    '.m':   "sourcecode.c.objc",

    '.mm':  "sourcecode.cpp.objcpp",

    '.cc':  "sourcecode.cpp.cpp",

    '.cpp': "sourcecode.cpp.cpp",
    '.C':   "sourcecode.cpp.cpp",
    '.cxx': "sourcecode.cpp.cpp",
    '.c++': "sourcecode.cpp.cpp",

    '.l':   "sourcecode.lex", # luthor
    '.ll':  "sourcecode.lex",

    '.y':   "sourcecode.yacc",
    '.yy':  "sourcecode.yacc",

    '.plist': "text.plist.xml",
    ".nib":   "wrapper.nib",
    ".xib":   "text.xib",
    "": "",
}

class XcodeProjectBuildContext(object):
    def __init__(self):
        self.id = 562000999
        self.nodes = {}
        self.generate_codenav_targets = True

    def newid(self):
        self.id = self.id + 1
        return "%04X%04X%04X%012d" % (0, 10000, 0, self.id)

    def regNode(self, node):
        type_name = node.__class__.__name__

        if not self.nodes.has_key(type_name):
            self.nodes[type_name] = []

        self.nodes[type_name].append(node)

    def print_section(self, file, t):
        if self.nodes.has_key(t.__name__):
            file.write('/* Begin %s section */\n' % t.__name__)
            for n in self.nodes[t.__name__]:
                n.write(file)
            file.write('/* End %s section */\n\n' % t.__name__)

class XcodeNode(object):
    def __init__(self, context, description):
        context.regNode(self)

        self._context = context
        self._id = context.newid()
        self._name = description
        
        self.isa = self.__class__.__name__

    def tostring(self, value):
        if isinstance(value, list):
            result = '(\n'
            for i in value:
                result = result + '\t\t\t\t%s,\n' % self.tostring(i)
            result = result + '\t\t\t)'
            return result
        elif isinstance(value, dict):
            result = '{\n'
            for k, v in value.iteritems():
                result = result + ('\t\t\t\t%s = %s;\n' % (self.tostring(k), self.tostring(v)))
            result = result + '\t\t\t}'
            return result
        elif isinstance(value, XcodeNode):
            return value._id + ' /* ' + value._name + ' */' if value._name != '' else value._id
        else:
            return str(value)

    def write(self, f):
        if self._name != '':
            f.write('\t\t%s /* %s */ = {\n' % (self._id, self._name))
        else:
            f.write('\t\t%s = {\n' % (self._id))

        for attribute,value in self.__dict__.items():
            if attribute[0] != '_':
                f.write('\t\t\t%s = %s;\n' % (attribute, self.tostring(value)))

        f.write('\t\t};\n')

class PBXFileReference(XcodeNode):
    def __init__(self, context, path, executable = False):
        XcodeNode.__init__(self, context, os.path.basename(path))

        if not executable:
            self.fileEncoding = 4
            self.name = self._name
            self.path = os.path.relpath(path, context.project_dir)
            self.sourceTree = '"<group>"'

            _, filetype = os.path.splitext(self._name)
            self.lastKnownFileType = MAP_EXT[filetype]
        else:
            self.explicitFileType = '"compiled.mach-o.executable"'
            self.includeInIndex = 0
            self.path = path
            self.sourceTree = 'BUILT_PRODUCTS_DIR'

    def is_header_file(self):
        _, ext = os.path.splitext(self._name)
        return ext in ['.h', '.hh', '.hpp']

    def is_inline_file(self):
        _, ext = os.path.splitext(self._name)
        return ext in ['.inl']

    def is_source_file(self):
        _, ext = os.path.splitext(self._name)
        return ext in ['.c', '.cpp', '.cc', '.m', '.mm', '.cxx']

class PBXLegacyTarget(XcodeNode):
    def __init__(self, context, description, buildSettings):
        XcodeNode.__init__(self, context, description)

        self.buildArgumentsString = '"--xcode=${ACTION} ${SCONS_BUILD_TARGET}"'
        self.buildWorkingDirectory = os.getcwd()
        self.buildConfigurationList = XCConfigurationList(context, 'Build configuration list for PBXLegacyTarget "%s"' % description, buildSettings)
        self.buildPhases = []
        self.buildToolPath = 'scons'
        self.dependencies = []
        self.productName = '"%s"' % description
        self.name = '"%s"' % description
        self.passBuildSettingsInEnvironment = 1
        self.productType = '"com.apple.product-type.tool"'
        #self.productType = '"com.apple.product-type.library.static"'
        #self.productType = '"com.apple.product-type.library.dynamic"'

class PBXNativeTarget(XcodeNode):
    def __init__(self, context, description, buildSettings = {}):
        XcodeNode.__init__(self, context, description)

        self.buildConfigurationList = XCConfigurationList(context, 'Build configuration list for PBXNativeTarget "%s"' % description, buildSettings)
        self.buildPhases = [PBXSourcesBuildPhase(context, 'Sources')]
        self.buildRules = []
        self.dependencies = []
        self.name = self._name
        self.productName = self._name
        self.productType = '"com.apple.product-type.tool"'

class PBXProject(XcodeNode):
    def __init__(self, context, description, project_name, buildSettings):
        XcodeNode.__init__(self, context, description)

        self.targets = [PBXLegacyTarget(context, project_name, buildSettings)]
        if context.generate_codenav_targets:
            self.targets.append(PBXNativeTarget(context, project_name + '-codenav', buildSettings))

        self.buildConfigurationList = XCConfigurationList(context, 'Build configuration list for PBXProject "%s.xcodeproj"' % project_name, buildSettings)
        self.compatibilityVersion = '"Xcode 3.2"'
        self.projectDirPath = '""';
        self.projectRoot = '""'
        self.mainGroup = PBXGroup(context, '')

class PBXGroup(XcodeNode):
    def __init__(self, context, description):
        XcodeNode.__init__(self, context, description)

        self.sourceTree = '"<group>"'
        self.children = []

        if description != '':
            self.name = description

        self._subgroups = {}

    # Find subgroup by name and create it if needed
    def subgroup_by_name(self, subgroup_name):
        if not self._subgroups.has_key(subgroup_name):
            self.children.append(self._subgroups.setdefault(subgroup_name, PBXGroup(self._context, subgroup_name)))

        return self._subgroups[subgroup_name]

    # Add source file to group creating all hierarchy of subgroups if needed
    def add_source(self, project_path, full_path, executable = False):
        file_ref = PBXFileReference(self._context, full_path, executable)

        group = reduce(lambda g, n: g.subgroup_by_name(n), project_path.split(os.path.sep)[:-1], self)
        group.children.append(file_ref)

        return file_ref

class PBXSourcesBuildPhase(XcodeNode):
    def __init__(self, context, description):
        XcodeNode.__init__(self, context, description)
        self.buildActionMask = 2147483647
        self.runOnlyForDeploymentPostprocessing = 0
        self.files = []

class PBXBuildFile(XcodeNode):
    def __init__(self, context, ref):
        XcodeNode.__init__(self, context, ref._name)
        self.fileRef = ref

class XCBuildConfiguration(XcodeNode):
    def __init__(self, context, description, buildSettings):
        XcodeNode.__init__(self, context, description)

        self.buildSettings = buildSettings
        self.name = self._name

class XCConfigurationList(XcodeNode):
    def __init__(self, context, description, buildSettings = {}):
        XcodeNode.__init__(self, context, description)

        self.buildConfigurations = map(lambda key: XCBuildConfiguration(context, key, buildSettings[key]), buildSettings)
        self.defaultConfigurationIsVisible = 0
        self.defaultConfigurationName = buildSettings.keys()[0]

def XcodeProjectAction(target, source, env):
    project_filename = str(target[0].srcnode())
    project_name = os.path.basename(project_filename).split('.')[0]

    print 'Generating Xcode project \"%s\"' % project_filename

    def custom_sort(x):
        if isinstance(x, PBXGroup):
            return '0' + x._name
        elif isinstance(x, PBXFileReference):
            if x.is_header_file(): return '1' + x._name
            if x.is_inline_file(): return '2' + x._name
            if x.is_source_file(): return '3' + x._name
            return '4' + x._name
        else:
            return '5' + x._name

    context = XcodeProjectBuildContext()
    context.generate_codenav_targets = env.get('generate_codenav_targets', 1)
    context.project_dir = os.path.dirname(project_filename)

    # Get param from environment. Ensure it is a string or a node or a list of strings or nodes. Makes strings lists
    def get_param(env, name, default_value):
        param = env.get(name, default_value)
        result = []

        if SCons.Util.is_String(param):
            result.append(param)
        elif SCons.Util.is_List(param):
            for i in param:
                if SCons.Util.is_String(i):
                    result.append(i)
                else:
                    try:
                        result.append(str(i))
                    except:
                        raise SCons.Errors.InternalError("'%s' must be a string, a node, a list of strings or nodes" % name)
        else:
            raise SCons.Errors.InternalError("'%s' must be a string, a node, a list of strings or nodes" % name)

        return result

    configuration = get_param(env, 'configuration', 'Default')
    buildtarget = get_param(env, 'buildtarget', '')
    runfile = get_param(env, 'runfile', '')

    # Validation
    if len(configuration) != len(buildtarget):
        raise SCons.Errors.InternalError("Sizes of 'configuration' and 'buildtarget' lists must be the same.")
    if len(configuration) != len(runfile):
        raise SCons.Errors.InternalError("Sizes of 'configuration' and 'runfile' lists must be the same.")

    include_search_path = map(os.path.abspath, env.get('CPPPATH', []))

    configurationBuildSettings = {}
    for i in range(len(configuration)):
        bs = {
            'BUILT_PRODUCTS_DIR': os.path.dirname(os.path.abspath(runfile[i])),
            'ARCHS': '"$(ARCHS_STANDARD_64_BIT)"',
            'HEADER_SEARCH_PATHS': include_search_path,
            'USER_HEADER_SEARCH_PATHS': include_search_path,
            'PRODUCT_NAME': '"$(TARGET_NAME)"',
            'SCONS_BUILD_TARGET': buildtarget[i],
        }
        configurationBuildSettings[configuration[i]] = bs



    print configuration
    print map(str, buildtarget)
    print runfile

    # buildSettings = {
    #     'BUILT_PRODUCTS_DIR': os.path.dirname(os.path.abspath(env.get('runfile', ''))),
    #     'ARCHS': '"$(ARCHS_STANDARD_64_BIT)"',
    #     'HEADER_SEARCH_PATHS': include_search_path,
    #     'USER_HEADER_SEARCH_PATHS': include_search_path,
    #     'PRODUCT_NAME': '"$(TARGET_NAME)"',
    #     'SCONS_BUILD_TARGET': env.get('buildtarget', ''),
    # }

    # Create project
    project = PBXProject(context, 'Project object', project_name,configurationBuildSettings)# buildSettings)
    sourcesGroup = project.mainGroup.subgroup_by_name(project_name)
    
    # Add all source files to project's source group
    files = env['srcs'] = env['srcs'] + env['incs']
    cp = os.path.commonprefix(files)
    for i in files:
        sourcesGroup.add_source(i[len(cp):], i)

    # Sort files in groups
    # TODO: Alphabetically? Headers than sources?
    for g in context.nodes['PBXGroup']:
        g.children.sort(key = custom_sort)

    # Generate fake native Xcode targets for code navigation to work
    if context.generate_codenav_targets:
        for f in context.nodes['PBXFileReference']:
            if f.is_source_file():
                project.targets[1].buildPhases[0].files.append(PBXBuildFile(context, f))

    # Add executable reference
    pp = PBXFileReference(context, 'scons-xcode', True)
    project.targets[0].productReference = pp
    project.targets[1].productReference = pp

    # Create folder hierarchy
    path = ''
    for dir_name in project_filename.split(os.path.sep):
        path = os.path.join(path, dir_name)
        try:
            os.mkdir(path)
        except:
            pass

    # Write down project.workspace file
    os.mkdir(os.path.join(project_filename, 'project.xcworkspace'))
    fw = open(os.path.join(project_filename, 'project.xcworkspace', 'contents.xcworkspacedata'), 'w')
    fw.write(contents_xcworkspacedata % project_filename)

    # Write down .pbxproj file
    fp = open(os.path.join(project_filename, 'project.pbxproj'), 'w')
    fp.write(project_pbxproj_header)

    for i in [PBXFileReference, PBXGroup, PBXSourcesBuildPhase, PBXLegacyTarget, PBXNativeTarget, PBXProject, PBXBuildFile, XCBuildConfiguration, XCConfigurationList]:
        context.print_section(fp, i)

    fp.write(project_pbxproj_footer % project.tostring(project))

def XcodeProjectEmitter(target, source, env):
    return target, source

projectBuilder = SCons.Builder.Builder(
    #emitter = XcodeProjectEmitter,
    action = SCons.Action.Action(XcodeProjectAction, None),
    suffix = '.xcodeproj',
    target_factory = SCons.Node.FS.Dir
)

def generate(env):
    """Add Builders and construction variables for Xcode project files to an Environment."""

    try:
        env['BUILDERS']['XcodeProject']
    except KeyError:
        env['BUILDERS']['XcodeProject'] = projectBuilder
    
def exists(env):
    return True

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: