1# -*- coding: utf-8 -*-
2# Copyright 2010-2018, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Copy Qt frameworks to the target application's frameworks directory.
32
33Typical usage:
34
35  % python copy_qt_frameworks.py --qtdir=/path/to/qtdir/ \
36      --target=/path/to/target.app/Contents/Frameworks/
37"""
38
39__author__ = "horo"
40
41import optparse
42import os
43
44from .copy_file import CopyFiles
45from .util import PrintErrorAndExit
46from .util import RunOrDie
47
48
49def ParseOption():
50  """Parse command line options."""
51  parser = optparse.OptionParser()
52  parser.add_option('--qtdir', dest='qtdir')
53  parser.add_option('--target', dest='target')
54
55  (opts, _) = parser.parse_args()
56
57  return opts
58
59
60def GetFrameworkPath(name, version):
61  """Return path to the library in the framework."""
62  return '%s.framework/Versions/%s/%s' % (name, version, name)
63
64
65def CopyQt(qtdir, qtlib, version, target, copy_resources=False):
66  """Copy a Qt framework from qtdir to target."""
67  framework_path = GetFrameworkPath(qtlib, version)
68  CopyFiles(['%s/lib/%s' % (qtdir, framework_path)],
69            '%s/%s' % (target, framework_path))
70
71  if copy_resources:
72    # Copies Resources of QtGui
73    CopyFiles(['%s/lib/%s.framework/Versions/%s/Resources' %
74               (qtdir, qtlib, version)],
75              '%s/%s.framework/Resources' % (target, qtlib),
76              recursive=True)
77
78  if version == '4':
79    # For codesign, Info.plist should be copied to Resources/.
80    CopyFiles(['%s/lib/%s.framework/Contents/Info.plist' % (qtdir, qtlib)],
81              '%s/%s.framework/Resources/Info.plist' % (target, qtlib))
82
83
84def ChangeReferences(qtdir, path, version, target, ref_to, references=None):
85  """Change the references of frameworks, by using install_name_tool."""
86
87  # Change id
88  cmd = ['install_name_tool',
89         '-id', '%s/%s' % (ref_to, path),
90         '%s/%s' % (target, path)]
91  RunOrDie(cmd)
92
93  if not references:
94    return
95
96  # Change references
97  for ref in references:
98    ref_framework_path = GetFrameworkPath(ref, version)
99    change_cmd = ['install_name_tool', '-change',
100                  '%s/lib/%s' % (qtdir, ref_framework_path),
101                  '%s/%s' % (ref_to, ref_framework_path),
102                  '%s/%s' % (target, path)]
103    RunOrDie(change_cmd)
104
105
106def main():
107  opt = ParseOption()
108
109  if not opt.qtdir:
110    PrintErrorAndExit('--qtdir option is mandatory.')
111
112  if not opt.target:
113    PrintErrorAndExit('--target option is mandatory.')
114
115  qtdir = os.path.abspath(opt.qtdir)
116  target = os.path.abspath(opt.target)
117
118  ref_to = '@executable_path/../../../ConfigDialog.app/Contents/Frameworks'
119
120  CopyQt(qtdir, 'QtCore', '5', target, copy_resources=True)
121  CopyQt(qtdir, 'QtGui', '5', target, copy_resources=True)
122  CopyQt(qtdir, 'QtWidgets', '5', target, copy_resources=True)
123  CopyQt(qtdir, 'QtPrintSupport', '5', target, copy_resources=True)
124
125  ChangeReferences(qtdir, GetFrameworkPath('QtCore', '5'),
126                   '5', target, ref_to)
127  ChangeReferences(qtdir, GetFrameworkPath('QtGui', '5'),
128                   '5', target, ref_to,
129                   references=['QtCore'])
130  ChangeReferences(qtdir, GetFrameworkPath('QtWidgets', '5'),
131                   '5', target, ref_to,
132                   references=['QtCore', 'QtGui'])
133  ChangeReferences(qtdir, GetFrameworkPath('QtPrintSupport', '5'),
134                   '5', target, ref_to,
135                   references=['QtCore', 'QtGui', 'QtWidgets'])
136
137  libqcocoa = 'QtCore.framework/Resources/plugins/platforms/libqcocoa.dylib'
138  CopyFiles(['%s/plugins/platforms/libqcocoa.dylib' % qtdir],
139            '%s/%s' % (target, libqcocoa))
140  ChangeReferences(qtdir, libqcocoa, '5', target, ref_to,
141                   references=['QtCore', 'QtGui',
142                               'QtWidgets', 'QtPrintSupport'])
143
144
145if __name__ == '__main__':
146  main()
147