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
|
# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import tempfile
import shutil
from cerbero.config import Architecture, Platform
from cerbero.ide.pkgconfig import PkgConfig
from cerbero.ide.xcode.fwlib import StaticFrameworkLibrary
from cerbero.errors import EmptyPackageError
from cerbero.packages import PackagerBase, PackageType
from cerbero.packages.package import Package, MetaPackage, SDKPackage, App,\
PackageBase
from cerbero.packages.osx.distribution import DistributionXML
from cerbero.packages.osx.bundles import FrameworkBundlePackager,\
ApplicationBundlePackager
from cerbero.packages.osx.buildtools import PackageBuild, ProductBuild
from cerbero.tools.osxrelocator import OSXRelocator
from cerbero.utils import shell, _
from cerbero.utils import messages as m
class FrameworkHeadersMixin(object):
def _create_framework_headers(self, prefix, include_dirs, tmp):
'''
To create a real OS X Framework we need to get rid of the versioned
directories for headers.
We should still keep the current tree in $PREFIX/include/ so that it
still works with pkg-config, but we will create a new $PREFIX/Headers
folder with links to the include directories removing the versioned
directories with the help of pkg-config getting something like:
include/gstreamer-0.10/gst/gst.h -> Headers/gst/gst.h
include/zlib.h -> Headers/zlib.h
'''
# Replace prefix path with the temporal directory one
include_dirs = [x.replace(os.path.abspath(prefix), tmp)
for x in include_dirs]
# Remove trailing /
include_dirs = [os.path.abspath(x) for x in include_dirs]
# Remove 'include' dir
include_dirs = [x for x in include_dirs if not
x.endswith(os.path.join(tmp, 'include'))]
include_dirs = [x for x in include_dirs if os.path.isdir(x)]
include = os.path.join(tmp, 'include/')
headers = os.path.join(tmp, 'Headers')
self._copy_unversioned_headers(include, include, headers, include_dirs)
self._copy_versioned_headers(headers, include_dirs)
def _copy_versioned_headers(self, headers, include_dirs):
# Path is listed as an includes dir by pkgconfig
# Copy files and directories to Headers
for inc_dir in include_dirs:
if not os.path.exists(inc_dir):
continue
for p in os.listdir(inc_dir):
src = os.path.join(inc_dir, p)
dest = os.path.join(headers, p)
if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
# include/cairo/cairo.h -> Headers/cairo.h
if os.path.isfile(src):
shutil.copy(src, dest)
# include/gstreamer-0.10/gst -> Headers/gst
elif os.path.isdir(src):
shell.copy_dir(src, dest)
def _copy_unversioned_headers(self, dirname, include, headers,
include_dirs):
if not os.path.exists(dirname):
return
for path in os.listdir(dirname):
path = os.path.join(dirname, path)
rel_path = path.replace(include, '')
# include/zlib.h -> Headers/zlib.h
if os.path.isfile(path):
p = os.path.join(headers, rel_path)
d = os.path.dirname(p)
if not os.path.exists(d):
os.makedirs(d)
shutil.copy(path, p)
# scan sub-directories
elif os.path.isdir(path):
if path in include_dirs:
continue
else:
# Copy the directory
self._copy_unversioned_headers(path, include,
headers, include_dirs)
class OSXPackage(PackagerBase, FrameworkHeadersMixin):
'''
Creates an osx package from a L{cerbero.packages.package.Package}
@ivar package: package used to create the osx package
@type package: L{cerbero.packages.package.Package}
'''
def __init__(self, config, package, store):
PackagerBase.__init__(self, config, package, store)
def pack(self, output_dir, devel=True, force=False, keep_temp=False,
version=None, install_dir=None, include_dirs=None,
sdk_version=None):
PackagerBase.pack(self, output_dir, devel, force, keep_temp)
self.install_dir = install_dir or self.package.get_install_dir()
self.version = version or self.package.version
self.sdk_version = sdk_version or self.version
self.include_dirs = include_dirs or PkgConfig.list_all_include_dirs()
# create the runtime package
try:
runtime_path = self._create_package(PackageType.RUNTIME,
output_dir, force)
except EmptyPackageError, e:
if not devel:
raise e
runtime_path = None
if not devel:
return [runtime_path, None]
try:
# create the development package
devel_path = self._create_package(PackageType.DEVEL, output_dir,
force)
except EmptyPackageError, e:
if runtime_path is None:
raise e
devel_path = None
return [runtime_path, devel_path]
def _get_install_dir(self):
if self.config.target_arch != Architecture.UNIVERSAL:
arch_dir = self.config.target_arch
else:
arch_dir = ''
return os.path.join(self.install_dir, 'Versions',
self.sdk_version, arch_dir)
def _create_package(self, package_type, output_dir, force):
self.package.set_mode(package_type)
files = self.files_list(package_type, force)
output_file = os.path.join(output_dir, '%s-%s-%s.pkg' %
(self.package.name, self.version, self.config.target_arch))
root, resources = self._create_bundle(files, package_type)
packagebuild = PackageBuild()
packagebuild.create_package(root, self.package.identifier(),
self.package.version, self.package.shortdesc, output_file,
self._get_install_dir(), scripts_path=resources)
return output_file
def _create_bundle(self, files, package_type):
'''
Moves all the files that are going to be packaged to a temporary
directory to create the bundle
'''
tmp = tempfile.mkdtemp()
root = os.path.join(tmp, 'Root')
resources = os.path.join(tmp, 'Resources')
for f in files:
in_path = os.path.join(self.config.prefix, f)
if not os.path.exists(in_path):
m.warning("File %s is missing and won't be added to the "
"package" % in_path)
continue
out_path = os.path.join(root, f)
out_dir = os.path.split(out_path)[0]
if not os.path.exists(out_dir):
os.makedirs(out_dir)
shutil.copy(in_path, out_path)
if package_type == PackageType.DEVEL:
self._create_framework_headers(self.config.prefix, self.include_dirs, root)
# Copy scripts to the Resources directory
os.makedirs(resources)
if os.path.exists(self.package.resources_preinstall):
shutil.copy(os.path.join(self.package.resources_preinstall),
os.path.join(resources, 'preinstall'))
if os.path.exists(self.package.resources_postinstall):
shutil.copy(os.path.join(self.package.resources_postinstall),
os.path.join(resources, 'postinstall'))
return root, resources
class ProductPackage(PackagerBase):
'''
Creates an osx package from a L{cerbero.package.package.MetaPackage} using
productbuild.
@ivar package: package with the info to build the installer package
@type package: L{cerbero.packages.package.MetaPackage}
'''
PKG_EXT = '.pkg'
def __init__(self, config, package, store):
PackagerBase.__init__(self, config, package, store)
self.packages = self.store.get_package_deps(package)
self.packages_paths = {}
self.empty_packages = {}
def pack(self, output_dir, devel=False, force=False, keep_temp=False):
PackagerBase.pack(self, output_dir, devel, force, keep_temp)
self._prepare_pack()
if isinstance(self.package, SDKPackage):
packager = self._create_framework_bundle_packager()
self._create_framework_bundle_layout(packager)
self._create_framework_bundle_package(packager)
self._create_packages()
paths = []
# create runtime package
r_path = self._create_product(PackageType.RUNTIME)
paths.append(r_path)
if devel:
# create devel package
d_path = self._create_product(PackageType.DEVEL)
paths.append(d_path)
self.package.set_mode(PackageType.RUNTIME)
if not keep_temp:
shutil.rmtree(self.tmp)
return paths
def _prepare_pack(self):
self.include_dirs = PkgConfig.list_all_include_dirs()
self.tmp = tempfile.mkdtemp()
self.fw_path = self.tmp
self.empty_packages = {PackageType.RUNTIME: [], PackageType.DEVEL: []}
self.packages_paths = {PackageType.RUNTIME: {}, PackageType.DEVEL: {}}
def _package_name(self, suffix):
return '%s-%s-%s%s' % (self.package.name, self.package.version,
self.config.target_arch, suffix)
def _create_framework_bundle_packager(self):
m.action(_("Creating framework package"))
packager = FrameworkBundlePackager(self.package, 'osx-framework',
'Framework Bundle',
'3ffe67c2-4565-411f-8287-e8faa892f853')
return packager
def _create_framework_bundle_layout(self, packager):
packager.create_bundle(self.fw_path)
def _create_framework_bundle_package(self, packager):
package = packager.package
package.install_dir = self.package.install_dir
self.store.add_package(package)
packages = self.package.packages[:] + [(package.name, True, True)]
self.package.packages = packages
path = packager.pack(self.output_dir, self.fw_path)[0]
if self.config.target_platform == Platform.IOS:
self.packages_paths[PackageType.DEVEL][package] = path
self.empty_packages[PackageType.RUNTIME].append(package)
else:
self.packages_paths[PackageType.RUNTIME][package] = path
self.empty_packages[PackageType.DEVEL].append(package)
def _create_product(self, package_type):
self.package.set_mode(package_type)
m.action(_("Creating Distribution.xml for package %s " % self.package))
distro = DistributionXML(self.package, self.store, self.tmp,
self.packages_paths[package_type],
self.empty_packages[package_type], package_type,
self.config.target_arch, home_folder=True)
distro_path = tempfile.NamedTemporaryFile().name
distro.write(distro_path)
output_file = os.path.join(self.output_dir, self._package_name('.pkg'))
output_file = os.path.abspath(output_file)
pb = ProductBuild()
pb.create_package(distro_path, output_file)
return output_file
def _create_packages(self):
for p in self.packages:
m.action(_("Creating package %s ") % p)
packager = OSXPackage(self.config, p, self.store)
try:
paths = packager.pack(self.output_dir, self.devel, self.force,
self.keep_temp, self.package.version,
install_dir=self.package.get_install_dir(),
include_dirs=self.include_dirs,
sdk_version=self.package.sdk_version)
m.action(_("Package created sucessfully"))
except EmptyPackageError:
paths = [None, None]
if paths[0] is not None:
self.packages_paths[PackageType.RUNTIME][p] = paths[0]
else:
self.empty_packages[PackageType.RUNTIME].append(p)
if paths[1] is not None:
self.packages_paths[PackageType.DEVEL][p] = paths[1]
else:
self.empty_packages[PackageType.DEVEL].append(p)
class ApplicationPackage(PackagerBase):
'''
Creates an osx package from a L{cerbero.packages.package.Package}
@ivar package: package used to create the osx package
@type package: L{cerbero.packages.package.Package}
'''
def __init__(self, config, package, store):
PackagerBase.__init__(self, config, package, store)
def pack(self, output_dir, devel=False, force=False, keep_temp=False):
PackagerBase.pack(self, output_dir, devel, force, keep_temp)
self.tmp = tempfile.mkdtemp()
self.appdir = os.path.join(self.tmp, '%s.app' % self.package.app_name)
# copy files to the bundle. it needs to be done first because the app
# bundle will try to create links for the main executable
self._create_bundle()
self._create_app_bundle()
self._strip_binaries()
self._relocate_binaries()
self._add_applications_link()
dmg = self._create_dmg()
return [dmg, None]
def _create_bundle(self):
'''
Moves all the files that are going to be packaged to the bundle's
temporary directory
'''
out_dir = os.path.join(self.appdir, 'Contents', 'Home')
os.makedirs(out_dir)
for f in self.package.files_list():
in_path = os.path.join(self.config.prefix, f)
if not os.path.exists(in_path):
m.warning("File %s is missing and won't be added to the "
"package" % in_path)
continue
out_path = os.path.join(out_dir, f)
odir = os.path.split(out_path)[0]
if not os.path.exists(odir):
os.makedirs(odir)
shutil.copy(in_path, out_path)
def _create_app_bundle(self):
''' Creates the OS X Application bundle in temporary directory '''
packager = ApplicationBundlePackager(self.package)
return packager.create_bundle(self.appdir)
def _strip_binaries(self):
pass
def _relocate_binaries(self):
prefix = self.config.prefix
if prefix[-1] == '/':
prefix = prefix[:-1]
for path in ['bin', 'lib', 'libexec']:
relocator = OSXRelocator(
os.path.join(self.appdir, 'Contents', 'Home', path),
self.config.prefix, '@executable_path/../', True)
relocator.relocate()
def _add_applications_link(self):
# Create link to /Applications
applications_link = os.path.join(self.tmp, 'Applications')
shell.call('ln -s /Applications %s' % applications_link)
def _create_dmg(self):
dmg_file = os.path.join(self.output_dir, '%s-%s-%s.dmg' % (
self.package.app_name, self.package.version, self.config.target_arch))
# Create Disk Image
cmd = 'hdiutil create %s -volname %s -ov -srcfolder %s' % \
(dmg_file, self.package.app_name, self.tmp)
shell.call(cmd)
return dmg_file
class IOSPackage(ProductPackage, FrameworkHeadersMixin):
'''
Creates an ios Framework package from a
L{cerbero.package.package.MetaPackage} using productbuild.
This platform only support static linking, so the final package
consists on a the framework library and the headers files.
The framework library is built merging all the static libraries
listed in this package and the headers are copied unversionned to
the 'Headers' directory of the framework bundle.
The product package will only contain the ios-framework package
'''
def pack(self, output_dir, devel=False, force=False, keep_temp=False):
PackagerBase.pack(self, output_dir, devel, force, keep_temp)
framework_name = self.package.ios_framework_library[0]
self._prepare_pack()
self.fw_path = os.path.join(self.tmp, '%s.framework' % framework_name)
os.mkdir(self.fw_path)
files = [os.path.join(self.config.prefix, x) for x in
self.package.all_files_list()]
version_dir = os.path.join(self.fw_path, 'Versions', self.package.sdk_version)
libname = os.path.join(version_dir, framework_name)
packager = self._create_framework_bundle_packager()
self._create_framework_bundle_layout(packager)
self._copy_templates(files)
self._copy_headers(files, version_dir)
self._create_framework_headers(self.config.prefix,
self.include_dirs, version_dir)
shutil.rmtree(os.path.join(version_dir, 'include'))
shutil.rmtree(os.path.join(version_dir, 'lib'))
self._create_merged_lib(libname, files)
self.package.packages = []
self.fw_path = self.tmp
self._create_framework_bundle_package(packager)
paths = [self._create_product(PackageType.DEVEL)]
if not keep_temp:
shutil.rmtree(self.tmp)
return paths
def _copy_files (self, files, root):
for f in files:
out_path = f.replace(self.config.prefix, root)
out_dir = os.path.split(out_path)[0]
if not os.path.exists(out_dir):
os.makedirs(out_dir)
shutil.copy(f, out_path)
def _copy_templates(self, files):
templates_prefix = 'share/xcode/templates/ios'
templates = [x for x in files if templates_prefix in x]
for f in templates:
out_path = f.replace(self.config.prefix,
os.path.join(self.tmp, 'Templates'))
out_path = out_path.replace(templates_prefix, '')
out_dir = os.path.split(out_path)[0]
if not os.path.exists(out_dir):
os.makedirs(out_dir)
shutil.copy(f, out_path)
def _copy_headers(self, files, version_dir):
# Get the list of headers
incl_dir = os.path.join(self.config.prefix, 'include')
include_files = [os.path.join(incl_dir, x) for x in
os.listdir(incl_dir) if x.endswith('.h')]
for d in self.include_dirs:
include_files += [x for x in files if d in x]
self._copy_files (include_files, version_dir)
def _create_framework_bundle_packager(self):
m.action(_("Creating framework package"))
packager = FrameworkBundlePackager(self.package, 'ios-framework',
'Framework Bundle',
'3ffe67c2-3421-411f-8287-e8faa892f853')
return packager
def _create_merged_lib(self, libname, files):
# Get the list of static libraries
static_files = [x for x in files if x.endswith('.a')]
fwlib = StaticFrameworkLibrary(libname, libname, static_files,
self.config.target_arch)
fwlib.use_pkgconfig = False
if self.config.target_arch == Architecture.UNIVERSAL:
fwlib.universal_archs = self.config.universal_archs
fwlib.create()
class Packager(object):
def __new__(klass, config, package, store):
if config.target_platform == Platform.IOS:
if not isinstance(package, MetaPackage):
raise FatalError ("iOS platform only support packages",
"for MetaPackage")
return IOSPackage(config, package, store)
if isinstance(package, Package):
return OSXPackage(config, package, store)
elif isinstance(package, MetaPackage):
return ProductPackage(config, package, store)
elif isinstance(package, App):
return ApplicationPackage(config, package, store)
def register():
from cerbero.packages.packager import register_packager
from cerbero.config import Distro
register_packager(Distro.OS_X, Packager)
register_packager(Distro.IOS, Packager)
|