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.

213 lines
6.4KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy 2008-2010
  4. """
  5. MacOSX related tools
  6. """
  7. import os, shutil, sys, platform
  8. from waflib import TaskGen, Task, Build, Options, 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. bld = self.bld
  47. dir = out.parent.find_or_declare(name)
  48. dir.mkdir()
  49. macos = dir.find_or_declare(['Contents', 'MacOS'])
  50. macos.mkdir()
  51. return dir
  52. def bundle_name_for_output(out):
  53. name = out.name
  54. k = name.rfind('.')
  55. if k >= 0:
  56. name = name[:k] + '.app'
  57. else:
  58. name = name + '.app'
  59. return name
  60. @feature('cprogram', 'cxxprogram')
  61. @after_method('apply_link')
  62. def create_task_macapp(self):
  63. """
  64. To compile an executable into a Mac application (a .app), set its *mac_app* attribute::
  65. def build(bld):
  66. bld.shlib(source='a.c', target='foo', mac_app=True)
  67. To force *all* executables to be transformed into Mac applications::
  68. def build(bld):
  69. bld.env.MACAPP = True
  70. bld.shlib(source='a.c', target='foo')
  71. """
  72. if self.env['MACAPP'] or getattr(self, 'mac_app', False):
  73. out = self.link_task.outputs[0]
  74. name = bundle_name_for_output(out)
  75. dir = self.create_bundle_dirs(name, out)
  76. n1 = dir.find_or_declare(['Contents', 'MacOS', out.name])
  77. self.apptask = self.create_task('macapp', self.link_task.outputs, n1)
  78. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Contents/MacOS/' % name
  79. self.bld.install_files(inst_to, n1, chmod=Utils.O755)
  80. if getattr(self, 'mac_files', None):
  81. # this only accepts files; they will be installed as seen from mac_files_root
  82. mac_files_root = getattr(self, 'mac_files_root', None)
  83. if isinstance(mac_files_root, str):
  84. mac_files_root = self.path.find_node(mac_files_root)
  85. if not mac_files_root:
  86. self.bld.fatal('Invalid mac_files_root %r' % self.mac_files_root)
  87. res_dir = n1.parent.parent.make_node('Resources')
  88. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Resources' % name
  89. for node in self.to_nodes(self.mac_files):
  90. relpath = node.path_from(mac_files_root or node.parent)
  91. tsk = self.create_task('macapp', node, res_dir.make_node(relpath))
  92. self.bld.install_as(os.path.join(inst_to, relpath), node)
  93. if getattr(self, 'mac_resources', None):
  94. # TODO remove in waf 1.9
  95. res_dir = n1.parent.parent.make_node('Resources')
  96. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Resources' % name
  97. for x in self.to_list(self.mac_resources):
  98. node = self.path.find_node(x)
  99. if not node:
  100. raise Errors.WafError('Missing mac_resource %r in %r' % (x, self))
  101. parent = node.parent
  102. if os.path.isdir(node.abspath()):
  103. nodes = node.ant_glob('**')
  104. else:
  105. nodes = [node]
  106. for node in nodes:
  107. rel = node.path_from(parent)
  108. tsk = self.create_task('macapp', node, res_dir.make_node(rel))
  109. self.bld.install_as(inst_to + '/%s' % rel, node)
  110. if getattr(self.bld, 'is_install', None):
  111. # disable the normal binary installation
  112. self.install_task.hasrun = Task.SKIP_ME
  113. @feature('cprogram', 'cxxprogram')
  114. @after_method('apply_link')
  115. def create_task_macplist(self):
  116. """
  117. Create a :py:class:`waflib.Tools.c_osx.macplist` instance.
  118. """
  119. if self.env['MACAPP'] or getattr(self, 'mac_app', False):
  120. out = self.link_task.outputs[0]
  121. name = bundle_name_for_output(out)
  122. dir = self.create_bundle_dirs(name, out)
  123. n1 = dir.find_or_declare(['Contents', 'Info.plist'])
  124. self.plisttask = plisttask = self.create_task('macplist', [], n1)
  125. plisttask.context = {
  126. 'app_name': self.link_task.outputs[0].name,
  127. 'env': self.env
  128. }
  129. plist_ctx = getattr(self, 'plist_context', None)
  130. if (plist_ctx):
  131. plisttask.context.update(plist_ctx)
  132. if getattr(self, 'mac_plist', False):
  133. node = self.path.find_resource(self.mac_plist)
  134. if node:
  135. plisttask.inputs.append(node)
  136. else:
  137. plisttask.code = self.mac_plist
  138. else:
  139. plisttask.code = app_info
  140. inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Contents/' % name
  141. self.bld.install_files(inst_to, n1)
  142. @feature('cshlib', 'cxxshlib')
  143. @before_method('apply_link', 'propagate_uselib_vars')
  144. def apply_bundle(self):
  145. """
  146. To make a bundled shared library (a ``.bundle``), set the *mac_bundle* attribute::
  147. def build(bld):
  148. bld.shlib(source='a.c', target='foo', mac_bundle = True)
  149. To force *all* executables to be transformed into bundles::
  150. def build(bld):
  151. bld.env.MACBUNDLE = True
  152. bld.shlib(source='a.c', target='foo')
  153. """
  154. if self.env['MACBUNDLE'] or getattr(self, 'mac_bundle', False):
  155. self.env['LINKFLAGS_cshlib'] = self.env['LINKFLAGS_cxxshlib'] = [] # disable the '-dynamiclib' flag
  156. self.env['cshlib_PATTERN'] = self.env['cxxshlib_PATTERN'] = self.env['macbundle_PATTERN']
  157. use = self.use = self.to_list(getattr(self, 'use', []))
  158. if not 'MACBUNDLE' in use:
  159. use.append('MACBUNDLE')
  160. app_dirs = ['Contents', 'Contents/MacOS', 'Contents/Resources']
  161. class macapp(Task.Task):
  162. """
  163. Create mac applications
  164. """
  165. color = 'PINK'
  166. def run(self):
  167. self.outputs[0].parent.mkdir()
  168. shutil.copy2(self.inputs[0].srcpath(), self.outputs[0].abspath())
  169. class macplist(Task.Task):
  170. """
  171. Create plist files
  172. """
  173. color = 'PINK'
  174. ext_in = ['.bin']
  175. def run(self):
  176. if getattr(self, 'code', None):
  177. txt = self.code
  178. else:
  179. txt = self.inputs[0].read()
  180. context = getattr(self, 'context', {})
  181. txt = txt.format(**context)
  182. self.outputs[0].write(txt)