_setup_util.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. # Software License Agreement (BSD License)
  4. #
  5. # Copyright (c) 2012, Willow Garage, Inc.
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # * Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # * Redistributions in binary form must reproduce the above
  15. # copyright notice, this list of conditions and the following
  16. # disclaimer in the documentation and/or other materials provided
  17. # with the distribution.
  18. # * Neither the name of Willow Garage, Inc. nor the names of its
  19. # contributors may be used to endorse or promote products derived
  20. # from this software without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. # POSSIBILITY OF SUCH DAMAGE.
  34. '''This file generates shell code for the setup.SHELL scripts to set environment variables'''
  35. from __future__ import print_function
  36. import argparse
  37. import copy
  38. import errno
  39. import os
  40. import platform
  41. import sys
  42. CATKIN_MARKER_FILE = '.catkin'
  43. system = platform.system()
  44. IS_DARWIN = (system == 'Darwin')
  45. IS_WINDOWS = (system == 'Windows')
  46. # subfolder of workspace prepended to CMAKE_PREFIX_PATH
  47. ENV_VAR_SUBFOLDERS = {
  48. 'CMAKE_PREFIX_PATH': '',
  49. 'LD_LIBRARY_PATH' if not IS_DARWIN else 'DYLD_LIBRARY_PATH': ['lib', os.path.join('lib', 'x86_64-linux-gnu')],
  50. 'PATH': 'bin',
  51. 'PKG_CONFIG_PATH': [os.path.join('lib', 'pkgconfig'), os.path.join('lib', 'x86_64-linux-gnu', 'pkgconfig')],
  52. 'PYTHONPATH': 'lib/python2.7/dist-packages',
  53. }
  54. def rollback_env_variables(environ, env_var_subfolders):
  55. '''
  56. Generate shell code to reset environment variables
  57. by unrolling modifications based on all workspaces in CMAKE_PREFIX_PATH.
  58. This does not cover modifications performed by environment hooks.
  59. '''
  60. lines = []
  61. unmodified_environ = copy.copy(environ)
  62. for key in sorted(env_var_subfolders.keys()):
  63. subfolders = env_var_subfolders[key]
  64. if not isinstance(subfolders, list):
  65. subfolders = [subfolders]
  66. value = _rollback_env_variable(unmodified_environ, key, subfolders)
  67. if value is not None:
  68. environ[key] = value
  69. lines.append(assignment(key, value))
  70. if lines:
  71. lines.insert(0, comment('reset environment variables by unrolling modifications based on all workspaces in CMAKE_PREFIX_PATH'))
  72. return lines
  73. def _rollback_env_variable(environ, name, subfolders):
  74. '''
  75. For each catkin workspace in CMAKE_PREFIX_PATH remove the first entry from env[NAME] matching workspace + subfolder.
  76. :param subfolders: list of str '' or subfoldername that may start with '/'
  77. :returns: the updated value of the environment variable.
  78. '''
  79. value = environ[name] if name in environ else ''
  80. env_paths = [path for path in value.split(os.pathsep) if path]
  81. value_modified = False
  82. for subfolder in subfolders:
  83. if subfolder:
  84. if subfolder.startswith(os.path.sep) or (os.path.altsep and subfolder.startswith(os.path.altsep)):
  85. subfolder = subfolder[1:]
  86. if subfolder.endswith(os.path.sep) or (os.path.altsep and subfolder.endswith(os.path.altsep)):
  87. subfolder = subfolder[:-1]
  88. for ws_path in _get_workspaces(environ, include_fuerte=True, include_non_existing=True):
  89. path_to_find = os.path.join(ws_path, subfolder) if subfolder else ws_path
  90. path_to_remove = None
  91. for env_path in env_paths:
  92. env_path_clean = env_path[:-1] if env_path and env_path[-1] in [os.path.sep, os.path.altsep] else env_path
  93. if env_path_clean == path_to_find:
  94. path_to_remove = env_path
  95. break
  96. if path_to_remove:
  97. env_paths.remove(path_to_remove)
  98. value_modified = True
  99. new_value = os.pathsep.join(env_paths)
  100. return new_value if value_modified else None
  101. def _get_workspaces(environ, include_fuerte=False, include_non_existing=False):
  102. '''
  103. Based on CMAKE_PREFIX_PATH return all catkin workspaces.
  104. :param include_fuerte: The flag if paths starting with '/opt/ros/fuerte' should be considered workspaces, ``bool``
  105. '''
  106. # get all cmake prefix paths
  107. env_name = 'CMAKE_PREFIX_PATH'
  108. value = environ[env_name] if env_name in environ else ''
  109. paths = [path for path in value.split(os.pathsep) if path]
  110. # remove non-workspace paths
  111. workspaces = [path for path in paths if os.path.isfile(os.path.join(path, CATKIN_MARKER_FILE)) or (include_fuerte and path.startswith('/opt/ros/fuerte')) or (include_non_existing and not os.path.exists(path))]
  112. return workspaces
  113. def prepend_env_variables(environ, env_var_subfolders, workspaces):
  114. '''
  115. Generate shell code to prepend environment variables
  116. for the all workspaces.
  117. '''
  118. lines = []
  119. lines.append(comment('prepend folders of workspaces to environment variables'))
  120. paths = [path for path in workspaces.split(os.pathsep) if path]
  121. prefix = _prefix_env_variable(environ, 'CMAKE_PREFIX_PATH', paths, '')
  122. lines.append(prepend(environ, 'CMAKE_PREFIX_PATH', prefix))
  123. for key in sorted([key for key in env_var_subfolders.keys() if key != 'CMAKE_PREFIX_PATH']):
  124. subfolder = env_var_subfolders[key]
  125. prefix = _prefix_env_variable(environ, key, paths, subfolder)
  126. lines.append(prepend(environ, key, prefix))
  127. return lines
  128. def _prefix_env_variable(environ, name, paths, subfolders):
  129. '''
  130. Return the prefix to prepend to the environment variable NAME, adding any path in NEW_PATHS_STR without creating duplicate or empty items.
  131. '''
  132. value = environ[name] if name in environ else ''
  133. environ_paths = [path for path in value.split(os.pathsep) if path]
  134. checked_paths = []
  135. for path in paths:
  136. if not isinstance(subfolders, list):
  137. subfolders = [subfolders]
  138. for subfolder in subfolders:
  139. path_tmp = path
  140. if subfolder:
  141. path_tmp = os.path.join(path_tmp, subfolder)
  142. # skip nonexistent paths
  143. if not os.path.exists(path_tmp):
  144. continue
  145. # exclude any path already in env and any path we already added
  146. if path_tmp not in environ_paths and path_tmp not in checked_paths:
  147. checked_paths.append(path_tmp)
  148. prefix_str = os.pathsep.join(checked_paths)
  149. if prefix_str != '' and environ_paths:
  150. prefix_str += os.pathsep
  151. return prefix_str
  152. def assignment(key, value):
  153. if not IS_WINDOWS:
  154. return 'export %s="%s"' % (key, value)
  155. else:
  156. return 'set %s=%s' % (key, value)
  157. def comment(msg):
  158. if not IS_WINDOWS:
  159. return '# %s' % msg
  160. else:
  161. return 'REM %s' % msg
  162. def prepend(environ, key, prefix):
  163. if key not in environ or not environ[key]:
  164. return assignment(key, prefix)
  165. if not IS_WINDOWS:
  166. return 'export %s="%s$%s"' % (key, prefix, key)
  167. else:
  168. return 'set %s=%s%%%s%%' % (key, prefix, key)
  169. def find_env_hooks(environ, cmake_prefix_path):
  170. '''
  171. Generate shell code with found environment hooks
  172. for the all workspaces.
  173. '''
  174. lines = []
  175. lines.append(comment('found environment hooks in workspaces'))
  176. generic_env_hooks = []
  177. generic_env_hooks_workspace = []
  178. specific_env_hooks = []
  179. specific_env_hooks_workspace = []
  180. generic_env_hooks_by_filename = {}
  181. specific_env_hooks_by_filename = {}
  182. generic_env_hook_ext = 'bat' if IS_WINDOWS else 'sh'
  183. specific_env_hook_ext = environ['CATKIN_SHELL'] if not IS_WINDOWS and 'CATKIN_SHELL' in environ and environ['CATKIN_SHELL'] else None
  184. # remove non-workspace paths
  185. workspaces = [path for path in cmake_prefix_path.split(os.pathsep) if path and os.path.isfile(os.path.join(path, CATKIN_MARKER_FILE))]
  186. for workspace in reversed(workspaces):
  187. env_hook_dir = os.path.join(workspace, 'etc', 'catkin', 'profile.d')
  188. if os.path.isdir(env_hook_dir):
  189. for filename in sorted(os.listdir(env_hook_dir)):
  190. if filename.endswith('.%s' % generic_env_hook_ext):
  191. # remove previous env hook with same name if present
  192. if filename in generic_env_hooks_by_filename:
  193. i = generic_env_hooks.index(generic_env_hooks_by_filename[filename])
  194. generic_env_hooks.pop(i)
  195. generic_env_hooks_workspace.pop(i)
  196. # append env hook
  197. generic_env_hooks.append(os.path.join(env_hook_dir, filename))
  198. generic_env_hooks_workspace.append(workspace)
  199. generic_env_hooks_by_filename[filename] = generic_env_hooks[-1]
  200. elif specific_env_hook_ext is not None and filename.endswith('.%s' % specific_env_hook_ext):
  201. # remove previous env hook with same name if present
  202. if filename in specific_env_hooks_by_filename:
  203. i = specific_env_hooks.index(specific_env_hooks_by_filename[filename])
  204. specific_env_hooks.pop(i)
  205. specific_env_hooks_workspace.pop(i)
  206. # append env hook
  207. specific_env_hooks.append(os.path.join(env_hook_dir, filename))
  208. specific_env_hooks_workspace.append(workspace)
  209. specific_env_hooks_by_filename[filename] = specific_env_hooks[-1]
  210. env_hooks = generic_env_hooks + specific_env_hooks
  211. env_hooks_workspace = generic_env_hooks_workspace + specific_env_hooks_workspace
  212. count = len(env_hooks)
  213. lines.append(assignment('_CATKIN_ENVIRONMENT_HOOKS_COUNT', count))
  214. for i in range(count):
  215. lines.append(assignment('_CATKIN_ENVIRONMENT_HOOKS_%d' % i, env_hooks[i]))
  216. lines.append(assignment('_CATKIN_ENVIRONMENT_HOOKS_%d_WORKSPACE' % i, env_hooks_workspace[i]))
  217. return lines
  218. def _parse_arguments(args=None):
  219. parser = argparse.ArgumentParser(description='Generates code blocks for the setup.SHELL script.')
  220. parser.add_argument('--extend', action='store_true', help='Skip unsetting previous environment variables to extend context')
  221. return parser.parse_known_args(args=args)[0]
  222. if __name__ == '__main__':
  223. try:
  224. try:
  225. args = _parse_arguments()
  226. except Exception as e:
  227. print(e, file=sys.stderr)
  228. sys.exit(1)
  229. # environment at generation time
  230. CMAKE_PREFIX_PATH = '/home/youchen/Documents/catkin_ws/devel;/opt/ros/melodic'.split(';')
  231. # prepend current workspace if not already part of CPP
  232. base_path = os.path.dirname(__file__)
  233. if base_path not in CMAKE_PREFIX_PATH:
  234. CMAKE_PREFIX_PATH.insert(0, base_path)
  235. CMAKE_PREFIX_PATH = os.pathsep.join(CMAKE_PREFIX_PATH)
  236. environ = dict(os.environ)
  237. lines = []
  238. if not args.extend:
  239. lines += rollback_env_variables(environ, ENV_VAR_SUBFOLDERS)
  240. lines += prepend_env_variables(environ, ENV_VAR_SUBFOLDERS, CMAKE_PREFIX_PATH)
  241. lines += find_env_hooks(environ, CMAKE_PREFIX_PATH)
  242. print('\n'.join(lines))
  243. # need to explicitly flush the output
  244. sys.stdout.flush()
  245. except IOError as e:
  246. # and catch potential "broken pipe" if stdout is not writable
  247. # which can happen when piping the output to a file but the disk is full
  248. if e.errno == errno.EPIPE:
  249. print(e, file=sys.stderr)
  250. sys.exit(2)
  251. raise
  252. sys.exit(0)