build: cope with binaries with subdir prefixes (for s3 waf build)
[mat/samba.git] / buildtools / wafsamba / samba_install.py
1 ###########################
2 # this handles the magic we need to do for installing
3 # with all the configure options that affect rpath and shared
4 # library use
5
6 import Options
7 from TaskGen import feature, before, after
8 from samba_utils import *
9
10 @feature('install_bin')
11 @after('apply_core')
12 @before('apply_link')
13 def install_binary(self):
14     '''install a binary, taking account of the different rpath varients'''
15     bld = self.bld
16
17     # get the ldflags we will use for install and build
18     install_ldflags = install_rpath(bld)
19     build_ldflags   = build_rpath(bld)
20
21     if not Options.is_install or not self.samba_install:
22         # just need to set rpath if we are not installing
23         self.env.append_value('LINKFLAGS', build_ldflags)
24         return
25
26     # work out the install path, expanding variables
27     install_path = self.samba_inst_path or '${BINDIR}'
28     install_path = bld.EXPAND_VARIABLES(install_path)
29
30     orig_target = self.target
31
32     if install_ldflags != build_ldflags:
33         # we will be creating a new target name, and using that for the
34         # install link. That stops us from overwriting the existing build
35         # target, which has different ldflags
36         self.target += '.inst'
37
38     # setup the right rpath link flags for the install
39     self.env.append_value('LINKFLAGS', install_ldflags)
40
41     # tell waf to install the right binary
42     bld.install_as(os.path.join(install_path, orig_target),
43                    os.path.join(self.path.abspath(bld.env), self.target),
44                    chmod=0755)
45
46
47
48 @feature('install_lib')
49 @after('apply_core')
50 @before('apply_link')
51 def install_library(self):
52     '''install a library, taking account of the different rpath varients'''
53     if getattr(self, 'done_install_library', False):
54         return
55
56     bld = self.bld
57
58     install_ldflags = install_rpath(bld)
59     build_ldflags   = build_rpath(bld)
60
61     if not Options.is_install or not self.samba_install:
62         # just need to set the build rpath if we are not installing
63         self.env.append_value('LINKFLAGS', build_ldflags)
64         return
65
66     # setup the install path, expanding variables
67     install_path = self.samba_inst_path or '${LIBDIR}'
68     install_path = bld.EXPAND_VARIABLES(install_path)
69
70     if install_ldflags != build_ldflags:
71         # we will be creating a new target name, and using that for the
72         # install link. That stops us from overwriting the existing build
73         # target, which has different ldflags
74         self.done_install_library = True
75         t = self.clone('default')
76         t.target += '.inst'
77         self.env.append_value('LINKFLAGS', build_ldflags)
78     else:
79         t = self
80
81     t.env.append_value('LINKFLAGS', install_ldflags)
82
83     if self.samba_realname:
84         install_name = self.samba_realname
85         install_link = None
86         inst_name    = t.target + '.so'
87     elif self.vnum:
88         vnum_base    = self.vnum.split('.')[0]
89         install_name = 'lib%s.so.%s' % (self.target, self.vnum)
90         install_link = 'lib%s.so.%s' % (self.target, vnum_base)
91         inst_name    = 'lib%s.so' % t.target
92     else:
93         install_name = 'lib%s.so' % self.target
94         install_link = None
95         inst_name    = 'lib%s.so' % t.target
96
97     # tell waf to install the library
98     bld.install_as(os.path.join(install_path, install_name),
99                    os.path.join(self.path.abspath(bld.env), inst_name))
100     if install_link:
101         # and the symlink if needed
102         bld.symlink_as(os.path.join(install_path, install_link),
103                        install_name)
104
105
106
107 ##############################
108 # handle the creation of links for libraries and binaries in the build tree
109
110 @feature('symlink_lib')
111 @after('apply_link')
112 def symlink_lib(self):
113     '''symlink a shared lib'''
114
115     if self.target.endswith('.inst'):
116         return
117
118     blddir = Utils.g_module.blddir
119     libpath = self.link_task.outputs[0].abspath(self.env)
120
121     # calculat the link target and put it in the environment
122     soext=""
123     vnum = getattr(self, 'vnum', None)
124     if vnum is not None:
125         soext = '.' + vnum.split('.')[0]
126
127     link_target = getattr(self, 'link_name', '')
128     if link_target == '':
129         link_target = '%s/lib%s.so%s' % (LIB_PATH, self.target, soext)
130
131     link_target = os.path.join(blddir, link_target)
132
133     libpath = os_path_relpath(libpath, os.path.dirname(link_target))
134
135     if os.path.lexists(link_target):
136         os.unlink(link_target)
137     os.symlink(libpath, link_target)
138
139
140 @feature('symlink_bin')
141 @after('apply_link')
142 def symlink_bin(self):
143     '''symlink a binary into the build directory'''
144
145     if self.target.endswith('.inst'):
146         return
147
148     blddir = Utils.g_module.blddir
149     binpath = self.link_task.outputs[0].abspath(self.env)
150     bldpath = os.path.join(blddir, os.path.basename(self.target))
151
152     binpath = os_path_relpath(binpath, os.path.dirname(bldpath))
153
154     if os.path.lexists(bldpath):
155         os.unlink(bldpath)
156     os.symlink(binpath, bldpath)