summaryrefslogtreecommitdiff
path: root/tools/build/src/tools/common.py
blob: b1ee26f0cf0a837a63169aee900ebbc9b0b5808b (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
#  Status: being ported by Steven Watanabe
#  Base revision: 47174
#
#  Copyright (C) Vladimir Prus 2002. Permission to copy, use, modify, sell and
#  distribute this software is granted provided this copyright notice appears in
#  all copies. This software is provided "as is" without express or implied
#  warranty, and with no claim as to its suitability for any purpose.

""" Provides actions common to all toolsets, such as creating directories and
    removing files.
"""

import re
import bjam
import os
import os.path
import sys

# for some reason this fails on Python 2.7(r27:82525)
# from b2.build import virtual_target 
import b2.build.virtual_target
from b2.build import feature, type
from b2.util.utility import *
from b2.util import path

__re__before_first_dash = re.compile ('([^-]*)-')

def reset ():
    """ Clear the module state. This is mainly for testing purposes.
        Note that this must be called _after_ resetting the module 'feature'.
    """    
    global __had_unspecified_value, __had_value, __declared_subfeature
    global __init_loc
    global __all_signatures, __debug_configuration, __show_configuration
    
    # Stores toolsets without specified initialization values.
    __had_unspecified_value = {}

    # Stores toolsets with specified initialization values.
    __had_value = {}
    
    # Stores toolsets with declared subfeatures.
    __declared_subfeature = {}
    
    # Stores all signatures of the toolsets.
    __all_signatures = {}

    # Stores the initialization locations of each toolset
    __init_loc = {}

    __debug_configuration = '--debug-configuration' in bjam.variable('ARGV')
    __show_configuration = '--show-configuration' in bjam.variable('ARGV')

    global __executable_path_variable
    OS = bjam.call("peek", [], "OS")[0]
    if OS == "NT":
        # On Windows the case and capitalization of PATH is not always predictable, so
        # let's find out what variable name was really set.
        for n in os.environ:
            if n.lower() == "path":
                __executable_path_variable = n
                break
    else:
        __executable_path_variable = "PATH"

    m = {"NT": __executable_path_variable,
         "CYGWIN": "PATH",
         "MACOSX": "DYLD_LIBRARY_PATH",
         "AIX": "LIBPATH"}
    global __shared_library_path_variable
    __shared_library_path_variable = m.get(OS, "LD_LIBRARY_PATH")
                            
reset()

def shared_library_path_variable():
    return __shared_library_path_variable

# ported from trunk@47174
class Configurations(object):
    """
        This class helps to manage toolset configurations. Each configuration
        has a unique ID and one or more parameters. A typical example of a unique ID
        is a condition generated by 'common.check-init-parameters' rule. Other kinds
        of IDs can be used. Parameters may include any details about the configuration
        like 'command', 'path', etc.

        A toolset configuration may be in one of the following states:

        - registered
              Configuration has been registered (e.g. by autodetection code) but has
              not yet been marked as used, i.e. 'toolset.using' rule has not yet been
              called for it.
          - used
              Once called 'toolset.using' rule marks the configuration as 'used'.

        The main difference between the states above is that while a configuration is
        'registered' its options can be freely changed. This is useful in particular
        for autodetection code - all detected configurations may be safely overwritten
        by user code.
    """

    def __init__(self):
        self.used_ = set()
        self.all_ = set()
        self.params_ = {}

    def register(self, id):
        """
            Registers a configuration.

            Returns True if the configuration has been added and False if
            it already exists. Reports an error if the configuration is 'used'.
        """
        if id in self.used_:
            #FIXME
            errors.error("common: the configuration '$(id)' is in use")

        if id not in self.all_:
            self.all_.add(id)

            # Indicate that a new configuration has been added.
            return True
        else:
            return False

    def use(self, id):
        """
            Mark a configuration as 'used'.

            Returns True if the state of the configuration has been changed to
            'used' and False if it the state wasn't changed. Reports an error
            if the configuration isn't known.
        """
        if id not in self.all_:
            #FIXME:
            errors.error("common: the configuration '$(id)' is not known")

        if id not in self.used_:
            self.used_.add(id)

            # indicate that the configuration has been marked as 'used'
            return True
        else:
            return False

    def all(self):
        """ Return all registered configurations. """
        return self.all_

    def used(self):
        """ Return all used configurations. """
        return self.used_

    def get(self, id, param):
        """ Returns the value of a configuration parameter. """
        return self.params_.get(param, {}).get(id)

    def set (self, id, param, value):
        """ Sets the value of a configuration parameter. """
        self.params_.setdefault(param, {})[id] = value

# Ported from trunk@47174
def check_init_parameters(toolset, requirement, *args):
    """ The rule for checking toolset parameters. Trailing parameters should all be
        parameter name/value pairs. The rule will check that each parameter either has
        a value in each invocation or has no value in each invocation. Also, the rule
        will check that the combination of all parameter values is unique in all
        invocations.

        Each parameter name corresponds to a subfeature. This rule will declare a
        subfeature the first time a non-empty parameter value is passed and will
        extend it with all the values.

        The return value from this rule is a condition to be used for flags settings.
    """
    # The type checking here is my best guess about
    # what the types should be.
    assert(isinstance(toolset, str))
    assert(isinstance(requirement, str) or requirement is None)
    sig = toolset
    condition = replace_grist(toolset, '<toolset>')
    subcondition = []
    
    for arg in args:
        assert(isinstance(arg, tuple))
        assert(len(arg) == 2)
        name = arg[0]
        value = arg[1]
        assert(isinstance(name, str))
        assert(isinstance(value, str) or value is None)
        
        str_toolset_name = str((toolset, name))

        # FIXME: is this the correct translation?
        ### if $(value)-is-not-empty
        if value is not None:
            condition = condition + '-' + value
            if __had_unspecified_value.has_key(str_toolset_name):
                raise BaseException("'%s' initialization: parameter '%s' inconsistent\n" \
                "no value was specified in earlier initialization\n" \
                "an explicit value is specified now" % (toolset, name))

            # The logic below is for intel compiler. It calls this rule
            # with 'intel-linux' and 'intel-win' as toolset, so we need to
            # get the base part of toolset name.
            # We can't pass 'intel' as toolset, because it that case it will
            # be impossible to register versionles intel-linux and
            # intel-win of specific version.
            t = toolset
            m = __re__before_first_dash.match(toolset)
            if m:
                t = m.group(1)

            if not __had_value.has_key(str_toolset_name):
                if not __declared_subfeature.has_key(str((t, name))):
                    feature.subfeature('toolset', t, name, [], ['propagated'])
                    __declared_subfeature[str((t, name))] = True

                __had_value[str_toolset_name] = True

            feature.extend_subfeature('toolset', t, name, [value])
            subcondition += ['<toolset-' + t + ':' + name + '>' + value ]

        else:
            if __had_value.has_key(str_toolset_name):
                raise BaseException ("'%s' initialization: parameter '%s' inconsistent\n" \
                "an explicit value was specified in an earlier initialization\n" \
                "no value is specified now" % (toolset, name))

            __had_unspecified_value[str_toolset_name] = True

        if value == None: value = ''
        
        sig = sig + value + '-'

    if __all_signatures.has_key(sig):
        message = "duplicate initialization of '%s' with the following parameters: " % toolset
        
        for arg in args:
            name = arg[0]
            value = arg[1]
            if value == None: value = '<unspecified>'
            
            message += "'%s' = '%s'\n" % (name, value)

        raise BaseException(message)

    __all_signatures[sig] = True
    # FIXME
    __init_loc[sig] = "User location unknown" #[ errors.nearest-user-location ] ;

    # If we have a requirement, this version should only be applied under that
    # condition. To accomplish this we add a toolset requirement that imposes
    # the toolset subcondition, which encodes the version.
    if requirement:
        r = ['<toolset>' + toolset, requirement]
        r = ','.join(r)
        toolset.add_requirements([r + ':' + c for c in subcondition])

    # We add the requirements, if any, to the condition to scope the toolset
    # variables and options to this specific version.
    condition = [condition]
    if requirement:
        condition += [requirement]

    if __show_configuration:
        print "notice:", condition
    return ['/'.join(condition)]

# Ported from trunk@47077
def get_invocation_command_nodefault(
    toolset, tool, user_provided_command=[], additional_paths=[], path_last=False):
    """
        A helper rule to get the command to invoke some tool. If
        'user-provided-command' is not given, tries to find binary named 'tool' in
        PATH and in the passed 'additional-path'. Otherwise, verifies that the first
        element of 'user-provided-command' is an existing program.
        
        This rule returns the command to be used when invoking the tool. If we can't
        find the tool, a warning is issued. If 'path-last' is specified, PATH is
        checked after 'additional-paths' when searching for 'tool'.
    """
    assert(isinstance(toolset, str))
    assert(isinstance(tool, str))
    assert(isinstance(user_provided_command, list))
    if additional_paths is not None:
        assert(isinstance(additional_paths, list))
        assert(all([isinstance(path, str) for path in additional_paths]))
    assert(all(isinstance(path, str) for path in additional_paths))
    assert(isinstance(path_last, bool))
    
    if not user_provided_command:
        command = find_tool(tool, additional_paths, path_last) 
        if not command and __debug_configuration:
            print "warning: toolset", toolset, "initialization: can't find tool, tool"
            #FIXME
            #print "warning: initialized from" [ errors.nearest-user-location ] ;
    else:
        command = check_tool(user_provided_command)
        assert(isinstance(command, list))
        command=' '.join(command)
        if not command and __debug_configuration:
            print "warning: toolset", toolset, "initialization:"
            print "warning: can't find user-provided command", user_provided_command
            #FIXME
            #ECHO "warning: initialized from" [ errors.nearest-user-location ]

    assert(isinstance(command, str))
    
    return command

# ported from trunk@47174
def get_invocation_command(toolset, tool, user_provided_command = [],
                           additional_paths = [], path_last = False):
    """ Same as get_invocation_command_nodefault, except that if no tool is found,
        returns either the user-provided-command, if present, or the 'tool' parameter.
    """

    assert(isinstance(toolset, str))
    assert(isinstance(tool, str))
    assert(isinstance(user_provided_command, list))
    if additional_paths is not None:
        assert(isinstance(additional_paths, list))
        assert(all([isinstance(path, str) for path in additional_paths]))
    assert(isinstance(path_last, bool))

    result = get_invocation_command_nodefault(toolset, tool,
                                              user_provided_command,
                                              additional_paths,
                                              path_last)

    if not result:
        if user_provided_command:
            result = user_provided_command[0]
        else:
            result = tool

    assert(isinstance(result, str))
    
    return result

# ported from trunk@47281
def get_absolute_tool_path(command):
    """
        Given an invocation command,
        return the absolute path to the command. This works even if commnad
        has not path element and is present in PATH.
    """
    if os.path.dirname(command):
        return os.path.dirname(command)
    else:
        programs = path.programs_path()
        m = path.glob(programs, [command, command + '.exe' ])
        if not len(m):
            if __debug_configuration:
                print "Could not find:", command, "in", programs
            return None
        return os.path.dirname(m[0])

# ported from trunk@47174
def find_tool(name, additional_paths = [], path_last = False):
    """ Attempts to find tool (binary) named 'name' in PATH and in
        'additional-paths'.  If found in path, returns 'name'.  If
        found in additional paths, returns full name.  If the tool
        is found in several directories, returns the first path found.
        Otherwise, returns the empty string.  If 'path_last' is specified,
        path is checked after 'additional_paths'.
    """
    assert(isinstance(name, str))
    assert(isinstance(additional_paths, list))
    assert(isinstance(path_last, bool))

    programs = path.programs_path()
    match = path.glob(programs, [name, name + '.exe'])
    additional_match = path.glob(additional_paths, [name, name + '.exe'])

    result = []
    if path_last:
        result = additional_match
        if not result and match:
            result = match

    else:
        if match:
            result = match

        elif additional_match:
            result = additional_match

    if result:
        return path.native(result[0])
    else:
        return ''

#ported from trunk@47281
def check_tool_aux(command):
    """ Checks if 'command' can be found either in path
        or is a full name to an existing file.
    """
    assert(isinstance(command, str))
    dirname = os.path.dirname(command)
    if dirname:
        if os.path.exists(command):
            return command
        # Both NT and Cygwin will run .exe files by their unqualified names.
        elif on_windows() and os.path.exists(command + '.exe'):
            return command
        # Only NT will run .bat files by their unqualified names.
        elif os_name() == 'NT' and os.path.exists(command + '.bat'):
            return command
    else:
        paths = path.programs_path()
        if path.glob(paths, [command]):
            return command

# ported from trunk@47281
def check_tool(command):
    """ Checks that a tool can be invoked by 'command'. 
        If command is not an absolute path, checks if it can be found in 'path'.
        If comand is absolute path, check that it exists. Returns 'command'
        if ok and empty string otherwise.
    """
    assert(isinstance(command, list))
    assert(all(isinstance(c, str) for c in command))
    #FIXME: why do we check the first and last elements????
    if check_tool_aux(command[0]) or check_tool_aux(command[-1]):
        return command

# ported from trunk@47281
def handle_options(tool, condition, command, options):
    """ Handle common options for toolset, specifically sets the following
        flag variables:
        - CONFIG_COMMAND to 'command'
        - OPTIOns for compile to the value of <compileflags> in options
        - OPTIONS for compile.c to the value of <cflags> in options
        - OPTIONS for compile.c++ to the value of <cxxflags> in options
        - OPTIONS for compile.fortran to the value of <fflags> in options
        - OPTIONs for link to the value of <linkflags> in options
    """
    from b2.build import toolset

    assert(isinstance(tool, str))
    assert(isinstance(condition, list))
    assert(isinstance(command, str))
    assert(isinstance(options, list))
    assert(command)
    toolset.flags(tool, 'CONFIG_COMMAND', condition, [command])
    toolset.flags(tool + '.compile', 'OPTIONS', condition, feature.get_values('<compileflags>', options))
    toolset.flags(tool + '.compile.c', 'OPTIONS', condition, feature.get_values('<cflags>', options))
    toolset.flags(tool + '.compile.c++', 'OPTIONS', condition, feature.get_values('<cxxflags>', options))
    toolset.flags(tool + '.compile.fortran', 'OPTIONS', condition, feature.get_values('<fflags>', options))
    toolset.flags(tool + '.link', 'OPTIONS', condition, feature.get_values('<linkflags>', options))

# ported from trunk@47281
def get_program_files_dir():
    """ returns the location of the "program files" directory on a windows
        platform
    """
    ProgramFiles = bjam.variable("ProgramFiles")
    if ProgramFiles:
        ProgramFiles = ' '.join(ProgramFiles)
    else:
        ProgramFiles = "c:\\Program Files"
    return ProgramFiles

# ported from trunk@47281
def rm_command():
    return __RM

# ported from trunk@47281
def copy_command():
    return __CP

# ported from trunk@47281
def variable_setting_command(variable, value):
    """
        Returns the command needed to set an environment variable on the current
        platform. The variable setting persists through all following commands and is
        visible in the environment seen by subsequently executed commands. In other
        words, on Unix systems, the variable is exported, which is consistent with the
        only possible behavior on Windows systems.
    """
    assert(isinstance(variable, str))
    assert(isinstance(value, str))

    if os_name() == 'NT':
        return "set " + variable + "=" + value + os.linesep
    else:
        # (todo)
        #   The following does not work on CYGWIN and needs to be fixed. On
        # CYGWIN the $(nl) variable holds a Windows new-line \r\n sequence that
        # messes up the executed export command which then reports that the
        # passed variable name is incorrect. This is most likely due to the
        # extra \r character getting interpreted as a part of the variable name.
        #
        #   Several ideas pop to mind on how to fix this:
        #     * One way would be to separate the commands using the ; shell
        #       command separator. This seems like the quickest possible
        #       solution but I do not know whether this would break code on any
        #       platforms I I have no access to.
        #     * Another would be to not use the terminating $(nl) but that would
        #       require updating all the using code so it does not simply
        #       prepend this variable to its own commands.
        #     * I guess the cleanest solution would be to update Boost Jam to
        #       allow explicitly specifying \n & \r characters in its scripts
        #       instead of always relying only on the 'current OS native newline
        #       sequence'.
        #
        #   Some code found to depend on this behaviour:
        #     * This Boost Build module.
        #         * __test__ rule.
        #         * path-variable-setting-command rule.
        #     * python.jam toolset.
        #     * xsltproc.jam toolset.
        #     * fop.jam toolset.
        #                                     (todo) (07.07.2008.) (Jurko)
        #
        # I think that this works correctly in python -- Steven Watanabe
        return variable + "=" + value + os.linesep + "export " + variable + os.linesep

def path_variable_setting_command(variable, paths):
    """
        Returns a command to sets a named shell path variable to the given NATIVE
        paths on the current platform.
    """
    assert(isinstance(variable, str))
    assert(isinstance(paths, list))
    sep = os.path.pathsep
    return variable_setting_command(variable, sep.join(paths))

def prepend_path_variable_command(variable, paths):
    """
        Returns a command that prepends the given paths to the named path variable on
        the current platform.
    """    
    return path_variable_setting_command(variable,
        paths + os.environ.get(variable, "").split(os.pathsep))

def file_creation_command():
    """
        Return a command which can create a file. If 'r' is result of invocation, then
        'r foobar' will create foobar with unspecified content. What happens if file
        already exists is unspecified.
    """
    if os_name() == 'NT':
        return "echo. > "
    else:
        return "touch "

#FIXME: global variable
__mkdir_set = set()
__re_windows_drive = re.compile(r'^.*:\$')

def mkdir(engine, target):
    # If dir exists, do not update it. Do this even for $(DOT).
    bjam.call('NOUPDATE', target)

    global __mkdir_set

    # FIXME: Where is DOT defined?
    #if $(<) != $(DOT) && ! $($(<)-mkdir):
    if target != '.' and target not in __mkdir_set:
        # Cheesy gate to prevent multiple invocations on same dir.
        __mkdir_set.add(target)

        # Schedule the mkdir build action.
        if os_name() == 'NT':
            engine.set_update_action("common.MkDir1-quick-fix-for-windows", target, [])
        else:
            engine.set_update_action("common.MkDir1-quick-fix-for-unix", target, [])

        # Prepare a Jam 'dirs' target that can be used to make the build only
        # construct all the target directories.
        engine.add_dependency('dirs', target)

        # Recursively create parent directories. $(<:P) = $(<)'s parent & we
        # recurse until root.

        s = os.path.dirname(target)
        if os_name() == 'NT':
            if(__re_windows_drive.match(s)):
                s = ''
                
        if s:
            if s != target:
                engine.add_dependency(target, s)
                mkdir(engine, s)
            else:
                bjam.call('NOTFILE', s)

__re_version = re.compile(r'^([^.]+)[.]([^.]+)[.]?([^.]*)')

def format_name(format, name, target_type, prop_set):
    """ Given a target, as given to a custom tag rule, returns a string formatted
        according to the passed format. Format is a list of properties that is
        represented in the result. For each element of format the corresponding target
        information is obtained and added to the result string. For all, but the
        literal, the format value is taken as the as string to prepend to the output
        to join the item to the rest of the result. If not given "-" is used as a
        joiner.

        The format options can be:

          <base>[joiner]
              ::  The basename of the target name.
          <toolset>[joiner]
              ::  The abbreviated toolset tag being used to build the target.
          <threading>[joiner]
              ::  Indication of a multi-threaded build.
          <runtime>[joiner]
              ::  Collective tag of the build runtime.
          <version:/version-feature | X.Y[.Z]/>[joiner]
              ::  Short version tag taken from the given "version-feature"
                  in the build properties. Or if not present, the literal
                  value as the version number.
          <property:/property-name/>[joiner]
              ::  Direct lookup of the given property-name value in the
                  build properties. /property-name/ is a regular expression.
                  e.g. <property:toolset-.*:flavor> will match every toolset.
          /otherwise/
              ::  The literal value of the format argument.

        For example this format:

          boost_ <base> <toolset> <threading> <runtime> <version:boost-version>

        Might return:

          boost_thread-vc80-mt-gd-1_33.dll, or
          boost_regex-vc80-gd-1_33.dll

        The returned name also has the target type specific prefix and suffix which
        puts it in a ready form to use as the value from a custom tag rule.
    """
    assert(isinstance(format, list))
    assert(isinstance(name, str))
    assert(isinstance(target_type, str) or not type)
    # assert(isinstance(prop_set, property_set.PropertySet))
    if type.is_derived(target_type, 'LIB'):
        result = "" ;
        for f in format:
            grist = get_grist(f)
            if grist == '<base>':
                result += os.path.basename(name)
            elif grist == '<toolset>':
                result += join_tag(get_value(f), 
                    toolset_tag(name, target_type, prop_set))
            elif grist == '<threading>':
                result += join_tag(get_value(f),
                    threading_tag(name, target_type, prop_set))
            elif grist == '<runtime>':
                result += join_tag(get_value(f),
                    runtime_tag(name, target_type, prop_set))
            elif grist.startswith('<version:'):
                key = grist[len('<version:'):-1]
                version = prop_set.get('<' + key + '>')
                if not version:
                    version = key
                version = __re_version.match(version)
                result += join_tag(get_value(f), version[1] + '_' + version[2])
            elif grist.startswith('<property:'):
                key = grist[len('<property:'):-1]
                property_re = re.compile('<(' + key + ')>')
                p0 = None
                for prop in prop_set.raw():
                    match = property_re.match(prop)
                    if match:
                        p0 = match[1]
                        break
                if p0:
                    p = prop_set.get('<' + p0 + '>')
                    if p:
                        assert(len(p) == 1)
                        result += join_tag(ungrist(f), p)
            else:
                result += f

        result = b2.build.virtual_target.add_prefix_and_suffix(
            ''.join(result), target_type, prop_set)
        return result

def join_tag(joiner, tag):
    if tag:
        if not joiner: joiner = '-'
        return joiner + tag
    return ''

__re_toolset_version = re.compile(r"<toolset.*version>(\d+)[.](\d*)")

def toolset_tag(name, target_type, prop_set):
    tag = ''

    properties = prop_set.raw()
    tools = prop_set.get('<toolset>')
    assert(len(tools) == 1)
    tools = tools[0]
    if tools.startswith('borland'): tag += 'bcb'
    elif tools.startswith('como'): tag += 'como'
    elif tools.startswith('cw'): tag += 'cw'
    elif tools.startswith('darwin'): tag += 'xgcc'
    elif tools.startswith('edg'): tag += edg
    elif tools.startswith('gcc'):
        flavor = prop_set.get('<toolset-gcc:flavor>')
        ''.find
        if flavor.find('mingw') != -1:
            tag += 'mgw'
        else:
            tag += 'gcc'
    elif tools == 'intel':
        if prop_set.get('<toolset-intel:platform>') == ['win']:
            tag += 'iw'
        else:
            tag += 'il'
    elif tools.startswith('kcc'): tag += 'kcc'
    elif tools.startswith('kylix'): tag += 'bck'
    #case metrowerks* : tag += cw ;
    #case mingw* : tag += mgw ;
    elif tools.startswith('mipspro'): tag += 'mp'
    elif tools.startswith('msvc'): tag += 'vc'
    elif tools.startswith('sun'): tag += 'sw'
    elif tools.startswith('tru64cxx'): tag += 'tru'
    elif tools.startswith('vacpp'): tag += 'xlc'

    for prop in properties:
        match = __re_toolset_version.match(prop)
        if(match):
            version = match
            break
    version_string = None
    # For historical reasons, vc6.0 and vc7.0 use different naming.
    if tag == 'vc':
        if version.group(1) == '6':
            # Cancel minor version.
            version_string = '6'
        elif version.group(1) == '7' and version.group(2) == '0':
            version_string = '7'

    # On intel, version is not added, because it does not matter and it's the
    # version of vc used as backend that matters. Ideally, we'd encode the
    # backend version but that would break compatibility with V1.
    elif tag == 'iw':
        version_string = ''

    # On borland, version is not added for compatibility with V1.
    elif tag == 'bcb':
        version_string = ''

    if version_string is None:
        version = version.group(1) + version.group(2)

    tag += version

    return tag


def threading_tag(name, target_type, prop_set):
    tag = ''
    properties = prop_set.raw()
    if '<threading>multi' in properties: tag = 'mt'

    return tag


def runtime_tag(name, target_type, prop_set ):
    tag = ''

    properties = prop_set.raw()
    if '<runtime-link>static' in properties: tag += 's'

    # This is an ugly thing. In V1, there's a code to automatically detect which
    # properties affect a target. So, if <runtime-debugging> does not affect gcc
    # toolset, the tag rules won't even see <runtime-debugging>. Similar
    # functionality in V2 is not implemented yet, so we just check for toolsets
    # which are known to care about runtime debug.
    if '<toolset>msvc' in properties \
       or '<stdlib>stlport' in properties \
       or '<toolset-intel:platform>win' in properties:
        if '<runtime-debugging>on' in properties: tag += 'g'

    if '<python-debugging>on' in properties: tag += 'y'
    if '<variant>debug' in properties: tag += 'd'
    if '<stdlib>stlport' in properties: tag += 'p'
    if '<stdlib-stlport:iostream>hostios' in properties: tag += 'n'

    return tag


## TODO:
##rule __test__ ( )
##{
##    import assert ;
##
##    local nl = "
##" ;
##
##    local save-os = [ modules.peek os : .name ] ;
##
##    modules.poke os : .name : LINUX ;
##
##    assert.result "PATH=foo:bar:baz$(nl)export PATH$(nl)"
##        : path-variable-setting-command PATH : foo bar baz ;
##
##    assert.result "PATH=foo:bar:$PATH$(nl)export PATH$(nl)"
##        : prepend-path-variable-command PATH : foo bar ;
##
##    modules.poke os : .name : NT ;
##
##    assert.result "set PATH=foo;bar;baz$(nl)"
##        : path-variable-setting-command PATH : foo bar baz ;
##
##    assert.result "set PATH=foo;bar;%PATH%$(nl)"
##        : prepend-path-variable-command PATH : foo bar ;
##
##    modules.poke os : .name : $(save-os) ;
##}

def init(manager):
    engine = manager.engine()

    engine.register_action("common.MkDir1-quick-fix-for-unix", 'mkdir -p "$(<)"')
    engine.register_action("common.MkDir1-quick-fix-for-windows", 'if not exist "$(<)\\" mkdir "$(<)"')

    import b2.tools.make
    import b2.build.alias

    global __RM, __CP, __IGNORE, __LN
    # ported from trunk@47281
    if os_name() == 'NT':
        __RM = 'del /f /q'
        __CP = 'copy'
        __IGNORE = '2>nul >nul & setlocal'
        __LN = __CP
        #if not __LN:
        #    __LN = CP
    else:
        __RM = 'rm -f'
        __CP = 'cp'
        __IGNORE = ''
        __LN = 'ln'
        
    engine.register_action("common.Clean", __RM + ' "$(>)"',
                           flags=['piecemeal', 'together', 'existing'])
    engine.register_action("common.copy", __CP + ' "$(>)" "$(<)"')
    engine.register_action("common.RmTemps", __RM + ' "$(>)" ' + __IGNORE,
                           flags=['quietly', 'updated', 'piecemeal', 'together'])

    engine.register_action("common.hard-link", 
        __RM + ' "$(<)" 2$(NULL_OUT) $(NULL_OUT)' + os.linesep +
        __LN + ' "$(>)" "$(<)" $(NULL_OUT)')