jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.3KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy 2008-2010
  4. """
  5. MacOSX related tools
  6. """
  7. import os, shutil, platform
  8. from waflib import Task, Utils, Errors
  9. from waflib.TaskGen import taskgen_method, feature, after_method, before_method
  10. app_info = '''
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
  13. <plist version="0.9">
  14. <dict>
  15. <key>CFBundlePackageType</key>
  16. <string>APPL</string>
  17. <key>CFBundleGetInfoString</key>
  18. <string>Created by Waf</string>
  19. <key>CFBundleSignature</key>
  20. <string>????</string>
  21. <key>NOTE</key>
  22. <string>THIS IS A GENERATED FILE, DO NOT MODIFY</string>
  23. <key>CFBundleExecutable</key>
  24. <string>{app_name}</string>
  25. </dict>
  26. </plist>
  27. '''
  28. """
  29. plist template
  30. """
  31. @feature('c', 'cxx')
  32. def set_macosx_deployment_target(self):
  33. """
  34. see WAF issue 285 and also and also http://trac.macports.org/ticket/17059
  35. """
  36. if self.env['MACOSX_DEPLOYMENT_TARGET']:
  37. os.environ['MACOSX_DEPLOYMENT_TARGET'] = self.env['MACOSX_DEPLOYMENT_TARGET']
  38. elif 'MACOSX_DEPLOYMENT_TARGET' not in os.environ:
  39. if Utils.unversioned_sys_platform() == 'darwin':
  40. os.environ['MACOSX_DEPLOYMENT_TARGET'] = '.'.join(platform.mac_ver()[0].split('.')[:2])
  41. @taskgen_method
  42. def create_bundle_dirs(self, name, out):
  43. """
  44. Create bundle folders, used by :py:func:`create_task_macplist` and :py:func:`create_task_macapp`
  45. """
  46. dir = out.parent.find_or_declare(name)
  47. dir.mkdir()
  48. macos = dir.find_or_declare(['Contents', 'MacOS'])
  49. macos.mkdir()
  50. return dir
  51. def bundle_name_for_output(out):
  52. name = out.name
  53. k = name.rfind('.')
  54. if k >= 0:
  55. name = name[:k] + '.app'
  56. else:
  57. name = name + '.app'
  58. return name
  59. @feature('cprogram', 'cxxprogram')
  60. @after_method('apply_link')
  61. def create_task_macapp(self):
  62. """
  63. To compile an executable into a Mac application (a .app), set its *mac_app* attribute::
  64. def build(bld):
  65. bld.shlib(source='a.c', target='foo', mac_app=True)
  66. To force *all* executables to be transformed into Mac applications::
  67. def build(bld):
  68. bld.env.MACAPP = True
  69. bld.shlib(source='a.c', target='foo')
  70. """
  71. if self.env['MACAPP'] or getattr(self, 'mac_app', False):
  72. out = self.link_task.outputs[0]
  73. name = bundle_name_for_output(out)
  74. dir = self.create_bundle_dirs(name, out)
  75. n1 = dir.find_or_declare(['Contents', 'MacOS', out.name])
  76. self.apptask = self.create_task('macapp', self.link_task.outputs, n1)
  77. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Contents/MacOS/' % name
  78. self.bld.install_files(inst_to, n1, chmod=Utils.O755)
  79. if getattr(self, 'mac_files', None):
  80. # this only accepts files; they will be installed as seen from mac_files_root
  81. mac_files_root = getattr(self, 'mac_files_root', None)
  82. if isinstance(mac_files_root, str):
  83. mac_files_root = self.path.find_node(mac_files_root)
  84. if not mac_files_root:
  85. self.bld.fatal('Invalid mac_files_root %r' % self.mac_files_root)
  86. res_dir = n1.parent.parent.make_node('Resources')
  87. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Resources' % name
  88. for node in self.to_nodes(self.mac_files):
  89. relpath = node.path_from(mac_files_root or node.parent)
  90. self.create_task('macapp', node, res_dir.make_node(relpath))
  91. self.bld.install_as(os.path.join(inst_to, relpath), node)
  92. if getattr(self, 'mac_resources', None):
  93. # TODO remove in waf 1.9
  94. res_dir = n1.parent.parent.make_node('Resources')
  95. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Resources' % name
  96. for x in self.to_list(self.mac_resources):
  97. node = self.path.find_node(x)
  98. if not node:
  99. raise Errors.WafError('Missing mac_resource %r in %r' % (x, self))
  100. parent = node.parent
  101. if os.path.isdir(node.abspath()):
  102. nodes = node.ant_glob('**')
  103. else:
  104. nodes = [node]
  105. for node in nodes:
  106. rel = node.path_from(parent)
  107. self.create_task('macapp', node, res_dir.make_node(rel))
  108. self.bld.install_as(inst_to + '/%s' % rel, node)
  109. if getattr(self.bld, 'is_install', None):
  110. # disable the normal binary installation
  111. self.install_task.hasrun = Task.SKIP_ME
  112. @feature('cprogram', 'cxxprogram')
  113. @after_method('apply_link')
  114. def create_task_macplist(self):
  115. """
  116. Create a :py:class:`waflib.Tools.c_osx.macplist` instance.
  117. """
  118. if self.env['MACAPP'] or getattr(self, 'mac_app', False):
  119. out = self.link_task.outputs[0]
  120. name = bundle_name_for_output(out)
  121. dir = self.create_bundle_dirs(name, out)
  122. n1 = dir.find_or_declare(['Contents', 'Info.plist'])
  123. self.plisttask = plisttask = self.create_task('macplist', [], n1)
  124. plisttask.context = {
  125. 'app_name': self.link_task.outputs[0].name,
  126. 'env': self.env
  127. }
  128. plist_ctx = getattr(self, 'plist_context', None)
  129. if (plist_ctx):
  130. plisttask.context.update(plist_ctx)
  131. if getattr(self, 'mac_plist', False):
  132. node = self.path.find_resource(self.mac_plist)
  133. if node:
  134. plisttask.inputs.append(node)
  135. else:
  136. plisttask.code = self.mac_plist
  137. else:
  138. plisttask.code = app_info
  139. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Contents/' % name
  140. self.bld.install_files(inst_to, n1)
  141. @feature('cshlib', 'cxxshlib')
  142. @before_method('apply_link', 'propagate_uselib_vars')
  143. def apply_bundle(self):
  144. """
  145. To make a bundled shared library (a ``.bundle``), set the *mac_bundle* attribute::
  146. def build(bld):
  147. bld.shlib(source='a.c', target='foo', mac_bundle = True)
  148. To force *all* executables to be transformed into bundles::
  149. def build(bld):
  150. bld.env.MACBUNDLE = True
  151. bld.shlib(source='a.c', target='foo')
  152. """
  153. if self.env['MACBUNDLE'] or getattr(self, 'mac_bundle', False):
  154. self.env['LINKFLAGS_cshlib'] = self.env['LINKFLAGS_cxxshlib'] = [] # disable the '-dynamiclib' flag
  155. self.env['cshlib_PATTERN'] = self.env['cxxshlib_PATTERN'] = self.env['macbundle_PATTERN']
  156. use = self.use = self.to_list(getattr(self, 'use', []))
  157. if not 'MACBUNDLE' in use:
  158. use.append('MACBUNDLE')
  159. app_dirs = ['Contents', 'Contents/MacOS', 'Contents/Resources']
  160. class macapp(Task.Task):
  161. """
  162. Create mac applications
  163. """
  164. color = 'PINK'
  165. def run(self):
  166. self.outputs[0].parent.mkdir()
  167. shutil.copy2(self.inputs[0].srcpath(), self.outputs[0].abspath())
  168. class macplist(Task.Task):
  169. """
  170. Create plist files
  171. """
  172. color = 'PINK'
  173. ext_in = ['.bin']
  174. def run(self):
  175. if getattr(self, 'code', None):
  176. txt = self.code
  177. else:
  178. txt = self.inputs[0].read()
  179. context = getattr(self, 'context', {})
  180. txt = txt.format(**context)
  181. self.outputs[0].write(txt)